Skip to main content

基于RedisStack向量数据库的Django数据模型应用。

Project description

django-vectorstore-indexed-model

基于RedisStack向量数据库的Django数据模型应用。

安装

pip install django-vectorstore-indexed-model

依赖说明

  • 数据模型对于向量数据库的操作深度依赖openai-redis-vectorstore。相关配置详见该项目文档。

使用

app/models.py

from typing import List
from django.db import models
from django_vectorstore_indexed_model.models import WithVectorStoreIndex
from django_model_helper.models import WithEnabledStatusFields
from django_model_helper.models import WithDeletedStatusFields


class QA(WithEnabledStatusFields, WithDeletedStatusFields, WithVectorStoreIndex):
    enable_auto_vectorstore_index = False

    kb = models.CharField(max_length=64)
    question = models.CharField(max_length=128)
    answer = models.TextField()

    def get_enable_vectorstore_index_flag(self) -> bool:
        """判断是否需要创建索引"""
        # 一般来说数据记录中应该有enabled或deleted等字段
        # 表示是否启用索引
        if not self.enabled:
            return False
        if self.deleted:
            return False
        return True

    def get_vectorstore_index_names(self):
        # 如果有多个index_name表示:
        # 本数据记录需要在多个向量数据库中建立索引
        return self.kb

    def get_vectorstore_index_contents(self) -> List[str]:
        # 向量数据库有索引长度的限制
        # 所以一般文档内容需要分片后进行索引
        # 这里返回分片列表
        return [f"问题:{self.question}\n参考答案:{self.answer}"]

可以重载的方法

重载以下3个方法可以快速解决contents和metas一致情况下的单索引或多重索引问题

  • get_vectorstore_index_names
  • get_vectorstore_index_contents
  • get_vectorstore_index_meta

重载以下方法可以解决contents和metas不一致情况下的多重索引问题

  • get_vectorstore_index_segments

注意:重载本方法后,get_vectorstore_index_namesget_vectorstore_index_contentsget_vectorstore_index_meta三个方法将失效。

版本记录

v0.1.0

  • 版本首发。

v0.1.1

  • 添加WithVectorStoreIndex.get_vectorstore_index_segments以解决content和meta各不相同的多重索引问题。
  • 修正打包时版本号引用问题。

v0.2.0

  • 优化:保存时不再自动重建索引。
  • 优化:使用匹配最新的openai-redis-vectorstore,支持字段知识库、文档、类型过滤。

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

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

File details

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

File metadata

File hashes

Hashes for django_vectorstore_indexed_model-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 529fa4cd2f240fd79a69f9cfc0bbd2d8a88b26122d0c5673d7af3246256b40dc
MD5 b53909a6f3e41c8f6389d4ae64606e07
BLAKE2b-256 8129f96790db40db3d0cea50c7bfb8fc0c76428c45b2e84fa9d440b0812f0838

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