使用 Vectara 的 Self-Query Retriever 实现高级查询功能

Vectaraprovides a Trusted Generative AI platform, allowing organizations to rapidly create a ChatGPT-like experience (an AI assistant) which is grounded in the data, documents, and knowledge that they have (technically, it is Retrieval-Augmented-Generation-as-a-service).

Vectara 的无服务器 RAG-as-a-service 提供了 RAG 的所有组件,并通过易于使用的 API 提供,包括文本提取、多模态搜索和基于 LLM 的生成性摘要等,能够输出有引用的生成性总结。本文将展示如何使用 Vectara 的 SelfQueryRetriever 实现高效的自查询功能。

技术背景介绍

Vectara 提供了一个创新的平台,使得 RAG(检索增强生成)服务更易于使用。通过提供与文档和知识库紧密结合的 AI 查询能力,可以实现类似 ChatGPT 的高级体验。

核心原理解析

Vectara 通过自己的内置矢量数据库和查询服务,可以将文本数据转换为嵌入,并检索最相关的文本片段。同时,通过 Vectara 的 API,用户可以轻松实现基于 RAG 的高级查询。SelfQueryRetriever是一个工具,它能够根据查询自动应用合适的过滤条件。

代码实现演示

以下示例代码将向您展示如何使用 Vectara 的 SelfQueryRetriever 进行文档查询:

import os
import getpass

# 配置环境变量,用于访问 Vectara 服务
os.environ["VECTARA_CUSTOMER_ID"] = getpass.getpass("Vectara Customer ID:")
os.environ["VECTARA_CORPUS_ID"] = getpass.getpass("Vectara Corpus ID:")
os.environ["VECTARA_API_KEY"] = getpass.getpass("Vectara API Key:")

from langchain.chains.query_constructor.base import AttributeInfo
from langchain.retrievers.self_query.base import SelfQueryRetriever
from langchain.schema import Document
from langchain_community.vectorstores import Vectara
from langchain_openai.chat_models import ChatOpenAI

# 定义数据集文档和元数据
docs = [
    Document(
        page_content="A bunch of scientists bring back dinosaurs and mayhem breaks loose",
        metadata={"year": 1993, "rating": 7.7, "genre": "science fiction"},
    ),
    Document(
        page_content="Leo DiCaprio gets lost in a dream within a dream within a dream within a ...",
        metadata={"year": 2010, "director": "Christopher Nolan", "rating": 8.2},
    ),
    # 更多文档...
]

vectara = Vectara()
for doc in docs:
    vectara.add_texts([doc.page_content], doc_metadata=doc.metadata)

# 创建自查询检索器
metadata_field_info = [
    AttributeInfo(
        name="genre",
        description="The genre of the movie",
        type="string or list[string]",
    ),
    AttributeInfo(
        name="year",
        description="The year the movie was released",
        type="integer",
    ),
    AttributeInfo(
        name="director",
        description="The name of the movie director",
        type="string",
    ),
    AttributeInfo(
        name="rating", description="A 1-10 rating for the movie", type="float"
    ),
]
document_content_description = "Brief summary of a movie"
llm = ChatOpenAI(temperature=0, model="gpt-4o", max_tokens=4069)
retriever = SelfQueryRetriever.from_llm(
    llm, vectara, document_content_description, metadata_field_info, verbose=True
)

# 示例查询
results = retriever.invoke("I want to watch a movie rated higher than 8.5")
for doc in results:
    print(doc.page_content, doc.metadata)

此代码展示了如何通过实例化 SelfQueryRetriever 来进行智能自查询。通过向检索器传递查询文本,您可以获得相关文档及其详细元数据。

应用场景分析

Vectara 的 Self-Query Retriever 可以广泛应用于需要高级文本检索的场景,如企业知识库查询、研究论文检索、媒体内容过滤等,为用户提供基于 AI 的高效可用解决方案。

实践建议

  1. 尝试创建不同的过滤器组合,以充分利用 Vectara 的查询能力。
  2. 仔细管理和保密 API 密钥,确保数据安全。
  3. 定期更新您的语料库,保持检索结果的最新性和准确性。

结束语:如果遇到问题欢迎在评论区交流。

—END—

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值