资料网站怎么做,婚庆公司取名大全集,wordpress单栏,原型图网站LLM是不存在真正逻辑的且并不是知晓万事万物的#xff08;至少目前是这样#xff09;在很多更垂直的环境下LLM并不能很好的赋能。 function calling的实现使LLM可以对接真正的世界以及真正有逻辑的系统#xff0c;这将很大程度上改变LLM的可用范围#xff08;当然安全问题依…LLM是不存在真正逻辑的且并不是知晓万事万物的至少目前是这样在很多更垂直的环境下LLM并不能很好的赋能。 function calling的实现使LLM可以对接真正的世界以及真正有逻辑的系统这将很大程度上改变LLM的可用范围当然安全问题依旧是不容忽视的事。 
环境简述 
model: gpt-3.5-turbo api: 理杏仁开放平台 
tools 
首先我们需要分析需求涉及到什么逻辑功能函数并且将该功能的用途参数及参数的用途进行格式化整理json/yaml格式。 针对理杏仁的使用以下为一个简单的需求 
可以获取到某天某指数的PE-TTM的当前分位点 
针对上述需求可以拆分出如下几点 
针对需求有两个api是我们将会调用的基础信息获取所有指数基本面数据获取日期的指数数据我们需要保证user指定的指数是存在的基础信息api从中拿到指数代码当无法找到时结束当前对话【get_code】我们需要指定日期user可能会使用类似今天昨天等词汇我们需要自构建函数可以处理日期【date】当我们有了指数代码和日期后便可以调用基本面数据api获得对应的数据【get_cvpos】 
step1: 构建函数 
针对上述分析我们需要三个函数其中两个函数主要工作是调用apiget_code 和 get_cvpos另外一个是获取当前日期的函数date 具体函数内容如下: 
def get_code(self, name: str):print(fname: {name})response  requests.post(f{self.url}, jsonself.base_json).json()for item in response[data]:if item[name]  name:return {stockcode: item[stockCode]}return {stockcode: f未找到{name}, 请检查输入}def get_cvpos(self, stockcode: str, date: str):print(fstockcode: {stockcode}, date: {date})data  self.base_jsondata.update({date: date,stockCodes: [stockcode],metricsList: [pe_ttm.y5.mcw.cvpos]})response  requests.post(urlf{self.url}/fundamental, jsondata)response  response.json()if not response[data]:return {cvpos: f所指定日期暂无数据请确定{date}是否为交易日}return {cvpos: response[data][0][pe_ttm.y5.mcw.cvpos]}def date(self, delta: str  0):print(fdelta: {delta})day  datetime.date.today()  datetime.timedelta(int(delta))return {date: day}有了函数之后我们需要通过用户的输入来判断我们什么时间需要调用哪一个函数且需要判断写入什么样的参数。从日常语言转换为结构化数据是一个复杂的过程当然我们可以通过各种方式约束用户的描述例如表单等但随着需要的参数越来越多逻辑就可能会越来越复杂。 
step2: 传递tools 
function calling就可以很好的实现自然语言到结构化数据的转变我们只需要将每个函数的作用及调用规则告诉模型即可模型会根据user的问题返回需要调用的函数及作为参数的结构化数据。 针对上述需求我们构建出如下 TOOLS 来描述函数 
TOOLS  [{type: function,function: {name: get_code,description: 获取某个指数的代码,parameters: {type: object,properties: {name: {type: string,description: 指数名称, 例如: 沪深300, 上证50,}},required: [name],}}},{type: function,function: {name: date,description: 获取前天/昨天/今天的日期, 如果直接指定了日期则无需调用该function,parameters: {type: object,properties: {delta: {type: string,description: 今天为0, 昨天为-1, 前天为-2}}}}},{type: function,function: {name: get_cvpos,description: 获取某天给定代码的指数指标,parameters: {type: object,properties: {stockcode: {type: string,description: 所给定的指数代码},date: {type: string,description: 日期, 格式为2024-06-25}},required: [code, date],}}}
]TOOLS的内容最终也会作为prompt的一部分交给大模型因此使用时也需要考虑token消耗带来的费用。 我们只需要将TOOLS通过tools参数传递给模型即可。 
response  client.chat.completions.create(modelmodel,messagesmessages,temperature0,seed1024,tool_choiceauto,tools  TOOLS)step3: 模型响应内容及函数调用 
user提问可以告诉我沪深300的代码吗? 正常情况下模型将会返回如下结果 
{content: null,role: assistant,function_call: null,tool_calls: [{id: call_WeIPPDsWwzOLH90IV0dSEp1q,function: {arguments: {\name\:\沪深300\},name: get_code},type: function}]
}在返回的结果中我们可以清楚的知道对于user的提问需要要调用get_code函数且入参为{“name”: “沪深300”}此时我们便可以轻松使用 getattr() 的方式来调用函数。 
messages完整流程 
user的一次对话可能会不止一次调用模型因此需要使用列表存储过程中产生的所有message来保证对话的连续。如下 
messages  [{role: system, content: 你是一个指数查询器此处的指数是指金融上的指数},{role: user, content: xxxx}]
response  self.get_completion(messages)
messages.append(response)while (response.tool_calls is not None):for tool_call in response.tool_calls:print(f start function {tool_call.function.name} )args  json.loads(tool_call.function.arguments)result  getattr(self.tools, tool_call.function.name)(**args)messages.append({tool_call_id: tool_call.id,  # 用于标识函数调用的 IDrole: tool,name: tool_call.function.name,content: str(result)  # 数值result 必须转成字符串})response  self.get_completion(messages)messages.append(response)print( 最终回复 )
print(response.content)结果展示 
user提供的prompt为请告诉我昨天沪深300的数据 注该文章完成时间为2024年7月16日因此昨天应为2024-07-15 
1. 最终回复 
昨天2024年7月15日沪深300指数的收盘点位为0.4026。2. 完整打印 
{role: system,content: 你是一个指数查询器此处的指数是指金融上的指数
}
{role: user,content: 请告诉我昨天沪深300的数据
}
{content: null,role: assistant,function_call: null,tool_calls: [{id: call_fLWvI0BVRsuzUEj4TTwvAnke,function: {arguments: {\delta\:\-1\},name: date},type: function}]
}
{tool_call_id: call_fLWvI0BVRsuzUEj4TTwvAnke,role: tool,name: date,content: {date: datetime.date(2024, 7, 15)}
}
{content: null,role: assistant,function_call: null,tool_calls: [{id: call_eDB0nZu5V5xLAZWs3osXdAXS,function: {arguments: {\name\:\沪深300\},name: get_code},type: function}]
}
{tool_call_id: call_eDB0nZu5V5xLAZWs3osXdAXS,role: tool,name: get_code,content: {stockcode: 000300}
}
{content: null,role: assistant,function_call: null,tool_calls: [{id: call_XtS9TkIngpdCVlRLNx5kYmeh,function: {arguments: {\stockcode\:\000300\,\date\:\2024-07-15\},name: get_cvpos},type: function}]
}
{tool_call_id: call_XtS9TkIngpdCVlRLNx5kYmeh,role: tool,name: get_cvpos,content: {cvpos: 0.40264026402640263}
}
{content: 昨天2024年7月15日沪深300的收盘指数为0.4026。,role: assistant,function_call: null,tool_calls: null
}
 文章转载自: http://www.morning.zqnmp.cn.gov.cn.zqnmp.cn http://www.morning.bwmq.cn.gov.cn.bwmq.cn http://www.morning.xkhxl.cn.gov.cn.xkhxl.cn http://www.morning.ldnrf.cn.gov.cn.ldnrf.cn http://www.morning.banzou2034.cn.gov.cn.banzou2034.cn http://www.morning.lqws.cn.gov.cn.lqws.cn http://www.morning.swkpq.cn.gov.cn.swkpq.cn http://www.morning.qpqb.cn.gov.cn.qpqb.cn http://www.morning.rfmzs.cn.gov.cn.rfmzs.cn http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn http://www.morning.fpjw.cn.gov.cn.fpjw.cn http://www.morning.qgkcs.cn.gov.cn.qgkcs.cn http://www.morning.fthqc.cn.gov.cn.fthqc.cn http://www.morning.wnjbn.cn.gov.cn.wnjbn.cn http://www.morning.xjqrn.cn.gov.cn.xjqrn.cn http://www.morning.mtdfn.cn.gov.cn.mtdfn.cn http://www.morning.mszls.cn.gov.cn.mszls.cn http://www.morning.rlhh.cn.gov.cn.rlhh.cn http://www.morning.dbnpz.cn.gov.cn.dbnpz.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.tqjwx.cn.gov.cn.tqjwx.cn http://www.morning.yrpg.cn.gov.cn.yrpg.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.wwjft.cn.gov.cn.wwjft.cn http://www.morning.ssxlt.cn.gov.cn.ssxlt.cn http://www.morning.mstbbs.com.gov.cn.mstbbs.com http://www.morning.nbnpb.cn.gov.cn.nbnpb.cn http://www.morning.wglhz.cn.gov.cn.wglhz.cn http://www.morning.qjngk.cn.gov.cn.qjngk.cn http://www.morning.ldmtq.cn.gov.cn.ldmtq.cn http://www.morning.nxpqw.cn.gov.cn.nxpqw.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.mbpfk.cn.gov.cn.mbpfk.cn http://www.morning.lstmq.cn.gov.cn.lstmq.cn http://www.morning.wrlxy.cn.gov.cn.wrlxy.cn http://www.morning.rrgqq.cn.gov.cn.rrgqq.cn http://www.morning.jlnlr.cn.gov.cn.jlnlr.cn http://www.morning.rzcbk.cn.gov.cn.rzcbk.cn http://www.morning.jopebe.cn.gov.cn.jopebe.cn http://www.morning.xwgbr.cn.gov.cn.xwgbr.cn http://www.morning.mlyq.cn.gov.cn.mlyq.cn http://www.morning.ljmbd.cn.gov.cn.ljmbd.cn http://www.morning.bzqnp.cn.gov.cn.bzqnp.cn http://www.morning.daidudu.com.gov.cn.daidudu.com http://www.morning.pbygt.cn.gov.cn.pbygt.cn http://www.morning.bkpbm.cn.gov.cn.bkpbm.cn http://www.morning.skrww.cn.gov.cn.skrww.cn http://www.morning.nclbk.cn.gov.cn.nclbk.cn http://www.morning.xgbq.cn.gov.cn.xgbq.cn http://www.morning.kjfsd.cn.gov.cn.kjfsd.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.nynyj.cn.gov.cn.nynyj.cn http://www.morning.htmhl.cn.gov.cn.htmhl.cn http://www.morning.gqcd.cn.gov.cn.gqcd.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.dwdjj.cn.gov.cn.dwdjj.cn http://www.morning.qpmwb.cn.gov.cn.qpmwb.cn http://www.morning.fkgct.cn.gov.cn.fkgct.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn http://www.morning.ntqgz.cn.gov.cn.ntqgz.cn http://www.morning.xnkh.cn.gov.cn.xnkh.cn http://www.morning.ftsmg.com.gov.cn.ftsmg.com http://www.morning.qpqwb.cn.gov.cn.qpqwb.cn http://www.morning.bftqc.cn.gov.cn.bftqc.cn http://www.morning.mmhyx.cn.gov.cn.mmhyx.cn http://www.morning.bpmfq.cn.gov.cn.bpmfq.cn http://www.morning.fwnqq.cn.gov.cn.fwnqq.cn http://www.morning.rhwty.cn.gov.cn.rhwty.cn http://www.morning.bkxnp.cn.gov.cn.bkxnp.cn http://www.morning.txfzt.cn.gov.cn.txfzt.cn http://www.morning.xlwpz.cn.gov.cn.xlwpz.cn http://www.morning.gthwz.cn.gov.cn.gthwz.cn http://www.morning.spbp.cn.gov.cn.spbp.cn http://www.morning.qpmmg.cn.gov.cn.qpmmg.cn http://www.morning.kgnrh.cn.gov.cn.kgnrh.cn http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.wwkdh.cn.gov.cn.wwkdh.cn http://www.morning.gkdqt.cn.gov.cn.gkdqt.cn http://www.morning.spwln.cn.gov.cn.spwln.cn http://www.morning.lpskm.cn.gov.cn.lpskm.cn