2024-09-09 17:18:08 +08:00
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
from .base import Base
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Session(Base):
|
|
|
|
|
def __init__(self, rag, res_dict):
|
|
|
|
|
self.id = None
|
|
|
|
|
self.name = "New session"
|
|
|
|
|
self.messages = [{"role": "assistant", "content": "Hi! I am your assistant,can I help you?"}]
|
2024-11-06 18:03:45 +08:00
|
|
|
|
for key,value in res_dict.items():
|
|
|
|
|
if key =="chat_id" and value is not None:
|
2024-12-04 16:23:22 +08:00
|
|
|
|
self.chat_id = None
|
2024-11-06 18:03:45 +08:00
|
|
|
|
self.__session_type = "chat"
|
|
|
|
|
if key == "agent_id" and value is not None:
|
2024-12-04 16:23:22 +08:00
|
|
|
|
self.agent_id = None
|
2024-11-06 18:03:45 +08:00
|
|
|
|
self.__session_type = "agent"
|
2024-09-09 17:18:08 +08:00
|
|
|
|
super().__init__(rag, res_dict)
|
|
|
|
|
|
2024-12-20 17:34:16 +08:00
|
|
|
|
def ask(self, question="",stream=True,**kwargs):
|
2024-11-06 18:03:45 +08:00
|
|
|
|
if self.__session_type == "agent":
|
2024-11-25 15:25:48 +08:00
|
|
|
|
res=self._ask_agent(question,stream)
|
2024-11-06 18:03:45 +08:00
|
|
|
|
elif self.__session_type == "chat":
|
2024-12-19 17:24:26 +08:00
|
|
|
|
res=self._ask_chat(question,stream,**kwargs)
|
2024-09-11 12:03:55 +08:00
|
|
|
|
for line in res.iter_lines():
|
|
|
|
|
line = line.decode("utf-8")
|
2024-09-26 16:05:25 +08:00
|
|
|
|
if line.startswith("{"):
|
|
|
|
|
json_data = json.loads(line)
|
2024-10-12 19:35:19 +08:00
|
|
|
|
raise Exception(json_data["message"])
|
2024-12-20 17:34:16 +08:00
|
|
|
|
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
|
|
|
|
|
answer = json_data["data"]["answer"]
|
|
|
|
|
reference = json_data["data"].get("reference", {})
|
|
|
|
|
temp_dict = {
|
|
|
|
|
"content": answer,
|
|
|
|
|
"role": "assistant"
|
|
|
|
|
}
|
|
|
|
|
if reference and "chunks" in reference:
|
|
|
|
|
chunks = reference["chunks"]
|
|
|
|
|
temp_dict["reference"] = chunks
|
|
|
|
|
message = Message(self.rag, temp_dict)
|
|
|
|
|
yield message
|
2024-11-06 18:03:45 +08:00
|
|
|
|
|
2024-12-19 17:24:26 +08:00
|
|
|
|
def _ask_chat(self, question: str, stream: bool,**kwargs):
|
|
|
|
|
json_data={"question": question, "stream": True,"session_id":self.id}
|
|
|
|
|
json_data.update(kwargs)
|
2024-11-06 18:03:45 +08:00
|
|
|
|
res = self.post(f"/chats/{self.chat_id}/completions",
|
2024-12-19 17:24:26 +08:00
|
|
|
|
json_data, stream=stream)
|
2024-11-06 18:03:45 +08:00
|
|
|
|
return res
|
2024-12-20 17:34:16 +08:00
|
|
|
|
|
2024-11-25 15:25:48 +08:00
|
|
|
|
def _ask_agent(self,question:str,stream:bool):
|
2024-11-06 18:03:45 +08:00
|
|
|
|
res = self.post(f"/agents/{self.agent_id}/completions",
|
|
|
|
|
{"question": question, "stream": True,"session_id":self.id}, stream=stream)
|
|
|
|
|
return res
|
|
|
|
|
|
2024-10-12 19:35:19 +08:00
|
|
|
|
def update(self,update_message):
|
2024-10-28 15:06:18 +08:00
|
|
|
|
res = self.put(f"/chats/{self.chat_id}/sessions/{self.id}",
|
2024-10-12 19:35:19 +08:00
|
|
|
|
update_message)
|
2024-09-11 12:03:55 +08:00
|
|
|
|
res = res.json()
|
2024-10-12 19:35:19 +08:00
|
|
|
|
if res.get("code") != 0:
|
|
|
|
|
raise Exception(res.get("message"))
|
2024-09-11 12:03:55 +08:00
|
|
|
|
|
2024-09-09 17:18:08 +08:00
|
|
|
|
class Message(Base):
|
|
|
|
|
def __init__(self, rag, res_dict):
|
2024-09-11 12:03:55 +08:00
|
|
|
|
self.content = "Hi! I am your assistant,can I help you?"
|
|
|
|
|
self.reference = None
|
2024-09-09 17:18:08 +08:00
|
|
|
|
self.role = "assistant"
|
2024-09-11 12:03:55 +08:00
|
|
|
|
self.prompt = None
|
2024-09-26 16:05:25 +08:00
|
|
|
|
self.id = None
|
2024-12-20 17:34:16 +08:00
|
|
|
|
super().__init__(rag, res_dict)
|