Skip to main content

如何处理查询分析中的多个检索器

有时,查询分析技术可能允许选择使用哪个检索器。为了使用它,您需要添加一些逻辑来选择要使用的检索器。我们将展示一个简单的示例(使用模拟数据)来说明如何做到这一点。

设置

安装依赖

# %pip install -qU langchain langchain-community langchain-openai langchain-chroma

设置环境变量

我们将在此示例中使用 OpenAI:

import getpass
import os
os.environ["OPENAI_API_KEY"] = getpass.getpass()
# 可选的,取消注释以使用 LangSmith 跟踪运行。在此处注册:https://smith.langchain.com。
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()

创建索引

我们将在虚假信息上创建一个向量存储。

from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
texts = ["Harrison worked at Kensho"]
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vectorstore = Chroma.from_texts(texts, embeddings, collection_name="harrison")
retriever_harrison = vectorstore.as_retriever(search_kwargs={"k": 1})
texts = ["Ankush worked at Facebook"]
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vectorstore = Chroma.from_texts(texts, embeddings, collection_name="ankush")
retriever_ankush = vectorstore.as_retriever(search_kwargs={"k": 1})

查询分析

我们将使用函数调用来结构化输出。我们将让它返回多个查询。

from typing import List, Optional
from langchain_core.pydantic_v1 import BaseModel, Field
class Search(BaseModel):
"""搜索有关某个人的信息。"""
query: str = Field(
...,
description="要查找的查询",
)
person: str = Field(
...,
description="要查找信息的人。应为 `HARRISON` 或 `ANKUSH`。",
)
from langchain_core.output_parsers.openai_tools import PydanticToolsParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI
output_parser = PydanticToolsParser(tools=[Search])
system = """You have the ability to issue search queries to get information to help answer user information."""
prompt = ChatPromptTemplate.from_messages(
[
("system", system),
("human", "{question}"),
]
)
llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(Search)
query_analyzer = {"question": RunnablePassthrough()} | prompt | structured_llm

我们可以看到,这允许在检索器之间进行路由。

query_analyzer.invoke("where did Harrison Work")
Search(query='workplace', person='HARRISON')
query_analyzer.invoke("where did ankush Work")
Search(query='workplace', person='ANKUSH')

使用查询分析进行检索

那么我们如何将其包含在链中呢?我们只需要一些简单的逻辑来选择检索器并传入搜索查询。

from langchain_core.runnables import chain
retrievers = {
"HARRISON": retriever_harrison,
"ANKUSH": retriever_ankush,
}
@chain
def custom_chain(question):
response = query_analyzer.invoke(question)
retriever = retrievers[response.person]
return retriever.invoke(response.query)
custom_chain.invoke("where did Harrison Work")
[Document(page_content='Harrison worked at Kensho')]
custom_chain.invoke("where did ankush Work")
[Document(page_content='Ankush worked at Facebook')]

Was this page helpful?


You can leave detailed feedback on GitHub.