Skip to main content

基于RedisStack向量数据库,集成embeddings和rerank模型,支持二阶段召回,支持添加和删除等管理功能。

Project description

openai-redis-vectorstore

基于RedisStack向量数据库,集成embeddings和rerank模型,支持二阶段召回,支持添加和删除等管理功能。

安装

pip install openai-redis-vectorstore

依赖说明

  • 使用python-environment-settings管理配置项。详见该项目的参考文档。
  • 深度依赖于openai-simple-embeddingsopenai-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

使用

将文本插入到向量数据库

代码:

from openai_redis_vectorstore import RedisVectorStore

index_name = str(uuid.uuid4())
doc_uid = str(uuid.uuid4())
rvs = RedisVectorStore()
uid = rvs.insert("hello", metadata={"id": 1}, index_name=index_name, id=doc_uid)
assert uid == f"{index_name}:{doc_uid}"

说明:

  • 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(
    ["开会了", "再见", "你好"],
    metas=[
        {"id": 1},
        {"id": 2},
        {"id": 3},
    ],
    index_name=index_name1,
)

# 向2号逻辑库中插入3条数据
rvs.insert_many(
    ["开会去", "好的", "谢谢"],
    metas=[
        {"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

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_urlapi_key
  • 变更(不兼容):使用langchain_redis取代langchain_community.vectorstores.redis

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openai_redis_vectorstore-0.2.0.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openai_redis_vectorstore-0.2.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file openai_redis_vectorstore-0.2.0.tar.gz.

File metadata

  • Download URL: openai_redis_vectorstore-0.2.0.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for openai_redis_vectorstore-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e29936391d293eef55867b5ede3abcb1def6ba8f84f138afaf248b5e4c3d04e2
MD5 80ffa06e1f2caa1a465191bd765a4dad
BLAKE2b-256 2d35879ffb3cd4979aaba51d3de9f0a5c6b185e786adeaee3d06b94a966177ca

See more details on using hashes here.

File details

Details for the file openai_redis_vectorstore-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_redis_vectorstore-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ce9825bb261dbb1aab0e0f37ce2a4643be909f4057f3ad8c432060c92e0d6221
MD5 121e4c7c79fc5664690e192349ae52d9
BLAKE2b-256 6adcc46266f64e2cf5facfd876d0ac6223a9e5518aa440d779c2d051800f26a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page