LLM自动进行数据分析-论文《Data-Copilot: Bridging Billions of Data and Humans with Autonomous Workflow》笔记

概要

论文《Data-Copilot: Bridging Billions of Data and Humans with Autonomous Workflow》是今年浙大发的一篇论文,对于用户的数据查询请求,Data-Copilot能够自动查询数据并给出一个可视化的分析结果。

论文摘要翻译:金融、气象、能源等各行各业每天都会生成大量的异构数据。人们急切需要一个工具来有效地管理、处理和展示这些数据。DataCopilot通过部署大语言模型(LLMs)来自主地管理和处理海量数据,即它连接不同领域(股票、基金、公司、经济和实时新闻)的丰富数据,满足多样化的用户查询,计算,预测,可视化等需求。只需要输入文字告诉DataCopilot你想看啥数据,无需繁琐的操作,无需自己编写代码, DataCopilot自主地将原始数据转化为最符合用户意图的可视化结果,因为它可以自主地帮你找数据,处理数据,分析数据,画图, 无需人类协助。

Data-Copilot框架的示意图(论文Figure 1)如下:

在这里插入图片描述

Data-Copilot包括接口设计和接口调度两个阶段,示意如下图(论文的Figure 2)

在这里插入图片描述

接口设计Interface Design

Data-Copilot的接口设计包括以下四步,这个过程是离线完成的。

在作者的实验里Data-Copilot自动生成的接口库包括了5类接口:data acquisition, processing, prediction, visualization, and DataFrame manipulation。如下图所示(论文中的Figure 4)。

在这里插入图片描述

Explore Data by Self-Request

这一步根据已有的数据源,让LLM生成不同的请求描述(也就是用户实际上交互时可能会问的问题)

  • 首先为每个数据源生成一个解析文件,用来帮助LLM理解数据。 每个解析文件都包含数据的描述、每列(属性)的名称、访问示例、以及文件的第一行和最后一行。

  • 将解析文件和几个种子请求作为prompt提供给LLM,由LLM输出更多样的请求。(论文没有给出示例prompt,只说受了self-instructLIDA论文 的启发)

Interface definition

这一步是对第一步生成的请求让LLM来设计相应的接口,并在每一次的迭代中结合下面的interface merge逐步优化接口设计。

  • 对于每一个请求会让LLM决定是可以用接口库(最开始为空,在迭代过程中逐渐积累)中已有的接口,还是需要一个新的接口定义来完成请求。
  • 每个迭代中生成的接口是JSON格式的:Interface1={Interface Name:"", Function description:"", Input and Output:""}, Interface2={Interface Name: "", Function description:"", Input and Output:""}, Solution=" You would first get the data using getBankData, then..."。 这里生成的接口完全是由自然语言来定义的函数及参数,是语法无关的;类似于软件架构,只是这一步是由Data-Copipot自动完成的。

在这里插入图片描述

prompt如上图所示(论文中的Figure10上半部分)所示,其具体的prompt内容拷贝如下:

# 
'''
Design_prompt:You are an experienced program designer, I propose a request and you can use the interface in interface library to solve the request or design new interface functions (similar to pseudocode) to implement my request. Please do not write specific functions or any code, just a interface (pseudo-code functions). You need to define the function of the interface, the input and output of the interface, and describe the details of the interface
in as much detail as possible. Finally please tell how to implement my request in language and put new designed interface function in interface_libary, don‘t miss any one. Please answer in following format:
Interface function Library: Interface1={Interface Name: %s, Function description:%s, Input and Output:%s},Interface2={Interface Name: %s, Function description:%s, Input and Output:%s},..}. The solution using these interface function: solution=[%s]
'''

Interface Merging

为了让接口更通用,对于新生成的接口会让LLM考虑能否与接口库中已有接口进行合并,例如在上面的Figure 2图片中的两个接口{Interface: Query-GDP} 和{Interface: Query-CPI}被合并成了一个接口{Interface: Query-GDP-CPI}。

在这里插入图片描述

这里的prompt如上图(论文中的Figure 10下半部分) 所示,其具体的prompt内容拷贝如下:

# 
'''
Merge_prompt:Please check if the functions in the interface library you designed can be merged. Especially for newly designed interfaces in the previous step, ,merging interfaces with similar functions and similar input and output formats into a new interface. You can use argument to control different inputs and outputs. Please export the merged interface library as following:
Interface function Library: Interface1={Interface Name: %s, Function description:%s, Input and Output:%s},
Interface2={Interface Name: %s, Function description:%s, Input and Output:%s},..}.
The merge solution: Merge Solution=[%s]
'''

Interface Implementation

这一步是让LLM为前面生成的接口库中的每个接口生成具体的代码实现。(这一步的prompt如何写的没有给出。个人目前觉得评估这一步生成的代码是否合理也很重要)

接口调度Interface Dispatch

对于用户的数据分析请求,Data-Copilot 通过规划和调用不同的接口,形成了从数据到多种形式结果的工作流程。主要包括以下的三个步骤:Intent Analysis、Planning Workflow、Multi-form Output。

作者开源的代码是金融分析的接口调度的实现demo。

Intent Analysis

为了更准确的分析用户的意图,Data-Copilot首先会去分析用户的意图,先获取当前时间,将用户问题和当前时间作为prompt的一部分,让LLM解析出结构化信息Time、Location、Object、Format。

论文中给出的prompt如下图(论文的Figure 11)
在这里插入图片描述

实际代码中的prompt如下,提供了29个例子(示例prompt里没有要求LLM改写的instruction 中给出 {Time:%s, Location :%s, Object :%s,Format :%s}结构化信息)

"""
Task prompt: 根据今天的日期填入Instruction,改写后的New Instruction必须保持语义完全一致并含有具体对应的时间和具体的指标,请务必将"今天","本季度","今年","近一年"等描述词替换成具体时间例如:2023年05月10日.对于上市公司财务报表,0331为一季报,0630为半年报,0930为三季报,1231为年报.财报指标包括:# current_ratio\t流动比率 # quick_ratio\t速动比率 # netprofit_margin\t销售净利率 # grossprofit_margin\t销售毛利率 # roe\t净资产收益率 # roe_dt\t净资产收益率(扣除非经常损益)# roa\t总资产报酬率 # debt_to_assets 资产负债率 # roa_yearly\t年化总资产净利率  # q_dtprofit\t扣除非经常损益后的单季度净利润 # q_eps\t每股收益(单季度) # q_netprofit_margin\t销售净利率(单季度) # q_gsprofit_margin\t销售毛利率(单季度) # basic_eps_yoy 基本每股收益同比增长率(%) # netprofit_yoy\t归属母公司股东的净利润同比增长率(%)   # q_netprofit_yoy\t归属母公司股东的净利润同比增长率(%)(单季度) # q_netprofit_qoq\t归属母公司股东的净利润环比增长率(%)(单季度) # equity_yoy\t净资产同比增长率\n. 每日技术指标包括:macd, kdj, rsi, boll, cci, turnover_rate, turnover_rate_f, volume_ratio, pe_ttm, pb, ps_ttm, dv_ttm, total_share, float_share, free_share, total_mv, circ_mv.请注意如果Instruction中没有明确要求计算中位数和均值年化收益率等指标,则无需计算均值和中位数或其他指标,只需要画图展示即可. 此外,除非明确开盘价/最高价等,其他情况下股价就是指收盘价.如果Instruction中提及一段时间里的收益率,优先考虑展示累计收益率的走势图.如果instruction中提取某个指数所有成分股的收益率或其他财务指标对比,则展示所有成分股收益率或者对应指标的分布.优先用画图来比较不同股票之间的差异.预测任务的起始时间都从20000101算,且只打印表格\n\nInstruction:今天的日期是2019-03-13,请帮我绘制贵州茅台的2018年1月23日到今天的股价走势图: ###New Instruction: 请帮我绘制贵州茅台的2018年1月23日到2019年3月13日的收盘价的走势图\n\nInstruction:今天的日期是2019-03-13,我想看中证500和科创50今年的指数走势: ###New Instruction: 请帮我绘制中证500和科创50指数收盘价格从2019年1月1日到20190313的走势图\n\nInstruction:今天的日期是2023-05-13,画一下五粮液和泸州老窖从2019年年初到2022年年中的收益率走势: ###New Instruction: 画一下五粮液和泸州老窖从2019年1月1日到2022年6月30日的期间的每天累积收益率走势\n\nInstruction:今天的日期是2023-05-13,画一下格力电器从2019年年初到2022年年中的收益率: ###New Instruction: 画一下格力电器从2019年1月1日到2022年6月30日的期间的每天累积收益率走势\n\nInstruction:今天的日期是2023-05-13,比较洋河集团和泸州老窖从2019年年初到2022年年中的收益率是怎么样的: ###New Instruction: 绘制洋河集团和泸州老窖从2019年1月1日到2022年6月30日的期间的每天累积收益率走势来比较两者这段时间收益率差异\n\nInstruction:今天的日期是2023-05-10,申万二级行业城商行Ⅱ里所有成分股近三个月的收益率: ###New Instruction: 申万二级行业城商行Ⅱ里所有成分股从2023年2月10日到2023年5月10日的收益率分布情况\n\nInstruction:今天的日期是2023-05-10,我想看看中国近十年gdp的走势: ###New Instruction: 我想看看中国从2013年5月10日到2023年5月10日的gdp的走势\n\nInstruction:今天的日期是2023-05-10,易方达旗下的那个名叫张坤的基金经理他管理的基金有几只: ###New Instruction: 易方达旗下的那个名叫张坤的基金经理在2023年5月10日管理的基金有几只\n\nInstruction:今天的日期是2023-05-10,今年一季度贵州茅台的净资产收益率是多少: ###New Instruction: 2023年1月1日到2023年3月31日贵州茅台的净资产收益率是多少\n\nInstruction:今天的日期是2022-05-10,比较下沪深300,创业板指,中证1000指数今年的收益率: ###New Instruction: 绘制沪深300,创业板指,中证1000指数从2022年1月1日到2022年5月10日的收益率走势图来比较三者差异###\n\nInstruction:今天的日期是2019-10-15,上证50的成分股近一年的涨跌幅: ###New Instruction: 展示上证50的每只成分股从2018年10月15日到2019年10月15日期间各自的收益率分布情况\n\nInstruction:今天的日期是2021-04-01,今年北上资金的净流入情况: ###New Instruction: 2021年1月1日到2021年4月1日北上资金的净流入情况\n\nInstruction:今天的日期是2021-04-01,我想看看宁德时代从2019年1月之后半年的股价走势: ###New Instruction: 我想看看宁德时代从2019年1月01到2019年6月30日的每日收盘价股走势图\n\nInstruction:今天的日期是2022-07-11,我想看看现在的新闻或者最新的消息: ###New Instruction: 展示财经网站上当前的最新的财经新闻和市场动态\n\nInstruction:今天的日期是2022-07-11,最新的新闻和财经消息有什么: ###New Instruction: 展示财经网站上当前的最新的财经新闻和市场动态\n\nInstruction:今天的日期是2022-07-11,中国过去十年的cpi走势是什么: ###New Instruction: 展示下2012年7月11日到2022年7月11日的中国每个月的月度cpi同比变化\n\nInstruction:今天的日期是2022-07-11,过去十年中国(我们国家)的GDP: ###New Instruction:  展示下从2012年7月11日到2022年7月11日的中国每个季度的季度GDP增速同比\n\nInstruction:今天的日期是2022-07-11,过去五年中国的货币供应量走势,并且打印保存: ###New Instruction:  展示下从2017年7月11日到2022年7月11日的每个月的货币供应M2的增速同比,并将数据打印保存表格\n\nInstruction:今天的日期是2023-01-23,我想看看周海栋管理的华商优势行业的近三年来的价格走势(净值): ###New Instruction:  查询基金净值,即展示下从2020年1月23日到2023年1月23日期间华商优势行业的基金净值曲线\n\nInstruction:今天的日期是2023-01-23,易方达的张坤管理了几个基金: ###New Instruction:  查询基金经理,展示下2023年01月23日易方达公司的基金经理张坤最新管理的基金名称\n\nInstruction:今天的日期是2023-05-03,基金经理周海栋名下的所有基金今年的收益率情况: ###New Instruction:  查询多只基金收益率,查询基金经理,展示周海栋旗下的管理的每只基金从2023年1月1日到2023年5月3日期间的收益率截面数据\n\nInstruction:今天的日期是2023-05-03,基金经理姜诚名下的所有基金: ###New Instruction:  查询基金经理,展示姜诚管理的基金名称\n\nInstruction:今天的日期是2023-05-03,介绍下贵州茅台,这公司是干什么的,主营业务是什么: ###New Instruction:  简要文字介绍下上市公司贵州茅台和其主营业务\n\nInstruction:今天的日期是2021-05-23,我想比较下中国平安和贵州茅台近十年的净资产回报率: ###New Instruction:  绘制中国平安和贵州茅台从2011年5月23日到2021年5月23日期间的财务指标净资产回报率(roe)走势图来比较两者差异\n\nInstruction:今天的日期是2021-05-23,我想看看下成都银行,杭州银行和成都银行近三年的净利润增速: ###New Instruction:  展示成都银行、杭州银行和宁波银行从2020年5月16日到2023年5月16日的财务指标每年的归母净利润增幅(netprofit_yoy)\n\nInstruction:今天的日期是2021-05-23,今年一季度上证50是成分股的归母净利润同比增速分别是: ###New Instruction:  展示上证50的所有成分股的一季报(2023年03月31日)的财务指标归母净利润增速同比(netprofit_yoy)的分布情况\n\nInstruction:今天的日期是2021-05-23,去年上证50所有成分股年报里净资产回报率的中位数和均值分别是: ###New Instruction:  展示并计算上证50的所有成分股的年报(2022年12月31日)的财务指标净资产回报率(roe)的中位数和均值\n\nInstruction:今天的日期是2021-05-23,看一下近三年宁德时代和贵州茅台的估值变化: ###New Instruction:  展示宁德时代和贵州茅台从2018年5月23日到2021年5月23日的技术指标市盈率估值(pe-ttm)走势\n\nInstruction:今天的日期是2021-05-23,预测未来12个季度的中国季度GDP增速数据: ###New Instruction:  展示基于20000101日到20210523日(今日)中国每个季度的GDP增速的数据预测未来12个季度的数据并打印表格\n\n\n\nInstruction:今天的日期是2023-08-25, 预测未来中国4个季度的GDP增长率 ###New Instruction:
"""

Planning workflow

这一步让LLM规划处一个处理用户请求的流程,通过prompt指导LLM生成固定的JSON格式表示规划中的每一步. 论文中给出的示例prompt如下图(论文的Figure 11)

在这里插入图片描述

开源代码中给出的prompt示例如下:

"""
Task prompt: 请根据给定的指令,选择最合适的task并生成其task_instruction,格式是task1={\'task_name\':\'task_instruction\'},可选的task有四类,包括[fund_task:用于提取和处理关于所有公募基金任务, stock_task: 用于提取和处理关于所有股票价格,指数信息,公司的财务数据等任务, economic_task: 用于提取和处理关于所有中国宏观经济和货币政策等任务以及查询公司和北向资金, visualization_task: 用于一张或者多张绘制K线图,走势图或者输出统计结果]. 生成task_instruction时涉及多个指标可以采用"依次获取","循环获取"等词. 时序数据一般绘制折线图,截面数据通常绘制柱状图,用以下格式输出task1={%s:%s},task2={%s:%s}. 对于预测任务只打印表格\n\nInstruction:请帮我绘制贵州茅台的2018年1月23日到2019年03月13日的收盘价的走势图:  ###Plan: task1={"stock_task":"获取贵州茅台的股价20180123到20190313的收盘价的时序数据"},task2={"visualization_task":"绘制贵州茅台的股价20180123到20190313的股价折线图"}\n\nInstruction:画一下五粮液和泸州老窖从2019年01月1日到2022年06月30日的累积收益率走势来比较两只股票这段时间收益率差异: ###Plan: task1={"stock_task":"依次获取五粮液和泸州老窖从20190101到20220630的每日的累计收益率的时序数据"}, task2={"visualization_task":"在一张图里绘制五粮液和泸州老窖从20190101到20220630的累计收益率折线图"}\n\nInstruction:申万二级行业城商行Ⅱ里所有成分股近三个月的收益率分布情况: ###Plan: task1={"stock_task":"循环获取城商行Ⅱ(申万二级行业)的所有成分股在从20230210日至20230510期间的各自的收益率截面数据"}, task2={"visualization_task":"在一张柱状图里绘制城商行Ⅱ(申万二级行业)的每只成分股在20230210日至20230510期间的收益率"}\n\nInstruction:我想看看中国2013年5月到2023年05月gdp的走势: ###Plan: task1={"economic_task":"获取中国2013年5月到2023年5月的十年GDP时序数据"},task2={"visualization_task":"绘制2013年5月到2023年5月整十年的gdp走势图"}\n\nInstruction:2023年5月10日,易方达旗下的那个名叫张坤的基金经理他管理的基金有几只: ###Plan: task1={"fund_task":"获取今日20230510张坤管理的最新的基金列表数据"},task2={"visualization_task":"统计并输出基金数量"}\n\nInstruction:2023年01月01日到2023年03月30日贵州茅台的净资产收益率是多少: ###Plan: task1={"finanical_task":"获取贵州茅台的2023年1季度的净资产收益率数据"},task2={"visualization_task":"统计并输出贵州茅台2023一季度的净资产收益率"}\n\nInstruction:绘图展示下沪深300,创业板指,中证1000指数从2023年01月01日到2023年05月10日期间的收益率走势图: ###Plan: task1={"stock_task":"依次获取沪深300,创业板指,中证1000指数三个指数2023年1月1日到2023年5月10日的每日累计收益率的时序数据"},task2={"visualization_task":"在一张图里分别绘制沪深300,创业板指,中证1000指数三个指数从2023年1月1日到2023年5月10日的每日的累计收益率折线图"}\n\nInstruction:展示上证50的每只成分股从2022年05月15日到2023年05月15日期间的各自的收益率分布: ###Plan: task1={"stock_task":"循环获取上证50的每只成分股的2022年05月15日到2023年5月15日的各自收益率的截面数据"},task2={"visualization_task":"绘制上证50指数的所有成分股里从2022年05月15日到2023年5月15日的收益率的柱状图"}\n\nInstruction:从2023年01月1日到2023年05月15日北上资金的净流入情况: ###Plan: task1={"economic_task":"依次获取20230101到20230515这段时间北向资金每日净流入和每日累计流入时序数据"},task2={"visualization_task":"在同一张图里绘制北向资金20220101到20230515的每日净流入柱状图和每日累计流入的折线图"}\n\nInstruction:我想看看2022年05月10日到2023年5月10的天坛生物的K线图和macd指标: ###Plan: task1={"stock_task":"依次获取20220510到20230510这段时间天坛生物K线和MACD指标的时序数据"},task2={"visualization_task":"同时绘制天坛生物从2022年05月10日到2023年05月10日k线图和MACD指标图"}\n\nInstruction:2020年01月10日到2020年2月10期间杭州银行的K线图和cci指标: ###Plan: task1={"stock_task":"依次获取20200110到20200210期间杭州银行的K线和cci指标的时序数据"},task2={"visualization_task":"同时绘制杭州银行从2020年01月10日到2020年02月10日k线图和MACD指标图"}\n\nInstruction:滚动展示财经网站上当前的最新的财经新闻和市场动态: ###Plan: task1={"economic_task":"爬取新浪财经当前最新的财经消息和市场动态新闻"},task2={"visualization_task":"滚动显示当前最新的财经消息和市场动态新闻"}\n\nInstruction:展示下2012年7月11日到2022年7月11日的中国每月的cpi同比变化: ###Plan: task1={"economic_task":"获取20120711到20220711期间每月的cpi全国同比变化时序数据"},task2={"visualization_task":"绘制2012年7月至2022年7月中国消费者价格指数CPI的月度同比(全国)折线图"}\n\nInstruction:展示下从2012年7月11日到2022年7月11日的中国每季度的GDP增速同比,并将数据表格打印保存: ###Plan: task1={"economic_task":"获取20120711到20220711中国每季度GDP增速同比数据"},task2={"visualization_task":"绘制2012年二季度至2022年二季度中国季度GDP同比增速折线图,打印并保存2012年二季度至2022年二季度中国季度GDP同比增速表"}\n\nInstruction:查询基金净值,展示下从2020年1月11日到2023年1月11日期间华商优势行业的基金净值曲线: ###Plan: task1={"fund_task":"查询基金净值,获取20200111到20230111期间华商优势行业的基金净值时序数据"},task2={"visualization_task":"绘制从2020年01月11日到2023年01月11日这段时间华商优势行业的基金净值折线图"}\n\nInstruction:查询基金经理,展示下2023年01月23日工银瑞信公司的基金经理杜洋最新管理的基金名称: ###Plan: task1={"fund_task":"查询基金经理,获取截止至20230123基金经理杜洋的所管理的所有基金名列表"},task2={"visualization_task":"打印截止至2023年01月23日基金经理杜洋的管理的所有基金名称表格"}\n\nInstruction:查询多只基金,展示周海栋旗下管理的所有基金从2023年1月1日到2023年5月3日期间的收益率截面数据: ###Plan: task1={"fund_task":"查询多只基金,循环获取从2023010到20230503期间基金经理周海栋名下所有基金的收益率的截面数据"},task2={"visualization_task":"绘制柱状图展示周海栋管理的每只基金在2023年1月1日到2023年5月3日期间的收益率"}\n\nInstruction:查询多只基金收益率走势,展示华商优势行业和易方达蓝筹精选从2023年1月1日到2023年5月16日的累计收益率对比走势图: ###Plan: task1={"fund_task":"查询多只基金收益率走势,依次获取从2023010到20230516期间华商优势行业和易方达蓝筹精选的每日累计收益率截面数据,直接查询基金"},task2={"visualization_task":"在同张图里用折线绘制华商优势行业和易方达蓝筹精选从2023年01月01日到2023年05月16日期间的每日累计收益率走势"}\n\nInstruction:简要文字介绍下上市公司贵州茅台和其主营业务: ###Plan: task1={"economic_task":"获取上市公司贵州茅台和其主营业务介绍信息"},task2={"visualization_task":"打印贵州茅台及其相关信息"}\n\nInstruction:绘图展示中国平安和贵州茅台从2011年5月23日到2021年5月23日期间的净资产回报率(roe): ###Plan: task1={"stock_task":"依次获取中国平安和贵州茅台从2011年5月23日到2021年5月23日期间的净资产回报率(roe)的时序数据"},task2={"visualization_task":"在同张图里用折线绘制中国平安和贵州茅台从2011年5月23日到2021年5月23日期间的净资产回报率roe"}\n\nInstruction:展示成都银行、杭州银行和宁波银行从2020年5月16日到2023年5月16日的财务指标每年的归母净利润增幅: ###Plan: task1={"stock_task":"依次获取成都银行,杭州银行和宁波银行从2020年5月16日到2023年5月16日期间的归母净利润同比增长率(netprofit_yoy)的时序数据"},task2={"visualization_task":"在同张图里用折线绘制成都银行,杭州银行和宁波银行从2020年5月16日到2023年5月16日期间的归母净利润同比增长率"}\n\nInstruction:展示上证50的所有成分股的一季报(2023年03月31日)的财务指标归母净利润增速同比(netprofit_yoy)的分布: ###Plan: task1={"stock_task":"循环获取上证50所有成分股的一季报20230331日的归母净利润增速同比(netprofit_yoy)截面数据"},task2={"visualization_task":"在同张图里用柱状图绘制上证50每只成分股的一季报20230331归母净利润增速同比"}\n\nInstruction:展示并计算上证50的所有成分股的年报(2022年12月31日)的财务指标净资产回报率(roe)的中位数和均值: ###Plan: task1={"stock_task":"循环获取上证50所有成分股的年报202201231日的净资产回报率(roe)的同比增速截面数据"},task2={"visualization_task":"在同张图里用柱状图绘制上证50每只成分股去年报20221231的净资产回报率,并计算所有股票的roe中位数和均值"}\n\nInstruction:展示宁德时代和贵州茅台从2018年5月23日到2021年5月23日的技术指标市盈率估值(pe-ttm)估值走势: ###Plan: task1={"stock_task":"依次获取宁德时代和贵州茅台的从20180523到20210523的技术指标市盈率估值(pe-ttm)时序数据"},task2={"visualization_task":"在一张图里用折线图绘制宁德时代和贵州茅台从20180523到20210523的的每日pe数据"}\n\nInstruction:展示基于20000101日到20210523日(今日)中国每个季度的GDP增速的数据预测未来12个季度的数据并打印表格: ###Plan: task1={"stock_task":"获取从20000101到20210523的季度GDP数据并预测此后的12个月的数据"},task2={"visualization_task":"打印未来12个季度的GDP预测数据表格"}\n\n\n\nInstruction:预测中国从2023年3月31日开始4个季度的GDP增长率 ###Plan:
"""

Multi-form Output

这一步会执行上一步生成的流程,并返回图形、表格或描述性文字等。此外还会让LLM根据输出的结果对整个流程做一个总结。

代码实现里,执行前面生成的规划流程的结果分为三步:

  1. 对三类工具任务stock、fund、economic让LLM根据具体规划t的ask类型选择不同的task相关的函数,再用python执行LLM选择的函数来查询数据。比如stock任务的一个prompt示例如下:

    '''
    Global prompt:  请利用给定的函数一步一步地完成Instruction,每一步你只能从以下函数库中选择一个或者多个无依赖关系的函数,并且为函数生成对应的参数,参数格式要严格按照函数说明,并行地生成对应多个result_i, 后面步骤的函数使用之前的result作为参数输入,以###结尾\n\nFunction Library:  get_stock_prices_data(stock_name: str=\'\', start_date: str=\'\', end_date: str=\'\',freq:str=\'daily\') -> pd.DataFrame:\n """\n        Retrieves the daily/weekly/monthly price data for a given stock name during a specific time period.\n        Args:\n   - start_date(end_date): The start and end date in format \'YYYYMMDD\'.\n    - freq: The frequency: \'daily\', \'weekly\', or \'monthly\'.\n\n        Returns:\n        - pd.DataFrame: A dataframe that contains the many price columns including stock_code, trade_date, open, high, low, close, pre_close(昨天收盘价), change(涨跌额), pct_chg(涨跌幅),vol(成交量),amount(成交额)\n """\n\nget_stock_technical_data(stock_name: str, start_date: str, end_date: str) -> pd.DataFrame:\n """\n        Retrieves the daily technical indicator of a stock including macd turnover rate, volume, pe-ttm, etc. Those technical indicators are usually plotted as subplots in a k-line chart.\n\n             Returns:\n            pd.DataFrame: A DataFrame containing the columns: ts_code, trade_date, close, and many technical indicators\n.   """\n\nget_index_data(index_name: str = \'\', start_date: str = \'\', end_date: str = \'\', freq: str = \'daily\') -> pd.DataFrame:\n """\n        This function retrieves daily, weekly, or monthly data for a given stock index.\n\n        Arguments:\n        - index_name: Name of the index\n     - freq: Frequency \'daily\', \'weekly\', or \'monthly\'\n\n   """\n\nget_index_constituent(index_name:str=\'\',start_date:str =\'\',end_date:str =\'\') -> pd.DataFrame:\n """\n        Query the constituent stocks of basic index (中证500) or a specified SW (申万) industry index\n\n      return:\n    A pandas DataFrame containing the following columns:\n            index_code\n            index_name\n            stock_code: the code of the constituent stock.\n            stock_name:  \n            weight: the weight of the constituent stock.\n    """\n\nget_Financial_data_from_time_range(stock_name:str, start_date:str, end_date:str, financial_index:str=\'\') -> pd.DataFrame:\n #查询财报数据,每年有四个财报"yyyy0331"为一季报,"yyyy0630"为半年报,"yyyy0930"为三季报,"yyyy1231"为年报,不能获取pe-ttm,pb等技术指标. 例如查询某段时间内所有财报:get_Financial_data_from_time_range(\'20190101\',\'20221231\',roe\')返回2019年到2022整三年12份财报里的roe数据\n 例如查询某一期财报:get_Financial_data_from_time_range("600519.SH", "20190331", "20190331", "roe").代表查询2019年一季报中roe数据\n   financial_index: The financial indicator:ROE, netprofit_yoy .\n\n"""\n\ncalculate_stock_index(stock_data: pd.DataFrame, index:str=\'close\') -> pd.DataFrame:\n """\n     Select or Calculate a index for a stock from source dataframe.\n\n     Index : The index could be price indicator: "close", "pct_chg" or technical indicator: "pe-ttm" or "Cumulative_Earnings_Rate"  and "candle_K"    \n    """\n\ncalculate_earning_between_two_time(stock_name: str = \'\', start_date: str = \'\', end_date: str = \'\', index: str = \'close\') -> float:\n """\n        Calculates the rate of return for a specified stock between two dates.\n\n        Args:\n        index: The index used to calculate the stock return. The default is \'close\' for stock and \'adj_nav\' for fund.\n\n      """\n\nloop_rank(df: pd.DataFrame,  func: callable, *args, **kwargs) -> pd.DataFrame:\n """\n        It iteratively applies the given function to each row and get a result using function. It then stores the calculated result in \'new_feature\' column and  sorted the \'new_feature\' column in descending order.\n\n     args:   The input DataFrame only have a single column\n        func : The function to be applied to each row: func(row, *args, **kwargs)\n        *args and **kwargs: arguments for `func` function.\n       Returns:\n   A output DataFrame with three columns: the constant column, input column, and new_feature column.\n\n        """\n\nmerge_indicator_for_same_stock(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame:\n """\n        Merges two DataFrames (two indicators of the same stock). Data of two different stocks cannot be merged. \n\n        Args:\n            df1: DataFrame contains some indicators for stock A.\n            df2: DataFrame contains other indicators for stock A.\n\n        Returns:\n            pd.DataFrame: The merged DataFrame contains two different indicators.\n    """\n\nselect_value_by_column(df1:pd.DataFrame, col_name: str = \'\', row_index: int = -1) -> Union[pd.DataFrame, Any]:\n """\n        Selects a specific column or a specific value within a DataFrame.\n\n        Args:\n       col_name: The name of the column to be selected.\n            row_index: The index of the row to be selected.\n\n  row_index=-1: df1[col_name].to_frame() or df1[col_name][row_index]\n    """\n\n\n\nInstruction: 获取贵州茅台的2018年1月23日到2019年3月13日的每日收盘价的时序数据  ###Function Call: step1={\n "arg1": ["贵州茅台","20180123","20190313","daily"],\n "function1": "get_stock_prices_data",\n "output1": "result1",\n "description1": "贵州茅台每日股价(开盘,收盘,最高,最低等相关价格)的时序数据"\n},step2={\n "arg1": ["result1","close"],\n "function1": "calculate_stock_index",\n "output1": "result2",\n "description1": "贵州茅台的收盘价时序数据"\n}###\n\nInstruction: 依次获取五粮液和泸州老窖从2019年年初到2022年年中的累积收益率时序数据  ###Function Call: step1={\n "arg1": ["五粮液","20190101","20220630","daily"],\n "function1": "get_stock_prices_data",\n "output1": "result1",\n "description1": "五粮液相关价格的时序数据",\n "arg2": ["泸州老窖","20190101","20220630","daily"],\n "function2": "get_stock_prices_data",\n "output2": "result2",\n "description2": "泸州老窖每日相关价格的时序数据"\n},step2={\n "arg1": ["result1","Cumulative_Earnings_Rate"],\n "function1": "calculate_stock_index",\n "output1": "result3",\n "description1": "五粮液的累计收益率时序数据",\n "arg2": ["result2","Cumulative_Earnings_Rate"],\n "function2": "calculate_stock_index",\n "output2": "result4",\n "description2": "泸州老窖的累计收益率的时序数据"\n}\n###\n\nInstruction: 依次获取2022年05月10日到2023年5月10的成都银行K线和macd指标时序数据  ###Function Call: \nstep1={\n "arg1": ["成都银行","20220510","20230510","daily"],\n "function1": "get_stock_prices_data",\n "output1": "result1",\n "description1": "成都银行每日股价(开盘,收盘,最高,最低等相关价格)时序数据",\n "arg2": ["成都银行","20220510","20230510"],\n "function2": "get_stock_technical_data",\n "output2": "result2",\n "description2": "成都银行技术指标的时序数据"\n},step2={\n "arg1": ["result1","candle_K"],\n "function1": "calculate_stock_index",\n "output1": "result3",\n "description1": "成都银行k线图相关的时序数据",\n "arg2": ["result2","macd"],\n "function2": "calculate_stock_index",\n "output2": "result4",\n "description2": "成都银行macd指标的时序数据"\n},step3={\n "arg1": ["result3","result4"],\n "function1": "merge_indicator_for_same_stock",\n "output1": "result5",\n "description1": "成都银行k线指标和macd指标合并后的时序数据"\n}###\n\nInstruction: 循环获取上证50指数的每只成分股里从2023年1月1日到2023年5月10日的收益率截面数据  ###Function Call: step1={\n "arg1": ["上证50","20230101","20230510"],\n "function1": "get_index_constituent",\n "output1": "result1",\n "description1": "上证50的所有成分股列表"\n},step2={\n "arg1": ["result1","stock_name",-1],\n "function1": "select_value_by_column",\n "output1": "result2",\n "description1": "所有上证50成分股的股票名称"\n},step3={\n "arg1": ["result2","calculate_earning_between_two_time","20230101","20230510"],\n "function1": "loop_rank",\n "output1": "result3",\n "description1": "每只成分股的收益率"\n}###请注意get_index_constituent是查询指数所有的成分股信息,get_index_data是获取指数本身的价格信息\n\nInstruction: 循环获取申万二级行业城商行Ⅱ的每只成分股里从2023年1月1日到2023年5月10日的收益率截面数据  ###Function Call: step1={\n "arg1": ["申万二级行业城商行Ⅱ","20230101","20230510"],\n "function1": "get_index_constituent",\n "output1": "result1",\n "description1": "城商行的所有成分股列表"\n},step2={\n "arg1": ["result1","stock_name",-1],\n "function1": "select_value_by_column",\n "output1": "result2",\n "description1": "所有成分股的股票名称"\n},step3={\n "arg1": ["result2","calculate_earning_between_two_time","20230101","20230510"],\n "function1": "loop_rank",\n "output1": "result3",\n "description1": "每只成分股的收益率"\n}###\n\nInstruction: 依次获取沪深300,创业板指这两个指数在20230101到20230510日的每日累计收益率时序数据  ###Function Call: step1={\n "arg1": ["沪深300","20230101","20230510","daily"],\n "function1": "get_index_data",\n "output1": "result1",\n "description1": "沪深300指数的日频指数价格时序数据",\n "arg2": ["创业板指","20230101","20230510","daily"],\n "function2": "get_index_data",\n "output2": "result2",\n "description2": "创业板指数的指数价格时序数据"\n},step2={\n "arg1": ["result1","Cumulative_Earnings_Rate"],\n "function1": "calculate_stock_index",\n "output1": "result4",\n "description1": "沪深300的累计收益率时序数据",\n "arg2": ["result2","Cumulative_Earnings_Rate"],\n "function2": "calculate_stock_index",\n "output2": "result5",\n "description2": "创业板指的累计收益率时序数据"\n}###\n\nInstruction: 获取中证500在20230101到20230510日的指数收盘价格时序数据  ###Function Call: step1={\n "arg1": ["中证500","20230101","20230510","daily"],\n "function1": "get_index_data",\n "output1": "result1",\n "description1": "中证500指数的指数价格时序数据"\n},step2={\n "arg1": ["result1","close"],\n "function1": "calculate_stock_index",\n "output1": "result3",\n "description1": "中证500的收盘价时序数据"\n}###这个指令不需要调用loop_rank和merge_indicator_for_same_stock这些不相干的函数,指数价格一般都是收盘价\n\nInstruction: 依次获取2018年01月01日到2023年5月10日宁德时代和天齐锂业的技术指标市盈率(pe-ttm)时序数据  ###Function Call: step1={\n "arg1": ["宁德时代","20180101","20230510"],\n "function1": "get_stock_technical_data",\n "output1": "result1",\n "description1": "宁德时代的技术指标时序数据",\n "arg2": ["天齐锂业","20180101","20230510"],\n "function2": "get_stock_technical_data",\n "output2": "result2",\n "description2": "天齐锂业的技术指标时序数据"\n},step2={\n "arg1": ["result1","pe_ttm"],\n "function1": "calculate_stock_index",\n "output1": "result3",\n "description1": "宁德时代的pe估值时序数据",\n "arg2": ["result2","pe_ttm"],\n "function2": "calculate_stock_index",\n "output2": "result4",\n "description2": "天齐锂业的pe估值时序数据"\n}###\n\nInstruction: 依次获取中国平安和贵州茅台从2011年5月23日到2021年5月23日期间的净资产回报率(roe)的时序数据  ###Function Call: step1={\n "arg1": ["中国平安","20110523","20210523","roe"],\n "function1": "get_Financial_data_from_time_range",\n "output1": "result1",\n "description1": "中国平安的十年roe时序数据",\n "arg2": ["贵州茅台","20110523","20210523","roe"],\n "function2": "get_Financial_data_from_time_range",\n "output2": "result2",\n "description2": "贵州茅台这十年净资产收益率的时序数据"\n}###\n\nInstruction: 循环获取创业板指所有成分股的20220630日半年报的归母净利润增速同比(netprofit_yoy)截面数据  ###Function Call: step1={\n "arg1": ["创业板指", "20220601", "20220630"],\n "function1": "get_index_constituent",\n "output1": "result1",\n "description1": "当月的创业板指的所有成分股信息"\n},step2={\n "arg1": ["result1", "stock_name", -1],\n "function1": "select_value_by_column",\n "output1": "result2",\n "description1": "创业板指成分股的股票名称"\n},step3={\n "arg1": ["result2","get_Financial_data_from_time_range", "20220630", "20220630", "netprofit_yoy"],\n "function1": "loop_rank",\n "output1": "result3",\n "description1": "创业板指的每只成分股的半年报20220630归母净利润同比增速的截面数据"\n}\n\n\n\nInstruction :依次获取宁德时代和贵州茅台的从20180523到20210523的技术指标市盈率估值(pe-ttm)时序数据 ###Function Call
    '''
    
  2. 让LLM根据请求和数据结果选择具体的可视化函数并用python执行。选择具体函数的prompt如下(这里示意的prompt的previous_result为空,实际上是有数据的):

    '''
    Global prompt:  根据Instruction和之前的结果Previous_result,利用给定的函数一步一步地完成指令,每一步你必须要从以下函数库中选择一个函数,并且生成函数参数。以json的格式生成每一步如下:step1={\n "arg": [arg1,arg2...],\n "function": "%s",\n "output": "%s",\n "description": "%s"\n}###, 每一步可以选择Previous_result中的input的作为参数,或者之前步骤里生成的result作为参数,以###结尾.请注意如果指令中提及多个对象绘制图像则需要多次调用相应函数\n\nFunction Library:  plot_stock_data(stock_data: pd.DataFrame, ax: Optional[plt.Axes] = None, figure_type: str = \'line\', title_name: str =\'\') -> plt.Axes:\n """\n    This function plots stock data.\n\n    Args:\n    - stock_data: the stock data to plot. The DataFrame should contain three columns:\n        - Column 1: trade date in \'YYYYMMDD\'\n        - Column 2: Stock name or code (string format)\n        - Column 3: Index value (numeric format)\n        The DataFrame can be time series data or cross-sectional data. If it is time-series data, the first column represents different trade time, the second column represents the same name. For cross-sectional data, the first column is the same, the second column contains different stocks.\n\n    - ax: matplotlib Axes object, the axes to plot the data on\n    - figure_type: either \'line\' or \'bar\'\n    - title_name\n\n    Returns:\n    - matplotlib Axes object, the axes containing the plot\n    """\n\nplot_k_line(stock_data: pd.DataFrame, title: str = \'\') -> None:\n """\n        Plots a K-line chart of stock price and volume.\n\n        Args:\n            stock_data : A pandas DataFrame containing the stock price information, in which each row\n                represents a daily record. The DataFrame must contain the \'trade_date\',\'open\', \'close\', \'high\', \'low\',\'volume\', \'name\' columns, which is used for k-line and volume.\n                如果dataframe中还含有\'macd\',\'kdj\', \'rsi\', \'cci\', \'boll\',\'pe_ttm\',\'turnover_rate\'等列,则在k线图下方绘制这些指标的子图.\n        """\n\nprint_save_table(df: pd.DataFrame, title_name: str, save:bool = False ,file_path: str = \'./output/\') -> None: """\n        It prints the dataframe and saves it to a CSV file at the specified file path.\n\n        Args:\n        - df: the dataframe to be printed and saved to a CSV file\n        - title_name: the name to be printed and saved\n        - save: whether to save as a CSV file\n        - file_path \n\n      """\n\noutput_mean_median_col(data: pd.DataFrame, col: str = \'new_feature\') -> float:\n # It calculates the mean and median value for the specified column.\n\n\n\nInstruction: 一张图里绘制贵州茅台和宁波银行的2018年1月23日到2019年3月13日的收盘价的折线图, Previous_result: {"input1":"贵州茅台20180123到20190313的收盘价时序数据","input2":"宁波银行20180123到20190313的收盘价时序数据"}  ###Function Call: step1={\n "arg": ["input1", null, "line", "贵州茅台和宁波银行2018年1月23日到2019年3月13日的收盘价图"],\n "function": "plot_stock_data",\n "output": "result1",\n "description": "贵州茅台股价折线图"\n},step2={\n "arg": ["input2", "result1", "line", "贵州茅台和宁波银行2018年1月23日到2019年3月13日的收盘价图"],\n "function": "plot_stock_data",\n "output": "result2",\n "description": "继续绘制宁波银行股价折线图"\n}\n###\n\nInstruction: 在一张图里绘制五粮液和泸州老窖从20190101到20220630的每日累计收益率的折线图, Previous_result: {"input1":"五粮液的每日累计收益率的时序数据","input2":"泸州老窖的每日累计收益率的时序数据"}  ###Function Call: step1={\n "arg": ["input1", null, "line", "五粮液和泸州老窖2019年01月1日到2022年6月30日的收益率图"],\n "function": "plot_stock_data",\n "output": "result1",\n "description": "五粮液收益率折线图"\n},step2={\n "arg": ["input2", "result1", "line", "五粮液和泸州老窖2019年01月1日到2022年6月30日的收益率图"],\n "function": "plot_stock_data",\n "output": "result2",\n "description": "同张图继续绘制泸州老窖收益率折线图"\n}###\n\nInstruction: 同时绘制成都银行从2022年01月1日到2023年05月10日k线图和MACD指标图, Previous_result: {"input1":"成都银行k线指标和macd指标合并的时序数据"}  ###Function Call: step1={\n "arg": ["input1", "The K plot and MACD index for chengduyinhang from 2022/01/01 to 2023/05/10 "],\n "function": "plot_k_line",\n "output": "result1",\n "description": "绘制k线图和macd图"\n}###\n\nInstruction: 绘制上证50指数的所有成分股里从2023年1月1日到2023年5月10日的股价涨跌幅(收益率)的柱状图, Previous_result: {"input1":"上证50所有成分股票的收益率截面数据"}  ###Function Call: step1={\n "arg": ["input1", null ,"bar", "上证50所有成分股在2023年1月1日到2023年5月10日期间的涨跌幅"],\n "function": "plot_stock_data",\n "output": "result1",\n "description": "绘制涨跌幅柱状图"\n}###\n\nInstruction: 在同一张图里绘制北向资金20220510到20230510的每日净流入柱状图和每日累计流入的折线图, Previous_result: {"input1":"北向资金每日的累计净流入量时序数据", "input2":"北向资金每日的净流入量时序数据"}  ###Function Call: step1={\n "arg": ["input1", null ,"line", "北向资金2022年05月10日到2023年05月10日的每日净流入和每日累计流入的折线图"],\n "function": "plot_stock_data",\n "output": "result1",\n "description": "绘制累计流进入的折线图"\n},step2={\n "arg": ["input2", "result1" ,"bar", "北向资金2022年05月10日到2023年05月10日的每日净流入和每日累计流入的折线图"],\n "function": "plot_stock_data",\n "output": "result2",\n "description": "继续绘制每日净流入柱状图"\n}###\n\nInstruction: 在一张柱状图里绘制城商行Ⅱ每个成分股从2023年2月10日到2023年5月10日的收益率分布, Previous_result: {"input1":"城商行Ⅱ的每只成分股的收益率截面数据"}  ###Function Call: step1={\n "arg": ["input1", null ,"bar", "城商行Ⅱ的成分股从2023年2月10日到2023年5月10日的收益率图"],\n "function": "plot_stock_data",\n "output": "result1",\n "description": "绘制收益率图"\n}###\n\nInstruction: 同时绘制2018年01月01日到2023年5月10日宁德时代和天齐锂业的PE估值的折线图, Previous_result: {"input1":"宁德时代的pe估值时序数据", "input2":"天齐锂业的pe估值时序数据"}  ###Function Call: step1={\n "arg": ["input1", null, "line", "2018年01月01日到2023年5月10日宁德时代和天齐锂业的PE估值对比"],\n "function": "plot_stock_data",\n "output": "result1",\n "description": "绘制宁德时代pe走势"\n},step2={\n "arg": ["input2", "result1" ,"line", "2018年01月01日到2023年5月10日宁德时代和天齐锂业的PE估值对比"],\n "function": "plot_stock_data",\n "output": "result2",\n "description": "绘制天齐锂业pe走势"\n}###\n\nInstruction: 绘制2023年1月4日当日全市场所有股票的涨幅前10的股票的收益率的柱状图,打印并保存涨幅前10的股票收益率的表格, Previous_result: {"input1":"当天涨幅前十只股票的收益率的截面数据"}  ###Function Call: step1={\n "arg": ["input1", "2023年1月4日当日全市场所有股票的涨幅前10的股票表","true"],\n "function": "print_save_table",\n "output": "result1",\n "description": "打印并保存表格"\n},step2={\n "arg": ["input1", null, "bar", "2023年1月4日当日全市场所有股票的涨幅前10的股票收益图"],\n "function": "plot_stock_data",\n "output": "result2",\n "description": "绘制涨幅前十股票收益"\n}###\n\nInstruction: 显示最新的财经和市场动态新闻或者打印贵州茅台及其相关信息, Previous_result: {"input1":"当前最新的财经新闻数据"}  ###Function Call: step1={\n "arg": ["input1", "今日财经新闻和市场动态", false],\n "function": "print_save_table",\n "output": "result1",\n "description": "显示新闻"\n}###\n\nInstruction: 绘制从2020年01月11日到2023年01月11日这段时间华商优势精选基金的净值折线图, Previous_result: {"input1":"基金的每日净值的时序数据"}  ###Function Call: step1={\n "arg": ["input1", null , "line", "2020年01月11日到2023年01月11日期间华商优势精选基金的净值走势"],\n "function": "plot_stock_data",\n "output": "result1",\n "description": "绘制净值走势图"\n}###\n\nInstruction: 打印保存截止至2023年01月23日易方达基金经理张坤的管理的所有基金的信息表格, Previous_result: {"input1":"张坤管理的所有基金表格数据"}  ###Function Call: step1={\n "arg": ["input1", "2023年01月23日易方达基金经理张坤管理的所有基金列表", true],\n "function": "print_save_table",\n "output": "result1",\n "description": "张坤名下基金列表"\n}###\n\nInstruction: 绘制柱状图展示周海栋管理的每只基金在2023年1月1日到2023年5月3日期间的收益率, Previous_result: {"input1":"周海栋名下每只基金从20230101到20230503期间的收益率截面数据"}  ###Function Call: step1={\n "arg": ["input1", null, "bar", "2023年01月1日到2023年5月3日期间经理基金周海栋管理的每只基金收益率"],\n "function": "plot_stock_data",\n "output": "result1",\n "description": "收益率柱状图"\n}###\n\nInstruction: 在同张图里用柱状图绘制上证50每只成分股去年报20221231的净资产回报率,并计算所有股票的roe的中位数和均值, Previous_result: {"input1":"上证50指数咋20221231年报中每只成分股roe截面数据"}  ###Function Call: step1={\n "arg": ["input1", null, "bar", "上证50每只成分股去年年报(20221231)的净资产回报率"],\n "function": "plot_stock_data",\n "output": "result1",\n "description": "柱状图绘制每只成分股的roe"\n},step2={\n "arg": ["input1", "new_feature","上证50每只成分股去年年报(20221231)的净资产回报率的中位数是:"],\n "function": "output_mean_median_col",\n "output": "result2",\n "description": "上证50每只成分股去年年报20221231的ROE的中位数"\n}###\n\nInstruction: 展示五粮液和贵州茅台从2013年5月20日到2023年5月20日的净资产回报率roe,并比较它们的平均值和中位数, Previous_result: {"input1":"五粮液的2013年0520到2023年0520的净资产回报率roe","input2":"贵州茅台的2013年0520到2023年0520的净资产回报率roe"}  ###Function Call: step1={\n "arg": ["input1", null, "line", "五粮液和贵州茅台从2013年5月20日到2023年5月20日的净资产回报率roe"],\n "function": "plot_stock_data",\n "output": "result1",\n "description": "绘制五粮液的股价折线图"\n},step2=\n{\n "arg": ["input2", "result1", "line", "五粮液和贵州茅台从2013年5月20日到2023年5月20日的净资产回报率roe"],\n "function": "plot_stock_data",\n "output": "result2",\n "description": "绘制贵州茅台的股价折线图"\n},step3=\n{\n "arg": ["input1", "roe"],\n "function": "output_mean_median_col",\n "output": "result3",\n "description": "计算五粮液近十年ROE的均值和中位数"\n},step4=\n{\n "arg": ["input2", "roe"],\n "function": "output_mean_median_col",\n "output": "result4",\n "description": "贵州茅台近十年ROE的均值和中位数"\n}###\n\n\n\nInstruction: 在一张图里用折线图绘制宁德时代和贵州茅台从20180523到20210523的的每日pe数据, Previous_result: {} ###Function Call
    '''
    
  3. 最后让LLM生成查询过程的总结,prompt相关的代码如下

output_prompt = "请用第一人称总结一下整个任务规划和解决过程,并且输出结果,用[Task]表示每个规划任务,用\{function\}表示每个任务里调用的函数." + \
                    "示例1:###我用将您的问题拆分成两个任务,首先第一个任务[stock_task],我依次获取五粮液和贵州茅台从2013年5月20日到2023年5月20日的净资产回报率roe的时序数据. \n然后第二个任务[visualization_task],我用折线图绘制五粮液和贵州茅台从2013年5月20日到2023年5月20日的净资产回报率,并计算它们的平均值和中位数. \n\n在第一个任务中我分别使用了2个工具函数\{get_stock_code\},\{get_Financial_data_from_time_range\}获取到两只股票的roe数据,在第二个任务里我们使用折线图\{plot_stock_data\}工具函数来绘制他们的roe十年走势,最后并计算了两只股票十年ROE的中位数\{output_median_col\}和均值\{output_mean_col\}.\n\n最后贵州茅台的ROE的均值和中位数是\{\},{},五粮液的ROE的均值和中位数是\{\},\{\}###" + \
                    "示例2:###我用将您的问题拆分成两个任务,首先第一个任务[stock_task],我依次获取20230101到20230520这段时间北向资金每日净流入和每日累计流入时序数据,第二个任务是[visualization_task],因此我在同一张图里同时绘制北向资金20230101到20230520的每日净流入柱状图和每日累计流入的折线图 \n\n为了完成第一个任务中我分别使用了2个工具函数\{get_north_south_money\},\{calculate_stock_index\}分别获取到北上资金的每日净流入量和每日的累计净流入量,第二个任务里我们使用折线图\{plot_stock_data\}绘制来两个指标的变化走势.\n\n最后我们给您提供了包含两个指标的折线图和数据表格." + \
                    "示例3:###我用将您的问题拆分成两个任务,首先第一个任务[economic_task],我爬取了上市公司贵州茅台和其主营业务介绍信息. \n然后第二个任务[visualization_task],我用表格打印贵州茅台及其相关信息. \n\n在第一个任务中我分别使用了1个工具函数\{get_company_info\} 获取到贵州茅台的公司信息,在第二个任务里我们使用折线图\{print_save_table\}工具函数来输出表格.\n"
# ## str_out是 前面的规划和执行结果的总结
output_result = send_chat_request_Azure(output_prompt + str_out + '###', openai_key=openai_key, api_base=api_base,engine=engine)
print(output_result)

总结

Data-Copilot的整个流程都是由LLM生成的,第一部分的接口设计跟self-instruct生成微调指令类似,但是这里是让LLM根据数据和种子请求生成更多的查询请求,并通过迭代的方式LLM对这些请求设计和完善查询涉及到的接口,并生成指定语言的接口实现。第二部分是让LLM规划调用接口库中的哪些接口能够相应用户的请求,执行完相应的接口后展示结果,并进一步让LLM总结整个过程,个人感觉这一步选择具体执行函数的时候,跟ReAct 的思路有点类似。

参考资料

  1. Zhang, Wenqi, Yongliang Shen, Weiming Lu, and Yueting Zhuang. 2023. “Data-Copilot: Bridging Billions of Data and Humans with Autonomous Workflow,” June.
  2. 论文开源代码
  3. 论文作者的知乎文章
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Data-Copilot是一个基于LLM的系统,用于处理与数据相关的任务,连接了数十亿条数据和多样化的用户需求。它独立设计接口工具,以高效地管理、调用、处理和可视化数据。在接收到复杂请求时,Data-Copilot会自主调用这些自设计的接口,构建一个工作流程来满足用户的意图。在没有人类协助的情况下,它能够熟练地将来自不同来源、不同格式的原始数据转化为人性化的输出,如图形、表格和文本。\[3\] Data-Copilot的主要贡献是设计了一个通用系统,将不同领域的数据源和多样化的用户需求连接起来,通过将LLM集成到整个流程中,减少了繁琐的劳动和专业知识。它可以自主管理、处理、分析、预测和可视化数据。当接收到请求时,它将原始数据转化为最符合用户意图的信息性结果。作为设计者和调度者,Data-Copilot包含两个阶段:离线接口设计(设计者)和在线接口调度(调度者)。\[3\] #### 引用[.reference_title] - *1* *3* [Data-Copilot: 大语言模型做你最贴心省事的数据助手](https://blog.csdn.net/weixin_37707435/article/details/131199542)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [智能编码助手Copilot](https://blog.csdn.net/polsnet/article/details/130721089)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值