基于RedisStack向量数据库,集成embeddings和rerank模型,支持二阶段召回,支持添加和删除等管理功能。
Project description
openai-redis-vectorstore
基于RedisStack向量数据库,集成embeddings和rerank模型,支持二阶段召回,支持添加和删除等管理功能。
安装
pip install openai-redis-vectorstore
依赖说明
- 使用
python-environment-settings管理配置项。详见该项目的参考文档。 - 深度依赖于
openai-simple-embeddings和openai-simple-rerank。同时也依赖于两者的配置项。详见这两个项目的参考文档。
配置项说明
- OPENAI_REDIS_VECTORSTORE_REDIS_STACK_URL: redis-stack服务器地址。如:redis://localhost:6379/0
- 要使用redis-stack向量功能,必须是0号库(否则会报
redis.exceptions.ResponseError: Cannot create index on db != 0错误) - (配置项别名)
- REDIS_STACK_URL
- REDIS_URL
- REDIS
- 要使用redis-stack向量功能,必须是0号库(否则会报
使用
将文本插入到向量数据库
代码:
from openai_redis_vectorstore import RedisVectorStore
index_name = str(uuid.uuid4())
page_id = str(uuid.uuid4())
rvs = RedisVectorStore(index_name=index_name)
uid = rvs.insert("hello", metadata={"id": 1}, page_id=page_id)
assert uid == f"{index_name}:{page_id}"
说明:
- id=1表示内容在业务系统中的唯一码。
- uid表示内容在向量数据库中的唯一码。可以根据uid唯一码,从向量数据库中删除相应内容。
搜索向量数据库(多索引支持)
代码:
from openai_redis_vectorstore.base import RedisVectorStore
index_name1 = str(uuid.uuid4())
index_name2 = str(uuid.uuid4())
rvs = RedisVectorStore()
# 向1号逻辑库中插入3条数据
rvs.insert_many(
["开会了", "再见", "你好"],
metadatas=[
{"id": 1},
{"id": 2},
{"id": 3},
],
index_name=index_name1,
)
# 向2号逻辑库中插入3条数据
rvs.insert_many(
["开会去", "好的", "谢谢"],
metadatas=[
{"id": 1},
{"id": 2},
{"id": 3},
],
index_name=index_name2,
)
# 从1号和2号逻辑库中搜索关键词并汇总
# 并要求匹配度不低于指定阈值
docs = rvs.similarity_search_and_rerank(
query="开会",
index_names=[index_name1, index_name2],
embeddings_score_threshold=0.65,
rerank_score_threshold=0.85,
)
assert len(docs) == 2
doc1 = docs[0]
doc2 = docs[1]
assert doc1.vs_index_name in [index_name1, index_name2]
assert doc2.vs_index_name in [index_name1, index_name2]
assert doc1.vs_rerank_score > doc2.vs_rerank_score
搜索向量数据库(kb_id和category过滤条件的支持)
from openai_redis_vectorstore.base import RedisVectorStore
index_name = str(uuid.uuid4())
rvs = RedisVectorStore(index_name=index_name)
rvs.insert("hello", kb_id="k1", category="c1")
rvs.insert("hi", kb_id="k2", category="c2")
docs = rvs.similarity_search_with_relevance_scores(
"hello",
kb_ids=["k1", "k2"],
category="c2",
)
assert len(docs) == 1
assert docs[0].vs_page_content == "hi"
v0.2.x 与 v0.1.x不兼容部分
- v0.2.x使用langchain_redis,不再使用langchain_community。
- 被索引的文本保存到了text字段,原来是content字段。
- 被索引的向量保存到了embedding字段,原来是content_vector字段。
- 由于上述的不兼容性,要求升级后全部重建索引。
版本记录
v0.1.0
- 版本首发。
v0.1.1
- 修改:搜索一个空向量库时,只在日志中记录WARNING信息并返回空数组。
v0.1.2
- 修改:
openai_redis_vectorstore.schemas.Document增加content字段。
v0.1.3
- 修正:查询结果page_content字段没有做反序列化的问题。
v0.2.0
- 优化:默认配置项与
embeddings/rerank相关配置项保持一致。 - 优化:允许
embeddings/rerank使用不同的base_url和api_key。 - 变更(不兼容):使用
langchain_redis取代langchain_community.vectorstores.redis。
v0.2.1
- 新增:支持
kb_id,category过滤条件。
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openai_redis_vectorstore-0.2.1-py3-none-any.whl.
File metadata
- Download URL: openai_redis_vectorstore-0.2.1-py3-none-any.whl
- Upload date:
- Size: 13.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
48c0e27e0c351842306bc76417c06b35fbe231aefeefc94326e5a2b6782f5dc3
|
|
| MD5 |
efee7a1d1026bbbce1ee0e18046fcc8a
|
|
| BLAKE2b-256 |
d30cefeff099d04b75b8f2c4fc125fab183eea2e1fc2ab0aec5e33ad0a896067
|