跳到内容

LlamaCloudIndex + LlamaCloudRetriever#

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

目前,LlamaCloud 支持

  • 托管摄取 API,处理解析和文档管理
  • 托管检索 API,为您的 RAG 系统配置最佳检索

有关 LlamaCloud 以及此特定集成的更多文档,请参考我们的官方 LlamaCloud 文档

访问#

我们正在向有限的企业合作伙伴开放托管摄取和检索 API 的私有测试版。如果您有兴趣集中管理您的数据管道并花更多时间专注于实际的 RAG 用例,请联系我们。

如果您有权访问 LlamaCloud,可以访问LlamaCloud登录并获取 API 密钥。

设置#

首先,请确保您已安装最新版本的 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-indices-managed-llama-cloud 包已包含在上述安装中,但您也可以直接安装

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

用法#

您可以使用以下代码在 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_index.indices.managed.llama_cloud 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_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)

检索器设置#

以下是检索器设置/kwargs 的完整列表

  • 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"

# 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.FULLCompositeRetrievalMode.ROUTING - full: 在此模式下,将查询所有附加的子索引,并对从这些子索引接收到的所有节点执行重新排序。- routing: 在此模式下,代理会根据您提供的子索引的 name & description 确定哪些子索引与提供的查询最相关,并仅查询那些被认为相关的索引。然后,仅对从所选子集索引中检索到的节点进行重新排序,然后才在检索响应中返回。- rerank_top_n: Optional[int] -- 确定对从所有索引检索到的节点进行重新排序后返回的节点数量