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

LlamaCloudIndex 把本框架接到 Index 的第一个版本,也就是 LlamaParse(LlamaIndex 团队的托管文档平台)里的托管摄取与检索服务。LlamaParse 此前名为 LlamaCloud,下面这些类名与包名正来源于此。

本集成面向的是 Index 的较早版本

LlamaCloudIndex 随 llama-cloud-services 包提供,而该包已被弃用、不再更新,它对接的是 Index 的第一个版本。安装它会锁定一个旧版的 llama-cloud,因此无法与当前的 llama-cloud SDK 共存于同一环境。新项目请通过 llama-cloud SDK 使用 Index v2。

Index 为你提供

  • 托管摄取:解析、分块、嵌入与文档管理,并与你的数据源保持同步
  • 托管检索:为你的 RAG 系统调好的检索端点,在这里可作为 LlamaIndex 的检索器或查询引擎使用

关于服务本身,见该版本 Index 的文档。

访问

先注册 LlamaParse拿到 API key,然后从控制台或用下面的代码创建一个索引。如果你需要自托管或独立区域部署,请联系我们了解企业方案。

设置

首先,请确保已安装最新的 LlamaIndex 版本。

pip uninstall llama-index  # run this if upgrading from v0.9.x or older
pip install -U llama-index --upgrade --no-cache-dir --force-reinstall

然后安装集成包——它并不包含在 llama-index 里:

pip install -U llama-cloud-services

用量

你可以用下面的代码在 LlamaCloud 上创建一个索引:

import os

os.environ[
    "LLAMA_CLOUD_API_KEY"
] = "llx-..."  # can provide API-key in env or in the constructor later on

from llama_index.core import SimpleDirectoryReader
from llama_cloud_services import LlamaCloudIndex

# create a new index
index = LlamaCloudIndex.from_documents(
    documents,
    "my_first_index",
    project_name="default",
    api_key="llx-...",
    verbose=True,
)

# connect to an existing index
index = LlamaCloudIndex("my_first_index", project_name="default")

你也可以为托管检索配置一个检索器:

# from the existing index
index.as_retriever()

# from scratch
from llama_cloud_services import LlamaCloudRetriever

retriever = LlamaCloudRetriever("my_first_index", project_name="default")

当然,你还可以用其他索引快捷方法来用上这个新建的托管索引:

query_engine = index.as_query_engine(llm=llm)

chat_engine = index.as_chat_engine(llm=llm)

检索器设置

下面是检索器全部设置/关键字参数清单:

  • dense_similarity_top_k: Optional[int] —— 大于 0 时,用稠密检索取回 k 个节点
  • sparse_similarity_top_k: Optional[int] —— 大于 0 时,用稀疏检索取回 k 个节点
  • enable_reranking: Optional[bool] —— 是否启用重排。以一部分速度换取准确度
  • rerank_top_n: Optional[int] —— 对初始检索结果重排之后返回的节点数量
  • alpha Optional[float] —— 稠密检索与稀疏检索之间的权重。1 表示完全稠密检索,0 表示完全稀疏检索。

组合检索用法

当你搭好了多个索引、分别摄取各种形式的数据后,你可能想创建一个应用,能跨所有索引的数据进行查询。

这时就可以用 LlamaCloudCompositeRetriever 类。下面这段代码演示如何配置这个组合检索器:

import os
from llama_cloud import CompositeRetrievalMode, RetrieverPipeline
from llama_cloud_services import (
    LlamaCloudIndex,
    LlamaCloudCompositeRetriever,
)

llama_cloud_api_key = os.environ["LLAMA_CLOUD_API_KEY"]
project_name = "Essays"

# Setup your indices
pg_documents = SimpleDirectoryReader("./examples/data/paul_graham").load_data()
pg_index = LlamaCloudIndex.from_documents(
    documents=pg_documents,
    name="PG Index",
    project_name=project_name,
    api_key=llama_cloud_api_key,
)

sama_documents = SimpleDirectoryReader(
    "./examples/data/sam_altman"
).load_data()
sama_index = LlamaCloudIndex.from_documents(
    documents=sama_documents,
    name="Sam Index",
    project_name=project_name,
    api_key=llama_cloud_api_key,
)

retriever = LlamaCloudCompositeRetriever(
    name="Essays Retriever",
    project_name=project_name,
    api_key=llama_cloud_api_key,
    # If a Retriever named "Essays Retriever" doesn't already exist, one will be created
    create_if_not_exists=True,
    # CompositeRetrievalMode.FULL will query each index individually and globally rerank results at the end
    mode=CompositeRetrievalMode.FULL,
    rerank_top_n=5,
)

# Add the above indices to the composite retriever
# Carefully craft the description as this is used internally to route a query to an attached sub-index when CompositeRetrievalMode.ROUTING is used
retriever.add_index(pg_index, description="A collection of Paul Graham essays")
retriever.add_index(
    sama_index, description="A collection of Sam Altman essays"
)

# Start retrieving context for your queries
# async .aretrieve() is also available
nodes = retriever.retrieve("What does YC do?")

组合检索相关参数

有几个参数专门用于调优组合检索:

  • mode: Optional[CompositeRetrievalMode] —— 可取 CompositeRetrievalMode.FULL 或 CompositeRetrievalMode.ROUTING

    • full:该模式下会查询所有挂载的子索引,并对这些子索引返回的全部节点做重排。
    • routing:该模式下由一个 agent 判断哪些子索引与给定查询最相关(依据你为每个子索引提供的 name 与 description),只查询那些被判定相关的索引。随后只对这一选中子集里的节点做重排,再作为检索结果返回。
  • rerank_top_n: Optional[int] —— 决定在对所有索引检索到的节点做重排之后返回多少个节点

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