File size: 1,933 Bytes
6fbdc72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243af25
6fbdc72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
from fastapi import FastAPI, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Optional
from mistralai import Mistral

app = FastAPI(title="ROME NAF Pro API — Mistral")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

api_key = os.environ.get("IUflaExH9FzhGEJhbp6tOH5dKJs36ZAV", "")
client = Mistral(api_key=api_key) if api_key else None

# Modèle utilisé — changeable via variable d'env HF
# Options : mistral-small-latest | mistral-medium-latest | mistral-large-latest
MODEL = os.environ.get("MISTRAL_MODEL", "mistral-small-latest")


class Message(BaseModel):
    role: str
    content: str


class ClaudeRequest(BaseModel):
    messages: List[Message]
    max_tokens: Optional[int] = 1200


@app.post("/api/claude")
async def call_mistral(body: ClaudeRequest):
    """
    Endpoint conserve le nom /api/claude pour ne pas modifier le frontend.
    Il appelle Mistral en coulisses.
    """
    if not client:
        raise HTTPException(
            status_code=500,
            detail="MISTRAL_API_KEY manquante. Configurez-la dans les Secrets du Space HF."
        )
    try:
        response = client.chat.complete(
            model=MODEL,
            max_tokens=body.max_tokens,
            messages=[{"role": m.role, "content": m.content} for m in body.messages]
        )
        text = response.choices[0].message.content
        return {"content": [{"text": text}]}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


@app.get("/api/health")
async def health():
    return {
        "status": "ok",
        "provider": "Mistral AI",
        "model": MODEL,
        "api_key_configured": bool(api_key)
    }


app.mount("/", StaticFiles(directory="static", html=True), name="static")