DingoDB 是一个分布式多模式向量数据库,结合了数据湖和向量数据库的特性,可以存储任何类型和大小的数据(键-值、PDF、音频、视频等)。它具有实时低延迟的处理能力,实现快速洞察和响应,并且能够高效地进行多模态数据的即时分析和处理。在本教程中,我们将演示如何使用 DingoDB 向量存储和 SelfQueryRetriever 实现数据检索。
技术背景介绍
DingoDB 是一个强大的工具,可以处理各种类型和规模的数据。通过与开源的 langchain
库结合,我们可以利用其强大的数据存储和检索功能,进行多模态数据处理和分析。SelfQueryRetriever 是 langchain
提供的一个高级组件,允许我们通过自然语言查询来检索相关数据。
核心原理解析
SelfQueryRetriever 使用预训练的语言模型来解析查询,并将其转换为具体的检索请求。与此同时,DingoDB 作为底层向量存储,提供高效的数据索引和检索服务。我们将通过嵌入向量将数据存储到 DingoDB 中,然后使用 SelfQueryRetriever 在这些嵌入向量中进行检索。
代码实现演示
1. 安装依赖
首先,我们需要安装 DingoDB 客户端以及相关的 Python 包。
%pip install --upgrade --quiet dingodb
# 或者安装最新版本
%pip install --upgrade --quiet git+https://git@github.com/dingodb/pydingo.git
2. 配置 OpenAI API 密钥
我们使用 OpenAI 提供的嵌入向量,因此需要配置 OpenAI API 密钥。
import os
OPENAI_API_KEY = "your-api-key"
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
3. 创建 DingoDB 向量存储
下面的代码演示了如何创建一个 DingoDB 向量存储,并加载一些示例数据。
from langchain_community.vectorstores import Dingo
from langchain_core.documents import Document
from langchain_openai import OpenAIEmbeddings
from dingodb import DingoDB
# 设置 OpenAI 嵌入向量
embeddings = OpenAIEmbeddings()
# 创建新的索引
index_name = "langchain_demo"
dingo_client = DingoDB(user="", password="", host=["172.30.14.221:13000"])
# 检查索引是否存在,如果不存在则创建
if (index_name not in dingo_client.get_index() and index_name.upper() not in dingo_client.get_index()):
dingo_client.create_index(index_name=index_name, dimension=1536, metric_type="cosine", auto_id=False)
# 示例文档
docs = [
Document(page_content="A bunch of scientists bring back dinosaurs and mayhem breaks loose", metadata={"year": 1993, "rating": 7.7, "genre": '"action", "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}),
Document(page_content="A psychologist / detective gets lost in a series of dreams within dreams within dreams and Inception reused the idea", metadata={"year": 2006, "director": "Satoshi Kon", "rating": 8.6}),
Document(page_content="A bunch of normal-sized women are supremely wholesome and some men pine after them", metadata={"year": 2019, "director": "Greta Gerwig", "rating": 8.3}),
Document(page_content="Toys come alive and have a blast doing so", metadata={"year": 1995, "genre": "animated"}),
Document(page_content="Three men walk into the Zone, three men walk out of the Zone", metadata={"year": 1979, "director": "Andrei Tarkovsky", "genre": '"science fiction", "thriller"', "rating": 9.9}),
]
# 将文档加载到向量存储中
vectorstore = Dingo.from_documents(docs, embeddings, index_name=index_name, client=dingo_client)
4. 创建 SelfQueryRetriever
创建 SelfQueryRetriever 需要提供文档的元数据字段信息以及文档内容的简要描述。
from langchain.chains.query_constructor.base import AttributeInfo
from langchain.retrievers.self_query.base import SelfQueryRetriever
from langchain_openai import OpenAI
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 = OpenAI(temperature=0)
retriever = SelfQueryRetriever.from_llm(llm, vectorstore, document_content_description, metadata_field_info, verbose=True)
5. 测试 SelfQueryRetriever
下面是一些使用 SelfQueryRetriever 进行数据检索的示例。
# 通过关键词检索电影
print(retriever.invoke("What are some movies about dinosaurs"))
# 通过评分过滤电影
print(retriever.invoke("I want to watch a movie rated higher than 8.5"))
# 通过导演和关键词检索电影
print(retriever.invoke("Has Greta Gerwig directed any movies about women"))
# 通过组合条件检索电影
print(retriever.invoke("What's a highly rated (above 8.5) science fiction film?"))
# 通过组合条件和关键词检索电影
print(retriever.invoke("What's a movie after 1990 but before 2005 that's all about toys, and preferably is animated"))
应用场景分析
这种配置可以用于各种多模态数据处理场景,例如电子商务推荐、智能搜索、语义分析等。通过结合 DingoDB 的高效存储和检索能力,可以快速响应用户查询,并提供高质量的搜索结果。
实战建议
- 数据准备: 确保数据的质量和丰富度,尽可能为每个文档提供详细的元数据信息。
- 索引优化: 根据具体应用场景优化索引参数,例如选择合适的嵌入向量维度和度量类型。
- 性能调优: 定期监控和优化系统性能,确保检索速度和准确性。
如果遇到问题欢迎在评论区交流。