django-vectorstore-indexed-model 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django-vectorstore-indexed-model-0.1.0/LICENSE +201 -0
- django-vectorstore-indexed-model-0.1.0/MANIFEST.in +9 -0
- django-vectorstore-indexed-model-0.1.0/PKG-INFO +77 -0
- django-vectorstore-indexed-model-0.1.0/README.md +60 -0
- django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model/__init__.py +0 -0
- django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model/admin.py +0 -0
- django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model/apps.py +6 -0
- django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model/migrations/__init__.py +0 -0
- django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model/models.py +137 -0
- django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model/schemas.py +8 -0
- django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model/tests.py +0 -0
- django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model/version.py +1 -0
- django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model/views.py +0 -0
- django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model.egg-info/PKG-INFO +77 -0
- django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model.egg-info/SOURCES.txt +20 -0
- django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model.egg-info/dependency_links.txt +1 -0
- django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model.egg-info/not-zip-safe +1 -0
- django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model.egg-info/requires.txt +3 -0
- django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model.egg-info/top_level.txt +1 -0
- django-vectorstore-indexed-model-0.1.0/requirements.txt +3 -0
- django-vectorstore-indexed-model-0.1.0/setup.cfg +4 -0
- django-vectorstore-indexed-model-0.1.0/setup.py +46 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
include MANIFEST.in
|
|
2
|
+
include LICENSE
|
|
3
|
+
include README.md
|
|
4
|
+
include requirements.txt
|
|
5
|
+
graft django_vectorstore_indexed_model
|
|
6
|
+
exclude django_vectorstore_indexed_model_demo
|
|
7
|
+
exclude django_vectorstore_indexed_model_example
|
|
8
|
+
global-exclude __pycache__
|
|
9
|
+
global-exclude *.py[co]
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: django-vectorstore-indexed-model
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 基于RedisStack向量数据库的Django数据模型应用。
|
|
5
|
+
Author: rRR0VrFP
|
|
6
|
+
Maintainer: rRR0VrFP
|
|
7
|
+
License: Apache License, Version 2.0
|
|
8
|
+
Keywords: django-vectorstore-indexed-model
|
|
9
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
|
|
18
|
+
# django-vectorstore-indexed-model
|
|
19
|
+
|
|
20
|
+
基于RedisStack向量数据库的Django数据模型应用。
|
|
21
|
+
|
|
22
|
+
## 安装
|
|
23
|
+
|
|
24
|
+
```shell
|
|
25
|
+
pip install django-vectorstore-indexed-model
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 依赖说明
|
|
29
|
+
|
|
30
|
+
- 数据模型对于向量数据库的操作深度依赖`openai-redis-vectorstore`。相关配置详见该项目文档。
|
|
31
|
+
|
|
32
|
+
## 使用
|
|
33
|
+
|
|
34
|
+
*app/models.py*
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from typing import List
|
|
38
|
+
from django.db import models
|
|
39
|
+
from django_vectorstore_indexed_model.models import WithVectorStoreIndex
|
|
40
|
+
from django_model_helper.models import WithEnabledStatusFields
|
|
41
|
+
from django_model_helper.models import WithDeletedStatusFields
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class QA(WithEnabledStatusFields, WithDeletedStatusFields, WithVectorStoreIndex):
|
|
45
|
+
enable_auto_vectorstore_index = False
|
|
46
|
+
|
|
47
|
+
kb = models.CharField(max_length=64)
|
|
48
|
+
question = models.CharField(max_length=128)
|
|
49
|
+
answer = models.TextField()
|
|
50
|
+
|
|
51
|
+
def get_enable_vectorstore_index_flag(self) -> bool:
|
|
52
|
+
"""判断是否需要创建索引"""
|
|
53
|
+
# 一般来说数据记录中应该有enabled或deleted等字段
|
|
54
|
+
# 表示是否启用索引
|
|
55
|
+
if not self.enabled:
|
|
56
|
+
return False
|
|
57
|
+
if self.deleted:
|
|
58
|
+
return False
|
|
59
|
+
return True
|
|
60
|
+
|
|
61
|
+
def get_vectorstore_index_names(self):
|
|
62
|
+
# 如果有多个index_name表示:
|
|
63
|
+
# 本数据记录需要在多个向量数据库中建立索引
|
|
64
|
+
return self.kb
|
|
65
|
+
|
|
66
|
+
def get_vectorstore_index_contents(self) -> List[str]:
|
|
67
|
+
# 向量数据库有索引长度的限制
|
|
68
|
+
# 所以一般文档内容需要分片后进行索引
|
|
69
|
+
# 这里返回分片列表
|
|
70
|
+
return [f"问题:{self.question}\n参考答案:{self.answer}"]
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## 版本记录
|
|
74
|
+
|
|
75
|
+
### v0.1.0
|
|
76
|
+
|
|
77
|
+
- 版本首发。
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# django-vectorstore-indexed-model
|
|
2
|
+
|
|
3
|
+
基于RedisStack向量数据库的Django数据模型应用。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
pip install django-vectorstore-indexed-model
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 依赖说明
|
|
12
|
+
|
|
13
|
+
- 数据模型对于向量数据库的操作深度依赖`openai-redis-vectorstore`。相关配置详见该项目文档。
|
|
14
|
+
|
|
15
|
+
## 使用
|
|
16
|
+
|
|
17
|
+
*app/models.py*
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
from typing import List
|
|
21
|
+
from django.db import models
|
|
22
|
+
from django_vectorstore_indexed_model.models import WithVectorStoreIndex
|
|
23
|
+
from django_model_helper.models import WithEnabledStatusFields
|
|
24
|
+
from django_model_helper.models import WithDeletedStatusFields
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class QA(WithEnabledStatusFields, WithDeletedStatusFields, WithVectorStoreIndex):
|
|
28
|
+
enable_auto_vectorstore_index = False
|
|
29
|
+
|
|
30
|
+
kb = models.CharField(max_length=64)
|
|
31
|
+
question = models.CharField(max_length=128)
|
|
32
|
+
answer = models.TextField()
|
|
33
|
+
|
|
34
|
+
def get_enable_vectorstore_index_flag(self) -> bool:
|
|
35
|
+
"""判断是否需要创建索引"""
|
|
36
|
+
# 一般来说数据记录中应该有enabled或deleted等字段
|
|
37
|
+
# 表示是否启用索引
|
|
38
|
+
if not self.enabled:
|
|
39
|
+
return False
|
|
40
|
+
if self.deleted:
|
|
41
|
+
return False
|
|
42
|
+
return True
|
|
43
|
+
|
|
44
|
+
def get_vectorstore_index_names(self):
|
|
45
|
+
# 如果有多个index_name表示:
|
|
46
|
+
# 本数据记录需要在多个向量数据库中建立索引
|
|
47
|
+
return self.kb
|
|
48
|
+
|
|
49
|
+
def get_vectorstore_index_contents(self) -> List[str]:
|
|
50
|
+
# 向量数据库有索引长度的限制
|
|
51
|
+
# 所以一般文档内容需要分片后进行索引
|
|
52
|
+
# 这里返回分片列表
|
|
53
|
+
return [f"问题:{self.question}\n参考答案:{self.answer}"]
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## 版本记录
|
|
57
|
+
|
|
58
|
+
### v0.1.0
|
|
59
|
+
|
|
60
|
+
- 版本首发。
|
|
File without changes
|
|
File without changes
|
django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model/migrations/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
from typing import Dict
|
|
2
|
+
from typing import List
|
|
3
|
+
|
|
4
|
+
import yaml
|
|
5
|
+
from openai_redis_vectorstore.base import RedisVectorStore
|
|
6
|
+
|
|
7
|
+
from django.db import models
|
|
8
|
+
from django.db import transaction
|
|
9
|
+
from django.utils.safestring import mark_safe
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class WithVectorStoreIndex(models.Model):
|
|
13
|
+
no_update_vectorstore_index_flag_key = (
|
|
14
|
+
"_with_vectorstore_index_no_update_vectorstore_index_flag"
|
|
15
|
+
)
|
|
16
|
+
# 是否自动索引,即每次在记录保存时进行索引
|
|
17
|
+
# 默认:启用自动索引
|
|
18
|
+
enable_auto_do_vectorstore_index = True
|
|
19
|
+
|
|
20
|
+
vectorstore_updated = models.BooleanField(
|
|
21
|
+
null=True,
|
|
22
|
+
blank=True,
|
|
23
|
+
verbose_name="已更新向量数据库",
|
|
24
|
+
help_text="None:表示待处理。<br />True:表示索引成功。<br />False:表示索引失败。",
|
|
25
|
+
)
|
|
26
|
+
vectorstore_uids_data = models.TextField(
|
|
27
|
+
null=True,
|
|
28
|
+
blank=True,
|
|
29
|
+
verbose_name="向量数据库记录编号",
|
|
30
|
+
help_text=mark_safe(
|
|
31
|
+
"添加至向量数据库后返回的编号。用于后续向量数据库的数据维护。"
|
|
32
|
+
),
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
class Meta:
|
|
36
|
+
abstract = True
|
|
37
|
+
|
|
38
|
+
def save(self, *args, **kwargs):
|
|
39
|
+
result = super().save(*args, **kwargs)
|
|
40
|
+
if self.enable_auto_do_vectorstore_index and (
|
|
41
|
+
not self.is_no_update_vectorstore_index_flag_marked()
|
|
42
|
+
):
|
|
43
|
+
transaction.on_commit(self.update_vectorstore_index)
|
|
44
|
+
return result
|
|
45
|
+
|
|
46
|
+
def is_no_update_vectorstore_index_flag_marked(self):
|
|
47
|
+
return getattr(self, self.no_update_vectorstore_index_flag_key, False)
|
|
48
|
+
|
|
49
|
+
def mark_no_update_vectorstore_index_flag(self):
|
|
50
|
+
setattr(self, self.no_update_vectorstore_index_flag_key, True)
|
|
51
|
+
|
|
52
|
+
def update_vectorstore_index(self, save=True):
|
|
53
|
+
# 对象已经触发了索引,则不能再次触发。
|
|
54
|
+
# 需要重新从数据库查询生成新的实例才能再次触发索引。
|
|
55
|
+
self.mark_no_update_vectorstore_index_flag()
|
|
56
|
+
if self.get_enable_vectorstore_index_flag():
|
|
57
|
+
self.upsert_index(save=False)
|
|
58
|
+
else:
|
|
59
|
+
self.delete_index(save=False)
|
|
60
|
+
self.vectorstore_updated = True
|
|
61
|
+
if save:
|
|
62
|
+
self.save()
|
|
63
|
+
|
|
64
|
+
# vectorstore_uids属性处理
|
|
65
|
+
def get_vectorstore_uids(self):
|
|
66
|
+
if not self.vectorstore_uids_data:
|
|
67
|
+
return []
|
|
68
|
+
else:
|
|
69
|
+
return yaml.safe_load(self.vectorstore_uids_data)
|
|
70
|
+
|
|
71
|
+
def set_vectorstore_uids(self, value):
|
|
72
|
+
if not value:
|
|
73
|
+
self.vectorstore_uids_data = None
|
|
74
|
+
else:
|
|
75
|
+
self.vectorstore_uids_data = yaml.safe_dump(value)
|
|
76
|
+
|
|
77
|
+
vectorstore_uids = property(get_vectorstore_uids, set_vectorstore_uids)
|
|
78
|
+
|
|
79
|
+
def get_enable_vectorstore_index_flag(self) -> bool:
|
|
80
|
+
"""判断是否需要创建索引"""
|
|
81
|
+
# 一般来说数据记录中应该有enabled或deleted等字段
|
|
82
|
+
# 表示是否启用索引
|
|
83
|
+
raise NotImplementedError()
|
|
84
|
+
|
|
85
|
+
def get_vectorstore_index_names(self) -> List[str]:
|
|
86
|
+
# 如果有多个index_name表示:
|
|
87
|
+
# 本数据记录需要在多个向量数据库中建立索引
|
|
88
|
+
raise NotImplementedError()
|
|
89
|
+
|
|
90
|
+
def get_vectorstore_index_contents(self) -> List[str]:
|
|
91
|
+
# 向量数据库有索引长度的限制
|
|
92
|
+
# 所以一般文档内容需要分片后进行索引
|
|
93
|
+
# 这里返回分片列表
|
|
94
|
+
raise NotImplementedError()
|
|
95
|
+
|
|
96
|
+
def get_vectorstore_index_metas(self, contents=None) -> List[Dict[str, str]]:
|
|
97
|
+
if contents is None:
|
|
98
|
+
contents = self.get_vectorstore_index_contents()
|
|
99
|
+
meta = self.get_vectorstore_index_meta()
|
|
100
|
+
return [meta] * len(contents)
|
|
101
|
+
|
|
102
|
+
def get_vectorstore_index_meta(self) -> Dict[str, str]:
|
|
103
|
+
return {
|
|
104
|
+
"app_label": self._meta.app_label,
|
|
105
|
+
"model_name": self._meta.model_name,
|
|
106
|
+
"id": self.id,
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
def delete_index(self, save=False):
|
|
110
|
+
if self.vectorstore_uids:
|
|
111
|
+
vs = RedisVectorStore()
|
|
112
|
+
vs.delete_many(self.vectorstore_uids)
|
|
113
|
+
self.vectorstore_uids = None
|
|
114
|
+
if save:
|
|
115
|
+
self.save()
|
|
116
|
+
|
|
117
|
+
def upsert_index(self, save=False):
|
|
118
|
+
vs = RedisVectorStore()
|
|
119
|
+
texts = self.get_vectorstore_index_contents()
|
|
120
|
+
metas = self.get_vectorstore_index_metas()
|
|
121
|
+
# 先删除
|
|
122
|
+
self.delete_index(save=False)
|
|
123
|
+
# 后添加
|
|
124
|
+
uids = []
|
|
125
|
+
index_names = self.get_vectorstore_index_names()
|
|
126
|
+
if isinstance(index_names, str):
|
|
127
|
+
index_names = [index_names]
|
|
128
|
+
for index_name in index_names:
|
|
129
|
+
uids += vs.insert_many(
|
|
130
|
+
texts=texts,
|
|
131
|
+
metas=metas,
|
|
132
|
+
index_name=index_name,
|
|
133
|
+
)
|
|
134
|
+
# 更新数据库记录
|
|
135
|
+
self.vectorstore_uids = uids
|
|
136
|
+
if save:
|
|
137
|
+
self.save()
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
VERSION = "0.1.0"
|
|
File without changes
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: django-vectorstore-indexed-model
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 基于RedisStack向量数据库的Django数据模型应用。
|
|
5
|
+
Author: rRR0VrFP
|
|
6
|
+
Maintainer: rRR0VrFP
|
|
7
|
+
License: Apache License, Version 2.0
|
|
8
|
+
Keywords: django-vectorstore-indexed-model
|
|
9
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
|
|
18
|
+
# django-vectorstore-indexed-model
|
|
19
|
+
|
|
20
|
+
基于RedisStack向量数据库的Django数据模型应用。
|
|
21
|
+
|
|
22
|
+
## 安装
|
|
23
|
+
|
|
24
|
+
```shell
|
|
25
|
+
pip install django-vectorstore-indexed-model
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 依赖说明
|
|
29
|
+
|
|
30
|
+
- 数据模型对于向量数据库的操作深度依赖`openai-redis-vectorstore`。相关配置详见该项目文档。
|
|
31
|
+
|
|
32
|
+
## 使用
|
|
33
|
+
|
|
34
|
+
*app/models.py*
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from typing import List
|
|
38
|
+
from django.db import models
|
|
39
|
+
from django_vectorstore_indexed_model.models import WithVectorStoreIndex
|
|
40
|
+
from django_model_helper.models import WithEnabledStatusFields
|
|
41
|
+
from django_model_helper.models import WithDeletedStatusFields
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class QA(WithEnabledStatusFields, WithDeletedStatusFields, WithVectorStoreIndex):
|
|
45
|
+
enable_auto_vectorstore_index = False
|
|
46
|
+
|
|
47
|
+
kb = models.CharField(max_length=64)
|
|
48
|
+
question = models.CharField(max_length=128)
|
|
49
|
+
answer = models.TextField()
|
|
50
|
+
|
|
51
|
+
def get_enable_vectorstore_index_flag(self) -> bool:
|
|
52
|
+
"""判断是否需要创建索引"""
|
|
53
|
+
# 一般来说数据记录中应该有enabled或deleted等字段
|
|
54
|
+
# 表示是否启用索引
|
|
55
|
+
if not self.enabled:
|
|
56
|
+
return False
|
|
57
|
+
if self.deleted:
|
|
58
|
+
return False
|
|
59
|
+
return True
|
|
60
|
+
|
|
61
|
+
def get_vectorstore_index_names(self):
|
|
62
|
+
# 如果有多个index_name表示:
|
|
63
|
+
# 本数据记录需要在多个向量数据库中建立索引
|
|
64
|
+
return self.kb
|
|
65
|
+
|
|
66
|
+
def get_vectorstore_index_contents(self) -> List[str]:
|
|
67
|
+
# 向量数据库有索引长度的限制
|
|
68
|
+
# 所以一般文档内容需要分片后进行索引
|
|
69
|
+
# 这里返回分片列表
|
|
70
|
+
return [f"问题:{self.question}\n参考答案:{self.answer}"]
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## 版本记录
|
|
74
|
+
|
|
75
|
+
### v0.1.0
|
|
76
|
+
|
|
77
|
+
- 版本首发。
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
requirements.txt
|
|
5
|
+
setup.py
|
|
6
|
+
django_vectorstore_indexed_model/__init__.py
|
|
7
|
+
django_vectorstore_indexed_model/admin.py
|
|
8
|
+
django_vectorstore_indexed_model/apps.py
|
|
9
|
+
django_vectorstore_indexed_model/models.py
|
|
10
|
+
django_vectorstore_indexed_model/schemas.py
|
|
11
|
+
django_vectorstore_indexed_model/tests.py
|
|
12
|
+
django_vectorstore_indexed_model/version.py
|
|
13
|
+
django_vectorstore_indexed_model/views.py
|
|
14
|
+
django_vectorstore_indexed_model.egg-info/PKG-INFO
|
|
15
|
+
django_vectorstore_indexed_model.egg-info/SOURCES.txt
|
|
16
|
+
django_vectorstore_indexed_model.egg-info/dependency_links.txt
|
|
17
|
+
django_vectorstore_indexed_model.egg-info/not-zip-safe
|
|
18
|
+
django_vectorstore_indexed_model.egg-info/requires.txt
|
|
19
|
+
django_vectorstore_indexed_model.egg-info/top_level.txt
|
|
20
|
+
django_vectorstore_indexed_model/migrations/__init__.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
django-vectorstore-indexed-model-0.1.0/django_vectorstore_indexed_model.egg-info/top_level.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
django_vectorstore_indexed_model
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import os
|
|
3
|
+
from io import open
|
|
4
|
+
from setuptools import setup
|
|
5
|
+
from setuptools import find_packages
|
|
6
|
+
from openai_redis_vectorstore.version import VERSION
|
|
7
|
+
|
|
8
|
+
here = os.path.abspath(os.path.dirname(__file__))
|
|
9
|
+
|
|
10
|
+
with open(os.path.join(here, "README.md"), "r", encoding="utf-8") as fobj:
|
|
11
|
+
long_description = fobj.read()
|
|
12
|
+
|
|
13
|
+
with open(os.path.join(here, "requirements.txt"), "r", encoding="utf-8") as fobj:
|
|
14
|
+
requires = [x.strip() for x in fobj.readlines() if x.strip()]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
setup(
|
|
18
|
+
name="django-vectorstore-indexed-model",
|
|
19
|
+
version=VERSION,
|
|
20
|
+
description="基于RedisStack向量数据库的Django数据模型应用。",
|
|
21
|
+
long_description=long_description,
|
|
22
|
+
long_description_content_type="text/markdown",
|
|
23
|
+
author="rRR0VrFP",
|
|
24
|
+
maintainer="rRR0VrFP",
|
|
25
|
+
license="Apache License, Version 2.0",
|
|
26
|
+
classifiers=[
|
|
27
|
+
"Development Status :: 5 - Production/Stable",
|
|
28
|
+
"Intended Audience :: Developers",
|
|
29
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
30
|
+
"License :: OSI Approved :: Apache Software License",
|
|
31
|
+
"Programming Language :: Python :: 3",
|
|
32
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
33
|
+
],
|
|
34
|
+
keywords=["django-vectorstore-indexed-model"],
|
|
35
|
+
packages=find_packages(
|
|
36
|
+
".",
|
|
37
|
+
exclude=[
|
|
38
|
+
"django_vectorstore_indexed_model_demo",
|
|
39
|
+
"django_vectorstore_indexed_model_example",
|
|
40
|
+
"django_vectorstore_indexed_model_example.migrations",
|
|
41
|
+
],
|
|
42
|
+
),
|
|
43
|
+
install_requires=requires,
|
|
44
|
+
zip_safe=False,
|
|
45
|
+
include_package_data=True,
|
|
46
|
+
)
|