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

LlamaIndex 与托管索引(Managed Index)有多个集成接入点。托管索引是一类特殊的索引:它不在 LlamaIndex 本地管理,而是通过 API 来管理,例如 Vectara。

使用托管索引

与 LlamaIndex 中的其他索引(树形、关键词表、列表)类似,任何 ManagedIndex 都可以由一组文档构建。构建完成后,索引即可用于查询。

如果该索引此前已经写入过文档,也可以直接用它来查询。

Google Generative Language Semantic Retriever

Google 的 Semantic Retrieve 同时提供查询与检索能力。创建一个托管索引、插入文档,然后就能在 LlamaIndex 的任何地方使用查询引擎或检索器!

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

# Create a corpus
index = GoogleIndex.create_corpus(display_name="My first corpus!")
print(f"Newly created corpus ID is {index.corpus_id}.")

# Ingestion
documents = SimpleDirectoryReader("data").load_data()
index.insert_documents(documents)

# Querying
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")

# Retrieving
retriever = index.as_retriever()
source_nodes = retriever.retrieve("What did the author do growing up?")

完整细节见 notebook 指南。

Vectara

首先注册,并在 Vectara 控制台创建一个 corpus(也就是索引),再加一个用于访问的 API key。拿到 API key 后,把它导出为环境变量:

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

# Load documents and build index
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_KEY 与 VECTARA_API_KEY 已在环境中,你就不必在调用里显式传入,VectaraIndex 类会从环境中读取。
  • 要连接多个 Vectara corpus,可以把 VECTARA_CORPUS_KEY 设为逗号分隔的列表,例如 12,51 就会同时连接 corpus 12 与 corpus 51。

如果你的 corpus 里已有文档,可以直接访问这些数据,只需像下面这样构造 VectaraIndex:

index = VectaraIndex()

VectaraIndex 会连到已有的 corpus,而不会加载任何新文档。

要查询该索引,只需像下面这样构建一个查询引擎:

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)

对话的行为符合直觉——后续的 chat 调用会保持会话历史。这一切都在 Vectara 平台上完成,你不需要添加任何额外逻辑。

更多示例见下文:

Vertex AI RAG(LlamaIndex on Vertex AI)

LlamaIndex on Vertex AI for RAG 是 Google Cloud Vertex AI 上的一个托管 RAG 索引。

首先创建一个 Google Cloud 项目并启用 Vertex AI API。然后运行下面的代码来创建托管索引。

from llama_index.indices.managed.vertexai import VertexAIIndex

# TODO(developer): Replace these values with your project information
project_id = "YOUR_PROJECT_ID"
location = "us-central1"

# Optional: If using an existing corpus
corpus_id = "YOUR_CORPUS_ID"

# Optional: If creating a new corpus
corpus_display_name = "my-corpus"
corpus_description = "Vertex AI Corpus for LlamaIndex"

# Create a corpus or provide an existing corpus 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}.")

# Import files from Google Cloud Storage or Google Drive
index.import_files(
    uris=["https://drive.google.com/file/123", "gs://my_bucket/my_files_dir"],
    chunk_size=512,  # Optional
    chunk_overlap=100,  # Optional
)

# Upload local file
index.insert_file(
    file_path="my_file.txt",
    metadata={"display_name": "my_file.txt", "description": "My file"},
)

# Querying
query_engine = index.as_query_engine()
response = query_engine.query("What is RAG and why it is helpful?")

# Retrieving
retriever = index.as_retriever()
nodes = retriever.retrieve("What is RAG and why it is helpful?")

完整细节见 notebook 指南。

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