使用LangChain构建第一个ReAct Agent

使用LangChain构建第一个ReAct Agent

准备环境

使用Anaconda
安装python 3.10
安装langchain、langchain_openai、langchain_community (安装命令 pip install XXX)
申请DeepSeek API:https://platform.deepseek.com/api_keys(也可以用kimi:https://platform.moonshot.cn/console/api-keys)

代码

from langchain import hub
from langchain.agents import create_structured_chat_agent, AgentExecutor
from langchain.memory import ConversationBufferMemory
from langchain.schema import HumanMessage
from langchain.tools import BaseTool
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent, Tool

# 模型 DeepSeek
# model = ChatOpenAI(model="deepseek-chat",
#                    openai_api_key="xx", #填写自己DeepSeek的api
#                    openai_api_base="https://api.deepseek.com/v1")

# kimi
model = ChatOpenAI(model="moonshot-v1-8k",
                   openai_api_key="sk-xx", #填写自己kimi的api
                   openai_api_base="https://api.moonshot.cn/v1")

from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper

api_wrapper = WikipediaAPIWrapper(top_k_results=3, doc_content_chars_max=2000)
tool = WikipediaQueryRun(api_wrapper=api_wrapper)

# 提示词,直接从langchain hub上下载,因为写这个ReAct机制的prompt比较复杂,直接用现成的。
prompt = hub.pull("hwchase17/structured-chat-agent")

print(prompt)

tools = [tool]
# 定义AI Agent
agent = create_structured_chat_agent(
    llm=model,
    tools=tools,
    prompt=prompt
)<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java知识技术分享

感谢支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值