当前位置:   article > 正文

LangChain入门教程 - 使用代理Agent_qianfanchatendpoint 实现多工具自动选择

qianfanchatendpoint 实现多工具自动选择

对于大模型,比如某些场景,需要数学计算,或者需要从某些网站获取参考资料,就必须使用专门的代理来完成任务。这里我们使用langchain提供的数学工具来实现一个最简单的例子,下一篇我们会讲如何自己实现代理。

首先创建一个对话模型,记得自己设置环境变量QIANFAN_AKQIANFAN_SK

  1. from langchain_community.chat_models import QianfanChatEndpoint
  2. chatModel = QianfanChatEndpoint(
  3. model='ERNIE-Bot',
  4. endpoint='completions'
  5. )

组装代理

我们使用lc库提供的数学包LLMMathChain组装数学代理,负责数学相关的计算工作。这里简单解释一下代理的工作原理,不完全正确,但大概原理是没跑的:大模型在收到问题后,和工具的描述匹配一下,决定使用哪些工具。然后把问题理解后生成合适的参数调用工具并返回结果。这里面是个大黑盒,怎么理解和拆分问题并匹配工具都是由大模型自己决定的,你只能通过提示词给出参考。对于百度的千帆,测试中发现对于数学问题用英文提问比较好,如果是中文理解就问有问题,没法正确调用工具。

  1. from langchain import LLMMathChain
  2. from langchain.agents import AgentType, initialize_agent
  3. from langchain.tools import BaseTool, Tool
  4. from pydantic import BaseModel, Field
  5. tools = []
  6. class CalculatorInput(BaseModel):
  7. question: str = Field()
  8. llm_math_chain = LLMMathChain(llm=chatModel, verbose=True)
  9. tools.append(
  10. Tool.from_function(
  11. func=llm_math_chain.run,
  12. name='牛逼的计算器',
  13. description='用于回答数学问题',
  14. args_schema=CalculatorInput
  15. )
  16. )
  17. # ZERO_SHOT_REACT_DESCRIPTION代表根据工具的描述进行选择
  18. agent = initialize_agent(
  19. tools, chatModel, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
  20. )

 这里args_schema参数用于构造输入参数,CalculatorInut的写法参考pydantic的文档:pydantic · PyPI

提问

  1. # 使用英文便于大模型理解
  2. agent.run(
  3. 'What is the result of 5.85 raised to the 2.16 power?'
  4. )
  5. """输出
  6. > Entering new AgentExecutor chain...
  7. This is a mathematical question that requires raising a number to a specific power.
  8. Action: 牛逼的计算器
  9. Action Input: Calculate 5.85 raised to the power of 2.16
  10. > Entering new LLMMathChain chain...
  11. Calculate 5.85 raised to the power of 2.16
  12. ```text
  13. 5.85**2.16
  14. ```
  15. ...numexpr.evaluate("5.85**2.16")...
  16. Answer: 45.400085499141575
  17. > Finished chain.
  18. Observation: Answer: 45.400085499141575
  19. Thought:I now know the result of 5.85 raised to the 2.16 power.
  20. Final Answer: 5.85 raised to the 2.16 power is equal to 45.400085499141575.
  21. > Finished chain.
  22. """

代理是如何执行的?

前面我们在构造tool时,func参数设置为llm_math_chain.run。那么这个函数支持哪些参数,被调用时传进来的参数又是什么样的呢?一种方法是找源码,那个比较费事,我们可以inspect库提供的方法查看信息,然后自己在run函数外面包一层,就能看到想要看的信息了。

使用inspect函数查看信息,我们可以看到run有哪些参数和参数的默认值。

  1. import inspect
  2. sig = inspect.signature(llm_math_chain.run)
  3. for name, para in sig.parameters.items():
  4. print(name, para.default)
  5. '''输出
  6. args <class 'inspect._empty'>
  7. callbacks None
  8. tags None
  9. metadata None
  10. kwargs <class 'inspect._empty'>
  11. '''

我们在这里只关心第一个参数args,我们自己定义一个函数打印传了什么参数进来。

  1. from langchain import LLMMathChain
  2. from langchain.tools import BaseTool, Tool
  3. from pydantic import BaseModel, Field
  4. tools = []
  5. class CalculatorInput(BaseModel):
  6. question: str = Field()
  7. llm_math_chain = LLMMathChain(llm=chatModel, verbose=True)
  8. def my_math_func(*args):
  9. print('输入参数', args)
  10. return llm_math_chain.run(*args)
  11. tools.append(
  12. Tool.from_function(
  13. func=my_math_func,
  14. name='牛逼的计算器',
  15. description='用于回答数学问题',
  16. args_schema=CalculatorInput
  17. )
  18. )
  19. '''
  20. 我们可以看到以下输出:
  21. Action: 牛逼的计算器
  22. Action Input: Calculate 5.85 raised to the 2.16 power.
  23. 输入参数 ('Calculate 5.85 raised to the 2.16 power.\n',)
  24. '''

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/687891
推荐阅读
相关标签
  

闽ICP备14008679号