Skip to content

Commit e312b54

Browse files
committed
feat: knowledge exists and list_docs
1 parent 76e99ce commit e312b54

File tree

3 files changed

+162
-3
lines changed

3 files changed

+162
-3
lines changed

veadk/database/database_adapter.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ def __init__(self, client):
2828

2929
self.client: RedisDatabase = client
3030

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+
3150
def add(self, data: list[str], index: str, **kwargs):
3251
logger.debug(f"Adding documents to Redis database: index={index}")
3352

@@ -99,6 +118,24 @@ def __init__(self, client):
99118

100119
self.client: MysqlDatabase = client
101120

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+
102139
def create_table(self, table_name: str):
103140
logger.debug(f"Creating table for SQL database: table_name={table_name}")
104141

@@ -188,6 +225,25 @@ def __init__(self, client):
188225

189226
self.client: OpenSearchVectorDatabase = client
190227

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+
191247
def _validate_index(self, index: str):
192248
"""
193249
Verify whether the string conforms to the naming rules of index_name in OpenSearch.
@@ -259,6 +315,25 @@ def __init__(self, client):
259315

260316
self.client: VikingDatabase = client
261317

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+
262337
def _validate_index(self, index: str):
263338
"""
264339
Only English letters, numbers, and underscores (_) are allowed.
@@ -341,6 +416,25 @@ def __init__(self, client):
341416

342417
self.client: VikingMemoryDatabase = client
343418

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+
344438
def _validate_index(self, index: str):
345439
if not (
346440
isinstance(index, str)
@@ -388,6 +482,23 @@ def __init__(self, client):
388482

389483
self.client: LocalDataBase = client
390484

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+
391502
def add(self, data: list[str], **kwargs):
392503
self.client.add(data)
393504

veadk/database/viking/viking_database.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
doc_del_path = "/api/knowledge/collection/delete"
4242
doc_add_path = "/api/knowledge/doc/add"
4343
doc_info_path = "/api/knowledge/doc/info"
44-
list_docs_path = "/api/knowledge/point/list"
44+
list_point_path = "/api/knowledge/point/list"
45+
list_docs_path = "/api/knowledge/doc/list"
4546
delete_docs_path = "/api/knowledge/point/delete"
4647

4748

@@ -550,7 +551,7 @@ def list_chunks(
550551

551552
list_doc_req = prepare_request(
552553
method="POST",
553-
path=list_docs_path,
554+
path=list_point_path,
554555
config=self.config,
555556
data=request_params,
556557
)
@@ -579,6 +580,38 @@ def list_chunks(
579580
]
580581
return data
581582

583+
def list_docs(
584+
self, collection_name: str, offset: int = 0, limit: int = -1
585+
) -> list[dict]:
586+
request_params = {
587+
"collection_name": collection_name,
588+
"project": self.config.project,
589+
"offset": offset,
590+
"limit": limit,
591+
}
592+
593+
list_doc_req = prepare_request(
594+
method="POST",
595+
path=list_docs_path,
596+
config=self.config,
597+
data=request_params,
598+
)
599+
resp = requests.request(
600+
method=list_doc_req.method,
601+
url="https://{}{}".format(g_knowledge_base_domain, list_doc_req.path),
602+
headers=list_doc_req.headers,
603+
data=list_doc_req.body,
604+
)
605+
606+
result = resp.json()
607+
if result["code"] != 0:
608+
logger.error(f"Error in list_docs: {result['message']}")
609+
raise ValueError(f"Error in list_docs: {result['message']}")
610+
611+
if not result["data"]["doc_list"]:
612+
return []
613+
return result["data"]["doc_list"]
614+
582615
def delete_by_id(self, collection_name: str, id: str) -> bool:
583616
request_params = {
584617
"collection_name": collection_name,

veadk/knowledgebase/knowledgebase.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,21 @@ def delete_doc(self, app_name: str, id: str) -> bool:
158158
index = build_knowledgebase_index(app_name)
159159
return self._adapter.delete_doc(index=index, id=id)
160160

161-
def list_docs(self, app_name: str, offset: int = 0, limit: int = 100) -> list[dict]:
161+
def list_chunks(
162+
self, app_name: str, offset: int = 0, limit: int = 100
163+
) -> list[dict]:
162164
index = build_knowledgebase_index(app_name)
163165
return self._adapter.list_chunks(index=index, offset=offset, limit=limit)
166+
167+
def list_docs(self, app_name: str, offset: int = 0, limit: int = 100) -> list[dict]:
168+
if self.backend == "viking":
169+
index = build_knowledgebase_index(app_name)
170+
return self._adapter.list_docs(index=index, offset=offset, limit=limit)
171+
else:
172+
raise NotImplementedError(
173+
f"list_docs not supported for {self.backend}, only viking support list_docs"
174+
)
175+
176+
def exists(self, app_name: str) -> bool:
177+
index = build_knowledgebase_index(app_name)
178+
return self._adapter.index_exists(index=index)

0 commit comments

Comments
 (0)