from .base_llm import BaseLLM
import requests
from datetime import timedelta
from typing import Dict, Any

class Ollama(BaseLLM):
    """
    Classe optimisée pour interagir avec l'API Ollama.
    """

    def __init__(self, modele: str = "qwen2.5:72b-instruct-q8_0"):
        super().__init__(modele)
        self.api_url = "http://217.182.105.173:11434/api/generate"

        
        self.params: Dict[str, Any] = {
            "temperature": 0.8,
            "top_p": 0.9,
            "top_k": 40,
            "num_ctx": 2048,
            "repeat_penalty": 1.1,
            "repeat_last_n": 64,
            "mirostat": 0,
            "mirostat_eta": 0.1,
            "mirostat_tau": 5,
            "keep_alive": int(timedelta(minutes=5).total_seconds()),
            "num_predict": -1,
            "min_p": 0,
            "seed": 0,
            "stop": [],
            "stream": False
        }

    def urlBase(self) -> str:
        """
        Retourne l'URL de base de l'API Ollama.
        """
        return "http://217.182.105.173:11434/"

    def cleAPI(self) -> str:
        """
        Ollama ne nécessite pas de clé API par défaut.
        """
        return ""

    def urlFonction(self) -> str:
        """
        Retourne l'URL spécifique à Ollama pour générer une réponse.
        """
        return "api/generate"

    def _preparer_contenu(self, question: str) -> Dict[str, Any]:
        """
        Prépare le contenu de la requête spécifique pour Ollama.
        """
        contenu = {
            "model": self.modele,
            "prompt": question,
            "options": {
                "temperature": self.params["temperature"],
                "top_p": self.params["top_p"],
                "top_k": self.params["top_k"],
                "num_ctx": self.params["num_ctx"],
                "repeat_penalty": self.params["repeat_penalty"],
                "repeat_last_n": self.params["repeat_last_n"],
                "mirostat": self.params["mirostat"],
                "mirostat_eta": self.params["mirostat_eta"],
                "mirostat_tau": self.params["mirostat_tau"],
                "keep_alive": self.params["keep_alive"],
                "num_predict": self.params["num_predict"],
                "min_p": self.params["min_p"],
                "seed": self.params["seed"],
                "stop": self.params["stop"],
            },
            "stream": self.params["stream"]
        }
        return contenu

    def _traiter_reponse(self, reponse: requests.Response) -> str:
        """
        Traite et retourne la réponse fournie par Ollama.
        """
        data = reponse.json()
        return data.get("response", "")

    def configurer(self, **kwargs):
        """
        Mise à jour facile des paramètres spécifiques à Ollama.
        """
        for key, value in kwargs.items():
            if key in self.params:
                self.params[key] = value
