arxiv_chatbot / chat /consumers.py
Artteiv's picture
ok
46d747a
raw
history blame
1.29 kB
import json
from . import model_manage2 as md
from channels.generic.websocket import WebsocketConsumer
from .database_manage import DataManage
class ChatConsumer(WebsocketConsumer):
def connect(self):
self.user = self.scope["user"]
if self.user.is_authenticated:
self.accept()
self.model, self.session = md.init_model("auto")
else:
self.close()
def disconnect(self, close_code):
del self.model, self.session
pass
def receive(self, text_data):
"""
After socket create with:
username: self.user
roomid: self.roomid
"""
text_data_json = json.loads(text_data)
self.database = DataManage()
roomid = text_data_json["roomid"]
message = text_data_json["messages"]
print(message)
question = message[-1]['content']
response, history_state = md.full_chain_history_question(question, self.session, mode="auto")
# print("First answer: ",response)
print("Session history:")
md.print_history(history_state)
self.send(text_data=json.dumps({"message": response}))
self.database.Add_prompt_response(roomid,question,response)