使用托管索引
LlamaIndex 提供了与托管索引的多个集成点。托管索引是一种特殊类型的索引,它并非作为 LlamaIndex 的一部分在本地管理,而是通过 API(例如 Vectara)进行管理。
与LlamaIndex中的任何其他索引(树状索引、关键词表索引、列表索引)类似,任何ManagedIndex都可以通过文档集合构建。构建完成后,该索引可用于查询。
如果索引之前已经填充了文档,也可以直接用于查询。
Google 生成式语言语义检索器
Section titled “Google Generative Language Semantic Retriever”谷歌的语义检索服务同时提供查询和检索功能。创建托管索引,插入文档,并在LlamaIndex中的任意位置使用查询引擎或检索器!
from llama_index.core import SimpleDirectoryReaderfrom llama_index.indices.managed.google import GoogleIndex
# Create a corpusindex = GoogleIndex.create_corpus(display_name="My first corpus!")print(f"Newly created corpus ID is {index.corpus_id}.")
# Ingestiondocuments = SimpleDirectoryReader("data").load_data()index.insert_documents(documents)
# Queryingquery_engine = index.as_query_engine()response = query_engine.query("What did the author do growing up?")
# Retrievingretriever = index.as_retriever()source_nodes = retriever.retrieve("What did the author do growing up?")查看笔记本指南获取完整详情。
Vectara
Section titled “Vectara”首先,注册并使用 Vectara 控制台创建一个语料库(又称索引),并添加用于访问的 API 密钥。 获取 API 密钥后,将其导出为环境变量:
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, SimpleDirectoryReadefrom llama_index.indices.managed.vectara import VectaraIndex
# Load documents and build indexvectara_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语料库,您可以将
VECTARA_CORPUS_KEY设置为逗号分隔的列表,例如:12,51将连接到语料库12和语料库51。
如果您语料库中已有文档,可以直接通过如下方式构建 VectaraIndex 来访问数据:
index = VectaraIndex()VectaraIndex 将连接到现有语料库,无需加载任何新文档。
要查询索引,只需按如下方式构建查询引擎:
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(基于Vertex AI的LlamaIndex)
Section titled “Vertex AI RAG (LlamaIndex on Vertex AI)”基于 Vertex AI 的 LlamaIndex RAG 实现是 Google Cloud Vertex AI 上的托管式 RAG 索引。
首先,创建一个谷歌云项目并启用 Vertex AI API。然后运行以下代码来创建托管索引。
from llama_index.indices.managed.vertexai import VertexAIIndex
# TODO(developer): Replace these values with your project informationproject_id = "YOUR_PROJECT_ID"location = "us-central1"
# Optional: If using an existing corpuscorpus_id = "YOUR_CORPUS_ID"
# Optional: If creating a new corpuscorpus_display_name = "my-corpus"corpus_description = "Vertex AI Corpus for LlamaIndex"
# Create a corpus or provide an existing corpus IDindex = 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 Driveindex.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 fileindex.insert_file( file_path="my_file.txt", metadata={"display_name": "my_file.txt", "description": "My file"},)
# Queryingquery_engine = index.as_query_engine()response = query_engine.query("What is RAG and why it is helpful?")
# Retrievingretriever = index.as_retriever()nodes = retriever.retrieve("What is RAG and why it is helpful?")查看笔记本指南获取完整详情。