[Agent]-----MRKLAgentForChatModels组件开发

参考资料:

https://python.langchain.com/docs/modules/agents/agent_types/react
https://python.langchain.com/docs/modules/agents/how_to/custom_mrkl_agent
https://python.langchain.com/docs/modules/agents/how_to/mrkl

该agent主要使用ReAct框架来决定操作的代理,从而优化聊天模型。
agent需要的组件:tools,chatmodels
实现步骤:
第一步:定义一个tool,这里采用计算器

llm_math_chain = LLMMathChain(llm=llm, verbose=True)
tool =  Tool(
        name="Calculator",
        func=llm_math_chain.run,
        description="useful for when you need to answer questions about math"
    )

第二步,定义agent方法,这里的AgentType采用的是Mrkl的chatReAct模型,使用聊天模型来代替llm去调用ReAct代理

param_dict = {
    "tools":[tool],
    "model":llm
    }

from langchain.agents import initialize_agent,AgentType

agentExcutor = initialize_agent(
    llm=param_dict.get("model"),
    tools=param_dict.get("tools"),
    agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
    )
resp = agentExcutor.run("写一个芒果的故事 ")
print(resp)

你可能感兴趣的:(基于langchain的开发,开发语言,python,langchain,aigc)