LangChain搭建Agent | 使用initialize_agent

大模型向量数据库云存储

picture.image

点击上方卡片 关注我们

picture.image

picture.image

picture.image

image

1.用create_tool_calling_agent报错

构建agent,这个方法是过时了吗?官方文档也没更新,官方示例也运行错误

  
from langchain_core.prompts import ChatPromptTemplate  
from langchain_core.runnables import ConfigurableField  
from langchain_core.tools import tool  
from langchain.agents import create_tool_calling_agent, AgentExecutor  
  
@tool  
def multiply(x: float, y: float) -> float:  
    """将 'x' 乘以 'y'。"""  
    return x * y  
  
@tool  
def exponentiate(x: float, y: float) -> float:  
    """将 'x' 乘以 'y' 的指数。"""  
    return x**y  
  
@tool  
def add(x: float, y: float) -> float:  
    """将 'x' 和 'y' 相加。"""  
    return x + y  
  
prompt = ChatPromptTemplate.from_messages([  
    ("system", "你是一个有用的助手"),   
    ("human", "{input}"),   
    ("placeholder", "{agent\_scratchpad}"),  
])  
  
tools = [multiply, exponentiate, add]  
  
# llm = ChatZhipuAI(  
#     model="glm-4",  
#     temperature=0.5,  
# )  
  
agent = create_tool_calling_agent(llm, tools, prompt)  
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)  
  
agent_executor.invoke({"input": "3 加上 5 的 2.743 次方是多少。还有 17.24 减去 918.1241 是多少。"})  

picture.image

image

有路过的大佬指点一二么?

2.【解决方案】用initialize_agent

llm就按照这篇文章配置任意一个LangChain连接国内大模型测试|智谱ai、讯飞星火、通义千问

  
from langchain.tools import BaseTool  
from langchain.agents import initialize_agent  
from langchain.agents import AgentType  
import os  
from langchain_community.chat_models import ChatZhipuAI  
os.environ["ZHIPUAI\_API\_KEY"] = 'xxx'  
llm = ChatZhipuAI(  
    model="glm-4",  
    temperature=0.5,  
)  
class Multiply(BaseTool):  
    name = "乘法"  
    description = "只做乘法运算"  
   
    def \_run(self, input: str) -> str:  
        # Your logic here  
        return "乘法计算完毕"  
   
    def \_arun(self, query: str):  
        raise NotImplementedError("This tool does not support async")  
class Add(BaseTool):  
    name = "加法"  
    description = "只做加法运算"  
   
    def \_run(self, input: str) -> str:  
        # Your logic here  
        return "加法计算完毕"  
   
    def \_arun(self, query: str):  
        raise NotImplementedError("This tool does not support async")  
class Exponentiate(BaseTool):  
    name = "幂运算"  
    description = "只做幂运算"  
   
    def \_run(self, input: str) -> str:  
        # Your logic here  
        return "幂运算计算完毕"  
   
    def \_arun(self, query: str):  
        raise NotImplementedError("This tool does not support async")  
  
tools = [Multiply(),Add(),Exponentiate()]  
  
# agent = initialize\_agent(tools, agent=AgentType.DEFAULT)  
agent = initialize_agent(tools,   
                         llm,   
                         verbose=True)  
agent("3乘以6,加上 5 的 4 次方是多少?")  

picture.image

image

就说清不清楚吧!

  
{'input': '3乘以6,加上 5 的 4 次方是多少?', 'output': '643'}  

【官方文档】

  • initialize_agent

picture.image

image

  • create_tool_calling_agentpicture.image

如果本文对你有帮助,还请你 点赞 在看 转发

你的支持就是我创作的最大动力,🙏谢谢啦!🌟

  1. 私信我【 加群 】欢迎加入AI交流群~

  2. 私信我【 科学 】送1个月科学链接~

picture.image

点击下方卡片 关注我们 技术前沿不迷路

picture.image

0
0
0
0
关于作者
关于作者

文章

0

获赞

0

收藏

0

相关资源
字节跳动 XR 技术的探索与实践
火山引擎开发者社区技术大讲堂第二期邀请到了火山引擎 XR 技术负责人和火山引擎创作 CV 技术负责人,为大家分享字节跳动积累的前沿视觉技术及内外部的应用实践,揭秘现代炫酷的视觉效果背后的技术实现。
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论