AutoGen多代理对话项目示例和工作流程分析

在这篇文章中,我将介绍AutoGen的多个代理的运行。这些代理将能够相互对话,协作评估股票价格,并使用AmCharts生成图表。

我们创建对话的目的是要求代理分析特定公司的股票价格,并制作股票价格图表。

为了实现这一目标,我们创建一下代理并协同工作:

金融分析师:分析师的任务是获取股票价格数据,进行分析,然后将数据传递给UI设计人员以创建图表。它还负责执行UI设计器的代码来生成和显示图表,并且可以在必要时请求丢失的数据。

软件工程师:软件工程师的主要角色是检索由财务分析师指定的所需天数的股票价格信息的函数。

UI设计师:UI设计师的主要职责是使用Amcharts股票图表库创建股票图表。这包括生成完整的代码,无缝地集成金融分析师提供的股票价格数据,并准备立即执行的代码。

AutoGen

AutoGen是一个框架,它通过使用可以彼此对话的多个代理来共同处理各种任务,从而促进大型语言模型(LLM)应用程序的创建。

AssistantAgent是专门设计用来作为人工智能助手来解决LLM任务的。

UserProxyAgent在每个交互回合中主要寻求人工输入作为其默认响应时进行调用。它还具有执行代码和调用函数的能力。如果不提供人工用户输入,该代理将在识别传入消息中的可执行代码块并自动启动代码执行。可以通过将code_execution_config参数配置为False来禁用代码执行。可以理解为它是代理的代理,可以执行代码并向其他代理提供反馈。

代码样板

首先,我们需要安装AutoGen库。

 !pip install pyautogen~=0.1.0

创建配置文件OAI_CONFIG_LIST。

 [{"model": "gpt-4-32k","api_key": "","api_base": "","api_type": "azure","api_version": "2023-07-01-preview"}]

加载配置文件

 config_list = autogen.config_list_from_json("OAI_CONFIG_LIST",filter_dict={"model": ["gpt-4", "gpt-4-0314", "gpt4", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4-32k-v0314"],},)

创建is_termination_msg函数来检查是否有终止消息来结束对话。

 def is_termination_msg(data):has_content = "content" in data and data["content"] is not Nonereturn has_content and "TERMINATE" in data["content"]

创建函数fetch_prices_for_symbol和fetch_prices检索指定时间范围内的历史价格数据,输入参数symbol和days。

 def fetch_prices_for_symbol(symbol: str, days: int) -> pd.DataFrame:# Define the date range for historical dataend_date = pd.Timestamp.now()start_date = end_date - pd.DateOffset(days=days)# Fetch historical data using yfinancedata = yf.download(symbol, start=start_date, end=end_date)# Reset the index and select relevant columnsdata.reset_index(inplace=True)data = data[["Date", "Open", "High", "Low", "Close", "Volume"]]data.rename(columns={"Date": "date", "Open": "open", "High": "high", "Low": "low", "Close": "close", "Volume": "volume"}, inplace=True)return datadef fetch_prices(days: int, symbol: str) -> str:symbol_df = fetch_prices_for_symbol(symbol, days)symbol_txt = symbol_df.to_string(index=None)    return f"""{symbol_txt}""".strip()

通过llm_config定义LLM推理配置。这里还指定了函数fetch_prices和所需的输入参数days和symbol。

 llm_config = {"config_list": config_list,"use_cache": False,"temperature": 0,"request_timeout": 120,"functions": [{"name": "fetch_prices","description": "Fetch daily stock prices","parameters": {"type": "object","properties": {"days": {"type": "integer","description": "days"},"symbol": {"type": "string","description": "symbol"}},"required": ["days", "symbol"]}}]}

代理配置

各个代理角色的配置主要通过Prompt来进行指定:

1、金融分析师

 analyst_system_message = f"""Financial Analyst: As a Senior Financial Analyst responsible for a stock price indicator.Follow the plan:1. Obtain stock price data from the engineer.2. Analyze the stock price data, provide comment, and pass it to the UI designer for chart creation.3. Upon receiving the code from the UI designer along with the complete dataset, if it's not provided, kindly request the UI designer to provide the missing data.4. Execute the code received from the UI designer on your local machine to generate and display the chart.Upon successful completion of the plan, add "TERMINATE" to conclude the message."""analyst = AssistantAgent(name = "analyst",system_message = analyst_system_message,llm_config=llm_config,is_termination_msg=is_termination_msg,code_execution_config=False)

2、软件工程师

 engineer_system_message = f"""Engineer. You are a Senior Software Engineer that executes the fetch_prices functions as requested by the Financial Analyst."""engineer = AssistantAgent(name="engineer",system_message=engineer_system_message,llm_config=llm_config,function_map={"fetch_prices": fetch_prices},code_execution_config=False)

3、UI设计师

 uidesigner_system_message = f"""UI Designer: You are a Senior UI/UX designer with a specialization in crafting charts using the Amcharts Stock Chart library (referenced at https://www.amcharts.com/demos/stock-chart/), your assignment is to develop a stock chart using the stock price data supplied by the Senior Financial Analyst.Your responsibility involves:1. Generating comprehensive code, including the actual COMPLETE data, for the Senior Analyst to successfully run the chart.2. Ensuring that the code is prepared for immediate execution, WITHOUT ANY PLACEHOLDERS TEXT or missing data.3. Guaranteeing the seamless integration of the received stock price data into the code.4. Structuring the code for execution and utilizing the Amcharts library to present the stock chart."""uidesigner = AssistantAgent(name = "uidesigner",system_message=uidesigner_system_message,code_execution_config=False,  # set to True or image name like "python:3" to use dockerllm_config=llm_config)

4、UserProxyAgent,最后我们在定义一个人工的管理员,这样可以在需要进行人工干预时进行处理

 user_proxy = UserProxyAgent(name="admin",system_message="Human Admin: Let's engage with the analyst to have a discussion about the stock price report. Please ensure that the final report is shared with me for approval before it's finalized.",code_execution_config=False,  # set to True or image name like "python:3" to use dockerhuman_input_mode="NEVER",is_termination_msg=is_termination_msg)

最后我们把这些角色加入到讨论群组中,并且指定讨论的轮数:

 groupchat = autogen.GroupChat(agents=[user_proxy, analyst, uidesigner, engineer], messages=[], max_round=20)manager=autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)

最后就是调用

 message = f"""Analyze stock price for GRAB for the last 30 days and create a chart."""user_proxy.initiate_chat(manager, clear_history=True, message=message)

日志解析

我们来看看并分析他们的对话,这样可以了解到工作的具体细节

对话由人工发起,也就是我们最后调用代码的prompt

 admin (to chat_manager):Analyze stock price for GRAB for the last 30 days and create a chart.

聊天管理器向分析师发送了一条消息,要求检索过去30天的Grab股票数据,并向聊天管理器提供输入参数。随后,聊天管理器将此信息传达给工程师。

 analyst (to chat_manager):***** Suggested function Call: fetch_prices *****Arguments: {"days": 30,"symbol": "GRAB"}

在收到来自聊天管理器的指令后,工程师继续执行该函数,获取输出,然后通知聊天管理器。

 >>>>>>>> EXECUTING FUNCTION fetch_prices...[*********************100%%**********************]  1 of 1 completedengineer (to chat_manager):***** Response from calling function "fetch_prices" *****date  open  high   low  close    volume2023-09-20  3.50 3.545 3.410   3.46  136397002023-09-21  3.45 3.460 3.380   3.46  137814002023-09-22  3.48 3.510 3.425   3.43   87082002023-09-25  3.43 3.470 3.385   3.43  109900002023-09-26  3.38 3.450 3.360   3.40  114588002023-09-27  3.40 3.460 3.360   3.38  209211002023-09-28  3.38 3.490 3.360   3.47  144823002023-09-29  3.52 3.590 3.520   3.54  155802002023-10-02  3.49 3.570 3.460   3.48  198456002023-10-03  3.48 3.480 3.330   3.35  273703002023-10-04  3.32 3.410 3.230   3.40  182243002023-10-05  3.37 3.500 3.370   3.48  169040002023-10-06  3.45 3.520 3.445   3.50   93869002023-10-09  3.47 3.480 3.380   3.44  135126002023-10-10  3.51 3.570 3.500   3.53  114927002023-10-11  3.51 3.550 3.480   3.49  109622002023-10-12  3.54 3.560 3.410   3.47  132169002023-10-13  3.44 3.460 3.360   3.42  254741002023-10-16  3.40 3.460 3.370   3.42  228835002023-10-17  3.39 3.460 3.380   3.43  119978002023-10-18  3.40 3.450 3.320   3.33  105019002023-10-19  3.29 3.330 3.260   3.30  14663405*********************************************************

按照分析师的计划,聊天管理器将股票价格数据提供给UI设计师,UI设计师随后使用AmCharts为图表创建代码,并随后向聊天管理器报告。

 uidesigner (to chat_manager):Here is the complete code for the stock chart using the Amcharts library. The code includes the actual data for the last 30 days of the GRAB stock price.```html<!DOCTYPE html><html><head><title>AmCharts Stock Chart</title><script src="https://www.amcharts.com/lib/3/amcharts.js"></script><script src="https://www.amcharts.com/lib/3/serial.js"></script><script src="https://www.amcharts.com/lib/3/amstock.js"></script><script src="https://www.amcharts.com/lib/3/themes/light.js"></script></head><body><div id="chartdiv" style="width: 100%; height: 500px;"></div><script>var chartData = [{ "date": new Date(2023, 8, 20), "open": 3.50, "high": 3.545, "low": 3.410, "close": 3.46, "volume": 13639700 },{ "date": new Date(2023, 8, 21), "open": 3.45, "high": 3.460, "low": 3.380, "close": 3.46, "volume": 13781400 },{ "date": new Date(2023, 8, 22), "open": 3.48, "high": 3.510, "low": 3.425, "close": 3.43, "volume": 8708200 },{ "date": new Date(2023, 8, 25), "open": 3.43, "high": 3.470, "low": 3.385, "close": 3.43, "volume": 10990000 },{ "date": new Date(2023, 8, 26), "open": 3.38, "high": 3.450, "low": 3.360, "close": 3.40, "volume": 11458800 },{ "date": new Date(2023, 8, 27), "open": 3.40, "high": 3.460, "low": 3.360, "close": 3.38, "volume": 20921100 },{ "date": new Date(2023, 8, 28), "open": 3.38, "high": 3.490, "low": 3.360, "close": 3.47, "volume": 14482300 },{ "date": new Date(2023, 8, 29), "open": 3.52, "high": 3.590, "low": 3.520, "close": 3.54, "volume": 15580200 },{ "date": new Date(2023, 9, 2), "open": 3.49, "high": 3.570, "low": 3.460, "close": 3.48, "volume": 19845600 },{ "date": new Date(2023, 9, 3), "open": 3.48, "high": 3.480, "low": 3.330, "close": 3.35, "volume": 27370300 },{ "date": new Date(2023, 9, 4), "open": 3.32, "high": 3.410, "low": 3.230, "close": 3.40, "volume": 18224300 },{ "date": new Date(2023, 9, 5), "open": 3.37, "high": 3.500, "low": 3.370, "close": 3.48, "volume": 16904000 },{ "date": new Date(2023, 9, 6), "open": 3.45, "high": 3.520, "low": 3.445, "close": 3.50, "volume": 9386900 },{ "date": new Date(2023, 9, 9), "open": 3.47, "high": 3.480, "low": 3.380, "close": 3.44, "volume": 13512600 },{ "date": new Date(2023, 9, 10), "open": 3.51, "high": 3.570, "low": 3.500, "close": 3.53, "volume": 11492700 },{ "date": new Date(2023, 9, 11), "open": 3.51, "high": 3.550, "low": 3.480, "close": 3.49, "volume": 10962200 },{ "date": new Date(2023, 9, 12), "open": 3.54, "high": 3.560, "low": 3.410, "close": 3.47, "volume": 13216900 },{ "date": new Date(2023, 9, 13), "open": 3.44, "high": 3.460, "low": 3.360, "close": 3.42, "volume": 25474100 },{ "date": new Date(2023, 9, 16), "open": 3.40, "high": 3.460, "low": 3.370, "close": 3.42, "volume": 22883500 },{ "date": new Date(2023, 9, 17), "open": 3.39, "high": 3.460, "low": 3.380, "close": 3.43, "volume": 11997800 },{ "date": new Date(2023, 9, 18), "open": 3.40, "high": 3.450, "low": 3.320, "close": 3.33, "volume": 10501900 },{ "date": new Date(2023, 9, 19), "open": 3.29, "high": 3.330, "low": 3.260, "close": 3.30, "volume": 14663405 }];var chart = AmCharts.makeChart("chartdiv", {"type": "stock","theme": "light","dataSets": [{"fieldMappings": [{"fromField": "open","toField": "open"}, {"fromField": "high","toField": "high"}, {"fromField": "low","toField": "low"}, {"fromField": "close","toField": "close"}, {"fromField": "volume","toField": "volume"}],"dataProvider": chartData,"categoryField": "date"}],"panels": [{"showCategoryAxis": false,"title": "Value","percentHeight": 70,"stockGraphs": [{"id": "g1","valueField": "close","comparable": true,"compareField": "close","balloonText": "[[title]]:<b>[[value]]</b>","compareGraphBalloonText": "[[title]]:<b>[[value]]</b>"}],"stockLegend": {"periodValueTextComparing": "[[percents.value.close]]%","periodValueTextRegular": "[[value.close]]"}}, {"title": "Volume","percentHeight": 30,"stockGraphs": [{"valueField": "volume","type": "column","showBalloon": false,"fillAlphas": 1}],"stockLegend": {"periodValueTextRegular": "[[value.close]]"}}],"chartScrollbarSettings": {"graph": "g1"},"chartCursorSettings": {"valueBalloonsEnabled": true,"fullWidth": true,"cursorAlpha": 0.1,"valueLineBalloonEnabled": true,"valueLineEnabled": true,"valueLineAlpha": 0.5},"periodSelector": {"position": "left","periods": [{"period": "MM","selected": true,"count": 1,"label": "1 month"}, {"period": "YYYY","count": 1,"label": "1 year"}, {"period": "YTD","label": "YTD"}, {"period": "MAX","label": "MAX"}]},"dataSetSelector": {"position": "left"},"export": {"enabled": true}});</script></body></html>

This code will generate a stock chart for the GRAB stock price for the last 30 days. The chart includes open, high, low, close prices, and volume. The chart also includes a period selector and a data set selector.


因为这个过程不需要任何的人工干预,所以管理员admin没有任何的输入

admin (to chat_manager):



最后分析师提供对股票价格的分析,并与Admin共享反馈,以及生成的图表代码,发送TERMINATE消息以结束对话。

analyst (to chat_manager):

The stock price data for GRAB over the last 30 days shows a slight decrease in the closing price from 3.46 to 3.30. The highest closing price in this period was 3.54, and the lowest was 3.30. The volume of stocks traded also varied significantly, with the highest volume being 27,370,300 and the lowest being 8,708,200.

The chart created by the UI designer will provide a visual representation of these trends, making it easier to understand the stock’s performance over the last month.

TERMINATE


上面就是AutoGen工作的一个完整的流程## 总结我们用AutoGen创建了多个代理。这些代理协作分析股票价格并使用AmCharts生成图表。软件工程师根据分析师的要求检索股票价格信息。设计师拿到股票价格数据、创建图表。而分析师则可以进行分析。AutoGen的AssistantAgents,促进了代理交互和代码执行,以对话的方式执行复杂的任务。[https://avoid.overfit.cn/post/2a2396c634a04041b1c0ffe6cf047ed1](https://avoid.overfit.cn/post/2a2396c634a04041b1c0ffe6cf047ed1)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/218586.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Unity | Shader基础知识(第一集:unity中最简单的shader)

一、unity的shader unity写的shader并不是真正意义上的shader。 简单解释&#xff1a;真正的shader语言写起来还是麻烦的&#xff0c;unity希望大家写起来简单一点&#xff0c;于是在原来的基础上&#xff0c;给大家优化了一个语言&#xff0c;叫shaderLab&#xff0c;所以我…

四、编写第一个 Shell 脚本

一、编写 Shell 脚本内容 打开文本编辑器&#xff08;可以使用 vi/vim 命令来创建文件&#xff09;&#xff0c;新建一个文件 chaoqing.sh&#xff0c;扩展名为 sh &#xff08;sh 表示 shell&#xff09;&#xff0c;扩展名不影响脚本的运行。 输入一些代码&#xff0c;如下…

OpenHarmony应用编译 - 如何在源码中编译复杂应用

功能简介 在OpenHarmony系统中预安装应用的hap包会随系统编译打包到镜像中&#xff0c;目前有两种编译预安装应用hap包的方式&#xff0c;一种为随系统编译时&#xff0c;编译应用源码生成hap包的方式&#xff0c;另一种是将已生成的hap包放入系统源码中&#xff0c;再进行打包…

跨境电商如何利用跨境客服软件提升销售额

随着全球化的推进&#xff0c;跨境电商成为了许多企业拓展市场的重要途径。然而&#xff0c;跨境电商面临着语言、文化、时差等多种挑战&#xff0c;为了提供更好的客户服务并提升销售额&#xff0c;跨境电商需要利用跨境客服软件。本文将探讨跨境电商如何利用跨境客服软件来提…

HarmonyOS:NativeWindow 开发指导

场景介绍 NativeWindow 是 HarmonyOS 本地平台化窗口&#xff0c;表示图形队列的生产者端。开发者可以通过 NativeWindow 接口进行申请和提交 Buffer&#xff0c;配置 Buffer 属性信息。 针对 NativeWindow&#xff0c;常见的开发场景如下&#xff1a; ● 通过 NativeWindow…

Day35 贪心算法 part04

Day35 贪心算法 part04 860.柠檬水找零 pass 406.根据身高重建队列 pass

小航助学2023年9月电子学会Scratch一级真题(含题库答题软件账号)

需要在线模拟训练的题库账号请点击 小航助学编程在线模拟试卷系统&#xff08;含题库答题软件账号&#xff09; 单选题2.00分 删除编辑附件图文 答案:C 第1题下列哪项内容是不可以修改的&#xff1f;&#xff08; &#xff09; A、角色名称B、造型名称C、舞台名称D、背景名…

巧用RTL原语实现MUX门级映射

对于前端设计人员&#xff0c;经常会需要一个MUX来对工作模式&#xff0c;数据路径进行明确&#xff08;explicit&#xff09;的声明&#xff0c;这个对于中后端工程师下约束也很重要。这里介绍一种巧用的RTL原语&#xff0c;实现MUX的方法。闲言少叙&#xff0c;ICerGo&#x…

如何退回chrome旧版ui界面?关闭Chrome浏览器新 UI 界面

之前启用新UI的方式 Chrome 已经很久没有进行过大的样式修改&#xff0c;但近期在稳定分支中添加了新的 flags 实验性标志&#xff0c;带来了全新的设计与外观&#xff0c;启用方式如下&#xff1a; 在 Chrome 浏览器的搜索栏中输入并访问 chrome://flags 搜索“refresh 2023…

西南科技大学数字电子技术实验六(智力竞赛抢答器的设计及FPGA实现)预习报告

一、计算/设计过程 说明&#xff1a;本实验是验证性实验&#xff0c;计算预测验证结果。是设计性实验一定要从系统指标计算出元件参数过程&#xff0c;越详细越好。用公式输入法完成相关公式内容&#xff0c;不得贴手写图片。&#xff08;注意&#xff1a;从抽象公式直接得出结…

彻底解决公网ip无法访问服务器的问题

用服务器的公网ip访问突然提示页面无法访问了&#xff0c;之前还是ok的&#xff1a; 解决方案&#xff1a; 步骤1. 检查云服务器的安全组规则是否有添加80端口映射&#xff0c;如果没有需要手动添加&#xff0c;否则不能使用公网访问&#xff0c;检查了一下是有的&#xff1…

陵园殡仪馆网站建设的效果如何

陵园墓地的需求度众多周知非常高&#xff0c;无论墓地坑位咨询还是事项/环境展示、资料预览等都是常见事项&#xff0c;由于行业的特殊性&#xff0c;对正常客户来说&#xff0c;并不会知悉各个事项、价格、服务、流程等内容。 而对企业来说&#xff0c;也有获客、品牌扩张等需…

TCP/IP详解——网络基本概念

文章目录 一、网络基本概念1. OSI 7层模型1.1 每层对应的协议1.2 每层涉及的设备1.2.1 物理层设备1.2.2 数据链路层设备1.2.3 网络层设备1.2.4 传输层设备1.2.5 交换机和路由器的应用1.2.6 问题 2. TCP/IP 4层模型3. 物理层传输介质3.1 冲突域 4. 数据链路层4.1 以太网帧结构4.…

GPT-4V 在保险行业的应用

在科技的进步中&#xff0c;人工智能与大数据技术的结合产生了巨大的能量&#xff0c;推动了各行各业的创新与变革。OpenAI&#xff0c;作为全球领先的人工智能研发机构&#xff0c;在今年的9月25日&#xff0c;以一种崭新的方式&#xff0c;升级了其旗下的GPT-4模型。这次的升…

HNU计算机视觉作业一

前言 选修的是蔡mj老师的计算机视觉&#xff0c;上课还是不错的&#xff0c;但是OpenCV可能需要自己学才能完整把作业写出来。由于没有认真学&#xff0c;这门课最后混了80多分&#xff0c;所以下面作业解题过程均为自己写的&#xff0c;并不是标准答案&#xff0c;仅供参考 …

LeetCode力扣每日一题(Java):58、最后一个单词的长度

一、题目 二、解题思路 1、我的思路 先将字符串转换成字符数组 由于我们需要获取最后一个单词的长度&#xff0c;所以我们从后往前遍历字符数组 我们还需判断所遍历的字符是不是字母&#xff0c;即判断每个字符对应的ASCII值即可&#xff0c;用计数器count来储存单词长度 …

1分钟让你快速了解私域运营流量增长底层逻辑!

私域运营说的直白一点&#xff0c;其实就是将用户引入到私域矩阵中&#xff0c;持续性的付费转化&#xff0c;维护好老顾客的同时&#xff0c;并通过他们分享裂变&#xff0c;低成本吸引新客户&#xff0c;再用不同的营销玩法&#xff0c;将新客户转化为老客户的一系列过程。

使用HTTP协议在Linux上进行API调用

在Linux系统上使用HTTP协议进行API调用是一种常见的操作&#xff0c;有时候我们需要调用一些外部API来获取数据或者进行自动化操作。下面是一个使用HTTP协议在Linux上进行API调用的代码示例&#xff0c;希望能够帮助你更好地理解这个过程。 首先&#xff0c;我们需要使用curl命…

【同步FIFO_2023.12.13】

同步fifo&#xff0c;写时钟和读时钟为同一个时钟&#xff0c;用于交互数据缓冲 fifo的深度&#xff1a;同一块数据内存的大小 reg [2:0] Mem [8];//宽度3&#xff0c;深度8典型同步fifo的三部分 fifo写控制逻辑&#xff1a;写地址、写有效信号&#xff0c;fifo写满、写错等状…

金数据企业版:广告推广效率提升的关键,无代码API集成与连接技术

深入理解无代码开发与API集成的重要性 在当今的电商竞争环境下&#xff0c;企业必须寻找提高效率和灵活性的办法。无代码开发平台&#xff0c;如金数据&#xff0c;提供了一种创新的方式来应对快速变化的市场需求&#xff0c;特别是在API集成方面。无代码开发意味着企业可以通…