veadk-python 0.2.7__py3-none-any.whl → 0.2.9__py3-none-any.whl
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.
Potentially problematic release.
This version of veadk-python might be problematic. Click here for more details.
- veadk/agent.py +3 -2
- veadk/auth/veauth/opensearch_veauth.py +75 -0
- veadk/auth/veauth/postgresql_veauth.py +75 -0
- veadk/cli/cli.py +3 -1
- veadk/cli/cli_eval.py +160 -0
- veadk/cli/cli_prompt.py +9 -2
- veadk/cli/cli_web.py +6 -1
- veadk/configs/database_configs.py +43 -0
- veadk/configs/model_configs.py +32 -0
- veadk/consts.py +11 -4
- veadk/evaluation/adk_evaluator/adk_evaluator.py +5 -2
- veadk/evaluation/base_evaluator.py +95 -68
- veadk/evaluation/deepeval_evaluator/deepeval_evaluator.py +23 -15
- veadk/evaluation/eval_set_recorder.py +2 -2
- veadk/integrations/ve_prompt_pilot/ve_prompt_pilot.py +9 -3
- veadk/integrations/ve_tls/utils.py +1 -2
- veadk/integrations/ve_tls/ve_tls.py +9 -5
- veadk/integrations/ve_tos/ve_tos.py +542 -68
- veadk/knowledgebase/backends/base_backend.py +59 -0
- veadk/knowledgebase/backends/in_memory_backend.py +82 -0
- veadk/knowledgebase/backends/opensearch_backend.py +136 -0
- veadk/knowledgebase/backends/redis_backend.py +144 -0
- veadk/knowledgebase/backends/utils.py +91 -0
- veadk/knowledgebase/backends/vikingdb_knowledge_backend.py +524 -0
- veadk/{database/__init__.py → knowledgebase/entry.py} +10 -2
- veadk/knowledgebase/knowledgebase.py +120 -139
- veadk/memory/__init__.py +22 -0
- veadk/memory/long_term_memory.py +124 -41
- veadk/{database/base_database.py → memory/long_term_memory_backends/base_backend.py} +10 -22
- veadk/memory/long_term_memory_backends/in_memory_backend.py +65 -0
- veadk/memory/long_term_memory_backends/mem0_backend.py +129 -0
- veadk/memory/long_term_memory_backends/opensearch_backend.py +120 -0
- veadk/memory/long_term_memory_backends/redis_backend.py +127 -0
- veadk/memory/long_term_memory_backends/vikingdb_memory_backend.py +148 -0
- veadk/memory/short_term_memory.py +80 -72
- veadk/memory/short_term_memory_backends/base_backend.py +31 -0
- veadk/memory/short_term_memory_backends/mysql_backend.py +41 -0
- veadk/memory/short_term_memory_backends/postgresql_backend.py +41 -0
- veadk/memory/short_term_memory_backends/sqlite_backend.py +48 -0
- veadk/runner.py +12 -19
- veadk/tools/builtin_tools/generate_image.py +355 -0
- veadk/tools/builtin_tools/image_edit.py +56 -16
- veadk/tools/builtin_tools/image_generate.py +51 -15
- veadk/tools/builtin_tools/video_generate.py +41 -41
- veadk/tools/builtin_tools/web_scraper.py +1 -1
- veadk/tools/builtin_tools/web_search.py +7 -7
- veadk/tools/load_knowledgebase_tool.py +2 -8
- veadk/tracing/telemetry/attributes/extractors/llm_attributes_extractors.py +21 -3
- veadk/tracing/telemetry/exporters/apmplus_exporter.py +24 -6
- veadk/tracing/telemetry/exporters/cozeloop_exporter.py +2 -0
- veadk/tracing/telemetry/exporters/inmemory_exporter.py +22 -8
- veadk/tracing/telemetry/exporters/tls_exporter.py +2 -0
- veadk/tracing/telemetry/opentelemetry_tracer.py +13 -10
- veadk/tracing/telemetry/telemetry.py +66 -63
- veadk/utils/misc.py +15 -0
- veadk/version.py +1 -1
- {veadk_python-0.2.7.dist-info → veadk_python-0.2.9.dist-info}/METADATA +28 -5
- {veadk_python-0.2.7.dist-info → veadk_python-0.2.9.dist-info}/RECORD +65 -56
- veadk/database/database_adapter.py +0 -533
- veadk/database/database_factory.py +0 -80
- veadk/database/kv/redis_database.py +0 -159
- veadk/database/local_database.py +0 -62
- veadk/database/relational/mysql_database.py +0 -173
- veadk/database/vector/opensearch_vector_database.py +0 -263
- veadk/database/vector/type.py +0 -50
- veadk/database/viking/__init__.py +0 -13
- veadk/database/viking/viking_database.py +0 -638
- veadk/database/viking/viking_memory_db.py +0 -525
- /veadk/{database/kv → knowledgebase/backends}/__init__.py +0 -0
- /veadk/{database/relational → memory/long_term_memory_backends}/__init__.py +0 -0
- /veadk/{database/vector → memory/short_term_memory_backends}/__init__.py +0 -0
- {veadk_python-0.2.7.dist-info → veadk_python-0.2.9.dist-info}/WHEEL +0 -0
- {veadk_python-0.2.7.dist-info → veadk_python-0.2.9.dist-info}/entry_points.txt +0 -0
- {veadk_python-0.2.7.dist-info → veadk_python-0.2.9.dist-info}/licenses/LICENSE +0 -0
- {veadk_python-0.2.7.dist-info → veadk_python-0.2.9.dist-info}/top_level.txt +0 -0
|
@@ -1,533 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
|
|
2
|
-
#
|
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
# you may not use this file except in compliance with the License.
|
|
5
|
-
# You may obtain a copy of the License at
|
|
6
|
-
#
|
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
#
|
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
# See the License for the specific language governing permissions and
|
|
13
|
-
# limitations under the License.
|
|
14
|
-
|
|
15
|
-
import re
|
|
16
|
-
import time
|
|
17
|
-
from typing import BinaryIO, TextIO
|
|
18
|
-
|
|
19
|
-
from veadk.database.base_database import BaseDatabase
|
|
20
|
-
from veadk.utils.logger import get_logger
|
|
21
|
-
|
|
22
|
-
logger = get_logger(__name__)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
class KVDatabaseAdapter:
|
|
26
|
-
def __init__(self, client):
|
|
27
|
-
from veadk.database.kv.redis_database import RedisDatabase
|
|
28
|
-
|
|
29
|
-
self.client: RedisDatabase = client
|
|
30
|
-
|
|
31
|
-
def index_exists(self, index: str) -> bool:
|
|
32
|
-
"""
|
|
33
|
-
Check if the index (key) exists in Redis.
|
|
34
|
-
|
|
35
|
-
Args:
|
|
36
|
-
index: The Redis key to check
|
|
37
|
-
|
|
38
|
-
Returns:
|
|
39
|
-
bool: True if the key exists, False otherwise
|
|
40
|
-
"""
|
|
41
|
-
try:
|
|
42
|
-
# Use Redis EXISTS command to check if key exists
|
|
43
|
-
return bool(self.client._client.exists(index))
|
|
44
|
-
except Exception as e:
|
|
45
|
-
logger.error(
|
|
46
|
-
f"Failed to check if key exists in Redis: index={index} error={e}"
|
|
47
|
-
)
|
|
48
|
-
return False
|
|
49
|
-
|
|
50
|
-
def add(self, data: list[str], index: str, **kwargs):
|
|
51
|
-
logger.debug(f"Adding documents to Redis database: index={index}")
|
|
52
|
-
|
|
53
|
-
try:
|
|
54
|
-
for _data in data:
|
|
55
|
-
self.client.add(key=index, value=_data)
|
|
56
|
-
logger.debug(f"Added {len(data)} texts to Redis database: index={index}")
|
|
57
|
-
except Exception as e:
|
|
58
|
-
logger.error(
|
|
59
|
-
f"Failed to add data to Redis database: index={index} error={e}"
|
|
60
|
-
)
|
|
61
|
-
raise e
|
|
62
|
-
|
|
63
|
-
def query(self, query: str, index: str, top_k: int = 0) -> list:
|
|
64
|
-
logger.debug(f"Querying Redis database: index={index} query={query}")
|
|
65
|
-
|
|
66
|
-
# ignore top_k, as KV search only return one result
|
|
67
|
-
_ = top_k
|
|
68
|
-
|
|
69
|
-
try:
|
|
70
|
-
result = self.client.query(key=index, query=query)
|
|
71
|
-
return result
|
|
72
|
-
except Exception as e:
|
|
73
|
-
logger.error(f"Failed to search from Redis: index={index} error={e}")
|
|
74
|
-
raise e
|
|
75
|
-
|
|
76
|
-
def delete(self, index: str) -> bool:
|
|
77
|
-
logger.debug(f"Deleting key from Redis database: index={index}")
|
|
78
|
-
try:
|
|
79
|
-
self.client.delete(key=index)
|
|
80
|
-
return True
|
|
81
|
-
except Exception as e:
|
|
82
|
-
logger.error(
|
|
83
|
-
f"Failed to delete key from Redis database: index={index} error={e}"
|
|
84
|
-
)
|
|
85
|
-
return False
|
|
86
|
-
|
|
87
|
-
def delete_doc(self, index: str, id: str) -> bool:
|
|
88
|
-
logger.debug(f"Deleting document from Redis database: index={index} id={id}")
|
|
89
|
-
try:
|
|
90
|
-
# For Redis, we need to handle deletion differently since RedisDatabase.delete_doc
|
|
91
|
-
# takes a key and a single id
|
|
92
|
-
result = self.client.delete_doc(key=index, id=id)
|
|
93
|
-
return result
|
|
94
|
-
except Exception as e:
|
|
95
|
-
logger.error(
|
|
96
|
-
f"Failed to delete document from Redis database: index={index} id={id} error={e}"
|
|
97
|
-
)
|
|
98
|
-
return False
|
|
99
|
-
|
|
100
|
-
def list_chunks(self, index: str, offset: int = 0, limit: int = 100) -> list[dict]:
|
|
101
|
-
logger.debug(f"Listing documents from Redis database: index={index}")
|
|
102
|
-
try:
|
|
103
|
-
# Get all documents from Redis
|
|
104
|
-
docs = self.client.list_docs(key=index)
|
|
105
|
-
|
|
106
|
-
# Apply offset and limit for pagination
|
|
107
|
-
return docs[offset : offset + limit]
|
|
108
|
-
except Exception as e:
|
|
109
|
-
logger.error(
|
|
110
|
-
f"Failed to list documents from Redis database: index={index} error={e}"
|
|
111
|
-
)
|
|
112
|
-
return []
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
class RelationalDatabaseAdapter:
|
|
116
|
-
def __init__(self, client):
|
|
117
|
-
from veadk.database.relational.mysql_database import MysqlDatabase
|
|
118
|
-
|
|
119
|
-
self.client: MysqlDatabase = client
|
|
120
|
-
|
|
121
|
-
def index_exists(self, index: str) -> bool:
|
|
122
|
-
"""
|
|
123
|
-
Check if the table (index) exists in MySQL database.
|
|
124
|
-
|
|
125
|
-
Args:
|
|
126
|
-
index: The table name to check
|
|
127
|
-
|
|
128
|
-
Returns:
|
|
129
|
-
bool: True if the table exists, False otherwise
|
|
130
|
-
"""
|
|
131
|
-
try:
|
|
132
|
-
return self.client.table_exists(index)
|
|
133
|
-
except Exception as e:
|
|
134
|
-
logger.error(
|
|
135
|
-
f"Failed to check if table exists in MySQL: index={index} error={e}"
|
|
136
|
-
)
|
|
137
|
-
return False
|
|
138
|
-
|
|
139
|
-
def create_table(self, table_name: str):
|
|
140
|
-
logger.debug(f"Creating table for SQL database: table_name={table_name}")
|
|
141
|
-
|
|
142
|
-
sql = f"""
|
|
143
|
-
CREATE TABLE `{table_name}` (
|
|
144
|
-
`id` BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
145
|
-
`data` TEXT NOT NULL,
|
|
146
|
-
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
147
|
-
) ENGINE=InnoDB DEFAULT CHARSET={self.client.config.charset};
|
|
148
|
-
"""
|
|
149
|
-
self.client.add(sql)
|
|
150
|
-
|
|
151
|
-
def add(self, data: list[str], index: str, **kwargs):
|
|
152
|
-
logger.debug(
|
|
153
|
-
f"Adding documents to SQL database: table_name={index} data_len={len(data)}"
|
|
154
|
-
)
|
|
155
|
-
|
|
156
|
-
if not self.client.table_exists(index):
|
|
157
|
-
logger.warning(f"Table {index} does not exist, creating a new table.")
|
|
158
|
-
self.create_table(index)
|
|
159
|
-
|
|
160
|
-
for _data in data:
|
|
161
|
-
sql = f"""
|
|
162
|
-
INSERT INTO `{index}` (`data`)
|
|
163
|
-
VALUES (%s);
|
|
164
|
-
"""
|
|
165
|
-
self.client.add(sql, params=(_data,))
|
|
166
|
-
logger.debug(f"Added {len(data)} texts to table {index}.")
|
|
167
|
-
|
|
168
|
-
def query(self, query: str, index: str, top_k: int) -> list[str]:
|
|
169
|
-
logger.debug(
|
|
170
|
-
f"Querying SQL database: table_name={index} query={query} top_k={top_k}"
|
|
171
|
-
)
|
|
172
|
-
|
|
173
|
-
if not self.client.table_exists(index):
|
|
174
|
-
logger.warning(
|
|
175
|
-
f"Querying SQL database, but table `{index}` does not exist, returning empty list."
|
|
176
|
-
)
|
|
177
|
-
return []
|
|
178
|
-
|
|
179
|
-
sql = f"""
|
|
180
|
-
SELECT `data` FROM `{index}` ORDER BY `created_at` DESC LIMIT {top_k};
|
|
181
|
-
"""
|
|
182
|
-
results = self.client.query(sql)
|
|
183
|
-
|
|
184
|
-
return [item["data"] for item in results]
|
|
185
|
-
|
|
186
|
-
def delete(self, index: str) -> bool:
|
|
187
|
-
logger.debug(f"Deleting table from SQL database: table_name={index}")
|
|
188
|
-
try:
|
|
189
|
-
self.client.delete(table=index)
|
|
190
|
-
return True
|
|
191
|
-
except Exception as e:
|
|
192
|
-
logger.error(
|
|
193
|
-
f"Failed to delete table from SQL database: table_name={index} error={e}"
|
|
194
|
-
)
|
|
195
|
-
return False
|
|
196
|
-
|
|
197
|
-
def delete_doc(self, index: str, id: str) -> bool:
|
|
198
|
-
logger.debug(f"Deleting document from SQL database: table_name={index} id={id}")
|
|
199
|
-
try:
|
|
200
|
-
# Convert single id to list for the client method
|
|
201
|
-
result = self.client.delete_doc(table=index, ids=[int(id)])
|
|
202
|
-
return result
|
|
203
|
-
except Exception as e:
|
|
204
|
-
logger.error(
|
|
205
|
-
f"Failed to delete document from SQL database: table_name={index} id={id} error={e}"
|
|
206
|
-
)
|
|
207
|
-
return False
|
|
208
|
-
|
|
209
|
-
def list_docs(self, index: str, offset: int = 0, limit: int = 100) -> list[dict]:
|
|
210
|
-
logger.debug(f"Listing documents from SQL database: table_name={index}")
|
|
211
|
-
try:
|
|
212
|
-
return self.client.list_docs(table=index, offset=offset, limit=limit)
|
|
213
|
-
except Exception as e:
|
|
214
|
-
logger.error(
|
|
215
|
-
f"Failed to list documents from SQL database: table_name={index} error={e}"
|
|
216
|
-
)
|
|
217
|
-
return []
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
class VectorDatabaseAdapter:
|
|
221
|
-
def __init__(self, client):
|
|
222
|
-
from veadk.database.vector.opensearch_vector_database import (
|
|
223
|
-
OpenSearchVectorDatabase,
|
|
224
|
-
)
|
|
225
|
-
|
|
226
|
-
self.client: OpenSearchVectorDatabase = client
|
|
227
|
-
|
|
228
|
-
def index_exists(self, index: str) -> bool:
|
|
229
|
-
"""
|
|
230
|
-
Check if the collection (index) exists in OpenSearch.
|
|
231
|
-
|
|
232
|
-
Args:
|
|
233
|
-
index: The collection name to check
|
|
234
|
-
|
|
235
|
-
Returns:
|
|
236
|
-
bool: True if the collection exists, False otherwise
|
|
237
|
-
"""
|
|
238
|
-
try:
|
|
239
|
-
self._validate_index(index)
|
|
240
|
-
return self.client.collection_exists(index)
|
|
241
|
-
except Exception as e:
|
|
242
|
-
logger.error(
|
|
243
|
-
f"Failed to check if collection exists in OpenSearch: index={index} error={e}"
|
|
244
|
-
)
|
|
245
|
-
return False
|
|
246
|
-
|
|
247
|
-
def _validate_index(self, index: str):
|
|
248
|
-
"""
|
|
249
|
-
Verify whether the string conforms to the naming rules of index_name in OpenSearch.
|
|
250
|
-
https://docs.opensearch.org/2.8/api-reference/index-apis/create-index/
|
|
251
|
-
"""
|
|
252
|
-
if not (
|
|
253
|
-
isinstance(index, str)
|
|
254
|
-
and not index.startswith(("_", "-"))
|
|
255
|
-
and index.islower()
|
|
256
|
-
and re.match(r"^[a-z0-9_\-.]+$", index)
|
|
257
|
-
):
|
|
258
|
-
raise ValueError(
|
|
259
|
-
"The index name does not conform to the naming rules of OpenSearch"
|
|
260
|
-
)
|
|
261
|
-
|
|
262
|
-
def add(self, data: list[str], index: str, **kwargs):
|
|
263
|
-
self._validate_index(index)
|
|
264
|
-
|
|
265
|
-
logger.debug(
|
|
266
|
-
f"Adding documents to vector database: index={index} data_len={len(data)}"
|
|
267
|
-
)
|
|
268
|
-
|
|
269
|
-
self.client.add(data, collection_name=index)
|
|
270
|
-
|
|
271
|
-
def query(self, query: str, index: str, top_k: int) -> list[str]:
|
|
272
|
-
logger.debug(
|
|
273
|
-
f"Querying vector database: collection_name={index} query={query} top_k={top_k}"
|
|
274
|
-
)
|
|
275
|
-
|
|
276
|
-
return self.client.query(
|
|
277
|
-
query=query,
|
|
278
|
-
collection_name=index,
|
|
279
|
-
top_k=top_k,
|
|
280
|
-
)
|
|
281
|
-
|
|
282
|
-
def delete(self, index: str) -> bool:
|
|
283
|
-
self._validate_index(index)
|
|
284
|
-
logger.debug(f"Deleting collection from vector database: index={index}")
|
|
285
|
-
try:
|
|
286
|
-
self.client.delete(collection_name=index)
|
|
287
|
-
return True
|
|
288
|
-
except Exception as e:
|
|
289
|
-
logger.error(
|
|
290
|
-
f"Failed to delete collection from vector database: index={index} error={e}"
|
|
291
|
-
)
|
|
292
|
-
return False
|
|
293
|
-
|
|
294
|
-
def delete_doc(self, index: str, id: str) -> bool:
|
|
295
|
-
self._validate_index(index)
|
|
296
|
-
logger.debug(f"Deleting documents from vector database: index={index} id={id}")
|
|
297
|
-
try:
|
|
298
|
-
self.client.delete_by_id(collection_name=index, id=id)
|
|
299
|
-
return True
|
|
300
|
-
except Exception as e:
|
|
301
|
-
logger.error(
|
|
302
|
-
f"Failed to delete document from vector database: index={index} id={id} error={e}"
|
|
303
|
-
)
|
|
304
|
-
return False
|
|
305
|
-
|
|
306
|
-
def list_chunks(self, index: str, offset: int = 0, limit: int = 1000) -> list[dict]:
|
|
307
|
-
self._validate_index(index)
|
|
308
|
-
logger.debug(f"Listing documents from vector database: index={index}")
|
|
309
|
-
return self.client.list_docs(collection_name=index, offset=offset, limit=limit)
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
class VikingDatabaseAdapter:
|
|
313
|
-
def __init__(self, client):
|
|
314
|
-
from veadk.database.viking.viking_database import VikingDatabase
|
|
315
|
-
|
|
316
|
-
self.client: VikingDatabase = client
|
|
317
|
-
|
|
318
|
-
def index_exists(self, index: str) -> bool:
|
|
319
|
-
"""
|
|
320
|
-
Check if the collection (index) exists in VikingDB.
|
|
321
|
-
|
|
322
|
-
Args:
|
|
323
|
-
index: The collection name to check
|
|
324
|
-
|
|
325
|
-
Returns:
|
|
326
|
-
bool: True if the collection exists, False otherwise
|
|
327
|
-
"""
|
|
328
|
-
try:
|
|
329
|
-
self._validate_index(index)
|
|
330
|
-
return self.client.collection_exists(index)
|
|
331
|
-
except Exception as e:
|
|
332
|
-
logger.error(
|
|
333
|
-
f"Failed to check if collection exists in VikingDB: index={index} error={e}"
|
|
334
|
-
)
|
|
335
|
-
return False
|
|
336
|
-
|
|
337
|
-
def _validate_index(self, index: str):
|
|
338
|
-
"""
|
|
339
|
-
Only English letters, numbers, and underscores (_) are allowed.
|
|
340
|
-
It must start with an English letter and cannot be empty. Length requirement: [1, 128].
|
|
341
|
-
For details, please see: https://www.volcengine.com/docs/84313/1254542?lang=zh
|
|
342
|
-
"""
|
|
343
|
-
if not (
|
|
344
|
-
isinstance(index, str)
|
|
345
|
-
and 0 < len(index) <= 128
|
|
346
|
-
and re.fullmatch(r"^[a-zA-Z][a-zA-Z0-9_]*$", index)
|
|
347
|
-
):
|
|
348
|
-
raise ValueError(
|
|
349
|
-
"The index name does not conform to the rules: it must start with an English letter, contain only letters, numbers, and underscores, and have a length of 1-128."
|
|
350
|
-
)
|
|
351
|
-
|
|
352
|
-
def get_or_create_collection(self, collection_name: str):
|
|
353
|
-
if not self.client.collection_exists(collection_name):
|
|
354
|
-
logger.warning(
|
|
355
|
-
f"Collection {collection_name} does not exist, creating a new collection."
|
|
356
|
-
)
|
|
357
|
-
self.client.create_collection(collection_name)
|
|
358
|
-
|
|
359
|
-
# After creation, it is necessary to wait for a while.
|
|
360
|
-
count = 0
|
|
361
|
-
while not self.client.collection_exists(collection_name):
|
|
362
|
-
print("here")
|
|
363
|
-
time.sleep(1)
|
|
364
|
-
count += 1
|
|
365
|
-
if count > 60:
|
|
366
|
-
raise TimeoutError(
|
|
367
|
-
f"Collection {collection_name} not created after 50 seconds"
|
|
368
|
-
)
|
|
369
|
-
|
|
370
|
-
def add(
|
|
371
|
-
self, data: str | list[str] | TextIO | BinaryIO | bytes, index: str, **kwargs
|
|
372
|
-
):
|
|
373
|
-
self._validate_index(index)
|
|
374
|
-
|
|
375
|
-
logger.debug(f"Adding documents to Viking database: collection_name={index}")
|
|
376
|
-
|
|
377
|
-
self.get_or_create_collection(index)
|
|
378
|
-
self.client.add(data, collection_name=index, **kwargs)
|
|
379
|
-
|
|
380
|
-
def query(self, query: str, index: str, top_k: int) -> list[str]:
|
|
381
|
-
self._validate_index(index)
|
|
382
|
-
|
|
383
|
-
logger.debug(f"Querying Viking database: collection_name={index} query={query}")
|
|
384
|
-
|
|
385
|
-
if not self.client.collection_exists(index):
|
|
386
|
-
return []
|
|
387
|
-
|
|
388
|
-
return self.client.query(query, collection_name=index, top_k=top_k)
|
|
389
|
-
|
|
390
|
-
def delete(self, index: str) -> bool:
|
|
391
|
-
self._validate_index(index)
|
|
392
|
-
logger.debug(f"Deleting collection from Viking database: index={index}")
|
|
393
|
-
return self.client.delete(name=index)
|
|
394
|
-
|
|
395
|
-
def delete_doc(self, index: str, id: str) -> bool:
|
|
396
|
-
self._validate_index(index)
|
|
397
|
-
logger.debug(f"Deleting documents from vector database: index={index} id={id}")
|
|
398
|
-
return self.client.delete_by_id(collection_name=index, id=id)
|
|
399
|
-
|
|
400
|
-
def list_chunks(self, index: str, offset: int, limit: int) -> list[dict]:
|
|
401
|
-
self._validate_index(index)
|
|
402
|
-
logger.debug(f"Listing documents from vector database: index={index}")
|
|
403
|
-
return self.client.list_chunks(
|
|
404
|
-
collection_name=index, offset=offset, limit=limit
|
|
405
|
-
)
|
|
406
|
-
|
|
407
|
-
def list_docs(self, index: str, offset: int, limit: int) -> list[dict]:
|
|
408
|
-
self._validate_index(index)
|
|
409
|
-
logger.debug(f"Listing documents from vector database: index={index}")
|
|
410
|
-
return self.client.list_docs(collection_name=index, offset=offset, limit=limit)
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
class VikingMemoryDatabaseAdapter:
|
|
414
|
-
def __init__(self, client):
|
|
415
|
-
from veadk.database.viking.viking_memory_db import VikingMemoryDatabase
|
|
416
|
-
|
|
417
|
-
self.client: VikingMemoryDatabase = client
|
|
418
|
-
|
|
419
|
-
def index_exists(self, index: str) -> bool:
|
|
420
|
-
"""
|
|
421
|
-
Check if the collection (index) exists in VikingMemoryDB.
|
|
422
|
-
|
|
423
|
-
Note:
|
|
424
|
-
VikingMemoryDatabase does not support checking if a collection exists.
|
|
425
|
-
This method always returns False.
|
|
426
|
-
|
|
427
|
-
Args:
|
|
428
|
-
index: The collection name to check
|
|
429
|
-
|
|
430
|
-
Returns:
|
|
431
|
-
bool: Always returns False as VikingMemoryDatabase does not support this functionality
|
|
432
|
-
"""
|
|
433
|
-
logger.warning(
|
|
434
|
-
"VikingMemoryDatabase does not support checking if a collection exists"
|
|
435
|
-
)
|
|
436
|
-
raise NotImplementedError("VikingMemoryDatabase does not support index_exists")
|
|
437
|
-
|
|
438
|
-
def _validate_index(self, index: str):
|
|
439
|
-
if not (
|
|
440
|
-
isinstance(index, str)
|
|
441
|
-
and 1 <= len(index) <= 128
|
|
442
|
-
and re.fullmatch(r"^[a-zA-Z][a-zA-Z0-9_]*$", index)
|
|
443
|
-
):
|
|
444
|
-
raise ValueError(
|
|
445
|
-
"The index name does not conform to the rules: it must start with an English letter, contain only letters, numbers, and underscores, and have a length of 1-128."
|
|
446
|
-
)
|
|
447
|
-
|
|
448
|
-
def add(self, data: list[str], index: str, **kwargs):
|
|
449
|
-
self._validate_index(index)
|
|
450
|
-
|
|
451
|
-
logger.debug(
|
|
452
|
-
f"Adding documents to Viking database memory: collection_name={index} data_len={len(data)}"
|
|
453
|
-
)
|
|
454
|
-
|
|
455
|
-
self.client.add(data, collection_name=index, **kwargs)
|
|
456
|
-
|
|
457
|
-
def query(self, query: str, index: str, top_k: int, **kwargs):
|
|
458
|
-
self._validate_index(index)
|
|
459
|
-
|
|
460
|
-
logger.debug(
|
|
461
|
-
f"Querying Viking database memory: collection_name={index} query={query} top_k={top_k}"
|
|
462
|
-
)
|
|
463
|
-
|
|
464
|
-
result = self.client.query(query, collection_name=index, top_k=top_k, **kwargs)
|
|
465
|
-
return result
|
|
466
|
-
|
|
467
|
-
def delete(self, index: str) -> bool:
|
|
468
|
-
self._validate_index(index)
|
|
469
|
-
logger.debug(f"Deleting collection from Viking database memory: index={index}")
|
|
470
|
-
raise NotImplementedError("VikingMemoryDatabase does not support delete")
|
|
471
|
-
|
|
472
|
-
def delete_docs(self, index: str, ids: list[int]):
|
|
473
|
-
raise NotImplementedError("VikingMemoryDatabase does not support delete_docs")
|
|
474
|
-
|
|
475
|
-
def list_chunks(self, index: str):
|
|
476
|
-
raise NotImplementedError("VikingMemoryDatabase does not support list_docs")
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
class LocalDatabaseAdapter:
|
|
480
|
-
def __init__(self, client):
|
|
481
|
-
from veadk.database.local_database import LocalDataBase
|
|
482
|
-
|
|
483
|
-
self.client: LocalDataBase = client
|
|
484
|
-
|
|
485
|
-
def index_exists(self, index: str) -> bool:
|
|
486
|
-
"""
|
|
487
|
-
Check if the index exists in LocalDataBase.
|
|
488
|
-
|
|
489
|
-
Note:
|
|
490
|
-
LocalDataBase does not support checking if an index exists.
|
|
491
|
-
This method always returns False.
|
|
492
|
-
|
|
493
|
-
Args:
|
|
494
|
-
index: The index name to check (not used in LocalDataBase)
|
|
495
|
-
|
|
496
|
-
Returns:
|
|
497
|
-
bool: Always returns False as LocalDataBase does not support this functionality
|
|
498
|
-
"""
|
|
499
|
-
logger.warning("LocalDataBase does not support checking if an index exists")
|
|
500
|
-
return True
|
|
501
|
-
|
|
502
|
-
def add(self, data: list[str], **kwargs):
|
|
503
|
-
self.client.add(data)
|
|
504
|
-
|
|
505
|
-
def query(self, query: str, **kwargs):
|
|
506
|
-
return self.client.query(query, **kwargs)
|
|
507
|
-
|
|
508
|
-
def delete(self, index: str) -> bool:
|
|
509
|
-
return self.client.delete()
|
|
510
|
-
|
|
511
|
-
def delete_doc(self, index: str, id: str) -> bool:
|
|
512
|
-
return self.client.delete_doc(id)
|
|
513
|
-
|
|
514
|
-
def list_chunks(self, index: str, offset: int = 0, limit: int = 100) -> list[dict]:
|
|
515
|
-
return self.client.list_docs(offset=offset, limit=limit)
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
MAPPING = {
|
|
519
|
-
"RedisDatabase": KVDatabaseAdapter,
|
|
520
|
-
"MysqlDatabase": RelationalDatabaseAdapter,
|
|
521
|
-
"LocalDataBase": LocalDatabaseAdapter,
|
|
522
|
-
"VikingDatabase": VikingDatabaseAdapter,
|
|
523
|
-
"OpenSearchVectorDatabase": VectorDatabaseAdapter,
|
|
524
|
-
"VikingMemoryDatabase": VikingMemoryDatabaseAdapter,
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
def get_knowledgebase_database_adapter(database_client: BaseDatabase):
|
|
529
|
-
return MAPPING[type(database_client).__name__](client=database_client)
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
def get_long_term_memory_database_adapter(database_client: BaseDatabase):
|
|
533
|
-
return MAPPING[type(database_client).__name__](client=database_client)
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
|
|
2
|
-
#
|
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
# you may not use this file except in compliance with the License.
|
|
5
|
-
# You may obtain a copy of the License at
|
|
6
|
-
#
|
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
#
|
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
# See the License for the specific language governing permissions and
|
|
13
|
-
# limitations under the License.
|
|
14
|
-
|
|
15
|
-
from veadk.utils.logger import get_logger
|
|
16
|
-
|
|
17
|
-
from .base_database import BaseDatabase
|
|
18
|
-
|
|
19
|
-
logger = get_logger(__name__)
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
class DatabaseBackend:
|
|
23
|
-
OPENSEARCH = "opensearch"
|
|
24
|
-
LOCAL = "local"
|
|
25
|
-
MYSQL = "mysql"
|
|
26
|
-
REDIS = "redis"
|
|
27
|
-
VIKING = "viking"
|
|
28
|
-
VIKING_MEM = "viking_mem"
|
|
29
|
-
|
|
30
|
-
@classmethod
|
|
31
|
-
def get_attr(cls) -> set[str]:
|
|
32
|
-
return {
|
|
33
|
-
value
|
|
34
|
-
for attr, value in cls.__dict__.items()
|
|
35
|
-
if not attr.startswith("__") and attr != "get_attr"
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
class DatabaseFactory:
|
|
40
|
-
@staticmethod
|
|
41
|
-
def create(backend: str, config=None) -> BaseDatabase:
|
|
42
|
-
if backend not in DatabaseBackend.get_attr():
|
|
43
|
-
logger.warning(f"Unknown backend: {backend}), change backend to `local`")
|
|
44
|
-
backend = "local"
|
|
45
|
-
|
|
46
|
-
if backend == DatabaseBackend.LOCAL:
|
|
47
|
-
from .local_database import LocalDataBase
|
|
48
|
-
|
|
49
|
-
return LocalDataBase()
|
|
50
|
-
if backend == DatabaseBackend.OPENSEARCH:
|
|
51
|
-
from .vector.opensearch_vector_database import OpenSearchVectorDatabase
|
|
52
|
-
|
|
53
|
-
return (
|
|
54
|
-
OpenSearchVectorDatabase()
|
|
55
|
-
if config is None
|
|
56
|
-
else OpenSearchVectorDatabase(config=config)
|
|
57
|
-
)
|
|
58
|
-
if backend == DatabaseBackend.MYSQL:
|
|
59
|
-
from .relational.mysql_database import MysqlDatabase
|
|
60
|
-
|
|
61
|
-
return MysqlDatabase() if config is None else MysqlDatabase(config=config)
|
|
62
|
-
if backend == DatabaseBackend.REDIS:
|
|
63
|
-
from .kv.redis_database import RedisDatabase
|
|
64
|
-
|
|
65
|
-
return RedisDatabase() if config is None else RedisDatabase(config=config)
|
|
66
|
-
if backend == DatabaseBackend.VIKING:
|
|
67
|
-
from .viking.viking_database import VikingDatabase
|
|
68
|
-
|
|
69
|
-
return VikingDatabase() if config is None else VikingDatabase(config=config)
|
|
70
|
-
|
|
71
|
-
if backend == DatabaseBackend.VIKING_MEM:
|
|
72
|
-
from .viking.viking_memory_db import VikingMemoryDatabase
|
|
73
|
-
|
|
74
|
-
return (
|
|
75
|
-
VikingMemoryDatabase()
|
|
76
|
-
if config is None
|
|
77
|
-
else VikingMemoryDatabase(config=config)
|
|
78
|
-
)
|
|
79
|
-
else:
|
|
80
|
-
raise ValueError(f"Unsupported database type: {backend}")
|