引言
在当今的数据驱动世界中,能够实时处理和分析多样化数据类型的数据库成为了许多组织的重要工具。DingoDB正是这样一个分布式多模态向量数据库,结合了数据湖和向量数据库的特点,能够存储任意类型和大小的数据(如Key-Value, PDF, 音频, 视频等)。本文将探讨如何使用DingoDB实现自查询检索器(SelfQueryRetriever),帮助你迅速获得对多模态数据的洞察力。
主要内容
创建DingoDB索引
首先,我们需要创建一个DingoDB向量存储,并通过一些数据进行初始化。我们准备了一组电影摘要的小型演示文档集。
准备工作
确保你已经启动并运行了DingoDB实例。在开始之前,你需要安装相关的Python包:
%pip install --upgrade --quiet dingodb
# 或安装最新版
%pip install --upgrade --quiet git+https://git@github.com/dingodb/pydingo.git
同时,我们需要获取OpenAI的API密钥以使用OpenAIEmbeddings:
import os
OPENAI_API_KEY = "" # 替换为你的OpenAI API密钥
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
创建DingoDB索引
使用以下代码示例,创建一个DingoDB索引并将其填充数据:
from langchain_community.vectorstores import Dingo
from langchain_core.documents import Document
from langchain_openai import OpenAIEmbeddings
from dingodb import DingoDB
# 实例化OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
index_name = "langchain_demo"
# 初始化DingoDB客户端
dingo_client = DingoDB(user="", password="", host=["172.30.14.221:13000"]) # 使用API代理服务提高访问稳定性
# 检查索引是否已存在,如果不存在则创建
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"'}),
# 其他文档...
]
# 将文档集导入Dingo向量存储
vectorstore = Dingo.from_documents(
docs, embeddings, index_name=index_name, client=dingo_client
)
创建自查询检索器
接下来,我们需要根据文档的元数据字段和内容创建一个自查询检索器。
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
)
测试自查询检索器
使用以下代码测试检索器:
# 只指定相关查询
retriever.invoke("What are some movies about dinosaurs")
# 只指定过滤条件
retriever.invoke("I want to watch a movie rated higher than 8.5")
# 指定查询和过滤器
retriever.invoke("Has Greta Gerwig directed any movies about women")
# 指定复合过滤器
retriever.invoke("What's a highly rated (above 8.5) science fiction film?")
常见问题和解决方案
在使用DingoDB时,可能会遇到以下问题:
-
连接错误:由于网络限制,某些地区可能无法直接访问DingoDB服务器。在这种情况下,可以考虑使用API代理服务来提高访问的稳定性。
-
API限流:在调用OpenAI接口时,可能会遇到API限流的问题。建议在代码中处理重试逻辑或者优化请求频率。
总结和进一步学习资源
本文介绍了如何在DingoDB中创建索引并使用自查询检索器进行数据查询。DingoDB强大的实时处理能力和多模态数据支持使其成为现代数据分析的强大工具。想要进一步研究,建议参考以下资源:
参考资料
- DingoDB GitHub仓库: github.com/dingodb/pyd…
- Langchain GitHub仓库: github.com/langchain/l…
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---