File size: 1,710 Bytes
46d747a
 
 
 
 
 
 
 
 
4b81092
46d747a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a165d25
46d747a
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
from .models import Room, ChatDetails
import uuid

class DataManage:
    def __init__(self):
        pass
    def DeleteRoom(self, roomid):
        try:
            room = Room.objects.get(RoomID=roomid)
            ChatDetails.objects.filter(RoomID=room.RoomID).delete()
            room.delete()
            return True
        except Room.DoesNotExist:
            return False

    def InsertRoom(self, username,roomname = "New chat"):
        new_room = Room(UserName=username,Name=roomname)
        new_room.save()
        return str(new_room.RoomID)

    def Add_prompt_response(self, roomid, prompt, response):
        try:
            room = Room.objects.get(RoomID=roomid)
            new_chat_detail = ChatDetails(RoomID=room, UserPrompt=prompt, BotResponse=response)
            new_chat_detail.save()
            return True
        except Room.DoesNotExist:
            return False

    def LoadRoomDetail(self, roomid):
        room = Room.objects.get(RoomID=roomid)
        print(room)
        chat_details = ChatDetails.objects.filter(RoomID=room.RoomID).order_by('order')
        print(chat_details)
        mes = []
        for x in chat_details:
            mes.append({'role':"user",'content':x.UserPrompt})
            mes.append({'role':"model",'content':x.BotResponse})

        return {'id':str(room.RoomID), 'user':room.UserName,'messages':mes}
    def LoadRooms(self,username):
        #return list of {id,name}
        try:
            rooms = Room.objects.filter(UserName=username)
            room_ids = [{'id':str(room.RoomID),'name':room.Name} for room in rooms]
            room_ids.reverse()
            return room_ids
        except Room.DoesNotExist:
            return []