Artteiv commited on
Commit
92cd7bb
1 Parent(s): 4ecad52

the last samurai

Browse files
chat/database_manage.py CHANGED
@@ -36,8 +36,19 @@ class DataManage:
36
  for x in chat_details:
37
  mes.append({'role':"user",'content':x.UserPrompt})
38
  mes.append({'role':"model",'content':x.BotResponse})
39
-
40
  return {'id':str(room.RoomID), 'user':room.UserName,'messages':mes}
 
 
 
 
 
 
 
 
 
 
 
 
41
  def LoadRooms(self,username):
42
  #return list of {id,name}
43
  try:
 
36
  for x in chat_details:
37
  mes.append({'role':"user",'content':x.UserPrompt})
38
  mes.append({'role':"model",'content':x.BotResponse})
 
39
  return {'id':str(room.RoomID), 'user':room.UserName,'messages':mes}
40
+
41
+ def LoadHistoryRoomForModel(self, roomid):
42
+ room = Room.objects.get(RoomID=roomid)
43
+ print(f"{room.RoomID} đã gọi chi tiết đoạn chat theo pattern context []")
44
+ chat_details = ChatDetails.objects.filter(RoomID=room.RoomID).order_by('order')
45
+ mes = []
46
+ for x in chat_details:
47
+ text = [{"text":str(x.UserPrompt)}]
48
+ mes.append({'role':"user",'parts':text})
49
+ text = [{"text":str(x.BotResponse)}]
50
+ mes.append({'role':"model",'parts':text})
51
+ return mes
52
  def LoadRooms(self,username):
53
  #return list of {id,name}
54
  try:
chat/templates/index.html CHANGED
@@ -78,7 +78,7 @@
78
  <!-- head -->
79
  <div class="flex flex-row justify-between items-center p-4 select-none">
80
  <h1 class="text-2xl font-medium text-center">
81
- TinyChat
82
  </h1>
83
  <div>
84
  <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"
@@ -217,6 +217,7 @@
217
  },
218
  ],
219
  isLoading: false,
 
220
  message: '',
221
  messages: roomdata.messages,
222
  // cs:roomdata,
@@ -244,8 +245,9 @@
244
  }
245
  // this.connect()
246
  this.ques = params.get('ques');
247
- if (this.ques != null) {
248
  this.message = this.ques;
 
249
  this.sendMessageAPI();
250
  }
251
  },
 
78
  <!-- head -->
79
  <div class="flex flex-row justify-between items-center p-4 select-none">
80
  <h1 class="text-2xl font-medium text-center">
81
+ <a :href="`/chat`">Arxiv Chatbot</a>
82
  </h1>
83
  <div>
84
  <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"
 
217
  },
218
  ],
219
  isLoading: false,
220
+ isFirst:true,
221
  message: '',
222
  messages: roomdata.messages,
223
  // cs:roomdata,
 
245
  }
246
  // this.connect()
247
  this.ques = params.get('ques');
248
+ if (this.ques != null && this.isFirst==true) {
249
  this.message = this.ques;
250
+ this.isFirst = false;
251
  this.sendMessageAPI();
252
  }
253
  },
chat/views.py CHANGED
@@ -51,23 +51,25 @@ def deletechat(request):
51
 
52
  @method_decorator(csrf_exempt, name='dispatch')
53
  class MessageView(View):
54
- def __init__(self) -> None:
55
- super().__init__()
56
- self.model = None
57
- self.session = None
58
- self.database = None
59
-
60
  def post(self, request):
61
- if not self.model:
62
- self.model, self.session = md.init_model("auto")
63
  text_data_json = json.loads(request.body)
64
- self.database = DataManage()
65
- print(text_data_json)
66
  roomid = text_data_json["roomid"]
67
  message = text_data_json["messages"]
 
 
 
 
 
 
 
 
 
 
 
 
68
  print(message)
69
  question = message[-1]['content']
70
- response, history_state = md.full_chain_history_question(question, self.session, mode="auto")
71
  # print("First answer: ",response)
72
  print("Session history:")
73
  md.print_history(history_state)
 
51
 
52
  @method_decorator(csrf_exempt, name='dispatch')
53
  class MessageView(View):
 
 
 
 
 
 
54
  def post(self, request):
 
 
55
  text_data_json = json.loads(request.body)
 
 
56
  roomid = text_data_json["roomid"]
57
  message = text_data_json["messages"]
58
+ self.database = DataManage()
59
+ history = self.database.LoadHistoryRoomForModel(roomid)
60
+ model, session = md.init_model("auto",history=history)
61
+ # if 'session' not in request.session:
62
+ # history = self.database.LoadHistoryRoomForModel(roomid)
63
+ # model, session = md.init_model("auto",history=history)
64
+ # # request.session['model'] = model
65
+ # request.session['session'] = session
66
+ # else:
67
+ # # model = request.session['model']
68
+ # session = request.session['session']
69
+ print(text_data_json)
70
  print(message)
71
  question = message[-1]['content']
72
+ response, history_state = md.full_chain_history_question(question, session, mode="auto")
73
  # print("First answer: ",response)
74
  print("Session history:")
75
  md.print_history(history_state)
chatbot_django/settings.py CHANGED
@@ -15,6 +15,10 @@ from pathlib import Path
15
  # Build paths inside the project like this: BASE_DIR / 'subdir'.
16
  BASE_DIR = Path(__file__).resolve().parent.parent
17
 
 
 
 
 
18
 
19
  # Quick-start development settings - unsuitable for production
20
  # See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
@@ -26,7 +30,7 @@ LOGIN_URL = '/users/login/'
26
 
27
 
28
  # SECURITY WARNING: don't run with debug turned on in production!
29
- DEBUG = False
30
 
31
  ALLOWED_HOSTS = ['artteiv-arxiv-chatbot.hf.space','arxivbot-arxiv-chatbot.hf.space','localhost','127.0.0.1']
32
 
 
15
  # Build paths inside the project like this: BASE_DIR / 'subdir'.
16
  BASE_DIR = Path(__file__).resolve().parent.parent
17
 
18
+ SESSION_ENGINE = 'django.contrib.sessions.backends.db'
19
+
20
+ # settings.py
21
+ SESSION_EXPIRE_AT_BROWSER_CLOSE = True # Cấu hình session hết hạn khi trình duyệt đóng
22
 
23
  # Quick-start development settings - unsuitable for production
24
  # See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/
 
30
 
31
 
32
  # SECURITY WARNING: don't run with debug turned on in production!
33
+ DEBUG = True
34
 
35
  ALLOWED_HOSTS = ['artteiv-arxiv-chatbot.hf.space','arxivbot-arxiv-chatbot.hf.space','localhost','127.0.0.1']
36
 
db.sqlite3 CHANGED
Binary files a/db.sqlite3 and b/db.sqlite3 differ