2024-09-14 13:24:21 +08:00
|
|
|
from .base import Base
|
|
|
|
|
|
|
|
|
|
|
|
class Chunk(Base):
|
|
|
|
def __init__(self, rag, res_dict):
|
|
|
|
self.id = ""
|
2024-09-18 11:08:19 +08:00
|
|
|
self.content = ""
|
|
|
|
self.important_keywords = []
|
2024-09-14 13:24:21 +08:00
|
|
|
self.create_time = ""
|
2024-09-18 12:50:05 +08:00
|
|
|
self.create_timestamp_float = 0.0
|
2024-09-18 11:08:19 +08:00
|
|
|
self.knowledgebase_id = None
|
|
|
|
self.document_name = ""
|
|
|
|
self.document_id = ""
|
2024-09-14 13:24:21 +08:00
|
|
|
self.status = "1"
|
2024-09-18 11:08:19 +08:00
|
|
|
for k in list(res_dict.keys()):
|
|
|
|
if k not in self.__dict__:
|
|
|
|
res_dict.pop(k)
|
2024-09-14 13:24:21 +08:00
|
|
|
super().__init__(rag, res_dict)
|
2024-09-18 11:08:19 +08:00
|
|
|
|
2024-09-14 13:24:21 +08:00
|
|
|
def delete(self) -> bool:
|
|
|
|
"""
|
|
|
|
Delete the chunk in the document.
|
|
|
|
"""
|
2024-09-18 11:08:19 +08:00
|
|
|
res = self.post('/doc/chunk/rm',
|
2024-09-18 18:46:37 +08:00
|
|
|
{"document_id": self.document_id, 'chunk_ids': [self.id]})
|
2024-09-14 13:24:21 +08:00
|
|
|
res = res.json()
|
|
|
|
if res.get("retmsg") == "success":
|
|
|
|
return True
|
2024-09-18 11:08:19 +08:00
|
|
|
raise Exception(res["retmsg"])
|
|
|
|
|
|
|
|
def save(self) -> bool:
|
|
|
|
"""
|
|
|
|
Save the document details to the server.
|
|
|
|
"""
|
|
|
|
res = self.post('/doc/chunk/set',
|
|
|
|
{"chunk_id": self.id,
|
2024-09-18 18:46:37 +08:00
|
|
|
"knowledgebase_id": self.knowledgebase_id,
|
2024-09-18 11:08:19 +08:00
|
|
|
"name": self.document_name,
|
2024-09-18 18:46:37 +08:00
|
|
|
"content": self.content,
|
|
|
|
"important_keywords": self.important_keywords,
|
2024-09-18 11:08:19 +08:00
|
|
|
"create_time": self.create_time,
|
2024-09-18 12:50:05 +08:00
|
|
|
"create_timestamp_flt": self.create_timestamp_float,
|
2024-09-18 18:46:37 +08:00
|
|
|
"document_id": self.document_id,
|
2024-09-18 11:08:19 +08:00
|
|
|
"status": self.status,
|
|
|
|
})
|
|
|
|
res = res.json()
|
|
|
|
if res.get("retmsg") == "success":
|
|
|
|
return True
|
|
|
|
raise Exception(res["retmsg"])
|
|
|
|
|