索引框架集成
索引与我们的开源Python框架和TypeScript框架无缝协作。
您可以将 LlamaCloudIndex 作为 VectorStoreIndex 的直接替代方案使用。
它提供开箱即用的更优性能,同时简化了设置与维护流程。
您可以通过框架创建索引,或连接到现有索引(例如通过无代码界面创建的索引)。
与通过无代码用户界面创建新索引相比,
您可以通过框架集成从Document对象创建索引。
这使您能够对以下方面进行更底层的控制:
- 您希望如何预处理数据,以及
- 使用来自 LlamaHub 的任何数据加载器。
请注意,在这种情况下,数据加载将在本地运行(即与框架代码一起运行)。 对于大规模数据摄取,最好通过无代码界面创建索引,或通过REST API 与客户端使用文件或数据源API。
加载文档
from llama_index.core import SimpleDirectoryReader
documents = SimpleDirectoryReader("data").load_data()创建 OpenAIFinetuneEngineLlamaCloudIndex
import osfrom llama_cloud_services import LlamaCloudIndex
os.environ[ "LLAMA_CLOUD_API_KEY"] = "llx-..." # can provide API-key in env or in the constructor later on
index = LlamaCloudIndex.from_documents( documents, "my_first_index", project_name="Default", api_key="llx-...", verbose=True,)加载文档
import { SimpleDirectoryReader } from "llamaindex";
const documents = await new SimpleDirectoryReader().loadData({ directoryPath: "node_modules/llamaindex/examples"});创建 OpenAIFinetuneEngineLlamaCloudIndex
import { LlamaCloudIndex } from "llama-cloud-services";
const index = await LlamaCloudIndex.fromDocuments({ documents: documents, name: "example-pipeline", projectName: "Default", apiKey: "llx-..."});您也可以选择性地向 .from_documents 方法提供一个 organization_id 字符串参数。
如果您在不同组织下拥有相同名称的多个项目,并且您是这些组织的成员,这可能会很有用(更多信息)。
通常,如果您的账户属于多个组织,建议提供此参数,以确保随着您所在组织中创建更多项目,您的代码能继续正常工作。
连接到现有索引
import os
os.environ[ "LLAMA_CLOUD_API_KEY"] = "llx-..." # can provide API-key in env or in the constructor later on
from llama_cloud_services import LlamaCloudIndex
index = LlamaCloudIndex("my_first_index", project_name="Default")连接到现有索引
const index = new LlamaCloudIndex({ name: "example-pipeline", projectName: "Default", apiKey: process.env.LLAMA_CLOUD_API_KEY, // can provide API-key in the constructor or in the env});在RAG/智能体应用中使用索引
Section titled “Use index in RAG/agent application”retriever = index.as_retriever()nodes = retriever.retrieve("Example query")...
query_engine = index.as_query_engine()answer = query_engine.query("Example query")...
chat_engine = index.as_chat_engine()message = chat_engine.chat("Example query")...查看完整框架文档
// query engineconst queryEngine = index.asQueryEngine({ similarityTopK: 5,});
const answer = await queryEngine.query({ query: "Example query",});
// retrievalconst retriever = index.asRetriever({ similarityTopK: 5,});
const nodes = await retriever.retrieve({ query: "Example query",});
// chatimport { ContextChatEngine } from "llamaindex";
const retriever = index.asRetriever({ similarityTopK: 5,});
const chatEngine = new ContextChatEngine({ retriever });
const responder = await chatEngine.chat({ message: query });