From 90f37e58742549fdf4c4d8e2dc10cb1501f938d8 Mon Sep 17 00:00:00 2001 From: xiaoweii Date: Mon, 18 Nov 2024 16:54:52 +0800 Subject: [PATCH] feat: auto deploy lambda function url and update readme --- README.md | 20 ++------- README_CN.md | 23 +++-------- server/template/SwiftChatAppRunner.template | 2 +- server/template/SwiftChatLambda.template | 46 ++++++++++++++++++++- 4 files changed, 56 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index daff40f..90ccb43 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ SwiftChat comes with the following default settings: [![Launch Stack](assets/launch-stack.png)](https://console.aws.amazon.com/cloudformation/home#/stacks/create/template?stackName=SwiftChatAPI&templateURL=https://aws-gcr-solutions.s3.amazonaws.com/swift-chat/latest/SwiftChatAppRunner.template) - - Lambda (Requires manual setup of Function URL with NONE auth) + - Lambda (Note: Make sure your AWS account allows public Lambda Function URL) [![Launch Stack](assets/launch-stack.png)](https://console.aws.amazon.com/cloudformation/home#/stacks/create/template?stackName=SwiftChatLambda&templateURL=https://aws-gcr-solutions.s3.amazonaws.com/swift-chat/latest/SwiftChatLambda.template) @@ -77,21 +77,8 @@ SwiftChat comes with the following default settings: acknowledge that AWS CloudFormation might create IAM resources" checkbox at the bottom. 4. Click **Next**, In the "Review and create" Review your configuration and click **Submit**. -Wait about 3-5 minutes for the deployment to finish, and get your API URL: - -**For App Runner**, click the CloudFormation stack and go to **Outputs** tab, you can find the **API URL** -(which looks like `https://xxx.xxx.awsapprunner.com`) - -**For Lambda**, follow these steps to get your **API URL**: - -1. Find your Lambda function in CloudFormation -> **Resources** -> APIHandler -2. Click to open the Lambda function (`SwiftChatLambda-xxx`) -3. Set up Function URL: - - Go to **Configuration** -> **Function URL**, Click **Create function URL** - - Select: **Auth type**: `NONE` - - Enable: **Invoke mode**: `RESPONSE_STREAM` under Additional settings -4. Click Save to get your API URL (`https://xxx.lambda-url.xxx.on.aws`) -5. Note: We use Bearer Token for API security verification. You can upgrade to `AWS_IAM` Auth type manually. +Wait about 3-5 minutes for the deployment to finish, then click the CloudFormation stack and go to **Outputs** tab, you +can find the **API URL** which looks like: `https://xxx.xxx.awsapprunner.com` or `https://xxx.lambda-url.xxx.on.aws` ### Step 3: Download the app and setup with API URL and API Key @@ -214,6 +201,7 @@ export API_KEY= ``` This API is used to implement streaming conversations, and it only returns the text required for display. + The `messages` under body fully complies with the messages structure specification in Amazon Bedrock [converse stream](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/bedrock-runtime/client/converse_stream.html) API. You can also add `image` or `document` according to the specification to support multimodal conversations. diff --git a/README_CN.md b/README_CN.md index 24a41bc..cab9eca 100644 --- a/README_CN.md +++ b/README_CN.md @@ -34,7 +34,8 @@ macOS 等多个平台。 ### 前置条件 请确保您有权限访问 Amazon Bedrock 基础模型。 -您可以参考 [Amazon Bedrock 用户指南](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access-modify.html) 来启用模型。 +您可以参考 [Amazon Bedrock 用户指南](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access-modify.html) +来启用模型。 SwiftChat 包含以下默认设置: @@ -60,7 +61,7 @@ SwiftChat 包含以下默认设置: [![启动堆栈](assets/launch-stack.png)](https://console.aws.amazon.com/cloudformation/home#/stacks/create/template?stackName=SwiftChatAPI&templateURL=https://aws-gcr-solutions.s3.amazonaws.com/swift-chat/latest/SwiftChatAppRunner.template) - - Lambda (需要手动设置无认证的 Function URL) + - Lambda (提示:请确保你的 AWS 账户允许公开 Lambda Function URL) [![启动堆栈](assets/launch-stack.png)](https://console.aws.amazon.com/cloudformation/home#/stacks/create/template?stackName=SwiftChatLambda&templateURL=https://aws-gcr-solutions.s3.amazonaws.com/swift-chat/latest/SwiftChatLambda.template) @@ -70,21 +71,8 @@ SwiftChat 包含以下默认设置: 3. 点击**下一步**,保持"配置堆栈选项"页面默认设置,阅读功能并勾选底部的"我确认,AWS CloudFormation可能会创建 IAM 资源"复选框。 4. 点击**下一步**,在"审核并创建"中检查配置并点击**提交**。 -等待约3-5分钟部署完成,然后获取 API URL: - -**对于使用 App Runner**,点击CloudFormation堆栈并转到**输出**选项卡,您可以找到**API URL**( -类似`https://xxx.xxx.awsapprunner.com`) - -**对于使用 Lambda**,按照以下步骤获取**API URL**: - -1. 在 CloudFormation -> **资源** -> APIHandler中找到您的Lambda函数 -2. 点击打开 Lambda 函数(`SwiftChatLambda-xxx`) -3. 设置 Function URL: - - 转到 **配置** -> **函数 URL**,点击**创建函数 URL** - - 选择: **授权类型**: `NONE` - - 启用: **调用模式**: 在其他设置下选择`RESPONSE_STREAM` -4. 点击保存获取API URL(`https://xxx.lambda-url.xxx.on.aws`) -5. 注意: 我们默认使用 Bearer Token 进行 API 安全验证。您可以手动升级到`AWS_IAM`认证类型。 +等待约3-5分钟部署完成,然后点击CloudFormation堆栈并转到**输出**选项卡,您可以找到**API URL** +类似`https://xxx.xxx.awsapprunner.com` 或 `https://xxx.lambda-url.xxx.on.aws`。 ### 第3步: 下载应用并设置 API URL 和 API Key @@ -204,6 +192,7 @@ export API_KEY= }' ``` 此 API 用于实现流式对话,它仅返回显示所需的文本。 + Body 中的 `messages` 完全符合 Amazon Bedrock [converse stream](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/bedrock-runtime/client/converse_stream.html) API 中的消息结构规范。您还可以根据规范添加 `image` 或 `document` 以支持多模态对话。 diff --git a/server/template/SwiftChatAppRunner.template b/server/template/SwiftChatAppRunner.template index ba31f7d..d47d0f5 100644 --- a/server/template/SwiftChatAppRunner.template +++ b/server/template/SwiftChatAppRunner.template @@ -294,7 +294,7 @@ } }, "Outputs": { - "APIURL": { + "API URL": { "Description": "API URL for SwiftChat App", "Value": { "Fn::Join": [ diff --git a/server/template/SwiftChatLambda.template b/server/template/SwiftChatLambda.template index fb8da65..3a9ef40 100644 --- a/server/template/SwiftChatLambda.template +++ b/server/template/SwiftChatLambda.template @@ -150,10 +150,43 @@ "aws:cdk:path": "SwiftChatAPILambda/APIHandler/Resource" } }, + "APIHandlerFunctionUrlD2500C9A": { + "Type": "AWS::Lambda::Url", + "Properties": { + "AuthType": "NONE", + "InvokeMode": "RESPONSE_STREAM", + "TargetFunctionArn": { + "Fn::GetAtt": [ + "APIHandler68F11976", + "Arn" + ] + } + }, + "Metadata": { + "aws:cdk:path": "SwiftChatAPILambda/APIHandler/FunctionUrl/Resource" + } + }, + "APIHandlerinvokefunctionurl0B3BB28F": { + "Type": "AWS::Lambda::Permission", + "Properties": { + "Action": "lambda:InvokeFunctionUrl", + "FunctionName": { + "Fn::GetAtt": [ + "APIHandler68F11976", + "Arn" + ] + }, + "FunctionUrlAuthType": "NONE", + "Principal": "*" + }, + "Metadata": { + "aws:cdk:path": "SwiftChatAPILambda/APIHandler/invoke-function-url" + } + }, "CDKMetadata": { "Type": "AWS::CDK::Metadata", "Properties": { - "Analytics": "v2:deflate64:H4sIAAAAAAAA/02MwQqCQBRFv6X9+FIjaJ0RtBP7gHiOr3jqzIuZMZHBfw+VoNU5i3NvDtkxg3SHo0900yU91xDvAXWniqct0aGhQE7h6B+RtIOK3uI5iJvO6EkxGoiV9LTkK0vpWU/rerVZ9WjqBiFeRHfkbgZfdB2sDix2yX4+z6oiL4PT29mfF2IbXhtlpSFo/f6TnSBP4bBrPXPiBhvYEFQbv6y3L9bWAAAA" + "Analytics": "v2:deflate64:H4sIAAAAAAAA/02Ny2rDQAxFvyX7sRInFLJuSiGrGJeuizJWi+KZUZBmGoLxvxdPH3R1Dpcr3S20Dy1sVnizxg9jE/gM00tGP7rDe+pQMVImdXizt4m8Qk9XMc6i90c0cowRpl4CLfXKTgL7e72uNruA8TwgTE/iR9JjxA96LslnlrTU/vxXXjUs+Q860shmLGmeXU8mRX1dO5V8Lbnu/ksPkgZevswuyUBwsfVnu4ftBnarizE3WlLmSNB/8wutZ8HgAQEAAA==" }, "Metadata": { "aws:cdk:path": "SwiftChatAPILambda/CDKMetadata/Default" @@ -161,6 +194,17 @@ "Condition": "CDKMetadataAvailable" } }, + "Outputs": { + "API URL": { + "Description": "API URL for SwiftChat App", + "Value": { + "Fn::GetAtt": [ + "APIHandlerFunctionUrlD2500C9A", + "FunctionUrl" + ] + } + } + }, "Conditions": { "CDKMetadataAvailable": { "Fn::Or": [