langchain.chains.history_aware_retriever.create_history_aware_retriever

langchain.chains.history_aware_retriever.create_history_aware_retriever(llm: Runnable[Union[PromptValue, str, Sequence[Union[BaseMessage, List[str], Tuple[str, str], str, Dict[str, Any]]]], Union[BaseMessage, str]], retriever: Runnable[str, List[Document]], prompt: BasePromptTemplate) Runnable[Any, List[Document]][source]

创建一个链条,接收对话历史并返回文档。

如果没有`chat_history`,则`input`将直接传递给检索器。如果存在`chat_history`,则将使用提示和LLM生成搜索查询。然后将该搜索查询传递给检索器。

参数:

llm: 用于根据对话历史生成搜索词的语言模型 retriever: 接受字符串输入并输出文档列表的RetrieverLike对象。 prompt: 用于为检索器生成搜索查询的提示。

返回:

一个LCEL Runnable。Runnable输入必须接收`input`,如果存在对话历史,则应以`chat_history`的形式接收。 Runnable输出是一个文档列表

示例:
# pip install -U langchain langchain-community

from langchain_community.chat_models import ChatOpenAI
from langchain.chains import create_history_aware_retriever
from langchain import hub

rephrase_prompt = hub.pull("langchain-ai/chat-langchain-rephrase")
llm = ChatOpenAI()
retriever = ...
chat_retriever_chain = create_history_aware_retriever(
    llm, retriever, rephrase_prompt
)

chain.invoke({"input": "...", "chat_history": })
Parameters
Return type

Runnable[Any, List[Document]]

Examples using create_history_aware_retriever