arxiv_chatbot / chat /database_manage.py
Artteiv's picture
change message protocol
204dd4d
raw
history blame
1.71 kB
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 []