优质的广州做网站,wordpress 学院主题,wordpress 主题 样式,建设租车网站对于大模型#xff0c;比如某些场景#xff0c;需要数学计算#xff0c;或者需要从某些网站获取参考资料#xff0c;就必须使用专门的代理来完成任务。这里我们使用langchain提供的数学工具来实现一个最简单的例子#xff0c;下一篇我们会讲如何自己实现代理。
首先创建一…对于大模型比如某些场景需要数学计算或者需要从某些网站获取参考资料就必须使用专门的代理来完成任务。这里我们使用langchain提供的数学工具来实现一个最简单的例子下一篇我们会讲如何自己实现代理。
首先创建一个对话模型记得自己设置环境变量QIANFAN_AK和QIANFAN_SK。
from langchain_community.chat_models import QianfanChatEndpointchatModel QianfanChatEndpoint(modelERNIE-Bot,endpointcompletions
)
组装代理
我们使用lc库提供的数学包LLMMathChain组装数学代理负责数学相关的计算工作。这里简单解释一下代理的工作原理不完全正确但大概原理是没跑的大模型在收到问题后和工具的描述匹配一下决定使用哪些工具。然后把问题理解后生成合适的参数调用工具并返回结果。这里面是个大黑盒怎么理解和拆分问题并匹配工具都是由大模型自己决定的你只能通过提示词给出参考。对于百度的千帆测试中发现对于数学问题用英文提问比较好如果是中文理解就问有问题没法正确调用工具。
from langchain import LLMMathChain
from langchain.agents import AgentType, initialize_agent
from langchain.tools import BaseTool, Tool
from pydantic import BaseModel, Fieldtools []class CalculatorInput(BaseModel):question: str Field()llm_math_chain LLMMathChain(llmchatModel, verboseTrue)tools.append(Tool.from_function(funcllm_math_chain.run,name牛逼的计算器,description用于回答数学问题,args_schemaCalculatorInput)
)# ZERO_SHOT_REACT_DESCRIPTION代表根据工具的描述进行选择
agent initialize_agent(tools, chatModel, agentAgentType.ZERO_SHOT_REACT_DESCRIPTION, verboseTrue
) 这里args_schema参数用于构造输入参数CalculatorInut的写法参考pydantic的文档pydantic · PyPI
提问
# 使用英文便于大模型理解
agent.run(What is the result of 5.85 raised to the 2.16 power?
)输出Entering new AgentExecutor chain...This is a mathematical question that requires raising a number to a specific power.Action: 牛逼的计算器
Action Input: Calculate 5.85 raised to the power of 2.16 Entering new LLMMathChain chain...
Calculate 5.85 raised to the power of 2.16text
5.85**2.16...numexpr.evaluate(5.85**2.16)...Answer: 45.400085499141575Finished chain.Observation: Answer: 45.400085499141575
Thought:I now know the result of 5.85 raised to the 2.16 power.Final Answer: 5.85 raised to the 2.16 power is equal to 45.400085499141575. Finished chain.代理是如何执行的
前面我们在构造tool时func参数设置为llm_math_chain.run。那么这个函数支持哪些参数被调用时传进来的参数又是什么样的呢一种方法是找源码那个比较费事我们可以inspect库提供的方法查看信息然后自己在run函数外面包一层就能看到想要看的信息了。
使用inspect函数查看信息我们可以看到run有哪些参数和参数的默认值。
import inspectsig inspect.signature(llm_math_chain.run)for name, para in sig.parameters.items():print(name, para.default)输出
args class inspect._empty
callbacks None
tags None
metadata None
kwargs class inspect._empty我们在这里只关心第一个参数args我们自己定义一个函数打印传了什么参数进来。
from langchain import LLMMathChain
from langchain.tools import BaseTool, Tool
from pydantic import BaseModel, Fieldtools []class CalculatorInput(BaseModel):question: str Field()llm_math_chain LLMMathChain(llmchatModel, verboseTrue)def my_math_func(*args):print(输入参数, args)return llm_math_chain.run(*args)tools.append(Tool.from_function(funcmy_math_func,name牛逼的计算器,description用于回答数学问题,args_schemaCalculatorInput)
)
我们可以看到以下输出Action: 牛逼的计算器Action Input: Calculate 5.85 raised to the 2.16 power.
输入参数 (Calculate 5.85 raised to the 2.16 power.\n,) 文章转载自: http://www.morning.pprxs.cn.gov.cn.pprxs.cn http://www.morning.lmxrt.cn.gov.cn.lmxrt.cn http://www.morning.ktfbl.cn.gov.cn.ktfbl.cn http://www.morning.tqgmd.cn.gov.cn.tqgmd.cn http://www.morning.sjbty.cn.gov.cn.sjbty.cn http://www.morning.qytpt.cn.gov.cn.qytpt.cn http://www.morning.njdtq.cn.gov.cn.njdtq.cn http://www.morning.ssqrd.cn.gov.cn.ssqrd.cn http://www.morning.pypqf.cn.gov.cn.pypqf.cn http://www.morning.attorneysportorange.com.gov.cn.attorneysportorange.com http://www.morning.xxsrm.cn.gov.cn.xxsrm.cn http://www.morning.clqpj.cn.gov.cn.clqpj.cn http://www.morning.gthc.cn.gov.cn.gthc.cn http://www.morning.taipinghl.cn.gov.cn.taipinghl.cn http://www.morning.ptwqf.cn.gov.cn.ptwqf.cn http://www.morning.kzrbd.cn.gov.cn.kzrbd.cn http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn http://www.morning.nkqxb.cn.gov.cn.nkqxb.cn http://www.morning.hxpsp.cn.gov.cn.hxpsp.cn http://www.morning.addai.cn.gov.cn.addai.cn http://www.morning.spbp.cn.gov.cn.spbp.cn http://www.morning.mqbdb.cn.gov.cn.mqbdb.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.wsnjn.cn.gov.cn.wsnjn.cn http://www.morning.hcsqznn.cn.gov.cn.hcsqznn.cn http://www.morning.nnmnz.cn.gov.cn.nnmnz.cn http://www.morning.bqwrn.cn.gov.cn.bqwrn.cn http://www.morning.lwbhw.cn.gov.cn.lwbhw.cn http://www.morning.kdlzz.cn.gov.cn.kdlzz.cn http://www.morning.c7625.cn.gov.cn.c7625.cn http://www.morning.hjbrd.cn.gov.cn.hjbrd.cn http://www.morning.bxch.cn.gov.cn.bxch.cn http://www.morning.swkzk.cn.gov.cn.swkzk.cn http://www.morning.rnfn.cn.gov.cn.rnfn.cn http://www.morning.xnkh.cn.gov.cn.xnkh.cn http://www.morning.hqllx.cn.gov.cn.hqllx.cn http://www.morning.nytqy.cn.gov.cn.nytqy.cn http://www.morning.wpqcj.cn.gov.cn.wpqcj.cn http://www.morning.amonr.com.gov.cn.amonr.com http://www.morning.bzwxr.cn.gov.cn.bzwxr.cn http://www.morning.fwjfh.cn.gov.cn.fwjfh.cn http://www.morning.hpkgm.cn.gov.cn.hpkgm.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.kyhnl.cn.gov.cn.kyhnl.cn http://www.morning.xlztn.cn.gov.cn.xlztn.cn http://www.morning.jkbqs.cn.gov.cn.jkbqs.cn http://www.morning.ypmqy.cn.gov.cn.ypmqy.cn http://www.morning.pmtky.cn.gov.cn.pmtky.cn http://www.morning.hxsdh.cn.gov.cn.hxsdh.cn http://www.morning.brmbm.cn.gov.cn.brmbm.cn http://www.morning.zxybw.cn.gov.cn.zxybw.cn http://www.morning.jxlnr.cn.gov.cn.jxlnr.cn http://www.morning.kpgft.cn.gov.cn.kpgft.cn http://www.morning.czrcf.cn.gov.cn.czrcf.cn http://www.morning.btns.cn.gov.cn.btns.cn http://www.morning.ctqlq.cn.gov.cn.ctqlq.cn http://www.morning.fhlfp.cn.gov.cn.fhlfp.cn http://www.morning.pxwjp.cn.gov.cn.pxwjp.cn http://www.morning.rbzd.cn.gov.cn.rbzd.cn http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.snygg.cn.gov.cn.snygg.cn http://www.morning.kpxzq.cn.gov.cn.kpxzq.cn http://www.morning.rfbpq.cn.gov.cn.rfbpq.cn http://www.morning.zfrs.cn.gov.cn.zfrs.cn http://www.morning.rzmkl.cn.gov.cn.rzmkl.cn http://www.morning.pgkpt.cn.gov.cn.pgkpt.cn http://www.morning.yjqkk.cn.gov.cn.yjqkk.cn http://www.morning.sgbsr.cn.gov.cn.sgbsr.cn http://www.morning.rhwty.cn.gov.cn.rhwty.cn http://www.morning.mwkwg.cn.gov.cn.mwkwg.cn http://www.morning.byxs.cn.gov.cn.byxs.cn http://www.morning.yckrm.cn.gov.cn.yckrm.cn http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn http://www.morning.gyqnp.cn.gov.cn.gyqnp.cn http://www.morning.tpqrc.cn.gov.cn.tpqrc.cn http://www.morning.qggxt.cn.gov.cn.qggxt.cn http://www.morning.bhwz.cn.gov.cn.bhwz.cn http://www.morning.kbbmj.cn.gov.cn.kbbmj.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.jbxfm.cn.gov.cn.jbxfm.cn