Skip to content

Commit

Permalink
feat: auto deploy lambda function url and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu-xiaowei committed Nov 18, 2024
1 parent 2aaf76e commit 90f37e5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 35 deletions.
20 changes: 4 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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

Expand Down Expand Up @@ -214,6 +201,7 @@ export API_KEY=<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.
Expand Down
23 changes: 6 additions & 17 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 包含以下默认设置:

Expand All @@ -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)

Expand All @@ -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

Expand Down Expand Up @@ -204,6 +192,7 @@ export API_KEY=<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` 以支持多模态对话。
Expand Down
2 changes: 1 addition & 1 deletion server/template/SwiftChatAppRunner.template
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
}
},
"Outputs": {
"APIURL": {
"API URL": {
"Description": "API URL for SwiftChat App",
"Value": {
"Fn::Join": [
Expand Down
46 changes: 45 additions & 1 deletion server/template/SwiftChatLambda.template
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,61 @@
"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"
},
"Condition": "CDKMetadataAvailable"
}
},
"Outputs": {
"API URL": {
"Description": "API URL for SwiftChat App",
"Value": {
"Fn::GetAtt": [
"APIHandlerFunctionUrlD2500C9A",
"FunctionUrl"
]
}
}
},
"Conditions": {
"CDKMetadataAvailable": {
"Fn::Or": [
Expand Down

0 comments on commit 90f37e5

Please sign in to comment.