-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
174 lines (146 loc) · 6.27 KB
/
app.py
File metadata and controls
174 lines (146 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
from flask import Flask, render_template, request, jsonify
from services.xai_service import XAIService
from config.config import XAI_API_KEY
import logging
from typing import Dict, Any
from werkzeug.exceptions import HTTPException
from services.conversation_manager import ConversationManager
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Initialize Flask app
app = Flask(__name__)
# Initialize XAI Service
try:
xai_service = XAIService(XAI_API_KEY)
except Exception as e:
logger.error(f"Failed to initialize XAI Service: {str(e)}")
xai_service = None
# Initialize conversation manager
conversation_manager = ConversationManager(max_messages=10)
@app.route('/')
def home():
"""Render the main chat page"""
return render_template('index.html')
@app.route('/chat', methods=['POST'])
def chat():
"""
Handle chat requests with enhanced error handling and response formatting
Expected JSON payload:
{
'message': str, # User's message
'target_language': str (optional) # Target language for response
}
"""
try:
# Validate request data
data = request.get_json()
if not data:
return jsonify({'error': 'No data provided'}), 400
# Extract message and language
user_message = data.get('message', '').strip()
target_language = data.get('target_language', 'English')
# Validate message
if not user_message:
return jsonify({'error': 'No message provided'}), 400
# Update conversation language if changed
if target_language != conversation_manager.get_current_language():
conversation_manager.set_language(target_language)
# Add a language transition message
system_message = f"""You are a multilingual AI assistant. IMPORTANT LANGUAGE INSTRUCTION: You MUST respond ONLY in {target_language}.
DO NOT use any other language in your response. Even if the user's message is in a different language, you must respond in {target_language}.
Format your responses as follows:
- Use bullet points for lists
- Keep paragraphs short (2-3 sentences max)
- Add line breaks between different sections
- Use markdown formatting where appropriate
- Respond directly and succinctly
Remember: Your response must be COMPLETELY in {target_language}."""
else:
system_message = f"""You are a multilingual AI assistant. IMPORTANT LANGUAGE INSTRUCTION: You MUST respond ONLY in {target_language}.
DO NOT use any other language in your response. Even if the user's message is in a different language, you must respond in {target_language}.
Format your responses as follows:
- Use bullet points for lists
- Keep paragraphs short (2-3 sentences max)
- Add line breaks between different sections
- Use markdown formatting where appropriate
- Respond directly and succinctly
Remember: Your response must be COMPLETELY in {target_language}."""
# Handle potential XAI service initialization failure
if xai_service is None:
return jsonify({
'error': 'AI service is currently unavailable',
'choices': [{'message': {'content': 'Sorry, the AI service is temporarily down.'}}]
}), 503
# Get conversation history
conversation_history = conversation_manager.get_context()
# Get AI response with conversation history
response = xai_service.get_response(
user_message=user_message,
conversation_history=conversation_history,
system_message=system_message
)
# Error handling for different response types
if isinstance(response, dict):
if 'error' in response:
logger.error(f"XAI Service Error: {response['error']}")
return jsonify({
'error': response['error'],
'choices': [{'message': {'content': 'An error occurred while processing your request.'}}]
}), 500
# If response is a dictionary, attempt to extract content
content = response.get('choices', [{}])[0].get('message', {}).get('content', '')
# Store the conversation with language
conversation_manager.add_message(role="user", content=user_message, language=conversation_manager.get_current_language())
conversation_manager.add_message(role="assistant", content=content, language=conversation_manager.get_current_language())
return jsonify({
'choices': [{
'message': {
'content': content or 'Sorry, I could not generate a response.'
}
}]
})
# Handle string or other response types
return jsonify({
'choices': [{
'message': {
'content': str(response)
}
}]
})
except HTTPException as http_err:
# Handle HTTP-specific exceptions
logger.error(f"HTTP Error: {http_err}")
return jsonify({
'error': str(http_err),
'choices': [{'message': {'content': 'An HTTP error occurred.'}}]
}), http_err.code
except Exception as e:
# Catch-all for unexpected errors
logger.error(f"Unexpected error in chat route: {str(e)}")
return jsonify({
'error': 'An unexpected error occurred',
'choices': [{'message': {'content': 'Sorry, something went wrong.'}}]
}), 500
# Error handlers
@app.errorhandler(400)
def bad_request(error):
"""Handle bad request errors"""
return jsonify({'error': 'Bad Request', 'message': str(error)}), 400
@app.errorhandler(500)
def internal_server_error(error):
"""Handle internal server errors"""
return jsonify({'error': 'Internal Server Error', 'message': str(error)}), 500
# Run the application
if __name__ == '__main__':
# Ensure debug mode is safe for production
debug_mode = os.environ.get('FLASK_DEBUG', 'False').lower() == 'true'
app.run(
host='0.0.0.0',
port=5001,
debug=debug_mode
)