Skip to content
LlamaIndex 文档版本 跟随上游 源快照 f426dc70 更新于 2026-10-18

本指南演示如何使用 LlamaIndex 和 Milvus 构建检索增强生成(RAG)系统。

RAG 系统将检索系统与生成模型相结合,根据给定提示生成新文本。系统首先使用 Milvus 等向量相似度搜索引擎从语料库中检索相关文档,然后使用生成模型基于检索到的文档生成新文本。

Milvus 是全球最先进的开源向量数据库,旨在为嵌入相似度搜索和 AI 应用提供支持。

在本 notebook 中,我们将快速演示如何使用 MilvusVectorStore。

开始之前

安装依赖

如果你在 Colab 中打开这个 Notebook,可能需要先安装 LlamaIndex 🦙。

%pip install llama-index-vector-stores-milvus
%pip install llama-index

本 notebook 将使用 Milvus Lite,这要求更高版本的 pymilvus:

%pip install pymilvus>=2.4.2

如果你使用 Google Colab,为了启用刚刚安装的依赖项,你可能需要重启运行时(点击屏幕顶部的 “Runtime” 菜单,然后从下拉菜单中选择 “Restart session”)。

设置 OpenAI

首先添加 OpenAI API 密钥。这将允许我们访问 ChatGPT。

import openai

openai.api_key = "sk-***********"

准备数据

你可以使用以下命令下载示例数据:

! mkdir -p "data/"
! wget "https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt" -O "data/paul_graham_essay.txt"
! wget "https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/10k/uber_2021.pdf" -O "data/uber_2021.pdf"

开始使用

生成数据

作为第一个示例,让我们从文件 paul_graham_essay.txt 生成文档。这是 Paul Graham 的一篇文章,标题为 What I Worked On。我们将使用 SimpleDirectoryReader 生成文档。

from llama_index.core import SimpleDirectoryReader

# load documents
documents = SimpleDirectoryReader(
    input_files=["./data/paul_graham_essay.txt"]
).load_data()

print("Document ID:", documents[0].doc_id)
Document ID: 95f25e4d-f270-4650-87ce-006d69d82033

为数据创建索引

现在我们有了文档,可以创建索引并插入文档。对于索引,我们将使用 MilvusVectorStore。MilvusVectorStore 接受几个参数:

基本参数

  • uri (str, optional):要连接的 URI,对于 Milvus 或 Zilliz Cloud 服务,格式为 “https://address:port”;对于本地 Lite 版 Milvus,格式为 “path/to/local/milvus.db”。默认为 ”./milvus_llamaindex.db”。
  • token (str, optional):用于登录的令牌。如果不使用 rbac,则为空;如果使用 rbac,则很可能是 “username:password”。
  • collection_name (str, optional):存储数据的集合名称。默认为 “llamalection”。
  • overwrite (bool, optional):是否覆盖同名的现有集合。默认为 False。

标量字段,包括文档 ID 和文本

  • doc_id_field (str, optional):集合中 doc_id 字段的名称。默认为 DEFAULT_DOC_ID_KEY。
  • text_key (str, optional):传入集合中存储文本的键。在使用自有集合时使用。默认为 DEFAULT_TEXT_KEY。
  • scalar_field_names (list, optional):要包含在集合 schema 中的额外标量字段名称。
  • scalar_field_types (list, optional):额外标量字段的类型。

稠密字段

  • enable_dense (bool):用于启用或禁用稠密嵌入的布尔标志。默认为 True。
  • dim (int, optional):集合中嵌入向量的维度。当 enable_sparse 为 False 创建新集合时必需。
  • embedding_field (str, optional):集合稠密嵌入字段的名称,默认为 DEFAULT_EMBEDDING_KEY。
  • index_config (dict, optional):用于构建稠密嵌入索引的配置。默认为 None。
  • search_config (dict, optional):用于搜索 Milvus 稠密索引的配置。请注意,它必须与 index_config 指定的索引类型兼容。默认为 None。
  • similarity_metric (str, optional):用于稠密嵌入的相似度度量,目前支持 IP、COSINE 和 L2。

稀疏字段

  • enable_sparse (bool):用于启用或禁用稀疏嵌入的布尔标志。默认为 False。
  • sparse_embedding_field (str):稀疏嵌入字段的名称,默认为 DEFAULT_SPARSE_EMBEDDING_KEY。
  • sparse_embedding_function (Union[BaseSparseEmbeddingFunction, BaseMilvusBuiltInFunction], optional):如果 enable_sparse 为 True,则应提供此对象以将文本转换为稀疏嵌入。如果为 None,则将使用默认稀疏嵌入函数(BM25BuiltInFunction)。
  • sparse_index_config (dict, optional):用于构建稀疏嵌入索引的配置。默认为 None。

混合重排序器

  • hybrid_ranker (str):指定混合搜索查询中使用的重排序器类型。目前仅支持 [“RRFRanker”, “WeightedRanker”]。默认为 “RRFRanker”。

  • hybrid_ranker_params (dict, optional):混合重排序器的配置参数。此字典的结构取决于所使用的具体重排序器:

    • 对于 “RRFRanker”,它应包含:

      • “k” (int):用于倒数排名融合(RRF)的参数。此值用于在 RRF 算法中计算排名分数,该算法将多种排名策略合并为一个分数,以提高搜索相关性。
    • 对于 “WeightedRanker”,它需要:

      • “weights”(浮点数列表):一个恰好包含两个权重的列表:

        1. 稠密嵌入分量的权重。
        2. 稀疏嵌入分量的权重。这些权重用于调整混合检索过程中嵌入的稠密和稀疏分量的重要性。默认为空字典,表示重排序器将使用其预定义的默认设置。

其他

  • collection_properties (dict, optional):集合属性,例如 TTL(生存时间)和 MMAP(内存映射)。默认为 None。它可以包括:

    • “collection.ttl.seconds” (int):设置此属性后,当前集合中的数据将在指定时间后过期。集合中过期的数据将被清理,不会参与搜索或查询。
    • “mmap.enabled” (bool):是否在集合级别启用内存映射存储。
  • index_management (IndexManagement):指定要使用的索引管理策略。默认为 “create_if_not_exists”。

  • batch_size (int):配置向 Milvus 插入数据时每批处理的文档数量。默认为 DEFAULT_BATCH_SIZE。

  • consistency_level (str, optional):新建集合使用哪种一致性级别。默认为 “Session”。

# Create an index over the documents
from llama_index.core import VectorStoreIndex, StorageContext
from llama_index.vector_stores.milvus import MilvusVectorStore

vector_store = MilvusVectorStore(
    uri="./milvus_demo.db", dim=1536, overwrite=True
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
    documents, storage_context=storage_context
)

有关 MilvusVectorStore 的参数:

  • 将 uri 设置为本地文件(例如 ./milvus.db)是最方便的方法,因为它会自动利用 Milvus Lite 将所有数据存储在该文件中。
  • 如果你有大规模数据,可以在 docker 或 kubernetes 上搭建一个更高性能的 Milvus 服务器。在此设置中,请使用服务器 URI,例如 http://localhost:19530,作为你的 uri。
  • 如果你想使用 Zilliz Cloud——Milvus 的全托管云服务,请调整 uri 和 token,它们对应 Zilliz Cloud 中的公共端点和 API 密钥。

查询数据

现在文档已存储在索引中,我们可以针对索引提问。索引将使用其中存储的数据作为 ChatGPT 的知识库。

query_engine = index.as_query_engine()
res = query_engine.query("What did the author learn?")
print(res)
The author learned that philosophy courses in college were boring to him, leading him to switch his focus to studying AI.
res = query_engine.query(
    "What challenges did the disease pose for the author?"
)
print(res)
The disease posed challenges for the author as it affected his mother's health, leading to a stroke caused by colon cancer. This resulted in her losing her balance and needing to be placed in a nursing home. The author and his sister were determined to help their mother get out of the nursing home and back to her house.

接下来的测试表明,覆盖操作会移除先前的数据。

from llama_index.core import Document

vector_store = MilvusVectorStore(
    uri="./milvus_demo.db", dim=1536, overwrite=True
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
    [Document(text="The number that is being searched for is ten.")],
    storage_context,
)
query_engine = index.as_query_engine()
res = query_engine.query("Who is the author?")
print(res)
The author is the individual who created the context information.

下一个测试展示向已存在的索引添加额外数据。

del index, vector_store, storage_context, query_engine

vector_store = MilvusVectorStore(uri="./milvus_demo.db", overwrite=False)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
    documents, storage_context=storage_context
)
query_engine = index.as_query_engine()
res = query_engine.query("What is the number?")
print(res)
The number is ten.
res = query_engine.query("Who is the author?")
print(res)
Paul Graham

元数据过滤

我们可以通过过滤特定来源来生成结果。以下示例演示如何从目录中加载所有文档,然后根据元数据对它们进行过滤。

from llama_index.core.vector_stores import ExactMatchFilter, MetadataFilters

# Load all the two documents loaded before
documents_all = SimpleDirectoryReader("./data/").load_data()

vector_store = MilvusVectorStore(
    uri="./milvus_demo.db", dim=1536, overwrite=True
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(documents_all, storage_context)

我们只想从文件 uber_2021.pdf 中检索文档。

filters = MetadataFilters(
    filters=[ExactMatchFilter(key="file_name", value="uber_2021.pdf")]
)
query_engine = index.as_query_engine(filters=filters)
res = query_engine.query(
    "What challenges did the disease pose for the author?"
)

print(res)
The disease posed challenges related to the adverse impact on the business and operations, including reduced demand for Mobility offerings globally, affecting travel behavior and demand. Additionally, the pandemic led to driver supply constraints, impacted by concerns regarding COVID-19, with uncertainties about when supply levels would return to normal. The rise of the Omicron variant further affected travel, resulting in advisories and restrictions that could adversely impact both driver supply and consumer demand for Mobility offerings.

这次从文件 paul_graham_essay.txt 中检索时,我们得到了不同的结果。

filters = MetadataFilters(
    filters=[ExactMatchFilter(key="file_name", value="paul_graham_essay.txt")]
)
query_engine = index.as_query_engine(filters=filters)
res = query_engine.query(
    "What challenges did the disease pose for the author?"
)

print(res)
The disease posed challenges for the author as it affected his mother's health, leading to a stroke caused by colon cancer. This resulted in his mother losing her balance and needing to be placed in a nursing home. The author and his sister were determined to help their mother get out of the nursing home and back to her house.

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。