一文教你在亚马逊AWS上搭建生成式AI应用

随着AI的发展,人们已经可以借助现有的云服务开发生成式AI软件应用。哪怕是0基础的新手用户也可以很快上手搭建属于自己的AI应用,本文就以Meta Llama 7B为例,带大家一起来在亚马逊云科技AWS上搭建生成式AI云原生问答机器人,希望可以对大家有所帮助,仅供参考。

本文涉及到亚马逊云服务:

1、Amazon S3

Amazon S3(Simple Storage Service)是一个高度可扩展的对象存储服务,适用于存储和检索任何数量的数据。目前亚马逊官网提供免费套餐包括5GB标准存储、20000个Get请求、2000个PUT/COPY/POST或LIST请求、100 GB的数据传出量,免费试用12个月。

Amazon S3

2、Amazon Lambda

Amazon Lambda是一种无服务器计算服务,允许用户运行代码而无需预置或管理服务器。目前亚马逊官网提供AWS Lambda永久免费使用服务,每月免费提供100万个请求。

Amazon Lambda

 

3、Amazon CloudFront

Amazon CloudFront是一种内容分发网络(CDN)服务,能够快速将数据、视频、应用程序和API安全地传递给全球客户。目前亚马逊官网提供免费套餐包括每月传出1 TB数据至互联网,每月1000万个HTTP或HTTPS请求,每月200万次CloudFront函数调用,每月200万次CloudFront KeyValueStore读取,永久免费使用。

Amazon CloudFront

 

4、Amazon API Gateway

Amazon API Gateway是一种完全托管的服务,使开发者能够轻松创建、发布、维护、监控和保护API。目前亚马逊官网提供免费套餐包括每月接收100万次API调用,使用时长为12个月。

Amazon API Gateway

新用户注册亚马逊云科技账户,在控制台中试用免费套餐产品。

点击获取:亚马逊免费云服务

相关推荐:《如何注册亚马逊AWS账号

一、配置需要的亚马逊云服务

1、进入亚马逊云科技官网,注册并登录账号。

2、进入AWS控制台,找到Amazon Lambda,点击Lambda函数“endpoint_test_function”。

配置需要的亚马逊云服务

3、进入AWS Lambda配置页面,配置Lambda函数。

配置需要的亚马逊云服务

4、点击“Edit”修改Lambda函数的基础配置。

配置需要的亚马逊云服务

5、修改Timeout时间到1分钟。

注:Amazon Lambda的Timeout配置是函数处理请求的超时时间限额,Lamda可配置的最长超时时间为15分钟,默认时间是3秒。

配置需要的亚马逊云服务

6、为lamda函数中的代码配置环境变量,点击“Edit”。

配置需要的亚马逊云服务

7、将Llama 7B模型API节点URL复制到Value部分。

配置需要的亚马逊云服务

二、搭建生成式AI云原生Serverless问答机器人

1、进入Amazon Lambda中查看调用AI大语言模型的Python代码,如下:

# Import necessary libraries
import json
import boto3
import os
import re
import logging

# Set up logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)

# Create a SageMaker client
sagemaker_client = boto3.client("sagemaker-runtime")

# Define Lambda function
def lambda_handler(event, context):
# Log the incoming event in JSON format
logger.info('Event: %s', json.dumps(event))

# Clean the body of the event: remove excess spaces and newline characters
cleaned_body = re.sub(r'\s+', ' ', event['body']).replace('\n', '')

# Log the cleaned body
logger.info('Cleaned body: %s', cleaned_body)

# Invoke the SageMaker endpoint with the cleaned body as payload and content type as JSON
response = sagemaker_client.invoke_endpoint(
EndpointName=os.environ["ENDPOINT_NAME"],
ContentType="application/json",
Body=cleaned_body
)

# Load the response body and decode it
result = json.loads(response["Body"].read().decode())

# Return the result with status code 200 and the necessary headers
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST'
},
'body': json.dumps(result)
}

2、进入进入AWS S3存储桶查看前端代码。

前端代码如下:

# Import necessary libraries
import json
import boto3
import os
import re
import logging

# Set up logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)

# Create a SageMaker client
sagemaker_client = boto3.client("sagemaker-runtime")

# Define Lambda function
def lambda_handler(event, context):
# Log the incoming event in JSON format
logger.info('Event: %s', json.dumps(event))

# Clean the body of the event: remove excess spaces and newline characters
cleaned_body = re.sub(r'\s+', ' ', event['body']).replace('\n', '')

# Log the cleaned body
logger.info('Cleaned body: %s', cleaned_body)

# Invoke the SageMaker endpoint with the cleaned body as payload and content type as JSON
response = sagemaker_client.invoke_endpoint(
EndpointName=os.environ["ENDPOINT_NAME"],
ContentType="application/json",
Body=cleaned_body
)

# Load the response body and decode it
result = json.loads(response["Body"].read().decode())

# Return the result with status code 200 and the necessary headers
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST'
},
'body': json.dumps(result)
}

3、在AWS CDN Cloudfront中获取问答机器人UI的URL。

搭建问答机器人

4、将URL复制到浏览器中,打开后出现问答机器人的UI。

搭建问答机器人

5、进入到Amazon API Gateway中,获取Invoke URL。

搭建问答机器人

6、在以下页面填入Invoke URL和大家想问的问题,就可以得到Llama 7B的模型回复了。

搭建问答机器人

相关推荐:《亚马逊云科技通过SageMaker JumpStart AI部署Llama 3模型

使用教程

n8n创建微博热点定时发送工作流教程

2025-5-21 11:22:07

使用教程

腾讯云如何绑定域名?

2023-12-13 13:22:50

相关推荐