Download src/config.py from xbizot/saearch: direct link, hf CLI and curl.
- Browser
- Download file 8.16 kB
-
https://huggingface.co/spaces/xbizot/saearch/resolve/main/src/config.py
- Command line
-
hf download hf://spaces/xbizot/saearch/src/config.py
-
curl -L -o config.py https://huggingface.co/spaces/xbizot/saearch/resolve/main/src/config.py
8.16 kB
| """ | |
| Configuration centralisΓ©e du projet RAG Catastrophes Climatiques. | |
| Hyperparamètres, spécialisation des LLM par agent, monitoring LLMOps. | |
| """ | |
| import os | |
| from langchain_anthropic import ChatAnthropic | |
| # ββ ModΓ¨les Anthropic disponibles βββββββββββββββββββββββββββββββββββββββββ | |
| MODEL_HAIKU = "claude-haiku-4-5-20251001" | |
| MODEL_SONNET = "claude-sonnet-4-20250514" | |
| MODEL_OPUS = "claude-opus-4-20250514" | |
| # ββ Tarifs Anthropic ($ / 1M tokens) pour estimation de coΓ»t ββββββββββββββ | |
| MODEL_PRICING = { | |
| MODEL_HAIKU: {"input": 0.25, "output": 1.25}, | |
| MODEL_SONNET: {"input": 3.00, "output": 15.00}, | |
| MODEL_OPUS: {"input": 15.00, "output": 75.00}, | |
| } | |
| # ββ Configuration par type d'agent ββββββββββββββββββββββββββββββββββββββββ | |
| AGENT_CONFIGS = { | |
| "orchestrator": { | |
| "model": MODEL_SONNET, | |
| "temperature": 0, | |
| "max_tokens": 2048, | |
| "description": "Orchestre les outils et génère la réponse finale", | |
| }, | |
| "rag": { | |
| "model": MODEL_SONNET, | |
| "temperature": 0.2, | |
| "max_tokens": 2048, | |
| "description": "RΓ©pond en citant les sources du corpus climatique", | |
| }, | |
| "meteo": { | |
| "model": MODEL_HAIKU, | |
| "temperature": 0, | |
| "max_tokens": 1024, | |
| "description": "Interprète les données météo (actuel/historique/prévisions)", | |
| }, | |
| "web": { | |
| "model": MODEL_HAIKU, | |
| "temperature": 0.2, | |
| "max_tokens": 1024, | |
| "description": "SynthΓ©tise les actualitΓ©s issues de la recherche web", | |
| }, | |
| "analyst": { | |
| "model": MODEL_OPUS, | |
| "temperature": 0.5, | |
| "max_tokens": 4096, | |
| "description": "Croise les donnΓ©es pour produire l'analyse de risque", | |
| }, | |
| "chat": { | |
| "model": MODEL_HAIKU, | |
| "temperature": 0.7, | |
| "max_tokens": 1024, | |
| "description": "Conversation simple, salutations, hors-sujet", | |
| }, | |
| } | |
| # ββ ModΓ¨le de fallback ββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| FALLBACK_MODEL = MODEL_HAIKU | |
| # ββ ParamΓ¨tres du chunking ββββββββββββββββββββββββββββββββββββββββββββββββ | |
| CHUNK_SIZE = 1500 | |
| CHUNK_OVERLAP = 150 | |
| # ββ ParamΓ¨tres des embeddings βββββββββββββββββββββββββββββββββββββββββββββ | |
| EMBEDDING_MODEL = "paraphrase-multilingual-MiniLM-L12-v2" | |
| FAISS_STORE_PATH = "faiss_store" | |
| # ββ ParamΓ¨tres du retriever βββββββββββββββββββββββββββββββββββββββββββββββ | |
| RETRIEVER_K = 12 | |
| RETRIEVER_FETCH_K = 40 | |
| RETRIEVER_SEARCH_TYPE = "mmr" | |
| RETRIEVER_LAMBDA = 0.7 # privilegie pertinence (1.0) vs diversite (0.0) | |
| # ββ Recherche hybride BM25 + Dense ββββββββββββββββββββββββββββββββββββββββ | |
| BM25_WEIGHT = 0.5 | |
| DENSE_WEIGHT = 0.5 | |
| # ββ MΓ©moire conversationnelle βββββββββββββββββββββββββββββββββββββββββββββ | |
| MEMORY_WINDOW_SIZE = 20 | |
| # ββ Monitoring LLMOps βββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| TOKEN_TRACKING = True | |
| class TokenCounter: | |
| """Compteur de tokens par agent, par session, avec estimation de coΓ»t.""" | |
| def __init__(self): | |
| self.total_input = 0 | |
| self.total_output = 0 | |
| self.calls_by_agent = {} | |
| self.tokens_by_agent = {} | |
| def log(self, agent_type: str, response): | |
| usage = response.usage_metadata | |
| input_tokens = usage["input_tokens"] | |
| output_tokens = usage["output_tokens"] | |
| self.total_input += input_tokens | |
| self.total_output += output_tokens | |
| self.calls_by_agent[agent_type] = self.calls_by_agent.get(agent_type, 0) + 1 | |
| if agent_type not in self.tokens_by_agent: | |
| self.tokens_by_agent[agent_type] = {"input": 0, "output": 0} | |
| self.tokens_by_agent[agent_type]["input"] += input_tokens | |
| self.tokens_by_agent[agent_type]["output"] += output_tokens | |
| def estimate_cost(self) -> float: | |
| """Estime le coΓ»t total en $ basΓ© sur les tarifs Anthropic.""" | |
| total_cost = 0.0 | |
| for agent_type, tokens in self.tokens_by_agent.items(): | |
| model = AGENT_CONFIGS.get(agent_type, {}).get("model", MODEL_HAIKU) | |
| pricing = MODEL_PRICING.get(model, MODEL_PRICING[MODEL_HAIKU]) | |
| cost_input = (tokens["input"] / 1_000_000) * pricing["input"] | |
| cost_output = (tokens["output"] / 1_000_000) * pricing["output"] | |
| total_cost += cost_input + cost_output | |
| return total_cost | |
| def summary(self) -> dict: | |
| return { | |
| "total_input_tokens": self.total_input, | |
| "total_output_tokens": self.total_output, | |
| "total_tokens": self.total_input + self.total_output, | |
| "calls_by_agent": self.calls_by_agent, | |
| "tokens_by_agent": self.tokens_by_agent, | |
| "estimated_cost_usd": self.estimate_cost(), | |
| } | |
| def reset(self): | |
| self.total_input = 0 | |
| self.total_output = 0 | |
| self.calls_by_agent = {} | |
| self.tokens_by_agent = {} | |
| # ββ API mΓ©tΓ©o OpenMeteo βββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| OPENMETEO_BASE_URL = "https://api.open-meteo.com/v1" | |
| OPENMETEO_GEOCODING_URL = "https://geocoding-api.open-meteo.com/v1/search" | |
| METEO_FORECAST_DAYS = 7 | |
| # ββ Chainlit ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| CHAINLIT_HOST = "0.0.0.0" | |
| CHAINLIT_PORT = 8000 | |
| # ββ Construction du LLM βββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def get_llm(agent_type: str) -> ChatAnthropic: | |
| """Retourne un LLM configurΓ© pour un type d'agent donnΓ©.""" | |
| if agent_type not in AGENT_CONFIGS: | |
| raise ValueError( | |
| f"Type d'agent inconnu : '{agent_type}'. " | |
| f"Disponibles : {list(AGENT_CONFIGS.keys())}" | |
| ) | |
| if not os.getenv("ANTHROPIC_API_KEY"): | |
| raise EnvironmentError( | |
| "ANTHROPIC_API_KEY manquante. Ajoutez-la dans votre fichier .env" | |
| ) | |
| config = AGENT_CONFIGS[agent_type] | |
| return ChatAnthropic( | |
| model=config["model"], | |
| temperature=config["temperature"], | |
| max_tokens=config["max_tokens"], | |
| ) | |
| def get_fallback_llm() -> ChatAnthropic: | |
| """LLM de secours Anthropic (Haiku) en cas d'échec du modèle principal.""" | |
| return ChatAnthropic( | |
| model=FALLBACK_MODEL, | |
| temperature=0.2, | |
| max_tokens=1024, | |
| ) | |
| def get_ollama_fallback(): | |
| """ | |
| LLM de secours local via Ollama (open source, gratuit, hors ligne). | |
| Nécessite Ollama installé + modèle téléchargé : ollama pull mistral | |
| UtilisΓ© quand l'API Anthropic est indisponible. | |
| """ | |
| try: | |
| import langchain_community.llms as community_llms | |
| ollama_cls = getattr(community_llms, "Ollama") | |
| return ollama_cls( | |
| model="mistral", | |
| temperature=0.2, | |
| num_predict=1024, | |
| ) | |
| except (ImportError, AttributeError) as exc: | |
| raise EnvironmentError( | |
| "Ollama non disponible. Installez langchain-community et Ollama : " | |
| "pip install langchain-community && curl -fsSL https://ollama.com/install.sh | sh && ollama pull mistral" | |
| ) from exc | |