v0.21.1-fastapi

This commit is contained in:
2025-11-04 16:06:36 +08:00
parent 3e58c3d0e9
commit d57b5d76ae
218 changed files with 19617 additions and 72339 deletions

View File

@@ -100,12 +100,51 @@ class DataSet(Base):
res = res.json()
if res.get("code") != 0:
raise Exception(res["message"])
def _get_documents_status(self, document_ids):
import time
terminal_states = {"DONE", "FAIL", "CANCEL"}
interval_sec = 1
pending = set(document_ids)
finished = []
while pending:
for doc_id in list(pending):
def fetch_doc(doc_id: str) -> Document | None:
try:
docs = self.list_documents(id=doc_id)
return docs[0] if docs else None
except Exception:
return None
doc = fetch_doc(doc_id)
if doc is None:
continue
if isinstance(doc.run, str) and doc.run.upper() in terminal_states:
finished.append((doc_id, doc.run, doc.chunk_count, doc.token_count))
pending.discard(doc_id)
elif float(doc.progress or 0.0) >= 1.0:
finished.append((doc_id, "DONE", doc.chunk_count, doc.token_count))
pending.discard(doc_id)
if pending:
time.sleep(interval_sec)
return finished
def async_parse_documents(self, document_ids):
res = self.post(f"/datasets/{self.id}/chunks", {"document_ids": document_ids})
res = res.json()
if res.get("code") != 0:
raise Exception(res.get("message"))
def parse_documents(self, document_ids):
try:
self.async_parse_documents(document_ids)
self._get_documents_status(document_ids)
except KeyboardInterrupt:
self.async_cancel_parse_documents(document_ids)
return self._get_documents_status(document_ids)
def async_cancel_parse_documents(self, document_ids):
res = self.rm(f"/datasets/{self.id}/chunks", {"document_ids": document_ids})

View File

@@ -33,35 +33,52 @@ class Session(Base):
self.__session_type = "agent"
super().__init__(rag, res_dict)
def ask(self, question="", stream=True, **kwargs):
def ask(self, question="", stream=False, **kwargs):
"""
Ask a question to the session. If stream=True, yields Message objects as they arrive (SSE streaming).
If stream=False, returns a single Message object for the final answer.
"""
if self.__session_type == "agent":
res = self._ask_agent(question, stream)
elif self.__session_type == "chat":
res = self._ask_chat(question, stream, **kwargs)
else:
raise Exception(f"Unknown session type: {self.__session_type}")
if stream:
for line in res.iter_lines():
line = line.decode("utf-8")
if line.startswith("{"):
json_data = json.loads(line)
raise Exception(json_data["message"])
if not line.startswith("data:"):
continue
json_data = json.loads(line[5:])
if json_data["data"] is True or json_data["data"].get("running_status"):
continue
message = self._structure_answer(json_data)
yield message
for line in res.iter_lines(decode_unicode=True):
if not line:
continue # Skip empty lines
line = line.strip()
if line.startswith("data:"):
content = line[len("data:"):].strip()
if content == "[DONE]":
break # End of stream
else:
content = line
try:
json_data = json.loads(content)
except json.JSONDecodeError:
continue # Skip lines that are not valid JSON
event = json_data.get("event")
if event == "message":
yield self._structure_answer(json_data)
elif event == "message_end":
return # End of message stream
else:
try:
json_data = json.loads(res.text)
json_data = res.json()
except ValueError:
raise Exception(f"Invalid response {res}")
return self._structure_answer(json_data)
yield self._structure_answer(json_data["data"])
def _structure_answer(self, json_data):
answer = json_data["data"]["answer"]
answer = json_data["data"]["content"]
reference = json_data["data"].get("reference", {})
temp_dict = {
"content": answer,