基础
数据检索是任何RAG应用中的关键步骤。最常见的用例是从您的数据中检索相关上下文以帮助回答问题。
一旦数据被摄取到LlamaCloud中,您可以使用检索API从您的数据中检索相关上下文。
我们的检索API允许您检索已摄入索引中与给定查询相关的真实文本片段。 以下代码片段展示了如何运行这种基础形式的检索:
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
# connect to existing indexindex = LlamaCloudIndex("my_first_index", project_name="Default")
# configure retriever# alpha=1.0 restricts it to vector search.retriever = index.as_retriever( dense_similarity_top_k=3, alpha=1.0, enable_reranking=False,)nodes = retriever.retrieve("Example query")import { LlamaCloudIndex } from "llama-cloud-services";
// connect to existing indexconst 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});
// configure retrieverconst retriever = index.asRetriever({ similarityTopK: 3, sparseSimilarityTopK: 3, alpha: 0.5, enableReranking: true, rerankTopN: 3,});
const nodes = retriever.retrieve({ query: "Example Query"});我们可以通过引入混合搜索、重排序和元数据过滤等高级功能来改进这种基础检索形式,从而提高检索的准确性。这些高级检索参数将在下一节中详细说明 ➡️