langchain构建一个 Agent 的过程

构建一个 Agent 的过程

概述

在使用大型语言模型(LLM)时,语言模型本身无法直接采取行动,只能输出文本。LangChain 的一个重要用例是创建代理(Agents)。代理是使用 LLM 作为推理引擎的系统,用于确定应采取哪些行动以及这些行动的输入应该是什么。然后,这些行动的结果可以反馈给代理,它会确定是否需要更多行动,或者是否可以结束。

在这个教程中,我们将构建一个可以与多种不同工具进行交互的代理:一个是本地数据库,另一个是搜索引擎。您将能够向这个代理提问,观察它调用工具,并与它进行对话。

构建代理所需的概念
  • 使用语言模型,特别是它们的工具调用能力
  • 创建一个检索器,以向我们的代理公开特定信息
  • 使用搜索工具在线查找事物
  • 使用 Chat History,使聊天机器人能够“记住”过去的互动,并在回答后续问题时加以考虑
  • 使用 LangSmith 调试和跟踪您的应用程序

步骤详解

1. 设置和安装

首先,确保您已安装 LangChain,并设置好 LangSmith 环境变量以记录跟踪:

pip install langchain

在 Jupyter Notebook 中:

import getpass
import os

os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()
2. 定义工具

我们需要创建一些工具供代理使用。在这里,我们将使用 Tavily 搜索引擎和本地检索器。

Tavily 搜索工具

from langchain_community.tools.tavily_search import TavilySearchResults

search = TavilySearchResults(max_results=2)

本地检索器

from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter

loader = WebBaseLoader("https://docs.smith.langchain.com/overview")
docs = loader.load()
documents = RecursiveCharacterTextSplitter(
    chunk_size=1000, chunk_overlap=200
).split_documents(docs)
vector = FAISS.from_documents(documents, OpenAIEmbeddings())
retriever = vector.as_retriever()

from langchain.tools.retriever import create_retriever_tool
retriever_tool = create_retriever_tool(
    retriever,
    "langsmith_search",
    "Search for information about LangSmith. For any questions about LangSmith, you must use this tool!",
)

tools = [search, retriever_tool]
3. 使用语言模型

选择一个语言模型并绑定工具:

from langchain_openai import ChatOpenAI

model = ChatOpenAI(model="gpt-4")
model_with_tools = model.bind_tools(tools)

测试工具调用:

from langchain_core.messages import HumanMessage

response = model_with_tools.invoke([HumanMessage(content="What's the weather in SF?")])
print(f"ContentString: {response.content}")
print(f"ToolCalls: {response.tool_calls}")
4. 创建代理

定义提示并创建代理:

from langchain import hub

prompt = hub.pull("hwchase17/openai-functions-agent")

from langchain.agents import create_tool_calling_agent

agent = create_tool_calling_agent(model, tools, prompt)
5. 创建 AgentExecutor 并运行代理
from langchain.agents import AgentExecutor

agent_executor = AgentExecutor(agent=agent, tools=tools)

response = agent_executor.invoke({"input": "hi!"})
print(response)

response = agent_executor.invoke({"input": "how can langsmith help with testing?"})
print(response)

response = agent_executor.invoke({"input": "whats the weather in sf?"})
print(response)
6. 添加内存

使代理具有记忆功能:

agent_executor.invoke({"input": "hi! my name is bob", "chat_history": []})

from langchain_core.messages import AIMessage, HumanMessage

response = agent_executor.invoke(
    {
        "chat_history": [
            HumanMessage(content="hi! my name is bob"),
            AIMessage(content="Hello Bob! How can I assist you today?"),
        ],
        "input": "what's my name?",
    }
)
print(response)

from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory

store = {}

def get_session_history(session_id: str) -> BaseChatMessageHistory:
    if session_id not in store:
        store[session_id] = ChatMessageHistory()
    return store[session_id]

agent_with_chat_history = RunnableWithMessageHistory(
    agent_executor,
    get_session_history,
    input_messages_key="input",
    history_messages_key="chat_history",
)

response = agent_with_chat_history.invoke(
    {"input": "hi! I'm bob"},
    config={"configurable": {"session_id": "<foo>"}},
)
print(response)

response = agent_with_chat_history.invoke(
    {"input": "what's my name?"},
    config={"configurable": {"session_id": "<foo>"}},
)
print(response)

通过上述步骤,您可以构建一个能够与多种工具交互的代理,并具备记忆功能。代理可以通过调用工具来处理复杂的任务,并能够记住先前的交互,以便在后续问题中做出更加智能的响应。

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值