- Introduction of dynamic Retrievers & Specialists

- Introduction of dynamic Processors
- Introduction of caching system
- Introduction of a better template manager
- Adaptation of ModelVariables to support dynamic Processors / Retrievers / Specialists
- Start adaptation of chat client
This commit is contained in:
Josako
2024-11-15 10:00:53 +01:00
parent 55a8a95f79
commit 1807435339
101 changed files with 4181 additions and 1764 deletions

View File

@@ -38,7 +38,6 @@ def track_socketio_event(func):
@track_socketio_event
def handle_connect():
try:
current_app.logger.debug(f'SocketIO: Connection handling started using {request.args}')
tenant_id = request.args.get('tenantId')
if not tenant_id:
raise Exception("Missing Tenant ID")
@@ -52,12 +51,10 @@ def handle_connect():
# Create JWT token
token = create_access_token(identity={"tenant_id": tenant_id, "api_key": api_key})
current_app.logger.debug(f'SocketIO: Connection handling created token: {token} for tenant {tenant_id}')
# Create a unique room for this client
room = f"{tenant_id}_{request.sid}"
join_room(room)
current_app.logger.debug(f'SocketIO: Client joined room: {room}')
# Create a unique session ID
if 'session_id' not in session:
@@ -67,11 +64,8 @@ def handle_connect():
session['room'] = room
# Communicate connection to client
current_app.logger.debug(f'SocketIO: Connection handling sending status to client for tenant {tenant_id}')
emit('connect', {'status': 'Connected', 'tenant_id': tenant_id, 'room': room})
current_app.logger.debug(f'SocketIO: Connection handling sending authentication token to client')
emit('authenticated', {'token': token, 'room': room}) # Emit custom event with the token
current_app.logger.debug(f'SocketIO: Connection handling sent token to client for tenant {tenant_id}')
except Exception as e:
current_app.logger.error(f'SocketIO: Connection failed: {e}')
# communicate connection problem to client
@@ -85,71 +79,60 @@ def handle_disconnect():
room = session.get('room')
if room:
leave_room(room)
current_app.logger.debug(f'SocketIO: Client left room: {room}')
current_app.logger.debug('SocketIO: Client disconnected')
@socketio.on('heartbeat')
def handle_heartbeat():
current_app.logger.debug('SocketIO: Heartbeat received')
last_activity = session.get('last_activity')
if datetime.now() - last_activity > current_app.config.get('SOCKETIO_MAX_IDLE_TIME'):
current_app.logger.debug('SocketIO: Heartbeat timed out, connection closed')
disconnect()
@socketio.on('user_message')
def handle_message(data):
try:
current_app.logger.debug(f"SocketIO: Message handling received message from tenant {data['tenantId']}: "
f"{data['message']} with token {data['token']}")
session['last_activity'] = datetime.now()
current_tenant_id = validate_incoming_data(data)
room = session.get('room')
# Offload actual processing of question
task = current_celery.send_task('ask_question',
task = current_celery.send_task('execute_specialist',
queue='llm_interactions',
args=[
current_tenant_id,
data['message'],
data['language'],
data['specialistId'],
data['arguments'],
session['session_id'],
data['timezone'],
room
])
current_app.logger.debug(f'SocketIO: Message offloading for tenant {current_tenant_id}, '
f'Question: {task.id}')
response = {
'tenantId': data['tenantId'],
'message': f'Processing question ... Session ID = {session["session_id"]}',
'taskId': task.id,
}
current_app.logger.debug(f"SocketIO: Message handling sent bot response: {response}")
current_app.logger.debug(f"Sent message with {data}, response {response}")
emit('bot_response', response, room=room)
except Exception as e:
current_app.logger.error(f'SocketIO: Message handling failed: {e}')
current_app.logger.error(f'SocketIO: Message handling failed: {str(e)}')
disconnect()
@socketio.on('check_task_status')
def check_task_status(data):
current_app.logger.debug(f'SocketIO: Checking Task Status ... {data}')
task_id = data.get('task_id')
room = session.get('room')
current_app.logger.debug(f'SocketIO: Check task status for task_id: {task_id}')
if not task_id:
emit('task_status', {'status': 'error', 'message': 'Missing task ID'}, room=room)
return
task_result = current_celery.AsyncResult(task_id)
if task_result.state == 'PENDING':
current_app.logger.debug(f'SocketIO: Task {task_id} is pending')
emit('task_status', {'status': 'pending', 'taskId': task_id}, room=room)
elif task_result.state == 'SUCCESS':
current_app.logger.debug(f'SocketIO: Task {task_id} has finished. Status: {task_result.state}, '
f'Result: {task_result.result}')
result = task_result.result
current_app.logger.debug(f'SocketIO: Task {task_id} returned: {result}')
response = {
'status': 'success',
'taskId': task_id,
@@ -167,8 +150,6 @@ def check_task_status(data):
@socketio.on('feedback')
def handle_feedback(data):
try:
current_app.logger.debug(f'SocketIO: Feedback handling received feedback with data: {data}')
current_tenant_id = validate_incoming_data(data)
interaction_id = data.get('interactionId')
@@ -177,7 +158,6 @@ def handle_feedback(data):
Database(current_tenant_id).switch_schema()
interaction = Interaction.query.get_or_404(interaction_id)
current_app.logger.debug(f'Processing feedback for interaction: {interaction}')
interaction.appreciation = 0 if feedback == 'down' else 100
try:
db.session.commit()
@@ -188,7 +168,7 @@ def handle_feedback(data):
emit('feedback_received', {'status': 'Could not register feedback', 'interaction_id': interaction_id})
raise e
except Exception as e:
current_app.logger.debug(f'SocketIO: Feedback handling failed: {e}')
current_app.logger.error(f'SocketIO: Feedback handling failed: {e}')
disconnect()
@@ -212,7 +192,6 @@ def validate_incoming_data(data):
if not token_sub:
raise Exception("Missing token subject")
tenant_id = token_sub.get('tenant_id')
current_tenant_id = token_sub.get('tenant_id')
if not current_tenant_id: