File size: 1,290 Bytes
75df934
3826b3b
75df934
46d747a
75df934
 
 
46d747a
 
 
 
 
 
9235b6d
75df934
3826b3b
75df934
3826b3b
75df934
46d747a
 
 
 
 
75df934
46d747a
 
f7e0e10
75df934
3826b3b
 
 
 
 
 
46d747a
75df934
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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)