Skip to main content

会话检索代理 (Conversational Retrieval Agent)

This is an agent specifically optimized for doing retrieval when necessary and also holding a conversation.
这是一个专门针对必要时进行检索并进行对话的代理。

To start, we will set up the retriever we want to use, and then turn it into a retriever tool. Next, we will use the high level constructor for this type of agent. Finally, we will walk through how to construct a conversational retrieval agent from components.
首先,我们将设置我们想要使用的检索器,然后将其转换为检索工具。接下来,我们将使用高级构造函数来构建这种类型的代理。最后,我们将介绍如何从组件构建一个对话检索代理。

检索器

To start, we need a retriever to use! The code here is mostly just example code. Feel free to use your own retriever and skip to the section on creating a retriever tool.
首先,我们需要一个检索器!这里的代码主要是示例代码。请随意使用您自己的检索器并跳到创建检索工具的部分。

from langchain.document_loaders import TextLoader
loader = TextLoader('../../../../../docs/extras/modules/state_of_the_union.txt')
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings

documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)
embeddings = OpenAIEmbeddings()
db = FAISS.from_documents(texts, embeddings)
retriever = db.as_retriever()

检索工具

Now we need to create a tool for our retriever. The main things we need to pass in are a name for the retriever as well as a description. These will both be used by the language model, so they should be informative.
现在我们需要为我们的检索器创建一个工具。我们需要传入的主要内容是检索器的名称和描述。这两者都将被语言模型使用,因此它们应该是有信息量的。

from langchain.agents.agent_toolkits import create_retriever_tool
tool = create_retriever_tool(
retriever,
"search_state_of_union",
"Searches and returns documents regarding the state-of-the-union."
)
tools = [tool]

代理构造函数

Here, we will use the high level create_conversational_retrieval_agent API to construct the agent. Notice that beside the list of tools, the only thing we need to pass in is a language model to use. Under the hood, this agent is using the OpenAIFunctionsAgent, so we need to use an ChatOpenAI model.
在这里,我们将使用高级的create_conversational_retrieval_agent API来构建代理。 请注意,除了工具列表之外,我们需要传入的唯一内容是要使用的语言模型。 在底层,这个代理使用的是OpenAIFunctionsAgent,所以我们需要使用ChatOpenAI模型。

from langchain.agents.agent_toolkits import create_conversational_retrieval_agent
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(temperature = 0)
agent_executor = create_conversational_retrieval_agent(llm, tools, verbose=True)

We can now try it out!
我们现在可以试试看!

result = agent_executor({"input": "hi, im bob"})
    

> Entering new AgentExecutor chain...
Hello Bob! How can I assist you today?

> Finished chain.
result["output"]
    'Hello Bob! How can I assist you today?'

Notice that it remembers your name
注意它记住了你的名字

result = agent_executor({"input": "whats my name?"})
    

> Entering new AgentExecutor chain...
Your name is Bob.

> Finished chain.
result["output"]
    'Your name is Bob.'

Notice that it now does retrieval 注意它现在进行了检索

result = agent_executor({"input": "what did the president say about kentaji brown jackson in the most recent state of the union?"})
    

> Entering new AgentExecutor chain...

Invoking: `search_state_of_union` with `{'query': 'Kentaji Brown Jackson'}`


[Document(page_content='Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \n\nTonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence