从零开始解析RAG(二):路由与查询构建——让数据主动响应问题

上一篇文章中,我们探讨了如何通过Query Translation(问题翻译)优化问题表达,使其更适合检索。探讨了Multi-Query、RAG-Fusion、Decomposition等让问题"更易被正确回答"的方法。然而检索前优化的工作不止于此。我们还需要解决两个关键问题:

  • 如何为问题选择最合适的数据源(如向量数据库、关系型数据库或图数据库))?
  • 如何弥合自然语言问题与结构化数据之间的差距? 为解决这些问题,我们需要引入两个关键环节:路由决策查询构建
  • 路由决策:根据问题的语义和上下文,将问题路由到合适的数据源。
  • 查询构建:专注于将自然语言问题转换为结构化查询语言(如SQL、Cypher等),或者生成适合特定数据源的查询指令。

接下来,我们将深入探讨路由决策和查询构建的核心原理,代码实现等等。

路由决策

路由,基本上就是将query导向正确的数据源,大部分情况下,这个数据源指的是不同的数据库, 比如有3个不同的库存储知识,包括关系数据库,图数据库,以及向量存储数据库。在回答一个问题的时候,要知道去哪个数据库里检索相关文档。数据源也可以指不同的prompt,比如针对数学问题,使用数学问题的prompt 模板。

在RAG应用中,根据决策类型不同,路由主要分为:

  • 逻辑路由:依赖LLM对问题进行推理,并根据推理结果来选择最合适的数据源。
  • 语义路由:根据问题与数据源的语义相似性选择最合适的prompt或者数据源。

逻辑路由

原理解析

逻辑路由是基于规则和推理的路由策略,主要依赖于LLM对用户输入进行推理分析 ,并根据推理的结果来选择最合适的数据源或处理流程。这种方法通常采用分类的方式,将查询归类到特定的数据源或处理管道中。

逻辑路由的实现较为简单,给定LLM关于不同数据源的信息,让LLM 推理当前输入的问题应该使用哪一个数据源。但是逻辑路由对模型的要求较高,需要LLM具有强大的推理能力。

代码实现

对于具有函数调用功能的LLM来说,逻辑路由的核心就是使用模型来做分类。 举个例子,要根据用户问题中涉及到了哪种语言,将其路由到对应的数据源(python,js, 还是 Golang 文档)。核心步骤是通过强制LLM输出预定义格式,确保下游检索步骤能精准定位知识库。

首先,定义RouteQuery,这是限制LLM必须遵循的输出格式。这里通过定义data_source属性必须是3个预定义值之一。

python 复制代码
from typing import Literal

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAI

# Data model
class RouteQuery(BaseModel):
    """Route a user query to the most relevant datasource."""
    # 使用Pydantic模型约束LLM输出格式,限定 datasource 字段只能为三个预定义值之一。
    datasource: Literal["python_docs", "js_docs", "golang_docs"] = Field(
        ...,
        # 通过 description 字段隐式引导模型关注「编程语言」这一关键特征。
        description="Given a user question choose which datasource would be most relevant for answering their question",
    )

这里通过LLM的函数调用实现结构化输出

python 复制代码
# LLM with function call 
llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(RouteQuery)

# 要求LLM根据question 中的编程语言特性进行决策
# Prompt 
system = """You are an expert at routing a user question to the appropriate data source.

Based on the programming language the question is referring to, route it to the relevant data source."""

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", system),
        ("human", "{question}"),
    ]
)

# Define router 
router = prompt | structured_llm

with_structured_output 是什么? 在要求LLM返回结构化输出的时候,这里使用llm.with_strucured_output()方法。这是langchain为了让LLMs返回结构化输出所定义的一个公共接口。通过调用这个方法(并传递一个JSON模式或Pydantic模型),模型将自动添加任何必要的参数和输出解析器。这张图展示了调用with_structured_output() 背后的逻辑:

  1. 需要定义schema ,对应示例中定义RouteQuery这一步,用于限制LLM的输出。
  2. 将schema 绑定到LLM中**
  3. 输入一个问题时,LLM就在输出上调用绑定的函数,以生成符合指定模式的输出。
  4. 输出会是一个包含data_source属性的字典,且该属性值必须是3个预定义值之一。此时,输出仍然是 JSON 字符串。
  5. 使用解析器,将 JSON 字符串解析为真正的python对象。

输入问题,调用 router。 问题中包含一段 langchain 代码,因此输出 python_docs

python 复制代码
question = """Why doesn't the following code work:

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages(["human", "speak in {language}"])
prompt.invoke("french")
"""

result = router.invoke({"question": question})

result.datasource # 'python_docs'

以上代码实现了在多个数据源中选择其一, 如果想要 同时查询多个索引, 可以通过更改预定的的schema 来实现。

注意,新定义data_sources 被定义为列表,代表接受一个数据源列表,description字段有有所更改,其余部分保持不变。

python 复制代码
from typing import List  
  
class RouteQuery(BaseModel):  
	"""Route a user query to the most relevant datasource."""  
	  
	datasources: List[Literal["python_docs", "js_docs", "golang_docs"]] = Field(  
	...,  
	description="Given a user question choose which datasources would be most relevant for answering their question",  
)

语义路由

原理解析

语义路由根据查询和数据源之间的语义相似性来决策数据源。假设现在有一组prompt,分别用于数学,物理,化学,生物等多种问题,同时将输入问题与这一组prompt分别进行嵌入,根据prompt 与 问题之间的语义相似性来决策要使用哪个prompt。

语义路由的核心在于计算问题与数据源之间的语义相似性

代码实现

在代码中,选择余弦距离作为衡量语义相似性的标准。这里定义了prompt_router函数来手动计算相似性。

python 复制代码
from langchain_community.utils.math import cosine_similarity
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnableLambda, RunnablePassthrough
from langchain_openai import OpenAIEmbeddings

physics_template = """You are a very smart physics professor. \
You are great at answering questions about physics in a concise and easy to understand manner. \
When you don't know the answer to a question you admit that you don't know.

Here is a question:
{query}"""

math_template = """You are a very good mathematician. You are great at answering math questions. \
You are so good because you are able to break down hard problems into their component parts, \
answer the component parts, and then put them together to answer the broader question.

Here is a question:
{query}"""

embeddings = OpenAIEmbeddings()
prompt_templates = [physics_template, math_template]
prompt_embeddings = embeddings.embed_documents(prompt_templates)


def prompt_router(input):
    query_embedding = embeddings.embed_query(input["query"])
    similarity = cosine_similarity([query_embedding], prompt_embeddings)[0]
    most_similar = prompt_templates[similarity.argmax()]
    print("Using MATH" if most_similar == math_template else "Using PHYSICS")
    return PromptTemplate.from_template(most_similar)


chain = (
    {"query": RunnablePassthrough()}
    | RunnableLambda(prompt_router)
    | ChatAnthropic(model="claude-3-haiku-20240307")
    | StrOutputParser()
)

查询构建

在经典RAG架构中,用户查询与文档均被转换为向量表示,通过语义相似度检索相关文本片段。这种方法在非结构化文本数据 (如PDF报告、文本)场景表现优异,但当知识库包含结构化数据 (关系型数据库)、半结构化数据(JSON日志、XML配置)时,简单的向量匹配难以充分利用数据的内在关联。因此出现了要将自然语言问题转换为结构化查询的需求,也就是这一节的主要内容------Query Construction 查询构建。

根据面向的数据结构不同,查询构建主要可以分为三种:

  • Text-to-SQL:针对关系型数据库的自然语言到结构化查询语言的转换。通过将自然语言问题解析为精确的SQL查询,实现对结构化数据的精确检索。
  • Text-to-Cypher:专为图数据库设计的查询构建方法。将自然语言问题转换为Cypher查询语言,以便在复杂的实体关系网络中进行高效的语义检索和关系推理。
  • Self-Retriever (自检索器):主要应用于向量数据库,通过语义嵌入和相似性匹配,自动构建语义检索查询。能够捕捉查询的深层语义,并在高维向量空间中进行相关性检索。
    我们重点关注Self-Retriver。

Self-Retriver (自检索器)

其中,Self-Retriver 主要是基于元数据过滤器实现。

元数据过滤

元数据过滤(Metadata Filtering)是一种在信息检索和数据处理中常用的技术,它通过利用与数据相关的元数据(如作者、日期、标签、类别等)来筛选和优化查询结果。

工作原理

  1. 元数据 :每个文档在向量存储中通常包含两部分内容:page_content(文档的主要内容)和metadata(文档的附加信息,如来源、日期等)。
  2. 筛选条件 :用户可以通过指定元数据的键值对来定义筛选条件。例如,筛选所有sourcetweet的文档。
  3. 结合语义搜索:元数据筛选通常与语义搜索(基于嵌入向量的相似性搜索)结合使用。首先通过语义搜索找到与查询相似的文档,然后根据元数据筛选条件进一步缩小结果范围。

假设现在有一个存储youtube 视频的索引,我们向问一个问题"2024年之后关于langchian 的视频",查询构建的过程就是将自然语言能够转换为可应用于向量存储的元数据过滤器,其底层原理如下:

  • 向量数据库本身具有一些元信息,可以在索引之后的 chunks 上进行结构化查询。这个例子就是要查询关于 "2024年发布的关于langchain的视频"的所有chunks.
  • 需要使用LLM的函数调用功能,最终要返回一个JSON格式的结构化查询。
  • 要实现这个目标,需要获取向量数据库中存在的元数据字段,并将其作为信息提供给LLM。LLM据此产生符合 schema 的结构化查询,最终将其解析为结构化对象。

代码实现

基于上述需要的代码实现如下:

首先,加载一个youtube 视频,可以看到,文档对象本身包含视频的元信息,比如标题,观看数,发布日期等等。

python 复制代码
from langchain_community.document_loaders import YoutubeLoader

docs = YoutubeLoader.from_youtube_url(
    "https://www.youtube.com/watch?v=pbAd8O1Lvm4", add_video_info=True
).load()

docs[0].metadata

"""
{'source': 'pbAd8O1Lvm4',
 'title': 'Self-reflective RAG with LangGraph: Self-RAG and CRAG',
 'description': 'Unknown',
 'view_count': 11922,
 'thumbnail_url': 'https://i.ytimg.com/vi/pbAd8O1Lvm4/hq720.jpg',
 'publish_date': '2024-02-07 00:00:00',
 'length': 1058,
 'author': 'LangChain'}
 """

假设现在有一个索引允许基于contenttitle进行非结构化查询,基于view_count 这类属性进行范围查询。

然后进行schema 的定义,用于让LLM返回结构化的输出。其中,为了呼应索引中有些contenttitle用于语义搜索,这两个属性是字符串类型。有些属性用于范围查找,比如view_count, 定义观看次数的最大值和最小值,对应字段是整型。

python 复制代码
import datetime
from typing import Literal, Optional, Tuple
from langchain_core.pydantic_v1 import BaseModel, Field

class TutorialSearch(BaseModel):
    """Search over a database of tutorial videos about a software library."""

    content_search: str = Field(
        ...,
        description="Similarity search query applied to video transcripts.",
    )
    title_search: str = Field(
        ...,
        description=(
            "Alternate version of the content search query to apply to video titles. "
            "Should be succinct and only include key words that could be in a video "
            "title."
        ),
    )
    min_view_count: Optional[int] = Field(
        None,
        description="Minimum view count filter, inclusive. Only use if explicitly specified.",
    )
    max_view_count: Optional[int] = Field(
        None,
        description="Maximum view count filter, exclusive. Only use if explicitly specified.",
    )
    earliest_publish_date: Optional[datetime.date] = Field(
        None,
        description="Earliest publish date filter, inclusive. Only use if explicitly specified.",
    )
    latest_publish_date: Optional[datetime.date] = Field(
        None,
        description="Latest publish date filter, exclusive. Only use if explicitly specified.",
    )
    min_length_sec: Optional[int] = Field(
        None,
        description="Minimum video length in seconds, inclusive. Only use if explicitly specified.",
    )
    max_length_sec: Optional[int] = Field(
        None,
        description="Maximum video length in seconds, exclusive. Only use if explicitly specified.",
    )

    def pretty_print(self) -> None:
        for field in self.__fields__:
            if getattr(self, field) is not None and getattr(self, field) != getattr(
                self.__fields__[field], "default", None
            ):
                print(f"{field}: {getattr(self, field)}")

像之前那样,将schema 绑定到LLM, LLM就能知道关于索引的所有信息,从而返回一个符合输出 schema 的JSON字符串,经过解析,得到一个对象。

python 复制代码
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

system = """You are an expert at converting user questions into database queries. \
You have access to a database of tutorial videos about a software library for building LLM-powered applications. \
Given a question, return a database query optimized to retrieve the most relevant results.

If there are acronyms or words you are not familiar with, do not try to rephrase them."""
prompt = ChatPromptTemplate.from_messages(
    [
        ("system", system),
        ("human", "{question}"),
    ]
)
llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(TutorialSearch)
query_analyzer = prompt | structured_llm

接下来只需要调用这个 query 分析器即可。 该例中询问的问题无关元信息,因此进行语义搜索,查询只涉及 contenttitle

python 复制代码
query_analyzer.invoke({"question": "rag from scratch"}).pretty_print()
"""
content_search: rag from scratch
title_search: rag from scratch
"""

该例中"要找到发布在2023年的视频", 也就是最早发布时间是2023.1.1 , 最迟的发布时间是 2024.1.1。(publish_date_search 在定义的时候使用的是左开右闭, 也就是 latest_publish_date 本身不包含在里面,所以这里是2024.1.1,而不是 2023.12.31 )。

python 复制代码
query_analyzer.invoke(
    {"question": "videos on chat langchain published in 2023"}
).pretty_print()
"""
content_search: chat langchain
title_search: 2023
earliest_publish_date: 2023-01-01
latest_publish_date: 2024-01-01
"""

总结

本文继续探讨检索前阶段的优化,聚焦于路由决策和查询构建两种方法。与上篇针对用户Query的优化不同,本篇重点在于如何根据问题选择数据源,以及将自然语言转换为结构化查询。下一篇文章将进入索引优化阶段,敬请期待。

参考链接

相关推荐
雪语.1 天前
AI大模型学习(五): LangChain(四)
数据库·学习·langchain
ILUUSION_S1 天前
结合RetrievalQA和agent的助手
python·学习·langchain
王毕业2 天前
从零解析RAG(一)
langchain
neoooo3 天前
LangChain与Ollama构建本地RAG知识库
人工智能·langchain·aigc
charles_vaez3 天前
开源模型应用落地-LangGraph101-探索 LangGraph人机交互-添加断点(一)
深度学习·自然语言处理·langchain
牛奶3 天前
前端学AI:LangChain、LangGraph和LangSmith的核心区别及定位
前端·langchain·ai 编程
牛奶3 天前
前端学AI:基于Node.js的Langchain开发-简单实战应用
前端·langchain·node.js
星星点点洲4 天前
【LangChain.js】Python版LangChain 的姊妹项目
javascript·langchain
星星点点洲4 天前
【智能体架构:Agent】LangChain智能体类型ReAct、Self-ASK的区别
langchain