From 94ac62d5850cad87397dc5bbd0ece76620fce50c Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Wed, 19 Nov 2025 23:06:44 +0000 Subject: [PATCH] chat with history --- core/__pycache__/views.cpython-311.pyc | Bin 6300 -> 6597 bytes core/views.py | 16 ++++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/core/__pycache__/views.cpython-311.pyc b/core/__pycache__/views.cpython-311.pyc index cdff9498aee39ce18f99f09b028f6c2ca1fce338..63db3ad8895c1873b2e6980a2a357c4723428ca7 100644 GIT binary patch delta 995 zcmZ`#O-LJ25Pq9YHgRK;O|nU>t7bI`CN;qZf2h`0X-%QFiVzFh=t0=v4;sVUcqr_u z*Fq1$cc`=$qzARM2U{pu+JXlKDLrgtsUcwT(tAAAL!r8~+sgMt3{2CDeKY%r|S#V|=&wbKAgFXY#buv8y1R$Fi%qde@qf@5CObhI5 zdY!~;A<>E1jlSA=l(u__vz2*(;L<+w=O2|40X^umE!>*Y;WCCy=&!awa7&evA=7m3 z%S6uYX*XgV0hec$EH~65aS*CPeU1emEX`HW&x=V|s2CT;DKRsi%7kc^L{kw?(yu}TVoX1qv%+e++!TEMslyw5DTLX_lKI& lQeFbsfEBFA@YB>aJ15M~ByYepTJZd;ZzhaI@Euyy6KQVJi;2+1W4B17)UsKL!fy4d zc*>v%M(|=P+Cx1Ef(Sz6MMSp}Fcj;pM={5o zFO09z{#W8tk$Onu9WjP3;!k4BW&E;`@e}_LllYxKPZ`hZf#Pj9m`ab-N5wmcbg%By z1zl`Vy5C}Y{tC5*nh4+}(LQX~2-i2rTdsfVIE$-Ndcfw^Gf6O`gF1lJA{cR-*T>A` z!=%NJ(jUVU%H}lC!R9ylb-wRws0KG_4Qi+!m_5(w!EH~ICesn^1A!?FVgQ}B*a|l| zZ@ZclsU_M+3F^Tx^5fq~ih(YT_P5=nBTL>cgg^VIXUvHK3%~+hw%itLCWkpQJ&Y?s zEYfL3Y0#syl^_$s--DajmaBn1J}uATd-9wTCl^L< zF{AfP#yBw<5B?%Y(PexjPcgl3$FoYzvqFMA(JGJ2%7)7klF$7P{-|WxE(C-P{70Eg zG2XHHgG8pCINOONI_a!)ZrO=zL^|oIH;faXCxRb^9xNS7F(*Fb6fU(3%Z{Y&Nm^Ud z9G`Y5t8Mw@H#zrJ&h2FPx!2S0^Nw2DQ%h~Ngl~ll y;a%>`;sWe0c#0{y#RrQCx|LvyDeQ#bq7{5U5=B?>R^({n1me2zA7f0C{r&=XKcvS1 diff --git a/core/views.py b/core/views.py index 8112439..7567fc5 100644 --- a/core/views.py +++ b/core/views.py @@ -73,11 +73,17 @@ def chat_view(request, conversation_id=None): if text: Message.objects.create(conversation=selected_conversation, content=text, is_from_user=True) + # Construct the conversation history for the AI + history = [] + for msg in selected_conversation.messages.order_by('created_at'): + role = "user" if msg.is_from_user else "assistant" + history.append({"role": role, "content": msg.content}) + try: response = LocalAIApi.create_response({ "input": [ {"role": "system", "content": "You are a helpful assistant."}, - {"role": "user", "content": text} + *history ] }) ai_text = LocalAIApi.extract_text(response) @@ -87,13 +93,15 @@ def chat_view(request, conversation_id=None): ai_text = f"An error occurred: {str(e)}" Message.objects.create(conversation=selected_conversation, content=ai_text, is_from_user=False) - - return redirect('core:chat_detail', conversation_id=selected_conversation.id) + + # Re-fetch the conversation to include the new messages for rendering + selected_conversation = get_object_or_404(Conversation, id=conversation_id) conversations = Conversation.objects.order_by('-created_at') - selected_conversation = None if conversation_id: selected_conversation = get_object_or_404(Conversation, id=conversation_id) + else: + selected_conversation = None return render(request, 'core/chat.html', { 'conversation_list': conversations,