Skip to content

LlamaCloudIndex + LlamaCloudRetriever#

LlamaCloud 是新一代的托管解析、数据摄取和检索服务,旨在为您的LLM和RAG应用提供生产级的上下文增强能力。

目前LlamaCloud支持:

  • 托管数据摄取API,处理文档解析与管理
  • 托管检索API,为RAG系统配置最优检索方案

有关LlamaCloud及本集成的更多文档,请参阅官方LlamaCloud文档

访问权限#

我们正在向有限数量的企业合作伙伴开放托管数据摄取和检索API的私有测试版。如果您希望集中管理数据管道,将更多时间投入实际RAG用例开发,欢迎联系我们

若已获得LlamaCloud访问权限,可登录LlamaCloud获取API密钥。

环境配置#

首先确保安装最新版LlamaIndex:

pip uninstall llama-index  # 如果从v0.9.x或更早版本升级需执行此命令
pip install -U llama-index --upgrade --no-cache-dir --force-reinstall

llama-index-indices-managed-llama-cloud包已包含在上述安装中,但也可单独安装:

pip install -U llama-index-indices-managed-llama-cloud

使用指南#

可通过以下代码在LlamaCloud上创建索引:

import os

os.environ[
    "LLAMA_CLOUD_API_KEY"
] = "llx-..."  # 可通过环境变量或后续构造函数提供API密钥

from llama_index.core import SimpleDirectoryReader
from llama_index.indices.managed.llama_cloud import LlamaCloudIndex

# 创建新索引
index = LlamaCloudIndex.from_documents(
    documents,
    "my_first_index",
    project_name="default",
    api_key="llx-...",
    verbose=True,
)

# 连接现有索引
index = LlamaCloudIndex("my_first_index", project_name="default")

也可配置托管检索器:

# 从现有索引创建
index.as_retriever()

# 全新创建
from llama_index.indices.managed.llama_cloud 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_index.indices.managed.llama_cloud import (
    LlamaCloudIndex,
    LlamaCloudCompositeRetriever,
)

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

# 配置索引
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,
    # 若名为"Essays Retriever"的检索器不存在,将自动创建
    create_if_not_exists=True,
    # CompositeRetrievalMode.FULL会单独查询每个索引并最终全局重排序结果
    mode=CompositeRetrievalMode.FULL,
    rerank_top_n=5,
)

# 将上述索引添加到复合检索器
# 需精心设计描述文本,当使用CompositeRetrievalMode.ROUTING模式时,该描述将用于内部路由查询到附属子索引
retriever.add_index(pg_index, description="保罗·格雷厄姆文集")
retriever.add_index(
    sama_index, description="萨姆·奥尔特曼文集"
)

# 开始为查询检索上下文
# 也可使用异步方法.aretrieve()
nodes = retriever.retrieve("YC是做什么的?")

复合检索相关参数#

以下是专用于调整复合检索行为的参数: - mode: Optional[CompositeRetrievalMode] -- 可选CompositeRetrievalMode.FULLCompositeRetrievalMode.ROUTING - full模式:查询所有附属子索引,并对从这些子索引获取的所有节点执行全局重排序 - routing模式:由智能代理根据子索引的namedescription判断哪些子索引与查询最相关,仅查询被判定相关的索引,最后对从选定子索引获取的节点进行重排序后返回 - rerank_top_n: Optional[int] -- 决定在所有索引检索到的节点中,经过重排序后返回的节点数量