跳到内容

LlamaIndex 提供了与托管索引的多个集成点。托管索引是一种特殊类型的索引,它不像 LlamaIndex 的一部分那样在本地管理,而是通过 API 进行管理,例如 Vectara

使用托管索引#

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

如果索引之前已填充了文档,它也可以直接用于查询。

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

Google 的语义检索提供了查询和检索功能。创建一个托管索引,插入文档,并在 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?")

Vectara#

首先,注册并使用 Vectara 控制台创建一个语料库(即索引),并添加 API 密钥以进行访问。获得 API 密钥后,将其导出为环境变量

然后构建 Vectara 索引并按如下方式查询

import os

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

注意:* 如果环境变量 VECTARA_CORPUS_KEYVECTARA_API_KEY 已在环境中,则无需在调用中显式指定它们,VectaraIndex 类将从环境中读取。* 要连接到多个 Vectara 语料库,可以将 VECTARA_CORPUS_KEY 设置为逗号分隔的列表,例如:12,51 将连接到语料库 12 和语料库 51

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,
)

如果您的语料库中已有文档,您可以直接通过以下方式构建 VectaraIndex 来访问数据

VectaraIndex 将连接到现有语料库,而无需加载任何新文档。

index = VectaraIndex()

要查询索引,只需按如下方式构建查询引擎

或者您可以使用聊天功能

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

聊天功能如您预期般工作,后续的 chat 调用会维护对话历史记录。所有这些都在 Vectara 平台上完成,因此您无需添加任何额外的逻辑。

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

更多示例 - 请参阅下方

Vectara 示例

Vertex AI 上的 LlamaIndex 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?")

返回顶部