# chat/views.py from django.shortcuts import render,redirect from django.http import HttpResponse from django.contrib.auth.decorators import login_required from .database_manage import DataManage from django.views.decorators.csrf import csrf_exempt from django.utils.decorators import method_decorator from django.views import View from django.http import JsonResponse from . import model_manage as md import json @login_required def index(request): roomid = request.GET.get('id') data = DataManage() rooms = data.LoadRooms(username=request.user.username) if roomid: # try: roomdata = data.LoadRoomDetail(roomid=roomid) if roomdata==None: raise("Room not found") context = { 'rooms': rooms, 'roomdata': json.dumps(roomdata), } return render(request, 'index.html',context=context) # except Exception as e: # print(e) # return HttpResponse("Room not found", status=404) else: roomdata = {'messages':[]} return render(request, "index.html",{"rooms":rooms,'roomdata':roomdata}) @login_required def newchat(request): data = DataManage() user = request.user.username ques = request.GET.get('ques') room = data.InsertRoom(user,ques[:23]) return redirect(f'/chat?id={room}&ques={ques}') def room(request, room_name): return render(request, "chat/room.html", {"room_name": room_name}) @login_required def deletechat(request): data = DataManage() roomid = request.GET.get('id') data.DeleteRoom(roomid) return redirect('/chat') @method_decorator(csrf_exempt, name='dispatch') class MessageView(View): def post(self, request): text_data_json = json.loads(request.body) roomid = text_data_json["roomid"] message = text_data_json["messages"] self.database = DataManage() history = self.database.LoadHistoryRoomForModel(roomid) model, session = md.init_model("auto",history=history) # if 'session' not in request.session: # history = self.database.LoadHistoryRoomForModel(roomid) # model, session = md.init_model("auto",history=history) # # request.session['model'] = model # request.session['session'] = session # else: # # model = request.session['model'] # session = request.session['session'] print(text_data_json) print(message) question = message[-1]['content'] response, history_state = md.full_chain_history_question(question, session, mode="auto") # print("First answer: ",response) print("Session history:") md.print_history(history_state) self.database.Add_prompt_response(roomid,question,response) return JsonResponse({"message": response}, status=201)