-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathknowledgebase.py
More file actions
308 lines (238 loc) · 11.6 KB
/
knowledgebase.py
File metadata and controls
308 lines (238 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from typing import Any, Callable, Literal, Union
from pydantic import BaseModel, Field
from veadk.knowledgebase.backends.base_backend import BaseKnowledgebaseBackend
from veadk.knowledgebase.entry import KnowledgebaseEntry
from veadk.knowledgebase.types import KnowledgebaseProfile
from veadk.utils.logger import get_logger
logger = get_logger(__name__)
def _get_backend_cls(backend: str) -> type[BaseKnowledgebaseBackend]:
match backend:
case "local":
from veadk.knowledgebase.backends.in_memory_backend import (
InMemoryKnowledgeBackend,
)
return InMemoryKnowledgeBackend
case "opensearch":
from veadk.knowledgebase.backends.opensearch_backend import (
OpensearchKnowledgeBackend,
)
return OpensearchKnowledgeBackend
case "viking":
from veadk.knowledgebase.backends.vikingdb_knowledge_backend import (
VikingDBKnowledgeBackend,
)
return VikingDBKnowledgeBackend
case "redis":
from veadk.knowledgebase.backends.redis_backend import (
RedisKnowledgeBackend,
)
return RedisKnowledgeBackend
case "tos_vector":
from veadk.knowledgebase.backends.tos_vector_backend import (
TosVectorKnowledgeBackend,
)
return TosVectorKnowledgeBackend
raise ValueError(f"Unsupported knowledgebase backend: {backend}")
class KnowledgeBase(BaseModel):
"""A knowledge base for storing user-related information.
This class represents a knowledge base used to store and retrieve user-specific data.
It supports multiple backend options, including in-memory, OpenSearch, Redis, and Volcengine's
VikingDB. The knowledge base allows for efficient document retrieval based on similarity,
with the ability to configure backend-specific settings.
Attributes:
name (str): The name of the knowledge base. Default is "user_knowledgebase".
description (str): A description of the knowledge base. Default is "This knowledgebase stores some user-related information."
backend (Union[Literal["local", "opensearch", "viking", "redis"], BaseKnowledgebaseBackend]):
The type of backend to use for storing and querying the knowledge base. Supported options include:
- 'local' for in-memory storage (data is lost when the program exits).
- 'opensearch' for OpenSearch (requires OpenSearch cluster).
- 'viking' for Volcengine VikingDB (requires VikingDB service).
- 'redis' for Redis with vector search capability (requires Redis).
Default is 'local'.
backend_config (dict): Configuration dictionary for the selected backend.
top_k (int): The number of top similar documents to retrieve during a search. Default is 10.
app_name (str): The name of the application associated with the knowledge base. If index is not provided, this value will be set to `index`.
index (str): The name of the knowledge base index.
Notes:
Please ensure that you have set the embedding-related configurations in environment variables.
"""
name: str = "user_knowledgebase"
description: str = "This knowledgebase stores some user-related information."
backend: Union[
Literal["local", "opensearch", "viking", "redis", "tos_vector"],
BaseKnowledgebaseBackend,
] = "local"
backend_config: dict = Field(default_factory=dict)
top_k: int = 10
app_name: str = ""
index: str = ""
enable_profile: bool = False
query_with_user_profile: bool = False
def model_post_init(self, __context: Any) -> None:
if isinstance(self.backend, BaseKnowledgebaseBackend):
self._backend = self.backend
self.index = self._backend.index
logger.info(
f"Initialized knowledgebase with provided backend instance {self._backend.__class__.__name__}"
)
return
# Once user define backend config, use it directly
if self.backend_config:
self._backend = _get_backend_cls(self.backend)(**self.backend_config)
return
self.index = self.index or self.app_name
if not self.index:
raise ValueError("Either `index` or `app_name` must be provided.")
logger.info(
f"Initializing knowledgebase: backend={self.backend} index={self.index} top_k={self.top_k}"
)
self._backend = _get_backend_cls(self.backend)(index=self.index)
logger.info(
f"Initialized knowledgebase with backend {self._backend.__class__.__name__}"
)
if self.query_with_user_profile:
logger.info(
"Enable user profile querying for knowledgebase. You *must* use Viking Memory backend to enjoy this feature."
)
def add_from_directory(self, directory: str, **kwargs) -> bool:
"""Add knowledge from file path to knowledgebase.
Add the files in the directory to knowledgebase backend.
Args:
directory (str): The directory path that needs to store.
Returns:
bool: True if successfully store the knowledgebase, False otherwise.
Examples:
Store a directory to knowledgebase:
```python
knowledgebase = Knowledgebase(backend="local")
if knowledgebase.add_from_directory("./knowledgebase"):
# add successfully
...
else:
raise RuntimeError("Uploaded directory failed.")
```
"""
return self._backend.add_from_directory(directory=directory, **kwargs)
def add_from_files(self, files: list[str], **kwargs) -> bool:
"""Add knowledge files to knowledgebase.
Add a list of files to knowledgebase backend.
Args:
files (str): The list of files.
Returns:
bool: True if successfully store the knowledgebase, False otherwise.
Examples:
Store files to knowledgebase:
```python
knowledgebase = Knowledgebase(backend="local")
if knowledgebase.add_from_files("./knowledgebase"):
# add successfully
...
else:
raise RuntimeError("Uploaded files failed.")
```
"""
return self._backend.add_from_files(files=files, **kwargs)
def add_from_text(self, text: str | list[str], **kwargs) -> bool:
"""Add a piece of text or a list of text to knowledgebase.
The `text` can be a string or a list of string. The text will be embedded and stored by the corresponding backend.
Args:
text (str | list[str]): The text string or a list of text strings.
Returns:
bool: True if successfully store the knowledgebase, False otherwise.
Examples:
Store a string or a list of string to knowledgebase:
```python
knowledgebase = Knowledgebase(backend="local")
if knowledgebase.add_from_text("./knowledgebase"):
# add successfully
...
else:
raise RuntimeError("Uploaded text failed.")
```
"""
return self._backend.add_from_text(text=text, **kwargs)
def search(self, query: str, top_k: int = 0, **kwargs) -> list[KnowledgebaseEntry]:
"""Search knowledge from knowledgebase"""
top_k = top_k if top_k != 0 else self.top_k
_entries = self._backend.search(query=query, top_k=top_k, **kwargs)
entries = []
for entry in _entries:
if isinstance(entry, KnowledgebaseEntry):
entries.append(entry)
elif isinstance(entry, str):
entries.append(KnowledgebaseEntry(content=entry))
else:
logger.error(
f"Unsupported entry type from backend search method: {type(entry)} with {entry}. Expected `KnowledgebaseEntry` or `str`. Skip for this entry."
)
return entries
def __getattr__(self, name) -> Callable:
"""In case of knowledgebase have no backends' methods (`delete`, `list_chunks`, etc)
For example, knowledgebase.delete(...) -> self._backend.delete(...)
"""
return getattr(self._backend, name)
async def generate_profiles(self, files: list[str], profile_path: str = ""):
"""Generate knowledgebase profiles.
Args:
files (list[str]): The list of files.
name (str): The name of the knowledgebase.
profile_path (str, optional): The path to store the generated profiles. If empty, the profiles will be stored in a default path.
Returns:
list[KnowledgebaseProfile]: A list of knowledgebase profiles.
"""
import json
from pathlib import Path
from veadk import Agent, Runner
from veadk.utils.misc import write_string_to_file
file_contents = [Path(file).read_text() for file in files]
agent = Agent(
name="profile_generator",
model_name="deepseek-v3-2-251201",
# model_extra_config={
# "extra_body": {"thinking": {"type": "disabled"}},
# },
description="A generator for generating knowledgebase profiles for the given files.",
instruction='Generate JSON-formatted profile for the given file content. The corresponding language should be consistent with the file content. Respond ONLY with a JSON object containing the capitalized fields. Format: {"name": "", "description": "", "tags": [], "keywords": []} (3-5 tags, 3-5 keywords)',
output_schema=KnowledgebaseProfile,
)
runner = Runner(agent=agent)
profiles = []
for idx, file_content in enumerate(file_contents):
response = await runner.run(
messages="file content: " + file_content,
session_id=f"profile_{idx}",
)
try:
profiles.append(KnowledgebaseProfile(**json.loads(response)))
except json.JSONDecodeError:
logger.error(
f"Failed to parse JSON response for file {files[idx]}: {response}. Skip for this file."
)
continue
logger.debug(f"Generated {len(profiles)} profiles: {profiles}.")
for idx, profile in enumerate(profiles):
if not profile_path:
profile_path = f"./profiles/knowledgebase/profiles_{self.index}"
write_string_to_file(
profile_path + f"/profile_{profile.name}.json",
json.dumps(profile.model_dump(), indent=4, ensure_ascii=False),
)
profile_names = [profile.name for profile in profiles]
write_string_to_file(
profile_path + "/profile_list.json",
json.dumps(profile_names, indent=4, ensure_ascii=False),
)