GPT4ALL 就是这样的选择之一,而不是使用 ChatGPT、Amazon Bedrock、Google Bard 等服务 - 有几种开源大型语言模型可用于在本地运行聊天机器人。
这让我想知道是否可以在 AWS Lambda 函数中运行 LLM。这些通常是小型、运行速度快、事件驱动的函数。它们并不适合用于复杂的处理任务,但它们可以缩放到零,并且您只需为使用的内容付费。在 Lambda 函数中拥有 LLM 似乎是一个有趣的实验,也是一种无需长时间运行的流程即可获得托管模型的方法。
此存储库包含使用容器映像创建自定义运行时 AWS Lambda 函数的代码。它也可以在 Docker 内部本地运行以进行测试。该函数包含完整的 LLM 模型和使用该模型的代码,允许通过 HTTP 调用生成基本文本。
这使用:
下载 GPT4ALL 模型。为此,我使用了 gpt4all-falcon-newbpe-q4_0.gguf,因为它是一个小模型(4GB...),具有良好的响应。其他模型应该可以工作,但它们需要足够小才能适应 Lambda 内存限制。
将bin
文件放置在function
文件夹内,位于 lambda_function.py 文件旁边。
使用 docker 构建映像: docker build --platform linux/amd64 -t makgpt:test1
。
构建完成后,您可以在本地运行它: docker run -p 9000:8080 makgpt:test1
运行后,您可以在另一个终端窗口中对其运行调用,例如curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"body":"{"action":"Set fire to a tree"}"}'
将映像推送到您的 AWS 账户内的 ECR。为此,您可以按照开发人员指南中Deploying the image
下的 AWS 指南进行操作。简单的步骤是:
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
从此映像创建 Lambda 函数,确保为其提供足够的内存(建议 10GB)和至少 5 分钟的超时。
为简单起见,为 Lambda 函数提供一个函数 URL,然后您可以通过 HTTP 访问它。
然后,您可以使用以下 URL 进行尝试: curl "https://INSERTHERE.lambda-url.us-east-1.on.aws/" -d '{"body":"{"action":"Set fire to a tree"}"}'
该函数作为一个基本的文本冒险游戏,提示如下:
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."
耗时: 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?"
耗时: 1m 1s