Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
#+€+
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import mimetypes
|
|
@@ -48,8 +47,8 @@ print(f"Dossier pour les sessions serveur configuré : {app.config['SESSION_FILE
|
|
| 48 |
server_session = Session(app)
|
| 49 |
|
| 50 |
# --- Configuration de l'API Gemini ---
|
| 51 |
-
MODEL_FLASH = 'gemini-
|
| 52 |
-
MODEL_PRO = 'gemini-
|
| 53 |
SYSTEM_INSTRUCTION = "Tu es un assistant intelligent et amical nommé Mariam. Tu assistes les utilisateurs au mieux de tes capacités. Tu as été créé par Aenir."
|
| 54 |
SAFETY_SETTINGS = [
|
| 55 |
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
|
@@ -247,16 +246,8 @@ def chat_api():
|
|
| 247 |
current_gemini_parts.append(final_prompt_for_gemini)
|
| 248 |
|
| 249 |
# --- Préparation des paramètres de configuration et modèle ---
|
| 250 |
-
config = {}
|
| 251 |
selected_model_name = MODEL_PRO if use_advanced else MODEL_FLASH
|
| 252 |
|
| 253 |
-
# --- Intégration Google Search ---
|
| 254 |
-
if use_web_search and final_prompt_for_gemini:
|
| 255 |
-
print(f"--- LOG [/api/chat]: Activation Google Search pour: '{final_prompt_for_gemini[:60]}...'")
|
| 256 |
-
# Configurer Google Search comme outil
|
| 257 |
-
config["tools"] = [{"google_search": {}}]
|
| 258 |
-
print(f" [/api/chat]: Configuration Google Search ajoutée")
|
| 259 |
-
|
| 260 |
# Vérifier si on a quelque chose à envoyer
|
| 261 |
if not current_gemini_parts:
|
| 262 |
print("--- ERREUR [/api/chat]: Aucune donnée (texte ou fichier valide) à envoyer après traitement.")
|
|
@@ -299,10 +290,18 @@ def chat_api():
|
|
| 299 |
system_instruction=SYSTEM_INSTRUCTION
|
| 300 |
)
|
| 301 |
print(f"--- LOG [/api/chat]: Envoi de la requête à {selected_model_name}...")
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
# --- Traitement de la réponse (avec logs) ---
|
| 308 |
response_text_raw = ""
|
|
@@ -317,34 +316,25 @@ def chat_api():
|
|
| 317 |
# Récupération des métadonnées de recherche si disponibles
|
| 318 |
if use_web_search and hasattr(response, 'candidates') and response.candidates:
|
| 319 |
candidate = response.candidates[0]
|
| 320 |
-
if hasattr(candidate, '
|
| 321 |
-
metadata = candidate.
|
| 322 |
search_metadata = {}
|
| 323 |
|
| 324 |
-
#
|
| 325 |
-
if hasattr(metadata, '
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
for site in metadata.grounding_chunks if hasattr(site, 'web')
|
| 337 |
-
]
|
| 338 |
-
search_metadata["search_pages"] = search_pages
|
| 339 |
-
|
| 340 |
-
# Contenu HTML rendu
|
| 341 |
-
if hasattr(metadata, 'search_entry_point') and hasattr(metadata.search_entry_point, 'rendered_content'):
|
| 342 |
-
search_metadata["rendered_content"] = metadata.search_entry_point.rendered_content
|
| 343 |
|
| 344 |
-
print(f"--- LOG [/api/chat]: Métadonnées de
|
| 345 |
-
if search_metadata.get("
|
| 346 |
-
print(f" Requêtes: {search_metadata['search_queries']}")
|
| 347 |
-
if search_metadata.get("search_pages"):
|
| 348 |
print(f" Pages: {len(search_metadata['search_pages'])} sources")
|
| 349 |
else:
|
| 350 |
feedback_info = f"Feedback: {response.prompt_feedback}" if response.prompt_feedback else "Pas de feedback détaillé."
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import mimetypes
|
|
|
|
| 47 |
server_session = Session(app)
|
| 48 |
|
| 49 |
# --- Configuration de l'API Gemini ---
|
| 50 |
+
MODEL_FLASH = 'gemini-1.5-flash-002' # Mise à jour des modèles
|
| 51 |
+
MODEL_PRO = 'gemini-1.5-pro-002' # Mise à jour des modèles
|
| 52 |
SYSTEM_INSTRUCTION = "Tu es un assistant intelligent et amical nommé Mariam. Tu assistes les utilisateurs au mieux de tes capacités. Tu as été créé par Aenir."
|
| 53 |
SAFETY_SETTINGS = [
|
| 54 |
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
|
|
|
| 246 |
current_gemini_parts.append(final_prompt_for_gemini)
|
| 247 |
|
| 248 |
# --- Préparation des paramètres de configuration et modèle ---
|
|
|
|
| 249 |
selected_model_name = MODEL_PRO if use_advanced else MODEL_FLASH
|
| 250 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
# Vérifier si on a quelque chose à envoyer
|
| 252 |
if not current_gemini_parts:
|
| 253 |
print("--- ERREUR [/api/chat]: Aucune donnée (texte ou fichier valide) à envoyer après traitement.")
|
|
|
|
| 290 |
system_instruction=SYSTEM_INSTRUCTION
|
| 291 |
)
|
| 292 |
print(f"--- LOG [/api/chat]: Envoi de la requête à {selected_model_name}...")
|
| 293 |
+
|
| 294 |
+
# Modification pour utiliser google_search_retrieval si la recherche web est activée
|
| 295 |
+
if use_web_search:
|
| 296 |
+
print(f"--- LOG [/api/chat]: Activation Google Search Retrieval...")
|
| 297 |
+
response = active_model.generate_content(
|
| 298 |
+
contents=contents_for_gemini,
|
| 299 |
+
tools='google_search_retrieval'
|
| 300 |
+
)
|
| 301 |
+
else:
|
| 302 |
+
response = active_model.generate_content(
|
| 303 |
+
contents=contents_for_gemini
|
| 304 |
+
)
|
| 305 |
|
| 306 |
# --- Traitement de la réponse (avec logs) ---
|
| 307 |
response_text_raw = ""
|
|
|
|
| 316 |
# Récupération des métadonnées de recherche si disponibles
|
| 317 |
if use_web_search and hasattr(response, 'candidates') and response.candidates:
|
| 318 |
candidate = response.candidates[0]
|
| 319 |
+
if hasattr(candidate, 'citation_metadata'):
|
| 320 |
+
metadata = candidate.citation_metadata
|
| 321 |
search_metadata = {}
|
| 322 |
|
| 323 |
+
# Extraire les citations et références
|
| 324 |
+
if hasattr(metadata, 'citations'):
|
| 325 |
+
search_pages = []
|
| 326 |
+
for citation in metadata.citations:
|
| 327 |
+
if hasattr(citation, 'start_index') and hasattr(citation, 'end_index'):
|
| 328 |
+
search_pages.append({
|
| 329 |
+
"title": citation.title if hasattr(citation, 'title') else "Sans titre",
|
| 330 |
+
"url": citation.uri if hasattr(citation, 'uri') else "#",
|
| 331 |
+
"snippet": f"Citation: {citation.start_index}-{citation.end_index}"
|
| 332 |
+
})
|
| 333 |
+
if search_pages:
|
| 334 |
+
search_metadata["search_pages"] = search_pages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
|
| 336 |
+
print(f"--- LOG [/api/chat]: Métadonnées de citation récupérées:")
|
| 337 |
+
if search_metadata and search_metadata.get("search_pages"):
|
|
|
|
|
|
|
| 338 |
print(f" Pages: {len(search_metadata['search_pages'])} sources")
|
| 339 |
else:
|
| 340 |
feedback_info = f"Feedback: {response.prompt_feedback}" if response.prompt_feedback else "Pas de feedback détaillé."
|