chat with history
This commit is contained in:
parent
e4f2931b77
commit
94ac62d585
Binary file not shown.
@ -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,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user