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