function chat
Flask route handler that processes chat requests with RAG (Retrieval-Augmented Generation) capabilities, managing conversation sessions, chat history, and document-based question answering.
/tf/active/vicechatdev/docchat/blueprint.py
104 - 182
complex
Purpose
This endpoint serves as the main chat interface for a document-based conversational AI system. It handles user questions, maintains conversation context through sessions, integrates with a RAG engine for document retrieval, supports multiple chat modes (basic, advanced), optional web search, reranking, and memory features. It persists chat history to disk and returns AI-generated responses with source citations.
Source Code
def chat():
"""Handle chat requests"""
try:
data = request.json
question = data.get('question', '').strip()
mode = data.get('mode', 'basic')
model = data.get('model', config.DEFAULT_MODEL)
enable_reranking = data.get('enable_reranking', True)
enable_memory = data.get('enable_memory', True)
enable_web_search = data.get('enable_web_search', False)
num_results = data.get('num_results', 5)
output_language = data.get('output_language', 'en')
custom_instructions = data.get('custom_instructions', '')
# Get session ID
if 'chat_session_id' not in session:
session['chat_session_id'] = str(uuid_module.uuid4())
chat_sessions[session['chat_session_id']] = {
'messages': [],
'created_at': datetime.now(),
'updated_at': datetime.now(),
'user': get_current_username()
}
session_id = session['chat_session_id']
if not question:
return jsonify({'error': 'Question is required'}), 400
# Get chat history if memory is enabled
chat_history = []
if enable_memory and session_id in chat_sessions:
for msg in chat_sessions[session_id]['messages'][-10:]:
chat_history.append({
'role': 'user',
'content': msg['question']
})
chat_history.append({
'role': 'assistant',
'content': msg['answer']
})
# Initialize RAG engine
rag = DocChatRAG(
llm_model=model,
enable_reranking=enable_reranking,
output_language=output_language
)
# Process query based on mode
result = rag.query(
question=question,
mode=mode,
chat_history=chat_history if enable_memory else None,
num_results=num_results,
enable_web_search=enable_web_search,
custom_instructions=custom_instructions
)
# Store in session
message_data = {
'question': question,
'answer': result['answer'],
'sources': result.get('sources', []),
'mode': mode,
'model': model,
'timestamp': datetime.now().isoformat()
}
if session_id in chat_sessions:
chat_sessions[session_id]['messages'].append(message_data)
chat_sessions[session_id]['updated_at'] = datetime.now()
save_session_to_disk(session_id)
return jsonify(result)
except Exception as e:
logger.error(f"Error in chat: {e}", exc_info=True)
return jsonify({'error': str(e)}), 500
Return Value
Returns a JSON response with status code. On success (200): returns the result dictionary from RAG engine containing 'answer' (string), 'sources' (list of document references), and potentially other metadata. On error (400): returns {'error': 'Question is required'} if question is missing. On error (500): returns {'error': <error_message>} for any exception during processing.
Dependencies
flaskflask-loginuuidwerkzeugpathlibloggingdatetimejsonosthreading
Required Imports
from flask import Blueprint, render_template, request, jsonify, session, redirect, url_for
from flask_login import login_required, current_user
import os
import json
import logging
from datetime import datetime
from pathlib import Path
import uuid as uuid_module
from werkzeug.utils import secure_filename
from config import config
from rag_engine import DocChatRAG
from document_indexer import DocumentIndexer
import threading
Usage Example
# Client-side example using requests library
import requests
# Assuming Flask app is running on localhost:5000 and user is authenticated
url = 'http://localhost:5000/api/chat'
headers = {'Content-Type': 'application/json'}
payload = {
'question': 'What are the main findings in the research paper?',
'mode': 'basic',
'model': 'gpt-4',
'enable_reranking': True,
'enable_memory': True,
'enable_web_search': False,
'num_results': 5,
'output_language': 'en',
'custom_instructions': 'Please provide a concise summary'
}
response = requests.post(url, json=payload, headers=headers, cookies={'session': 'your_session_cookie'})
if response.status_code == 200:
result = response.json()
print(f"Answer: {result['answer']}")
print(f"Sources: {result.get('sources', [])}")
else:
print(f"Error: {response.json().get('error')}")
Best Practices
- Always validate and strip user input (question) before processing
- Implement proper error handling and logging for production use
- Consider rate limiting to prevent abuse of the chat endpoint
- The function maintains session state - ensure proper session cleanup mechanisms
- Chat history is limited to last 10 messages to prevent context overflow
- Session data is persisted to disk - ensure adequate storage and cleanup policies
- The function requires authentication via @login_required decorator
- Consider implementing request timeouts for long-running RAG queries
- Monitor memory usage as chat_sessions dictionary grows in production
- Validate model parameter against allowed models to prevent injection
- Consider implementing streaming responses for better UX with long answers
- Ensure thread-safety if save_session_to_disk uses threading module
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function api_chat 88.4% similar
-
function api_send_chat_message 84.9% similar
-
function api_send_chat_message_v1 84.7% similar
-
function index_v2 77.2% similar
-
function chat_v1 74.9% similar