搜尋
首頁後端開發Python教學如何將 AI 應用程式(包含大型部門)部署到 AWS Lambda

How to Deploy an AI App (w/ Large Deps) to AWS Lambda

我最近花了兩個小時在 AWS Lambda 上運行一個簡單的 LlamaIndex 應用程式。雖然函數本身僅由幾行 Python 程式碼組成(如下所示),但管理依賴項和部署可能很棘手。

import json
from llama_index.llms.openai import OpenAI

llm=OpenAI(model="gpt-4o-mini")

def lambda_handler(event, context):
    response = llm.complete("What public transportation might be available in a city?")
    return {
        'statusCode': 200,
        'body': str(response),
    }

以下是一些對我有幫助的關鍵提示:

首先,安裝適用於正確平台的軟體包。安裝「manylinux2014_x86_64」目標平台的所有軟體包非常重要。否則,某些套件可能與 AWS Lambda 執行時期不相容。為了確保相容性,請使用以下命令安裝相依性:

pip install -r requirements.txt --platform manylinux2014_x86_64 --target ./deps --only-binary=:all:

其次,Lambda 對總程式碼套件大小有 250MB 的限制,如果您使用 LlamaIndex 或其他大型依賴項,這很快就會成為問題。如果您的套件超出此限制,請檢查依賴項資料夾中最大的檔案:

du -h -d 2 | sort -hr | head -n20

就我而言,我發現 pandas/tests 目錄佔用了大約 35MB 的空間,這對於我的功能來說是不必要的,因此我刪除了它以使包大小回到限制範圍內。

rm -r deps/pandas/tests

然後,壓縮所有內容並透過 S3 上傳。修剪掉不必要的檔案後,建立一個包含程式碼和依賴項的 zip 檔案。由於 Lambda 的 Web 控制台有 50MB 的上傳限制,因此您需要將較大的 zip 檔案上傳到 S3 儲存桶並使用 S3 URI 來部署該函數。

zip -r test_lambda.zip data/ lambda_function.py
cd deps/
zip -r ../test_lambda.zip .

最後,在部署之前調整您的 Lambda 設定。預設情況下,Lambda 函數僅分配 128MB 記憶體和 3 秒逾時,這對於許多涉及大量依賴項和呼叫 LLM 的 AI 應用程式來說是不夠的。我會將記憶體增加到 512MB,並將超時時間延長到 30 秒。此外,不要忘記設定必要的環境變量,例如 OpenAI API 金鑰。

我花了好幾次嘗試才找到為 Lambda 安裝軟體包並將所有內容捆綁在一起的正確方法。 AWS Lambda 對於基本腳本來說是用戶友好的,但是一旦添加更大的依賴項,事情就會變得更加複雜。

這是最終的步驟序列:

# Install dependencies
pip install -r requirements.txt --platform manylinux2014_x86_64 --target ./deps --only-binary=:all:

# Create a zip file for code and data
zip -r test_lambda.zip data/ lambda_function.py

# Include dependencies in the zip file, while removing large unused files
cd deps/
rm -r pandas/tests
zip -r ../test_lambda.zip .

p.s.,我也嘗試在 DBOS Cloud 上部署類似的功能,只需要一個指令:

dbos-cloud app deploy

在DBOS中,依賴管理是透過requirements.txt檔案自動處理的,環境變數在dbos-config.yaml中設定。我可能有偏見,但我喜歡 DBOS Cloud 部署過程的簡單性。

以上是如何將 AI 應用程式(包含大型部門)部署到 AWS Lambda的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Python:深入研究彙編和解釋Python:深入研究彙編和解釋May 12, 2025 am 12:14 AM

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

Python是一種解釋或編譯語言,為什麼重要?Python是一種解釋或編譯語言,為什麼重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

對於python中的循環時循環與循環:解釋了關鍵差異對於python中的循環時循環與循環:解釋了關鍵差異May 12, 2025 am 12:08 AM

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

循環時:實用指南循環時:實用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

Python:它是真正的解釋嗎?揭穿神話Python:它是真正的解釋嗎?揭穿神話May 12, 2025 am 12:05 AM

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

與同一元素的Python串聯列表與同一元素的Python串聯列表May 11, 2025 am 12:08 AM

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

解釋與編譯語言:Python的位置解釋與編譯語言:Python的位置May 11, 2025 am 12:07 AM

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

循環時:您什麼時候在Python中使用?循環時:您什麼時候在Python中使用?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中