|
|
@@ -7,8 +7,6 @@ import time
|
|
|
from datetime import datetime
|
|
|
from typing import List, Dict, Any, Tuple, Optional
|
|
|
|
|
|
-import httpx
|
|
|
-import redis
|
|
|
import numpy as np
|
|
|
from dotenv import load_dotenv
|
|
|
from serpapi import GoogleSearch
|
|
|
@@ -19,7 +17,6 @@ from sklearn.metrics.pairwise import cosine_similarity
|
|
|
|
|
|
from src.service.embedding_service import hybrid_search
|
|
|
from src.service.memory_service import retrieve_relevant_memory, save_memory_entry
|
|
|
-from src.utils.security import load_security_model
|
|
|
from .state import state
|
|
|
|
|
|
|
|
|
@@ -33,10 +30,19 @@ load_dotenv(override=True)
|
|
|
|
|
|
|
|
|
# Initialize Scaleway OpenAI client
|
|
|
+# SCW_ENDPOINT: vide ou "default" = https://api.scaleway.ai/v1 (projet par défaut)
|
|
|
+# Sinon: https://api.scaleway.ai/{project_id}/v1
|
|
|
SCW_API_KEY = os.getenv("SCW_API_KEY")
|
|
|
+SCW_ENDPOINT = os.getenv("SCW_ENDPOINT", "").strip()
|
|
|
+SCW_MODEL = os.getenv("SCW_MODEL", "mistral-small-3.2-24b-instruct-2506")
|
|
|
+SCW_BASE_URL = (
|
|
|
+ "https://api.scaleway.ai/v1"
|
|
|
+ if not SCW_ENDPOINT or SCW_ENDPOINT.lower() == "default"
|
|
|
+ else f"https://api.scaleway.ai/{SCW_ENDPOINT}/v1"
|
|
|
+)
|
|
|
if not SCW_API_KEY:
|
|
|
logger.error("❌ SCW_API_KEY is missing in environment variables.")
|
|
|
-client = OpenAI(api_key=SCW_API_KEY, base_url="https://api.scaleway.ai/v1") if SCW_API_KEY else None
|
|
|
+scw_client = OpenAI(api_key=SCW_API_KEY, base_url=SCW_BASE_URL) if SCW_API_KEY else None
|
|
|
|
|
|
|
|
|
# --- Topic detection ---
|
|
|
@@ -90,50 +96,55 @@ def quote_keywords(question: str) -> str:
|
|
|
result.append(token.text)
|
|
|
return " ".join(result)
|
|
|
|
|
|
-# --- Scaleway Llama3 call ---
|
|
|
+# --- Scaleway LLM call (Mistral via OpenAI-compatible API) ---
|
|
|
def call_scaleway_llama3(
|
|
|
prompt: str,
|
|
|
- model: str = "llama-3.3-70b-instruct",
|
|
|
- temperature: float = 0.5,
|
|
|
- top_p: float = 0.95,
|
|
|
- max_tokens: int = 800,
|
|
|
+ model: Optional[str] = None,
|
|
|
+ temperature: float = 0.15,
|
|
|
+ top_p: float = 1.0,
|
|
|
+ max_tokens: int = 512,
|
|
|
+ presence_penalty: float = 0.0,
|
|
|
timeout: float = 40.0,
|
|
|
retries: int = 2,
|
|
|
) -> str:
|
|
|
- if not SCW_API_KEY:
|
|
|
+ """Appelle le LLM Scaleway (Mistral) via l'API OpenAI-compatible."""
|
|
|
+ if not scw_client:
|
|
|
logger.error("SCW_API_KEY is not set; cannot call Scaleway API.")
|
|
|
return "Erreur : clé API Scaleway manquante."
|
|
|
|
|
|
- url = "https://api.scaleway.ai/v1/chat/completions"
|
|
|
- headers = {"Authorization": f"Bearer {SCW_API_KEY}"}
|
|
|
- payload = {
|
|
|
- "model": model,
|
|
|
- "messages": [{"role": "user", "content": prompt}],
|
|
|
- "temperature": temperature,
|
|
|
- "top_p": top_p,
|
|
|
- "max_tokens": max_tokens,
|
|
|
- "presence_penalty": 0.3,
|
|
|
- "frequency_penalty": 0.2,
|
|
|
- }
|
|
|
+ model = model or SCW_MODEL
|
|
|
|
|
|
for attempt in range(1, retries + 1):
|
|
|
try:
|
|
|
- with httpx.Client(timeout=timeout) as client:
|
|
|
- response = client.post(url, headers=headers, json=payload)
|
|
|
- response.raise_for_status()
|
|
|
- data = response.json()
|
|
|
- content = data.get("choices", [{}])[0].get("message", {}).get("content")
|
|
|
- if content:
|
|
|
- return content
|
|
|
- else:
|
|
|
- logger.warning("Scaleway API returned no content.")
|
|
|
- return "Erreur : Réponse vide du modèle."
|
|
|
- except httpx.TimeoutException:
|
|
|
- logger.warning(f"Timeout during Scaleway API call (attempt {attempt}/{retries})")
|
|
|
- if attempt == retries:
|
|
|
- return "Erreur : Timeout lors de l'appel au modèle."
|
|
|
- except Exception:
|
|
|
- logger.exception("Exception during Scaleway API call")
|
|
|
+ response = scw_client.chat.completions.create(
|
|
|
+ model=model,
|
|
|
+ messages=[
|
|
|
+ {"role": "system", "content": "Tu es un assistant documentaire. Réponds de manière factuelle en français."},
|
|
|
+ {"role": "user", "content": prompt},
|
|
|
+ ],
|
|
|
+ max_tokens=max_tokens,
|
|
|
+ temperature=temperature,
|
|
|
+ top_p=top_p,
|
|
|
+ presence_penalty=presence_penalty,
|
|
|
+ stream=False,
|
|
|
+ )
|
|
|
+ content = response.choices[0].message.content if response.choices else None
|
|
|
+ if content:
|
|
|
+ return content
|
|
|
+ logger.warning("Scaleway API returned no content.")
|
|
|
+ return "Erreur : Réponse vide du modèle."
|
|
|
+ except Exception as e:
|
|
|
+ err_msg = str(e)
|
|
|
+ if "403" in err_msg or "forbidden" in err_msg.lower():
|
|
|
+ logger.error(
|
|
|
+ "403 Forbidden: Vérifiez que SCW_API_KEY a les droits IAM pour l'API AI, "
|
|
|
+ "et que SCW_ENDPOINT (project_id) correspond à votre projet. "
|
|
|
+ "Essayez SCW_ENDPOINT= (vide) pour utiliser le projet par défaut."
|
|
|
+ )
|
|
|
+ elif "timeout" in err_msg.lower() or "timed out" in err_msg.lower():
|
|
|
+ logger.warning(f"Timeout during Scaleway API call (attempt {attempt}/{retries})")
|
|
|
+ else:
|
|
|
+ logger.exception("Exception during Scaleway API call")
|
|
|
if attempt == retries:
|
|
|
return "Erreur : Impossible d'obtenir une réponse du modèle."
|
|
|
return "Erreur inconnue lors de l'appel au modèle."
|