Skip to content

使用托管索引#

LlamaIndex 提供了与托管索引的多种集成方式。托管索引是一种特殊类型的索引,它并非由 LlamaIndex 在本地管理,而是通过 API(如 Vectara)进行远程管理。

使用托管索引#

与 LlamaIndex 中的其他索引(树状索引、关键词表索引、列表索引)类似,任何 ManagedIndex 都可以通过文档集合构建。构建完成后,该索引即可用于查询。

如果索引已预先填充了文档内容,也可以直接用于查询。

Google 生成式语言语义检索器#

Google 语义检索器同时提供查询和检索功能。您可以创建托管索引、插入文档,并在 LlamaIndex 的任何位置使用查询引擎或检索器!

from llama_index.core import SimpleDirectoryReader
from llama_index.indices.managed.google import GoogleIndex

# 创建语料库
index = GoogleIndex.create_corpus(display_name="My first corpus!")
print(f"Newly created corpus ID is {index.corpus_id}.")

# 文档注入
documents = SimpleDirectoryReader("data").load_data()
index.insert_documents(documents)

# 查询功能
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")

# 检索功能
retriever = index.as_retriever()
source_nodes = retriever.retrieve("What did the author do growing up?")

完整示例请参阅 笔记本指南

Vectara 集成#

首先注册账号并使用 Vectara 控制台创建语料库(即索引),同时添加 API 访问密钥。获取 API 密钥后,将其设置为环境变量:

import os

os.environ["VECTARA_API_KEY"] = "<YOUR_VECTARA_API_KEY>"
os.environ["VECTARA_CORPUS_KEY"] = "<YOUR_VECTARA_CORPUS_KEY>"

然后按以下方式构建 Vectara 索引并进行查询:

from llama_index.core import ManagedIndex, SimpleDirectoryReade
from llama_index.indices.managed.vectara import VectaraIndex

# 加载文档并构建索引
vectara_corpus_key = os.environ.get("VECTARA_CORPUS_KEY")
vectara_api_key = os.environ.get("VECTARA_API_KEY")

documents = SimpleDirectoryReader("../paul_graham_essay/data").load_data()
index = VectaraIndex.from_documents(
    documents,
    vectara_corpus_key=vectara_corpus_key,
    vectara_api_key=vectara_api_key,
)

注意事项: * 若环境变量 VECTARA_CORPUS_KEYVECTARA_API_KEY 已预先设置,则无需在调用时显式指定,VectaraIndex 类会自动从环境中读取。 * 如需连接多个 Vectara 语料库,可将 VECTARA_CORPUS_KEY 设置为逗号分隔的列表,例如:12,51 表示同时连接语料库 12 和 51。

若语料库已包含文档,可直接通过以下方式访问数据:

index = VectaraIndex()

此时 VectaraIndex 将直接连接现有语料库而无需加载新文档。

查询索引时,只需构建查询引擎:

query_engine = index.as_query_engine(summary_enabled=True)
print(query_engine.query("What did the author do growing up?"))

或使用聊天功能:

chat_engine = index.as_chat_engine()
print(chat_engine.chat("What did the author do growing up?").response)

聊天功能支持连续对话,所有会话历史均由 Vectara 平台自动维护,无需额外逻辑处理。

更多示例请参考: - Vectara 演示 - Vectara 自动检索器

Vertex AI RAG(基于 Vertex AI 的 LlamaIndex)#

基于 Vertex AI 的 LlamaIndex RAG 是 Google Cloud Vertex AI 上的托管式 RAG 索引。

首先创建 Google Cloud 项目并启用 Vertex AI API,然后运行以下代码创建托管索引:

from llama_index.indices.managed.vertexai import VertexAIIndex

# TODO(developer): 替换为您的项目信息
project_id = "YOUR_PROJECT_ID"
location = "us-central1"

# 可选:使用现有语料库
corpus_id = "YOUR_CORPUS_ID"

# 可选:创建新语料库
corpus_display_name = "my-corpus"
corpus_description = "Vertex AI Corpus for LlamaIndex"

# 创建新语料库或指定现有语料库ID
index = VertexAIIndex(
    project_id,
    location,
    corpus_display_name=corpus_display_name,
    corpus_description=corpus_description,
)
print(f"Newly created corpus name is {index.corpus_name}.")

# 从Google云存储或Google Drive导入文件
index.import_files(
    uris=["https://drive.google.com/file/123", "gs://my_bucket/my_files_dir"],
    chunk_size=512,  # 可选
    chunk_overlap=100,  # 可选
)

# 上传本地文件
index.insert_file(
    file_path="my_file.txt",
    metadata={"display_name": "my_file.txt", "description": "My file"},
)

# 查询功能
query_engine = index.as_query_engine()
response = query_engine.query("What is RAG and why it is helpful?")

# 检索功能
retriever = index.as_retriever()
nodes = retriever.retrieve("What is RAG and why it is helpful?")

完整示例请参阅 笔记本指南