ai-station/app.py

564 lines
19 KiB
Python
Raw Normal View History

2025-12-25 14:54:33 +00:00
import os
import re
import uuid
2025-12-26 16:48:51 +00:00
import shutil
from datetime import datetime
2025-12-29 05:50:06 +00:00
from typing import Optional, Dict, List
2025-12-26 16:48:51 +00:00
import chainlit as cl
2025-12-26 09:11:06 +00:00
import ollama
import fitz # PyMuPDF
2025-12-26 09:58:49 +00:00
from qdrant_client import AsyncQdrantClient
from qdrant_client.models import PointStruct, Distance, VectorParams
2025-12-26 10:07:15 +00:00
from chainlit.data.sql_alchemy import SQLAlchemyDataLayer
2025-12-29 05:50:06 +00:00
from chainlit.data.storage_clients import BaseStorageClient
2025-12-26 10:07:15 +00:00
2025-12-26 16:48:51 +00:00
# === CONFIGURAZIONE ===
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql+asyncpg://ai_user:secure_password_here@postgres:5432/ai_station")
OLLAMA_URL = os.getenv("OLLAMA_URL", "http://192.168.1.243:11434")
QDRANT_URL = os.getenv("QDRANT_URL", "http://qdrant:6333")
2025-12-29 05:50:06 +00:00
WORKSPACES_DIR = "./workspaces"
STORAGE_DIR = "./.files"
os.makedirs(STORAGE_DIR, exist_ok=True)
# === MAPPING UTENTI E RUOLI ===
USER_PROFILES = {
"giuseppe@defranceschi.pro": {
"role": "admin",
"name": "Giuseppe",
"workspace": "admin_workspace",
"rag_collection": "admin_docs",
"capabilities": ["debug", "system_prompts", "user_management", "all_models"],
"show_code": True
},
"giuseppe.defranceschi@gmail.com": {
"role": "admin",
"name": "Giuseppe",
"workspace": "admin_workspace",
"rag_collection": "admin_docs",
"capabilities": ["debug", "system_prompts", "user_management", "all_models"],
"show_code": True
},
"federica.tecchio@gmail.com": {
"role": "business",
"name": "Federica",
"workspace": "business_workspace",
"rag_collection": "contabilita",
"capabilities": ["pdf_upload", "basic_chat"],
"show_code": False
},
"riccardob545@gmail.com": {
"role": "engineering",
"name": "Riccardo",
"workspace": "engineering_workspace",
"rag_collection": "engineering_docs",
"capabilities": ["code_execution", "data_viz", "advanced_chat"],
"show_code": True
},
"giuliadefranceschi05@gmail.com": {
"role": "architecture",
"name": "Giulia",
"workspace": "architecture_workspace",
"rag_collection": "architecture_manuals",
"capabilities": ["visual_chat", "pdf_upload", "image_gen"],
"show_code": False
}
}
# === CUSTOM LOCAL STORAGE CLIENT ===
class LocalStorageClient(BaseStorageClient):
"""Storage locale su filesystem per file/elementi"""
def __init__(self, storage_path: str):
self.storage_path = storage_path
os.makedirs(storage_path, exist_ok=True)
async def upload_file(
self,
object_key: str,
data: bytes,
mime: str = "application/octet-stream",
overwrite: bool = True,
) -> Dict[str, str]:
"""Salva file localmente"""
file_path = os.path.join(self.storage_path, object_key)
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, "wb") as f:
f.write(data)
return {
"object_key": object_key,
"url": f"/files/{object_key}"
}
2025-12-26 09:11:06 +00:00
2025-12-26 16:48:51 +00:00
# === INIZIALIZZAZIONE DATA LAYER ===
2025-12-29 05:50:06 +00:00
storage_client = LocalStorageClient(storage_path=STORAGE_DIR)
2025-12-26 16:48:51 +00:00
try:
2025-12-29 05:50:06 +00:00
data_layer = SQLAlchemyDataLayer(
conninfo=DATABASE_URL,
storage_provider=storage_client,
user_thread_limit=1000,
show_logger=False
)
2025-12-26 16:48:51 +00:00
cl.data_layer = data_layer
2025-12-29 05:50:06 +00:00
print("✅ SQLAlchemyDataLayer + LocalStorage initialized successfully")
2025-12-26 16:48:51 +00:00
except Exception as e:
print(f"❌ Failed to initialize data layer: {e}")
cl.data_layer = None
2025-12-25 18:00:13 +00:00
2025-12-29 05:50:06 +00:00
# === OAUTH CALLBACK CON RUOLI ===
@cl.oauth_callback
def oauth_callback(
provider_id: str,
token: str,
raw_user_data: Dict[str, str],
default_user: cl.User,
) -> Optional[cl.User]:
"""Validazione e arricchimento dati utente con ruoli"""
if provider_id == "google":
email = raw_user_data.get("email", "").lower()
# Verifica se utente è autorizzato
if email not in USER_PROFILES:
print(f"❌ Utente non autorizzato: {email}")
return None # Nega accesso
# Arricchisci metadata con profilo
profile = USER_PROFILES[email]
default_user.metadata.update({
"picture": raw_user_data.get("picture", ""),
"locale": raw_user_data.get("locale", "en"),
"role": profile["role"],
"workspace": profile["workspace"],
"rag_collection": profile["rag_collection"],
"capabilities": profile["capabilities"],
"show_code": profile["show_code"],
"display_name": profile["name"]
})
print(f"✅ Utente autorizzato: {email} - Ruolo: {profile['role']}")
return default_user
return default_user
2025-12-25 18:00:13 +00:00
2025-12-26 16:48:51 +00:00
# === UTILITY FUNCTIONS ===
2025-12-29 05:50:06 +00:00
def get_user_profile(user_email: str) -> Dict:
"""Recupera profilo utente"""
return USER_PROFILES.get(user_email.lower(), {
"role": "guest",
"name": "Ospite",
"workspace": "guest_workspace",
"rag_collection": "documents",
"capabilities": [],
"show_code": False
})
def create_workspace(workspace_name: str) -> str:
2025-12-26 16:48:51 +00:00
"""Crea directory workspace se non esiste"""
2025-12-29 05:50:06 +00:00
workspace_path = os.path.join(WORKSPACES_DIR, workspace_name)
2025-12-26 16:48:51 +00:00
os.makedirs(workspace_path, exist_ok=True)
return workspace_path
2025-12-25 14:54:33 +00:00
2025-12-29 05:50:06 +00:00
def save_code_to_file(code: str, workspace: str) -> str:
2025-12-26 16:48:51 +00:00
"""Salva blocco codice come file .py"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
2025-12-25 14:54:33 +00:00
file_name = f"code_{timestamp}.py"
2025-12-29 05:50:06 +00:00
file_path = os.path.join(WORKSPACES_DIR, workspace, file_name)
2025-12-26 16:48:51 +00:00
with open(file_path, "w", encoding="utf-8") as f:
f.write(code)
2025-12-25 14:54:33 +00:00
return file_path
def extract_text_from_pdf(pdf_path: str) -> str:
"""Estrae testo da PDF usando PyMuPDF"""
try:
doc = fitz.open(pdf_path)
text_parts = []
for page_num in range(len(doc)):
page = doc[page_num]
text = page.get_text()
text_parts.append(f"--- Pagina {page_num + 1} ---\n{text}\n")
doc.close()
return "\n".join(text_parts)
except Exception as e:
print(f"❌ Errore estrazione PDF: {e}")
return ""
2025-12-26 16:48:51 +00:00
# === QDRANT FUNCTIONS ===
async def get_qdrant_client() -> AsyncQdrantClient:
"""Connessione a Qdrant"""
2025-12-29 05:50:06 +00:00
return AsyncQdrantClient(url=QDRANT_URL)
async def ensure_collection(collection_name: str):
"""Crea collection se non esiste"""
client = await get_qdrant_client()
2025-12-26 09:58:49 +00:00
if not await client.collection_exists(collection_name):
await client.create_collection(
collection_name=collection_name,
2025-12-26 09:58:49 +00:00
vectors_config=VectorParams(size=768, distance=Distance.COSINE)
2025-12-25 14:54:33 +00:00
)
2025-12-26 16:48:51 +00:00
async def get_embeddings(text: str) -> list:
"""Genera embeddings con Ollama"""
client = ollama.Client(host=OLLAMA_URL)
2025-12-26 16:48:51 +00:00
max_length = 2000
if len(text) > max_length:
text = text[:max_length]
2025-12-26 09:11:06 +00:00
try:
response = client.embed(model='nomic-embed-text', input=text)
if 'embeddings' in response:
return response['embeddings'][0]
2025-12-26 16:48:51 +00:00
return response.get('embedding', [])
2025-12-26 09:11:06 +00:00
except Exception as e:
2025-12-26 16:48:51 +00:00
print(f"❌ Errore Embedding: {e}")
2025-12-26 09:11:06 +00:00
return []
2025-12-25 18:00:13 +00:00
2025-12-29 05:50:06 +00:00
async def index_document(file_name: str, content: str, collection_name: str) -> bool:
"""Indicizza documento su Qdrant in collection specifica"""
try:
2025-12-29 05:50:06 +00:00
await ensure_collection(collection_name)
chunks = chunk_text(content, max_length=1500)
2025-12-26 16:48:51 +00:00
qdrant_client = await get_qdrant_client()
points = []
2025-12-26 16:48:51 +00:00
for i, chunk in enumerate(chunks):
embeddings = await get_embeddings(chunk)
if not embeddings:
continue
point_id = str(uuid.uuid4())
point = PointStruct(
id=point_id,
vector=embeddings,
payload={
"file_name": file_name,
"content": chunk,
"chunk_index": i,
"total_chunks": len(chunks),
"indexed_at": datetime.now().isoformat()
}
)
points.append(point)
2025-12-26 16:48:51 +00:00
if points:
2025-12-29 05:50:06 +00:00
await qdrant_client.upsert(collection_name=collection_name, points=points)
return True
return False
2025-12-26 16:48:51 +00:00
except Exception as e:
print(f"❌ Errore indicizzazione: {e}")
return False
def chunk_text(text: str, max_length: int = 1500, overlap: int = 200) -> list:
"""Divide testo in chunks con overlap"""
if len(text) <= max_length:
return [text]
chunks = []
start = 0
while start < len(text):
end = start + max_length
if end < len(text):
last_period = text.rfind('.', start, end)
last_newline = text.rfind('\n', start, end)
split_point = max(last_period, last_newline)
if split_point > start:
end = split_point + 1
chunks.append(text[start:end].strip())
start = end - overlap
return chunks
2025-12-29 05:50:06 +00:00
async def search_qdrant(query_text: str, collection_name: str, limit: int = 5) -> str:
"""Ricerca documenti rilevanti in collection specifica"""
2025-12-26 16:48:51 +00:00
try:
qdrant_client = await get_qdrant_client()
2025-12-29 05:50:06 +00:00
# Verifica se collection esiste
if not await qdrant_client.collection_exists(collection_name):
return ""
query_embedding = await get_embeddings(query_text)
2025-12-26 09:11:06 +00:00
if not query_embedding:
return ""
2025-12-26 16:48:51 +00:00
2025-12-26 09:58:49 +00:00
search_result = await qdrant_client.query_points(
2025-12-29 05:50:06 +00:00
collection_name=collection_name,
2025-12-26 09:58:49 +00:00
query=query_embedding,
2025-12-26 16:48:51 +00:00
limit=limit
)
contexts = []
seen_files = set()
2025-12-26 16:48:51 +00:00
for hit in search_result.points:
if hit.payload:
file_name = hit.payload.get('file_name', 'Unknown')
content = hit.payload.get('content', '')
chunk_idx = hit.payload.get('chunk_index', 0)
2025-12-26 16:48:51 +00:00
score = hit.score if hasattr(hit, 'score') else 0
file_key = f"{file_name}_{chunk_idx}"
if file_key not in seen_files:
seen_files.add(file_key)
contexts.append(
f"📄 **{file_name}** (chunk {chunk_idx+1}, score: {score:.2f})\n"
2025-12-29 05:50:06 +00:00
f"``````"
)
2025-12-26 16:48:51 +00:00
return "\n\n".join(contexts) if contexts else ""
except Exception as e:
2025-12-26 16:48:51 +00:00
print(f"❌ Errore ricerca Qdrant: {e}")
return ""
2025-12-25 18:00:13 +00:00
2025-12-26 16:48:51 +00:00
# === CHAINLIT HANDLERS ===
2025-12-25 14:54:33 +00:00
@cl.on_chat_start
2025-12-26 16:48:51 +00:00
async def on_chat_start():
2025-12-29 05:50:06 +00:00
"""Inizializzazione chat con profili utente"""
user = cl.user_session.get("user")
if user:
user_email = user.identifier
profile = get_user_profile(user_email)
user_name = profile["name"]
user_role = profile["role"]
workspace = profile["workspace"]
user_picture = user.metadata.get("picture", "")
show_code = profile["show_code"]
capabilities = profile["capabilities"]
else:
user_email = "guest@local"
user_name = "Ospite"
user_role = "guest"
workspace = "guest_workspace"
user_picture = ""
show_code = False
capabilities = []
create_workspace(workspace)
2025-12-25 14:54:33 +00:00
2025-12-29 05:50:06 +00:00
# Salva in sessione
cl.user_session.set("email", user_email)
cl.user_session.set("name", user_name)
cl.user_session.set("role", user_role)
cl.user_session.set("workspace", workspace)
cl.user_session.set("show_code", show_code)
cl.user_session.set("capabilities", capabilities)
cl.user_session.set("rag_collection", profile.get("rag_collection", "documents"))
# Settings basati su ruolo
settings_widgets = [
cl.input_widget.Select(
id="model",
label="Modello AI",
2025-12-29 05:50:06 +00:00
values=["glm-4.6:cloud", "llama3.2", "mistral", "qwen2.5-coder:32b"],
initial_value="glm-4.6:cloud",
),
cl.input_widget.Slider(
id="temperature",
label="Temperatura",
2025-12-29 05:50:06 +00:00
initial=0.7,
min=0,
max=2,
step=0.1,
),
]
# Solo admin può disabilitare RAG
if user_role == "admin":
settings_widgets.append(
cl.input_widget.Switch(
id="rag_enabled",
label="Abilita RAG",
2025-12-29 05:50:06 +00:00
initial=True,
)
)
settings = await cl.ChatSettings(settings_widgets).send()
2025-12-29 05:50:06 +00:00
# Emoji ruolo
role_emoji = {
"admin": "👑",
"business": "💼",
"engineering": "⚙️",
"architecture": "🏛️",
"guest": "👤"
}
2025-12-25 14:54:33 +00:00
2025-12-26 16:48:51 +00:00
persistence_status = "✅ Attiva" if cl.data_layer else "⚠️ Disattivata"
2025-12-25 14:54:33 +00:00
2025-12-29 05:50:06 +00:00
welcome_msg = f"{role_emoji.get(user_role, '👋')} **Benvenuto, {user_name}!**\n\n"
if user_picture:
welcome_msg += f"![Avatar]({user_picture})\n\n"
welcome_msg += (
f"🎭 **Ruolo**: {user_role.upper()}\n"
f"📁 **Workspace**: `{workspace}`\n"
f"💾 **Persistenza**: {persistence_status}\n"
f"🤖 **Modello**: `glm-4.6:cloud`\n\n"
)
# Capabilities specifiche
if "debug" in capabilities:
welcome_msg += "🔧 **Modalità Debug**: Attiva\n"
if "user_management" in capabilities:
welcome_msg += "👥 **Gestione Utenti**: Disponibile\n"
if not show_code:
welcome_msg += "🎨 **Modalità Visuale**: Codice nascosto\n"
welcome_msg += "\n⚙️ Usa le **Settings** per personalizzare!"
2025-12-29 05:50:06 +00:00
await cl.Message(content=welcome_msg).send()
@cl.on_settings_update
async def on_settings_update(settings):
"""Gestisce aggiornamento settings utente"""
cl.user_session.set("settings", settings)
await cl.Message(content=f"✅ Settings aggiornati").send()
2025-12-25 14:54:33 +00:00
@cl.on_message
2025-12-26 16:48:51 +00:00
async def on_message(message: cl.Message):
2025-12-29 05:50:06 +00:00
"""Gestione messaggi utente con RAG intelligente"""
user_email = cl.user_session.get("email", "guest")
2025-12-26 16:48:51 +00:00
user_role = cl.user_session.get("role", "guest")
2025-12-29 05:50:06 +00:00
workspace = cl.user_session.get("workspace", "guest_workspace")
show_code = cl.user_session.get("show_code", False)
rag_collection = cl.user_session.get("rag_collection", "documents")
settings = cl.user_session.get("settings", {})
model = settings.get("model", "glm-4.6:cloud")
temperature = settings.get("temperature", 0.7)
# Admin può disabilitare RAG, altri lo hanno sempre attivo
rag_enabled = settings.get("rag_enabled", True) if user_role == "admin" else True
try:
if message.elements:
2025-12-29 05:50:06 +00:00
await handle_file_uploads(message.elements, workspace, rag_collection)
2025-12-26 16:48:51 +00:00
# RAG Search solo se abilitato E ci sono documenti
2025-12-29 05:50:06 +00:00
context_text = ""
if rag_enabled:
context_text = await search_qdrant(message.content, rag_collection, limit=5)
2025-12-26 09:58:49 +00:00
# SYSTEM PROMPT MODIFICATO: Risponde SEMPRE, anche senza contesto
2025-12-26 09:58:49 +00:00
if context_text:
2025-12-29 05:50:06 +00:00
system_prompt = (
"Sei un assistente AI esperto. "
"Usa il seguente contesto per arricchire la tua risposta, "
"ma puoi anche rispondere usando la tua conoscenza generale se il contesto non è sufficiente."
)
full_prompt = f"{system_prompt}\n\n**CONTESTO DOCUMENTI:**\n{context_text}\n\n**DOMANDA UTENTE:**\n{message.content}"
else:
2025-12-29 05:50:06 +00:00
system_prompt = "Sei un assistente AI esperto e disponibile. Rispondi in modo chiaro e utile."
full_prompt = f"{system_prompt}\n\n**DOMANDA UTENTE:**\n{message.content}"
2025-12-26 09:58:49 +00:00
2025-12-26 16:48:51 +00:00
client = ollama.Client(host=OLLAMA_URL)
msg = cl.Message(content="")
await msg.send()
2025-12-25 14:54:33 +00:00
messages = [{"role": "user", "content": full_prompt}]
2025-12-26 16:48:51 +00:00
stream = client.chat(
2025-12-29 05:50:06 +00:00
model=model,
2025-12-26 16:48:51 +00:00
messages=messages,
2025-12-29 05:50:06 +00:00
stream=True,
options={"temperature": temperature}
2025-12-26 16:48:51 +00:00
)
full_response = ""
for chunk in stream:
content = chunk['message']['content']
full_response += content
await msg.stream_token(content)
2025-12-26 16:48:51 +00:00
await msg.update()
# Estrai codice Python
2025-12-29 05:50:06 +00:00
code_blocks = re.findall(r"``````", full_response, re.DOTALL)
2025-12-26 16:48:51 +00:00
if code_blocks:
elements = []
2025-12-29 05:50:06 +00:00
# Se show_code è False, nascondi il codice dalla risposta
if not show_code:
# Rimuovi blocchi codice dalla risposta visibile
cleaned_response = re.sub(r"``````", "[Codice eseguito internamente]", full_response, flags=re.DOTALL)
2025-12-29 05:50:06 +00:00
await msg.update(content=cleaned_response)
2025-12-26 16:48:51 +00:00
for code in code_blocks:
2025-12-29 05:50:06 +00:00
file_path = save_code_to_file(code.strip(), workspace)
2025-12-26 16:48:51 +00:00
elements.append(
cl.File(
name=os.path.basename(file_path),
path=file_path,
2025-12-29 05:50:06 +00:00
display="inline" if show_code else "side"
2025-12-26 16:48:51 +00:00
)
)
2025-12-29 05:50:06 +00:00
if show_code:
await cl.Message(
content=f"💾 Codice salvato in workspace",
2025-12-29 05:50:06 +00:00
elements=elements
).send()
2025-12-26 16:48:51 +00:00
2025-12-25 14:54:33 +00:00
except Exception as e:
2025-12-26 16:48:51 +00:00
await cl.Message(content=f"❌ **Errore:** {str(e)}").send()
2025-12-29 05:50:06 +00:00
async def handle_file_uploads(elements, workspace: str, collection_name: str):
"""Gestisce upload e indicizzazione file in collection specifica"""
2025-12-26 16:48:51 +00:00
for element in elements:
try:
2025-12-29 05:50:06 +00:00
dest_path = os.path.join(WORKSPACES_DIR, workspace, element.name)
2025-12-26 16:48:51 +00:00
shutil.copy(element.path, dest_path)
content = None
if element.name.lower().endswith('.pdf'):
await cl.Message(content=f"📄 Elaborazione PDF **{element.name}**...").send()
content = extract_text_from_pdf(dest_path)
if not content:
await cl.Message(
content=f"⚠️ **{element.name}**: PDF vuoto o non leggibile"
).send()
continue
elif element.name.lower().endswith('.txt'):
2025-12-26 16:48:51 +00:00
with open(dest_path, 'r', encoding='utf-8') as f:
content = f.read()
else:
await cl.Message(
content=f"📁 **{element.name}** salvato (supportati: .pdf, .txt)"
).send()
continue
if content:
2025-12-29 05:50:06 +00:00
success = await index_document(element.name, content, collection_name)
2025-12-26 16:48:51 +00:00
if success:
word_count = len(content.split())
2025-12-26 16:48:51 +00:00
await cl.Message(
2025-12-29 05:50:06 +00:00
content=f"✅ **{element.name}** indicizzato in `{collection_name}`\n"
f"📊 Parole estratte: {word_count:,}"
2025-12-26 16:48:51 +00:00
).send()
else:
await cl.Message(
content=f"⚠️ Errore indicizzazione **{element.name}**"
2025-12-26 16:48:51 +00:00
).send()
except Exception as e:
await cl.Message(
content=f"❌ Errore con **{element.name}**: {str(e)}"
2025-12-26 16:48:51 +00:00
).send()