diff --git a/core/__pycache__/views.cpython-311.pyc b/core/__pycache__/views.cpython-311.pyc index cdff949..63db3ad 100644 Binary files a/core/__pycache__/views.cpython-311.pyc and b/core/__pycache__/views.cpython-311.pyc differ 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,