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!