import httpx
from app.core.config import settings


async def build_embedding(text: str) -> dict:
    """
    Génère un vecteur d'embedding en interrogeant le conteneur local llama.cpp
    """
    url = f"{settings.embedding_base_url}/embeddings"
    
    headers = {
        "Content-Type": "application/json"
    }
    
    payload = {
        "input": text,
        "model": settings.embedding_model
    }
    
    async with httpx.AsyncClient(timeout=30.0) as client:
        try:
            response = await client.post(url, json=payload, headers=headers)
            response.raise_for_status()
            
            data = response.json()
            vector = data["data"][0]["embedding"]
            
            return {
                "model": settings.embedding_model,
                "text_length": len(text),
                "vector": vector,
            }
            
        except httpx.HTTPError as e:
            print(f"Erreur lors de la génération de l'embedding : {e}")
            return {
                "model": settings.embedding_model,
                "text_length": len(text),
                "vector": [],
            }