En lugar de utilizar servicios como ChatGPT, Amazon Bedrock, Google Bard (existen varios modelos de lenguaje grande de código abierto que se pueden utilizar para ejecutar un chatbot localmente), GPT4ALL es una de esas opciones.
Esto me llevó a preguntarme si era posible ejecutar un LLM dentro de la función AWS Lambda. Suelen ser funciones pequeñas, rápidas de ejecutar y controladas por eventos. No están destinados a ser utilizados para tareas de procesamiento complejas, pero escalan a cero y usted solo paga por lo que usa. Tener un LLM dentro de una función Lambda parece un experimento divertido y una forma de tener un modelo alojado que no requiere un proceso de larga duración.
Este repositorio contiene código para crear una función AWS Lambda de tiempo de ejecución personalizada utilizando una imagen de contenedor. También se puede ejecutar localmente dentro de Docker para realizar pruebas. La función contiene el modelo LLM completo y el código para usar el modelo, lo que permite la generación de texto básico a partir de una llamada HTTP.
Esto utiliza:
Descargue un modelo GPT4ALL. Para esto utilicé gpt4all-falcon-newbpe-q4_0.gguf porque es un modelo pequeño (4GB...) que tiene buenas respuestas. Otros modelos deberían funcionar, pero deben ser lo suficientemente pequeños como para caber en los límites de la memoria Lambda.
Coloque el archivo bin
dentro de la carpeta function
, al lado del archivo lambda_function.py.
Cree la imagen con Docker: docker build --platform linux/amd64 -t makgpt:test1
.
Una vez compilado, puede ejecutarlo localmente: docker run -p 9000:8080 makgpt:test1
Una vez que se esté ejecutando, puede ejecutar invocaciones en otra ventana de terminal, como curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"body":"{"action":"Set fire to a tree"}"}'
Envíe la imagen a ECR dentro de su cuenta de AWS. Para ello, puede seguir la guía de AWS en Deploying the image
en la Guía para desarrolladores. Los pasos simples son:
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 111122223333.dkr.ecr.us-east-1.amazonaws.com
aws ecr create-repository --repository-name hello-world --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE
docker tag docker-image:test :latest
docker tag docker-image:test 111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
docker push 111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
Cree una función Lambda a partir de esta imagen, asegurándose de darle suficiente memoria (se recomiendan 10 GB) y un tiempo de espera de al menos 5 minutos.
Para simplificar, asigne a la función Lambda una URL de función y luego podrá acceder a ella a través de HTTP.
Luego puedes probarlo con una URL como: curl "https://INSERTHERE.lambda-url.us-east-1.on.aws/" -d '{"body":"{"action":"Set fire to a tree"}"}'
Esta función está diseñada como un juego de aventuras de texto básico con el mensaje:
You are a Text-Based Fantasy RPG. The player is exploring the dark forest and you are the dungeon master. The player performs the action: {action} What happens next? You are talking to the player.
{
"action": "Throw your sword at the angry orc"
}
"nAs the player throws their sword at the angry orc, it strikes true and lands squarely in its chest. The orc roars in pain and anger, charging towards the player with a fierce look in its eyes.nnThe player quickly dodges out of the way, but not before noticing something strange about the orc's eyes. They seem to be glowing with an otherworldly light, as if they are possessed by some dark force.nnWith the orc now dangerously close, the player decides to take a risk and try to reason with it. "Stop! I mean you no harm," they say, holding out their hands in a gesture of peace.nnTo their surprise, the orc seems to understand them and hesitates for a moment before continuing its attack. The player quickly grabs their sword and prepares to defend themselves if necessary.nnAs the fight continues, it becomes clear that this orc is not like any other they have faced before. Its movements are unnaturally quick and its strength is unmatched. But with each blow of the sword, the player gains a little more confidence in their abilities.nnFinally, after what seems like an eternity, the orc falls to the ground, defeated."
Tiempo empleado: 1m 33s
{
"action": "A wizard casts a spell on the player and turns them into a pencil"
}
"nAs the wizard finishes casting his spell, the player suddenly finds themselves transformed into a pencil. They look down at their new form in confusion before noticing that they are no longer holding the wand they were using to cast spells.nnThe player tries to move around, but find that they are unable to do so without making a noise. They try to speak, but find that they cannot make any sounds with their new pencil body.nnSuddenly, they hear a voice from behind them. "Well, well, well. Look who we have here." The player turns around to see the wizard who cast the spell on them.nnThe wizard chuckles at the player's transformation and begins to speak in a mocking tone. "I bet you didn't expect this when you decided to mess with magic, did you?"nnThe player tries to respond, but finds that they are unable to do so without making any noise. They feel helpless and frustrated as they try to come up with a plan to escape their new form.nnWhat will the player do next?"
Tiempo empleado: 1m 1s