Sagify提供了一個簡化的界面,以管理AWS SageMaker上的機器學習工作流程,從而幫助您專注於構建ML模型而不是基礎架構。它的模塊化體系結構包括一個LLM網關模塊,以提供一個統一的接口,用於利用開源和專有的大型語言模型。 LLM Gateway通過簡單的API可訪問各種LLM,使您可以輕鬆地將它們整合到工作流程中。
有關Sagify的詳細引用,請轉到:閱讀文檔
下屬需要以下內容:
在命令行:
pip install sagify
確保通過按照部分配置AWS帳戶的說明來配置您的AWS帳戶
最後,運行以下命令:
sagify cloud foundation-model-deploy --model-id model-txt2img-stabilityai-stable-diffusion-v2-1-base --model-version 1. * -n 1 -e ml.p3.2xlarge --aws-region us-east-1 --aws-profile sagemaker-dev您可以使用首選的EC2類型(-e),AWS區域和AWS配置文件的值更改值。
一旦部署了穩定的擴散模型,就可以使用生成的代碼段來查詢它。享受!
提供以下模型進行聊天完成:
| 模型名稱 | URL |
|---|---|
| GPT-4 | https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo |
| GPT-4-32K | https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo |
| GPT-3.5-Turbo | https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo |
對於圖像創建,您可以依靠以下模型:
| 模型名稱 | URL |
|---|---|
| dall-e-3 | https://platform.openai.com/docs/models/dall-e |
| dall-e-2 | https://platform.openai.com/docs/models/dall-e |
並用於嵌入:
| 模型名稱 | URL |
|---|---|
| 文本插入3大 | https://platform.openai.com/docs/models/embeddings |
| text-embedding-3-small | https://platform.openai.com/docs/models/embeddings |
| 文本插入-ADA-002 | https://platform.openai.com/docs/models/embeddings |
可以通過運行命令sagify llm models --all --provider openai來檢索所有這些支持模型的所有這些列表。如果您只想專注於聊天完成模型,請運行sagify llm models --chat-completions --provider openai 。對於圖像創建和嵌入,分別sagify llm models --image-creations --provider openai和sagify llm models --embeddings --provider openai 。
提供以下開源型號進行聊天完成:
| 模型名稱 | URL |
|---|---|
| Llama-2-7b | https://huggingface.co/meta-llama/llama-2-7b |
| Llama-2-13b | https://huggingface.co/meta-llama/llama-2-13b |
| Llama-2-70B | https://huggingface.co/meta-llama/llama-2-70b |
對於圖像創建,您可以依靠以下開源模型:
| 模型名稱 | URL |
|---|---|
| 穩定穩定的擴散-V2 | https://huggingface.co/stocietyai/stable-diffusion-2 |
| 穩定穩定的擴散-V2-1基鹼 | https://huggingface.co/stocietyai/stable-diffusion-2-1-base |
| 穩定性穩定 - 擴散-V2-FP16 | https://huggingface.co/stocietyai/stable-diffusion-2/tree/fp16 |
並用於嵌入:
| 模型名稱 | URL |
|---|---|
| bge-large-en | https://huggingface.co/baai/bge-large-en |
| bge-base-en | https://huggingface.co/baai/bge-base-en |
| GTE-LARGE | https://huggingface.co/thenlper/gte-large |
| GTE基礎 | https://huggingface.co/thenlper/gte-base |
| e5-large-v2 | https://huggingface.co/intfloat/e5-large-v2 |
| bge-small-en | https://huggingface.co/baai/bge-small-en |
| e5-base-v2 | https://huggingface.co/intfloat/e5-base-v2 |
| 多語言-E5大 | https://huggingface.co/intfloat/multlingual-e5-large |
| e5總 | https://huggingface.co/intfloat/e5-large |
| GTE-SMALL | https://huggingface.co/thenlper/gte-small |
| e5鹼 | https://huggingface.co/intfloat/e5-base |
| E5-SMALL-V2 | https://huggingface.co/intfloat/e5-small-v2 |
| 多語言-E5基詞 | https://huggingface.co/intfloat/multlingual-e5-base |
| All-Minilm-L6-V2 | https://huggingface.co/sentence-transformers/all-minilm-l6-v2 |
所有這些受支持的開源模型列表均在AWS SageMaker上支持,並且可以通過運行命令sagify llm models --all --provider sagemaker來檢索。如果您只想專注於聊天完成模型,請運行sagify llm models --chat-completions --provider sagemaker 。對於圖像創建和嵌入, sagify llm models --image-creations --provider sagemaker和sagify llm models --embeddings --provider sagemaker 。
在啟動LLM網關服務器之前,您需要定義以下ENV變量:
OPENAI_API_KEY :您的OpenAI API密鑰。示例: export OPENAI_API_KEY=...OPENAI_CHAT_COMPLETIONS_MODEL :它應該在此處或此處具有值之一。OPENAI_EMBEDDINGS_MODEL :它應該在這裡具有一個值之一。OPENAI_IMAGE_CREATION_MODEL :它應該在這裡具有一個值之一。 第一步是部署LLM模型。您可以選擇部署所有後端服務(聊天完成,圖像創建,嵌入)或其中一些。
如果要部署所有這些,請運行sagify llm start --all 。此命令將使用以下配置部署所有後端服務(聊天完成,圖像創建,嵌入):
{
"chat_completions" : {
"model" : " llama-2-7b " ,
"instance_type" : " ml.g5.2xlarge " ,
"num_instances" : 1 ,
},
"image_creations" : {
"model" : " stabilityai-stable-diffusion-v2-1-base " ,
"instance_type" : " ml.p3.2xlarge " ,
"num_instances" : 1 ,
},
"embeddings" : {
"model" : " gte-small " ,
"instance_type" : " ml.g5.2xlarge " ,
"num_instances" : 1 ,
},
}您可以通過培養自己的配置文件來更改此配置,然後可以運行sagify llm start -all --config YOUR_CONFIG_FILE.json 。
將所有後端服務部署為SageMaker端點需要15到30分鐘。
已部署的模型名稱是SageMaker端點名稱,並將其打印出來並存儲在隱藏的文件.sagify_llm_infra.json中。您也可以從AWS SageMaker Web控制台訪問它們。
設置後端平台後,您可以在本地部署FastAPI LLM網關。
如果使用AWS SageMaker平台,則需要在啟動LLM Gateway Server之前定義以下ENV變量:
AWS_ACCESS_KEY_ID :它可以與您在本地使用的sagify相同。它應該可以訪問Sagemaker和S3。示例: export AWS_ACCESS_KEY_ID=...AWS_SECRET_ACCESS_KEY :它可以與您在本地使用的相同。它應該可以訪問Sagemaker和S3。示例: export AWS_ACCESS_KEY_ID=...AWS_REGION_NAME :部署LLM後端服務(SageMaker端點)的AWS區域。S3_BUCKET_NAME :S3存儲牌名稱,其中存儲了圖像創建後端服務創建的圖像。IMAGE_URL_TTL_IN_SECONDS :在創建圖像的臨時URL的幾秒鐘內TTL。默認值:3600。SM_CHAT_COMPLETIONS_MODEL :部署聊天完成模型的SageMaker端點名稱。SM_EMBEDDINGS_MODEL :部署嵌入模型的SageMaker端點名稱。SM_IMAGE_CREATION_MODEL :部署圖像創建模型的SageMaker端點名稱。如果使用OpenAI平台,則需要在啟動LLM Gateway Server之前定義以下ENV變量:
OPENAI_API_KEY :您的OpenAI API密鑰。示例: export OPENAI_API_KEY=...OPENAI_CHAT_COMPLETIONS_MODEL :它應該在此處或此處具有值之一。OPENAI_EMBEDDINGS_MODEL :它應該在這裡具有一個值之一。OPENAI_IMAGE_CREATION_MODEL :它應該在這裡具有一個值之一。現在,您可以運行命令sagify llm gateway --image sagify-llm-gateway:v0.1.0 --start-local啟動LLM網關。您可以通過--image參數更改圖像的名稱。
此命令將輸出Docker容器ID。您可以通過執行docker stop <CONTAINER_ID>來停止容器。
例子
(請記住首先導出您需要的所有環境變量)
在您要創建Docker映像然後運行的情況下
sagify llm gateway --image sagify-llm-gateway:v0.1.0 --start-local
如果您想使用只需構建圖像
sagify llm gateway --image sagify-llm-gateway:v0.1.0
如果您想支持兩個平台(OpenAI和AWS SageMaker),請通過兩個平台傳遞所有ENV變量。
如果您想將LLM網關部署到AWS Fargate,則可以按照以下一般步驟:
這是一個示例CloudFormation模板,用於部署FastApi服務,以使用5個環境變量AWS Fargate:
Resources :
MyFargateTaskDefinition :
Type : AWS::ECS::TaskDefinition
Properties :
Family : my-fargate-task
ContainerDefinitions :
- Name : fastapi-container
Image : <YOUR_ECR_REPOSITORY_URI>
Memory : 512
PortMappings :
- ContainerPort : 80
Environment :
- Name : AWS_ACCESS_KEY_ID
Value : " value1 "
- Name : AWS_SECRET_ACCESS_KEY
Value : " value2 "
- Name : AWS_REGION_NAME
Value : " value3 "
- Name : S3_BUCKET_NAME
Value : " value4 "
- Name : IMAGE_URL_TTL_IN_SECONDS
Value : " value5 "
- Name : SM_CHAT_COMPLETIONS_MODEL
Value : " value6 "
- Name : SM_EMBEDDINGS_MODEL
Value : " value7 "
- Name : SM_IMAGE_CREATION_MODEL
Value : " value8 "
- Name : OPENAI_CHAT_COMPLETIONS_MODEL
Value : " value9 "
- Name : OPENAI_EMBEDDINGS_MODEL
Value : " value10 "
- Name : OPENAI_IMAGE_CREATION_MODEL
Value : " value11 "
MyFargateService :
Type : AWS::ECS::Service
Properties :
Cluster : default
TaskDefinition : !Ref MyFargateTaskDefinition
DesiredCount : 2
LaunchType : FARGATE
NetworkConfiguration :
AwsvpcConfiguration :
Subnets :
- <YOUR_SUBNET_ID>
SecurityGroups :
- <YOUR_SECURITY_GROUP_ID> 部署LLM網關後,您可以在HOST_NAME/docs上訪問它。
curl --location --request POST ' /v1/chat/completions '
--header ' Content-Type: application/json '
--data-raw ' {
"provider": "sagemaker",
"messages": [
{
"role": "system",
"content": "you are a cook"
},
{
"role": "user",
"content": "what is the recipe of mayonnaise"
}
],
"temperature": 0,
"max_tokens": 600,
"top_p": 0.9,
"seed": 32
} '示例響應
200響應
{
"id" : " chatcmpl-8167b99c-f22b-4e04-8e26-4ca06d58dc86 " ,
"object" : " chat.completion " ,
"created" : 1708765682 ,
"provider" : " sagemaker " ,
"model" : " meta-textgeneration-llama-2-7b-f-2024-02-24-08-49-32-123 " ,
"choices" : [
{
"index" : 0 ,
"message" : {
"role" : " assistant " ,
"content": " Ah, a fellow foodie! Mayonnaise is a classic condiment that's easy to make and can elevate any dish. Here's my trusty recipe for homemade mayonnaise:nnIngredients:nn* 2 egg yolksn* 1/2 cup (120 ml) neutral-tasting oil, such as canola or grapeseedn* 1 tablespoon lemon juice or vinegarn* Salt and pepper to tastennInstructions:nn1. In a small bowl, whisk together the egg yolks and lemon juice or vinegar until well combined.n2. Slowly pour in the oil while continuously whisking the mixture. You can do this by hand with a whisk or use an electric mixer on low speed.n3. Continue whisking until the mixture thickens and emulsifies, which should take about 5-7 minutes. You'll know it's ready when it reaches a thick, creamy consistency.n4. Taste and adjust the seasoning as needed. You can add more salt, pepper, or lemon juice to taste.n5. Transfer the mayonnaise to a jar or airtight container and store it in the fridge for up to 1 week.nnThat's it! Homemade mayonnaise is a great way to control the ingredients and flavor, and it's also a fun kitchen experiment. Enjoy!"
}
}
]
}curl --location --request POST ' /v1/embeddings '
--header ' Content-Type: application/json '
--data-raw ' {
"provider": "sagemaker",
"input": [
"The mayonnaise was delicious"
]
} '示例響應
200響應
{
"data" : [
{
"object" : " embedding " ,
"embedding" : [
-0.04274585098028183 ,
0.021814687177538872 ,
-0.004705613013356924 ,
...
-0.07548460364341736 ,
0.036427777260541916 ,
0.016453085467219353 ,
0.004641987383365631 ,
-0.0072729517705738544 ,
0.02343473769724369 ,
-0.002924458822235465 ,
0.0339619480073452 ,
0.005262510851025581 ,
-0.06709178537130356 ,
-0.015170316211879253 ,
-0.04612169787287712 ,
-0.012380547821521759 ,
-0.006663458421826363 ,
-0.0573800653219223 ,
0.007938326336443424 ,
0.03486081212759018 ,
0.021514462307095528
],
"index" : 0
}
],
"provider" : " sagemaker " ,
"model" : " hf-sentencesimilarity-gte-small-2024-02-24-09-24-27-341 " ,
"object" : " list "
}curl --location --request POST ' /v1/images/generations '
--header ' Content-Type: application/json '
--data-raw ' {
"provider": "sagemaker",
"prompt":
"A baby sea otter"
,
"n": 1,
"width": 512,
"height": 512,
"seed": 32,
"response_format": "url"
} '示例響應
200響應
{
"provider" : " sagemaker " ,
"model" : " stable-diffusion-v2-1-base-2024-02-24-11-43-32-177 " ,
"created" : 1708775601 ,
"data" : [
{
"url" : " https://your-bucket.s3.amazonaws.com/31cedd17-ccd7-4cba-8dea-cb7e8b915782.png?AWSAccessKeyId=AKIAUKEQBDHITP26MLXH&Signature=%2Fd1J%2FUjOWbRnP5cwtkSzYUVoEoo%3D&Expires=1708779204 "
}
]
}電子郵件:[email protected]
我們意識到沒有一個LLM來統治所有這些LLM!