Skip to main content
Open on GitHub

检索

安全

此处回顾的一些概念利用模型生成查询(例如用于 SQL 或图数据库)。 这样做存在固有风险。 请确保您的数据库连接权限范围尽可能狭窄,仅满足应用程序的需求。 这将减轻(尽管不能完全消除)构建能够查询数据库的模型驱动系统的风险。 有关一般安全最佳实践的更多信息,请参阅我们的 安全指南

概览

检索系统是许多人工智能应用的基础,能够高效地从大型数据集中识别相关信息。 这些系统支持多种数据格式:

  • 非结构化文本(例如文档)通常存储在向量存储或词汇搜索索引中。
  • 结构化数据通常存储在具有定义模式的 relational(关系型)或 graph(图)数据库中。

尽管数据格式日益多样化,现代 AI 应用正越来越致力于通过自然语言接口使所有类型的数据变得可访问。 模型在此过程中发挥着关键作用,它们将自然语言查询转换为与底层搜索索引或数据库兼容的格式。 这种转换使得与复杂数据结构的交互更加直观和灵活。

关键概念

Retrieval

(1) 查询分析: 模型转换或构建搜索查询以优化检索的过程。

(2) 信息检索: 搜索查询用于从各种检索系统中获取信息。

查询分析

虽然用户通常倾向于使用自然语言与检索系统进行交互,但这些系统可能需要特定的查询语法或受益于某些关键词。 查询分析充当了原始用户输入与优化搜索查询之间的桥梁。查询分析的常见应用包括:

  1. 查询重写: 查询可以被重写或扩展,以改善语义或词汇搜索。
  2. 查询构建: 搜索索引可能需要结构化查询(例如,数据库使用 SQL)。

查询分析采用模型将原始用户输入转换或构建为优化的搜索查询。

查询重写

检索系统理想情况下应能处理各种各样的用户输入,从简单且措辞不佳的查询到复杂的多方面问题。 为了实现这种多功能性,一种流行的方法是使用模型将原始用户查询转换为更有效的搜索查询。 这种转换范围可以从简单的关键词提取到复杂的查询扩展和重写。 以下是使用模型进行非结构化数据检索中查询分析的一些关键优势:

  1. 查询澄清: 模型可以重新表述模糊或措辞不当的查询以提高清晰度。
  2. 语义理解: 它们能够捕捉查询背后的意图,超越字面关键词匹配。
  3. 查询扩展: 模型可以生成相关术语或概念以扩大搜索范围。
  4. 复杂查询处理: 它们可以将多部分问题分解为更简单的子查询。

已开发出多种技术来利用模型进行查询重写,包括:

名称何时使用描述
Multi-queryWhen you want to ensure high recall in retrieval by providing multiple phrasings of a question.Rewrite the user question with multiple phrasings, retrieve documents for each rewritten question, return the unique documents for all queries.
DecompositionWhen a question can be broken down into smaller subproblems.Decompose a question into a set of subproblems / questions, which can either be solved sequentially (use the answer from first + retrieval to answer the second) or in parallel (consolidate each answer into final answer).
Step-backWhen a higher-level conceptual understanding is required.First prompt the LLM to ask a generic step-back question about higher-level concepts or principles, and retrieve relevant facts about them. Use this grounding to help answer the user question. Paper.
HyDEIf you have challenges retrieving relevant documents using the raw user inputs.Use an LLM to convert questions into hypothetical documents that answer the question. Use the embedded hypothetical documents to retrieve real documents with the premise that doc-doc similarity search can produce more relevant matches. Paper.

作为示例,查询分解可以简单地通过使用提示词和强制要求输出子问题列表的结构化输出来实现。 这些子问题随后可以在下游检索系统中按顺序或并行执行。

from typing import List

from pydantic import BaseModel, Field
from langchain_openai import ChatOpenAI
from langchain_core.messages import SystemMessage, HumanMessage

# Define a pydantic model to enforce the output structure
class Questions(BaseModel):
questions: List[str] = Field(
description="A list of sub-questions related to the input query."
)

# Create an instance of the model and enforce the output structure
model = ChatOpenAI(model="gpt-4o", temperature=0)
structured_model = model.with_structured_output(Questions)

# Define the system prompt
system = """You are a helpful assistant that generates multiple sub-questions related to an input question. \n
The goal is to break down the input into a set of sub-problems / sub-questions that can be answered independently. \n"""

# Pass the question to the model
question = """What are the main components of an LLM-powered autonomous agent system?"""
questions = structured_model.invoke([SystemMessage(content=system)]+[HumanMessage(content=question)])
提示

查看我们的从零开始构建 RAG 视频,了解几种不同的具体方法:

查询构建

查询分析还可以专注于将自然语言查询转换为专用查询语言或过滤器。 这种转换对于有效地与存储结构化或半结构化数据的各种类型数据库进行交互至关重要。

  1. 结构化数据示例: 对于关系型和图数据库,领域特定语言(DSL)用于查询数据。

  2. 半结构化数据示例:对于向量存储,查询可以结合语义搜索与元数据过滤。

这些方法利用模型来弥合用户意图与不同数据存储系统特定查询需求之间的差距。以下是一些流行的技术:

名称何时使用描述
Self QueryIf users are asking questions that are better answered by fetching documents based on metadata rather than similarity with the text.This uses an LLM to transform user input into two things: (1) a string to look up semantically, (2) a metadata filter to go along with it. This is useful because oftentimes questions are about the METADATA of documents (not the content itself).
Text to SQLIf users are asking questions that require information housed in a relational database, accessible via SQL.This uses an LLM to transform user input into a SQL query.
Text-to-CypherIf users are asking questions that require information housed in a graph database, accessible via Cypher.This uses an LLM to transform user input into a Cypher query.

作为示例,以下是如何使用 SelfQueryRetriever 将自然语言查询转换为元数据过滤器。

metadata_field_info = schema_for_metadata 
document_content_description = "Brief summary of a movie"
llm = ChatOpenAI(temperature=0)
retriever = SelfQueryRetriever.from_llm(
llm,
vectorstore,
document_content_description,
metadata_field_info,
)
进一步阅读

信息检索

通用检索系统

词汇搜索索引

许多搜索引擎基于将查询中的单词与每个文档中的单词进行匹配。 这种方法称为词汇检索,使用通常基于词频的搜索算法。 其直观逻辑很简单:如果一个单词在用户的查询和某个特定文档中都频繁出现,那么该文档可能是一个良好的匹配结果。

实现这一点的特定数据结构通常是一个倒排索引。 此类索引包含一个单词列表,以及每个单词在各种文档中出现位置的映射列表。 利用这种数据结构,可以高效地将搜索查询中的单词与其出现的文档进行匹配。 BM25TF-IDF两种流行的词汇搜索算法

进一步阅读

向量索引

向量索引是索引和存储非结构化数据的一种替代方式。 请参阅我们的概念指南向量存储有关详细概述,请参阅。
简而言之,向量存储不是使用词频,而是使用嵌入模型将文档压缩为高维向量表示。 这使得可以通过简单的数学运算(如余弦相似度)高效地对嵌入向量进行相似性搜索。

进一步阅读

关系型数据库

关系型数据库是许多应用程序中使用的结构化数据存储的基本类型。 它们将数据组织为具有预定义模式的表格,其中每个表格代表一个实体或关系。 数据以行(记录)和列(属性)的形式存储,允许通过 SQL(结构化查询语言)进行高效的查询和操作。 关系型数据库在维护数据完整性、支持复杂查询以及处理不同数据实体之间的关系方面表现出色。

进一步阅读

图数据库

图数据库是一种专门用于存储和管理高度互联数据的数据库类型。 与传统的关系型数据库不同,图数据库采用灵活的结构,由节点(实体)、边(关系)和属性组成。 这种结构能够高效地表示和查询复杂、互联的数据。 图数据库以图结构存储数据,包含节点、边和属性。 它们特别适用于存储和查询数据点之间的复杂关系,例如社交网络、供应链管理、欺诈检测和推荐服务。

进一步阅读

检索器

LangChain 通过 检索器 概念为与各种检索系统进行交互提供了统一的接口。该接口非常直观:

  1. 输入:查询(字符串)
  2. 输出:文档列表(标准化的 LangChain Document 对象)

您可以使用前面提到的任何检索系统来创建检索器。我们讨论过的查询分析技术在此特别有用,因为它们能够为通常需要结构化查询语言的数据库提供自然语言接口。 例如,您可以为 SQL 数据库构建一个基于文本到 SQL 转换的检索器。这使得自然语言查询(字符串)能够在幕后被转换为 SQL 查询。 无论底层检索系统如何,LangChain 中的所有检索器都共享一个通用接口。您可以通过简单的 invoke 方法使用它们:

docs = retriever.invoke(query)
进一步阅读