Update app.py
Browse files
app.py
CHANGED
|
@@ -32,26 +32,42 @@ class Query(BaseModel):
|
|
| 32 |
message: str
|
| 33 |
domain: str
|
| 34 |
subdomain: str
|
| 35 |
-
system_message: str = "
|
| 36 |
-
max_tokens: int =
|
| 37 |
-
temperature: float = 0.
|
| 38 |
top_p: float = 0.95
|
| 39 |
|
| 40 |
-
|
| 41 |
def load_collection(domain, subdomain):
|
| 42 |
path = os.path.join(VECTORSTORE_BASE_DIR, domain.lower(), subdomain.lower())
|
| 43 |
client_chroma = PersistentClient(path=path)
|
| 44 |
return client_chroma.get_or_create_collection(name=f"{domain.lower()}_{subdomain.lower()}")
|
| 45 |
|
| 46 |
-
|
| 47 |
def respond(message, history, system_message, domain, subdomain, max_tokens, temperature, top_p):
|
| 48 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
collection = load_collection(domain, subdomain)
|
| 50 |
query_embedding = embedding_model.encode(message).tolist()
|
| 51 |
results = collection.query(query_embeddings=[query_embedding], n_results=3)
|
| 52 |
context = "\n".join(results["documents"][0]) if results.get("documents") and results["documents"][0] else "Aucun contexte trouvé."
|
| 53 |
-
if subdomain == "sql":
|
| 54 |
-
system_message += "\nTu es un expert SQL. Donne une requête en SQL."
|
| 55 |
|
| 56 |
full_prompt = [
|
| 57 |
{"role": "system", "content": f"{system_message}\n\nContexte:\n{context}"},
|
|
@@ -71,7 +87,6 @@ def respond(message, history, system_message, domain, subdomain, max_tokens, tem
|
|
| 71 |
except Exception as e:
|
| 72 |
return f"❌ Erreur : {e}"
|
| 73 |
|
| 74 |
-
|
| 75 |
@api.post("/ask")
|
| 76 |
def ask_api(query: Query):
|
| 77 |
try:
|
|
@@ -80,11 +95,8 @@ def ask_api(query: Query):
|
|
| 80 |
except Exception as e:
|
| 81 |
return {"error": str(e)}
|
| 82 |
|
| 83 |
-
|
| 84 |
def start_api():
|
| 85 |
-
uvicorn.run(api, host="0.0.0.0", port=
|
| 86 |
-
#uvicorn.run(api, host="0.0.0.0", port=7880)
|
| 87 |
-
|
| 88 |
|
| 89 |
def login(username, password):
|
| 90 |
if username == EXCELIS_USER and password == EXCELIS_MDP:
|
|
@@ -92,7 +104,6 @@ def login(username, password):
|
|
| 92 |
else:
|
| 93 |
return gr.update(visible=False), gr.update(visible=True), gr.update(value="❌ Identifiants incorrects.", visible=True)
|
| 94 |
|
| 95 |
-
|
| 96 |
with gr.Blocks(css="""
|
| 97 |
body {
|
| 98 |
background: linear-gradient(135deg, #0f172a, #1e293b);
|
|
@@ -174,7 +185,6 @@ textarea.svelte-1ipelgc {
|
|
| 174 |
with gr.Row(visible=False) as main_ui:
|
| 175 |
with gr.Column(scale=1, min_width=150):
|
| 176 |
gr.Markdown("### ⚙️ Paramètres")
|
| 177 |
-
sys_msg = gr.Textbox(label="Message système", lines=3, value="Tu es un assistant intelligent pour CorisMoney.")
|
| 178 |
domain = gr.Dropdown(label="Domaine", choices=["corismoney", "ptibank", "cmf", "cma-bf", "assurance", "expertise"], value="corismoney")
|
| 179 |
subdomain = gr.Dropdown(label="Sous-domaine", choices=["incident", "demande", "reclamations", "sql"], value="incident")
|
| 180 |
max_tokens = gr.Slider(1, 2048, value=600, label="Max tokens")
|
|
@@ -185,8 +195,8 @@ textarea.svelte-1ipelgc {
|
|
| 185 |
gr.Markdown("## 💬 Assistant IA Excelis")
|
| 186 |
gr.ChatInterface(
|
| 187 |
fn=respond,
|
| 188 |
-
additional_inputs=[
|
| 189 |
-
title="Assistant CoMo, CMF, Assurance,FKW",
|
| 190 |
description="Posez une question en précisant le domaine et le sous-domaine."
|
| 191 |
)
|
| 192 |
|
|
|
|
| 32 |
message: str
|
| 33 |
domain: str
|
| 34 |
subdomain: str
|
| 35 |
+
system_message: str = ""
|
| 36 |
+
max_tokens: int = 800
|
| 37 |
+
temperature: float = 0.7
|
| 38 |
top_p: float = 0.95
|
| 39 |
|
|
|
|
| 40 |
def load_collection(domain, subdomain):
|
| 41 |
path = os.path.join(VECTORSTORE_BASE_DIR, domain.lower(), subdomain.lower())
|
| 42 |
client_chroma = PersistentClient(path=path)
|
| 43 |
return client_chroma.get_or_create_collection(name=f"{domain.lower()}_{subdomain.lower()}")
|
| 44 |
|
|
|
|
| 45 |
def respond(message, history, system_message, domain, subdomain, max_tokens, temperature, top_p):
|
| 46 |
try:
|
| 47 |
+
# Sélection automatique du message système selon le sous-domaine
|
| 48 |
+
if subdomain == "incident":
|
| 49 |
+
system_message = (
|
| 50 |
+
"Tu es CorisBot, un assistant support de CorisMoney. "
|
| 51 |
+
"Ta mission est d’aider les utilisateurs à décrire précisément les incidents rencontrés "
|
| 52 |
+
"(problèmes techniques, blocage d’accès, erreurs dans l’application). "
|
| 53 |
+
"Tu analyses les messages pour identifier la nature du problème, les étapes à suivre "
|
| 54 |
+
"et tu proposes une réponse claire ou des actions à entreprendre."
|
| 55 |
+
)
|
| 56 |
+
elif subdomain == "demande":
|
| 57 |
+
system_message = (
|
| 58 |
+
"Tu es CorisBot, l’assistant virtuel de CorisMoney. "
|
| 59 |
+
"Tu aides les utilisateurs à formuler ou compléter leurs demandes de service "
|
| 60 |
+
"(contrats, attestations, congés, services,configuration). "
|
| 61 |
+
"Tu dois comprendre ce qu’ils souhaitent faire, identifier les informations nécessaires, "
|
| 62 |
+
"et proposer une réponse structurée ou générer un document si nécessaire."
|
| 63 |
+
)
|
| 64 |
+
elif subdomain == "sql":
|
| 65 |
+
system_message += "\nTu es un expert SQL. Donne une requête en SQL."
|
| 66 |
+
|
| 67 |
collection = load_collection(domain, subdomain)
|
| 68 |
query_embedding = embedding_model.encode(message).tolist()
|
| 69 |
results = collection.query(query_embeddings=[query_embedding], n_results=3)
|
| 70 |
context = "\n".join(results["documents"][0]) if results.get("documents") and results["documents"][0] else "Aucun contexte trouvé."
|
|
|
|
|
|
|
| 71 |
|
| 72 |
full_prompt = [
|
| 73 |
{"role": "system", "content": f"{system_message}\n\nContexte:\n{context}"},
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
return f"❌ Erreur : {e}"
|
| 89 |
|
|
|
|
| 90 |
@api.post("/ask")
|
| 91 |
def ask_api(query: Query):
|
| 92 |
try:
|
|
|
|
| 95 |
except Exception as e:
|
| 96 |
return {"error": str(e)}
|
| 97 |
|
|
|
|
| 98 |
def start_api():
|
| 99 |
+
uvicorn.run(api, host="0.0.0.0", port=7880)
|
|
|
|
|
|
|
| 100 |
|
| 101 |
def login(username, password):
|
| 102 |
if username == EXCELIS_USER and password == EXCELIS_MDP:
|
|
|
|
| 104 |
else:
|
| 105 |
return gr.update(visible=False), gr.update(visible=True), gr.update(value="❌ Identifiants incorrects.", visible=True)
|
| 106 |
|
|
|
|
| 107 |
with gr.Blocks(css="""
|
| 108 |
body {
|
| 109 |
background: linear-gradient(135deg, #0f172a, #1e293b);
|
|
|
|
| 185 |
with gr.Row(visible=False) as main_ui:
|
| 186 |
with gr.Column(scale=1, min_width=150):
|
| 187 |
gr.Markdown("### ⚙️ Paramètres")
|
|
|
|
| 188 |
domain = gr.Dropdown(label="Domaine", choices=["corismoney", "ptibank", "cmf", "cma-bf", "assurance", "expertise"], value="corismoney")
|
| 189 |
subdomain = gr.Dropdown(label="Sous-domaine", choices=["incident", "demande", "reclamations", "sql"], value="incident")
|
| 190 |
max_tokens = gr.Slider(1, 2048, value=600, label="Max tokens")
|
|
|
|
| 195 |
gr.Markdown("## 💬 Assistant IA Excelis")
|
| 196 |
gr.ChatInterface(
|
| 197 |
fn=respond,
|
| 198 |
+
additional_inputs=["text", domain, subdomain, max_tokens, temperature, top_p],
|
| 199 |
+
title="Assistant CoMo, CMF, Assurance, FKW",
|
| 200 |
description="Posez une question en précisant le domaine et le sous-domaine."
|
| 201 |
)
|
| 202 |
|