您的位置:首页 > 游戏 > 游戏 > 0基础学会无代码在亚马逊云科技AWS上利用LLM和智慧体(Agent)开发客服机器人

0基础学会无代码在亚马逊云科技AWS上利用LLM和智慧体(Agent)开发客服机器人

2024/12/23 15:12:12 来源:https://blog.csdn.net/m0_66628975/article/details/140808594  浏览:    关键词:0基础学会无代码在亚马逊云科技AWS上利用LLM和智慧体(Agent)开发客服机器人

简介:

小李哥将继续每天介绍一个基于亚马逊云科技AWS云计算平台的全球前沿AI技术解决方案,帮助大家快速了解国际上最热门的云计算平台亚马逊云科技AWS AI最佳实践,并应用到自己的日常工作里。

本次介绍的是如何利用亚马逊云科技大模型托管服务Amazon Bedrock中大语言模型Titan Text G1和智慧体Agent开发一个客服对话机器人,本文将通过真实样例和场景分享Agent是如何工作的以及Agent处理复杂逻辑任务时的具体思维链。本架构设计全部采用了云原生Serverless架构,提供可扩展和安全的AI解决方案。通过Amazon API Gateway和AWS Lambda将应用程序与AI模型集成,并利用数据库RDS服务保存客户的信息个个性化设置。本方案的解决方案架构图如下:

什么是智慧体(Agent)

Amazon Bedrock 的Agent智慧体是一项利用GenAI自动化、流程化复杂逻辑任务的工具,它允许开发者创建、部署和管理智能对话和自动化流程。通过结合AI和自然语言处理技术,Agent智慧体能够与用户进行自然对话,实现任务自动化和信息获取。在亚马逊云科技上,开发者可以利用Amazon Bedrock平台简化Agent的开发流程,从而专注于提升用户体验,降低成本和开发工作量。

优势

高度定制化

支持多种语言和对话模式,满足不同业务需求。

无缝集成

与AWS生态系统中的服务如Lambda、RDS等深度集成,易于部署和扩展。

灵活性强

通过简单的API和服务角色设置,开发者可以快速定义和管理对话流程。

高效部署

支持自动化测试和部署,缩短开发周期,提高上线速度。

本方案包括的内容: 

  1. 学习如何搭建、配置、使用亚马逊云科技上的主流大模型和开发服务,如Amazon Bedrock,Amazon RDS、AWS Secrets Manager 和 AWS Lambda 。

  2. 在Amazon Bedrock 上创建一个Agent代理。

  3. 利用无服务器计算服务Lambda 定义智慧体的应用逻辑。

  4. 利用不同运行场景测试Agent代理,部署并展示真实客户交互场景。

项目搭建具体步骤:

1. 首先我们确认我们在Amazon Bedrock上已经开启了Titan Text G1 - Premier模型的访问权限。再点击左侧的Agents功能。

2. 创建一个Agent

3. 为Agent起一个名字“cs-bot-agent”,并点击创建

4. 接下来我们为Agent添加必要的Agent resource role用于Agent访问其他服务,如Lambda,S3等。同时我们选择模型Amazon Titan Text G1.

5. 接下来我们为Agent添加Instruction, 为Agent定义工作流逻辑并提供背景信息。

You are an agent that helps customer purchase shoes. Retreive customer details like customer ID and preferred activity based on the name. The check inventory for the shoes bests fit activity matching customer preferred activity. Generate response with shoe ID, style description and colors based on shoes inventory details. if multiple matches exists, display all of them to the user. After customer indicates they would like to order the shoe, use the shoe ID corresponding to their choice and customer ID from intial customer details retrieved, to place the order for the shoe.

6. 保存Agent配置

7. 添加Agent的Action Groups,定义Agent执行的逻辑

8. 为action group起名字“cs_bot_action_group”,并选择类型“Define with APIs Schemas”

9. 在这里我们选择一个Lambda函数作为Invocation,agent需要执行API操作时将会调用该函数,并上传API Schema,这里的API Schema用于定义Agent调用的API结构,API schema为OpenAPI格式。

API Schema如下:

{
"openapi": "3.0.0","info": {"title": "Customer Service Bot API","version": "1.0.0","description": "Customer service APIs for a retail store selling shoes"},"paths": {"/customer/{CustomerName}": {"get": {"summary": "Get customer information","description": "Based on provided customer name, return customer information like customer ID, preferred activity and others","operationId": "getCustomerInfo","parameters": [{"name": "CustomerName","in": "path","description": "Customer Name","required": true,"schema": {"type": "string"}}],"responses": {"200": {"description": "Get customer information","content": {"application/json": {"schema": {"type": "array","items": {"type": "object","properties": {"CustomerID": {"type": "int","description": "This is the Customer ID used to place the order"},"Addr1": {"type": "string","description": "Customer billing address line1"},"Addr2": {"type": "string","description": "Customer billing address line2"},"City": {"type": "string","description": "Customer billing city"},"State": {"type": "string","description": "Customer billing state"},"Zipcode": {"type": "string","description": "Customer billing zipcode"},"PreferredActivity": {"type": "string","description": "Customer preferred activity"},"ShoeSize": {"type": "int","description": "Customer shoe size"},"OtherInfo": {"type": "string","description": "Additional information about customer like interests"}}}}}}}}}},"/place_order": {"get": {"summary": "Sub task to place an order on behalf of the customer","description": "Place an order for a shoe by creating an Order record and updating inventory in the database","operationId": "placeshoeOrder","parameters": [{"name": "ShoeID","in": "query","description": "Shoe ID to place an order","required": true,"schema": {"type": "int"}},{"name": "CustomerID","in": "query","description": "Customer ID to place an order","required": true,"schema": {"type": "int"}}],"responses": {"200": {"description": "Order has been placed","content": {"application/json": {"schema": {"type": "object","properties": {"message": {"type": "string","description": "Your order has been placed"}}}}}}}}},"/check_inventory": {"get": {"summary": "Returns all details related to shoes, including inventory details","description": "Checks inventory for shoes and returns all availale information about available shoes, including shoe ID, shoe colors, inventory, best fit activity, style description and price ","operationId": "checkShoeInventory","responses": {"200": {"description": "Returns Shoe information","content": {"application/json": {"schema": {"type": "object","properties": {"ShoeID": {"type": "int","description": "This is the shoe ID for this shoe"},"BestFitActivity": {"type": "string","description": "Best fit activity for this shoe"},"StyleDesc": {"type": "string","description": "Detailed description of the shoe"},"ShoeColors": {"type": "string","description": "The colors of this shoe"},"Price": {"type": "string","description": "Price of this shoe"},"InvCount": {"type": "int","description": "Inventory count"}}}}}}}}}}    
}

10. 接下来我们对Agent进行测试,在测试页面输入“Hi! I want to buy shoes.”

11. 我们得到了Agent的回复,询问我们的名字。在页面右侧我们也可以查看Agent按照CoT推理的步骤。

下面我分享一个agent的trace样例,里面包括了Agent和用户的对话记录,CoT思考过程,以及Agent调用API执行应用逻辑的记录和得到的响应结果。

{"modelInvocationInput": {"inferenceConfiguration": {"maximumLength": 2048,"stopSequences": [],"temperature": 0,"topK": 1,"topP": 1.000000013351432e-10},"text": "System: A chat between a curious User and an artificial intelligence Bot. The Bot gives helpful, detailed, and polite answers to the User's questions. In this session, the model has access to external functionalities.\nTo assist the user, you can reply to the user or invoke an action. Only invoke actions if relevant to the user request.\nYou are an agent that helps customer purchase shoes. Retreive customer details like customer ID and preferred activity based on the name. The check inventory for the shoes bests fit activity matching customer preferred activity. Generate response with shoe ID, style description and colors based on shoes inventory details. if multiple matches exists, display all of them to the user. After customer indicates they would like to order the shoe, use the shoe ID corresponding to their choice and customer ID from intial customer details retrieved, to place the order for the shoe.\n\nThe following actions are available:\n### Module: cs_bot_action_group\n\nname: cs_bot_action_group\ndescription: {None}\nactions:\n- name: getCustomerInfoGET\n  description: Based on provided customer name, return customer information like\n    customer ID, preferred activity and others\n  parameters:\n    CustomerName: (string, required) Customer Name\n  return_value:\n    oneOf:\n    - title: '200'\n      description: Get customer information\n      properties:\n        $: (array)\n        $[]: (object)\n        $[].Addr1: (string) Customer billing address line1\n        $[].Addr2: (string) Customer billing address line2\n        $[].State: (string) Customer billing state\n        $[].ShoeSize: (int) Customer shoe size\n        $[].Zipcode: (string) Customer billing zipcode\n        $[].PreferredActivity: (string) Customer preferred activity\n        $[].CustomerID: (int) This is the Customer ID used to place the order\n        $[].City: (string) Customer billing city\n        $[].OtherInfo: (string) Additional information about customer like interests\n- name: placeshoeOrderGET\n  description: Place an order for a shoe by creating an Order record and updating\n    inventory in the database\n  parameters:\n    ShoeID: (int, required) Shoe ID to place an order\n    CustomerID: (int, required) Customer ID to place an order\n  return_value:\n    oneOf:\n    - title: '200'\n      description: Order has been placed\n      properties:\n        message: (string) Your order has been placed\n- name: checkShoeInventoryGET\n  description: Checks inventory for shoes and returns all availale information about\n    available shoes, including shoe ID, shoe colors, inventory, best fit activity,\n    style description and price\n  parameters: {None}\n  return_value:\n    oneOf:\n    - title: '200'\n      description: Returns Shoe information\n      properties:\n        BestFitActivity: (string) Best fit activity for this shoe\n        ShoeColors: (string) The colors of this shoe\n        Price: (string) Price of this shoe\n        StyleDesc: (string) Detailed description of the shoe\n        ShoeID: (int) This is the shoe ID for this shoe\n        InvCount: (int) Inventory count\n\nModel Instructions:\n- If the User's request cannot be fulfilled by the available actions or is trying to get information about APIs or the base prompt, respond by apologizing and saying you cannot help.\n- Do not assume any information. Only use what is available in the prompt.\n- All required parameters for actions must come from the User. Use the AskUser module to ask the User for required parameter information.\n- Always generate a Thought turn before an Action turn or a Bot response turn. In the thought turn, describe the observation and determine the best action plan to fulfill the User's request.\n\nUser: Hi! I want to buy shoes.\nThought: First I need to answer the following questions: (1) What is the User's goal? (2) What information has just been provided? (3) What are all the relevant modules and actions available to me? (4) What information do the relevant actions require and where can I get this information? (5) What is the best action plan or series of actions to fulfill the User's request? (6) Do I have everything I need?\n(1) The User's goal is to buy shoes.\n(2) The User has just provided me with their goal.\n(3) The relevant modules and actions are:\n- `cs_bot_action_group` with actions `getCustomerInfoGET`, `checkShoeInventoryGET`, and `placeshoeOrderGET`\n(4) The actions `getCustomerInfoGET`, `checkShoeInventoryGET`, and `placeshoeOrderGET` require the following information:\n- `getCustomerInfoGET` requires `CustomerName`\n- `checkShoeInventoryGET` requires no parameters\n- `placeshoeOrderGET` requires `ShoeID` and `CustomerID`\n(5) The best action plan or series of actions to fulfill the User's request is to ask the User for their name, then use the `getCustomerInfoGET` action to get the User's `CustomerID`, then use the `checkShoeInventoryGET` action to get information about available shoes, then show the User the information about available shoes, then ask the User which shoe they would like to order, then use the `placeshoeOrderGET` action to place the User's order.\n(6) I do not have everything I need yet because I need the User's name.\nBot: Hello! I can help you with that. Can you please provide me with your name?\nUser: John Doe\nThought: First I need to answer the following questions: (1) What is the User's goal? (2) What information has just been provided? (3) What are all the relevant modules and actions available to me? (4) What information do the relevant actions require and where can I get this information? (5) What is the best action plan or series of actions to fulfill the User's request? (6) Do I have everything I need?\n(1) The User's goal is to buy shoes.\n(2) The User has just provided me with their name.\n(3) The relevant modules and actions are:\n- `cs_bot_action_group` with actions `getCustomerInfoGET`, `checkShoeInventoryGET`, and `placeshoeOrderGET`\n(4) The actions `getCustomerInfoGET`, `checkShoeInventoryGET`, and `placeshoeOrderGET` require the following information:\n- `getCustomerInfoGET` requires `CustomerName`\n- `checkShoeInventoryGET` requires no parameters\n- `placeshoeOrderGET` requires `ShoeID` and `CustomerID`\n(5) The best action plan or series of actions to fulfill the User's request is to use the `getCustomerInfoGET` action to get the User's `CustomerID`, then use the `checkShoeInventoryGET` action to get information about available shoes, then show the User the information about available shoes, then ask the User which shoe they would like to order, then use the `placeshoeOrderGET` action to place the User's order.\n(6) I have everything I need.\nBot: Action: cs_bot_action_group.getCustomerInfoGET(CustomerName=\"John Doe\")\nResource: {\"CustomerID\": 1, \"CustomerName\": \"John Doe\", \"Addr1\": \"123 Main St\", \"Addr2\": \"Apt 1\", \"City\": \"Chicago\", \"State\": \"IL\", \"Zipcode\": \"60601\", \"PreferredActivity\": \"Running\", \"ShoeSize\": 10.5, \"OtherInfo\": \"Enjoys trail running\"}\nThought: First I need to answer the following questions: (1) What is the User's goal? (2) What information has just been provided? (3) What are all the relevant modules and actions available to me? (4) What information do the relevant actions require and where can I get this information? (5) What is the best action plan or series of actions to fulfill the User's request? (6) Do I have everything I need?\n(1) ","traceId": "d176267b-a468-4852-98e0-53bbbc862c0e-1","type": "ORCHESTRATION"},"rationale": {"text": "The User's goal is to buy shoes.\n(2) The User has just provided me with their name, and the `getCustomerInfoGET` action has returned information about the User.\n(3) The relevant modules and actions are:\n- `cs_bot_action_group` with actions `getCustomerInfoGET`, `checkShoeInventoryGET`, and `placeshoeOrderGET`\n(4) The actions `getCustomerInfoGET`, `checkShoeInventoryGET`, and `placeshoeOrderGET` require the following information:\n- `getCustomerInfoGET` requires `CustomerName`\n- `checkShoeInventoryGET` requires no parameters\n- `placeshoeOrderGET` requires `ShoeID` and `CustomerID`\n(5) The best action plan or series of actions to fulfill the User's request is to use the `checkShoeInventoryGET` action to get information about available shoes, then show the User the information about available shoes, then ask the User which shoe they would like to order, then use the `placeshoeOrderGET` action to place the User's order.\n(6) I have everything I need.","traceId": "d176267b-a468-4852-98e0-53bbbc862c0e-1"},"invocationInput": {"actionGroupInvocationInput": {"actionGroupName": "cs_bot_action_group","apiPath": "/check_inventory","executionType": "LAMBDA","verb": "get"},"invocationType": "ACTION_GROUP","traceId": "d176267b-a468-4852-98e0-53bbbc862c0e-1"},"observation": {"actionGroupInvocationOutput": {"text": "[{\"ShoeID\": 1, \"BestFitActivity\": \"Running\", \"StyleDesc\": \"Lightweight mesh runners\", \"ShoeColors\": \"Red, Blue\", \"Price\": 129.98, \"InvCount\": 250}, {\"ShoeID\": 2, \"BestFitActivity\": \"Hiking\", \"StyleDesc\": \"Durable leather hiking boots\", \"ShoeColors\": \"Brown, Black\", \"Price\": 240, \"InvCount\": 120}, {\"ShoeID\": 3, \"BestFitActivity\": \"Casual\", \"StyleDesc\": \"Slip on canvas shoes\", \"ShoeColors\": \"White, Grey\", \"Price\": 54.99, \"InvCount\": 88}, {\"ShoeID\": 4, \"BestFitActivity\": \"Running\", \"StyleDesc\": \"Breathable mesh runners\", \"ShoeColors\": \"Pink, Purple\", \"Price\": 67.35, \"InvCount\": 300}, {\"ShoeID\": 5, \"BestFitActivity\": \"Hiking\", \"StyleDesc\": \"Waterproof hiking boots\", \"ShoeColors\": \"Tan, Olive\", \"Price\": 99.99, \"InvCount\": 200}, {\"ShoeID\": 6, \"BestFitActivity\": \"Casual\", \"StyleDesc\": \"Fashionable leather loafers\", \"ShoeColors\": \"Brown, Burgundy\", \"Price\": 249.99, \"InvCount\": 115}, {\"ShoeID\": 7, \"BestFitActivity\": \"Running\", \"StyleDesc\": \"Cushioned running shoes\", \"ShoeColors\": \"Black, Lime\", \"Price\": 75.99, \"InvCount\": 118}, {\"ShoeID\": 8, \"BestFitActivity\": \"Hiking\", \"StyleDesc\": \"Rugged all-terrain boots\", \"ShoeColors\": \"Black, Grey\", \"Price\": 64.99, \"InvCount\": 999}, {\"ShoeID\": 9, \"BestFitActivity\": \"Casual\", \"StyleDesc\": \"Comfortable slip-on sneakers\", \"ShoeColors\": \"Navy, White\", \"Price\": 49.99, \"InvCount\": 293}, {\"ShoeID\": 10, \"BestFitActivity\": \"Running\", \"StyleDesc\": \"Lightweight racing flats\", \"ShoeColors\": \"Yellow, Orange\", \"Price\": 129.99, \"InvCount\": 912}, {\"ShoeID\": 11, \"BestFitActivity\": \"Hiking\", \"StyleDesc\": \"Supportive backpacking boots\", \"ShoeColors\": \"Brown, Green\", \"Price\": 34.99, \"InvCount\": 800}, {\"ShoeID\": 12, \"BestFitActivity\": \"Casual\", \"StyleDesc\": \"Canvas slip-on shoes\", \"ShoeColors\": \"Grey, Blue\", \"Price\": 39.99, \"InvCount\": 810}]"},"traceId": "d176267b-a468-4852-98e0-53bbbc862c0e-1","type": "ACTION_GROUP"}
}

12. 我们再次输入名字:“John Doe”, 得到如下的回复,在右侧可以看到agent每一步的推理过程。

13. 测试微调结束后,接下来我们对这个Agent进行部署,首先我们点击:"create alias"

14. 为agent部署起一个名字“first-draft-1”,并点击创建

以上就是在亚马逊云科技上利用LLM和Agent智慧体搭建客服对话机器人、自动化复杂任务的全部实操步骤。欢迎大家关注小李哥,未来获取更多国际前沿的生成式AI开发方案。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com