Public Access
22 lines
1.0 KiB
Python
22 lines
1.0 KiB
Python
import os
|
|
from openai import OpenAI
|
|
from modules.logger import log_event
|
|
|
|
def interroger_huggingface(prompt_securise):
|
|
api_key = os.getenv("HF_API_KEY")
|
|
if not api_key: raise Exception("Clé API HuggingFace manquante.")
|
|
client = OpenAI(base_url="https://api-inference.huggingface.co/v1/", api_key=api_key)
|
|
modeles = ["meta-llama/Meta-Llama-3-8B-Instruct", "mistralai/Mistral-7B-Instruct-v0.3"]
|
|
for modele in modeles:
|
|
try:
|
|
resp = client.chat.completions.create(
|
|
model=modele,
|
|
messages=[{"role": "system", "content": "Réponds en français en Markdown."}, {"role": "user", "content": prompt_securise}],
|
|
max_tokens=1500
|
|
)
|
|
if resp and resp.choices:
|
|
log_event("INFO", f"HF-{modele}", "Succès.")
|
|
return resp.choices[0].message.content, "#fbbf24", "HUGGINGFACE", modele
|
|
except Exception as e:
|
|
log_event("WARNING", f"HF-{modele}", f"Erreur: {e}")
|
|
raise Exception("Tous les modèles HuggingFace ont échoué.") |