Spaces:
Configuration error
Configuration error
| # core/fable_engine.py | |
| """ | |
| Moteur Fable — itérations successives avec auto-amélioration. | |
| Production → Capture → Observation → Correction → Livraison | |
| """ | |
| import time | |
| import logging | |
| from typing import Dict, Any | |
| log = logging.getLogger("vortex.fable") | |
| class FableEngine: | |
| def __init__(self, llm_engine, memory, error_tree): | |
| self.llm = llm_engine | |
| self.memory = memory | |
| self.error_tree = error_tree | |
| async def run(self, task: str, max_iterations: int = 3) -> Dict[str, Any]: | |
| log.info(f"[Fable] Démarrage : {task[:80]}... (max {max_iterations} iter)") | |
| start_time = time.perf_counter() | |
| best_result = None | |
| best_score = 0.0 | |
| history = [] | |
| current_task = task | |
| for i in range(max_iterations): | |
| log.info(f"[Fable] Itération {i + 1}/{max_iterations}") | |
| try: | |
| resp = await self.llm.call( | |
| agent="fable", | |
| system="Tu es un expert en résolution de problèmes. Améliore ta réponse à chaque itération.", | |
| user=f"Tâche : {current_task}\nPropose une solution détaillée.", | |
| max_tokens=500, | |
| temperature=0.4 | |
| ) | |
| solution = resp.content if hasattr(resp, "content") else str(resp) | |
| except Exception as e: | |
| log.error(f"[Fable] Erreur itération {i + 1}: {e}") | |
| solution = f"[Erreur] {e}" | |
| try: | |
| self.error_tree.add_error(str(e), f"Fable iter={i+1} task={task[:100]}") | |
| except Exception: | |
| pass | |
| score = min(0.8, len(solution) / 1000) * 0.8 + 0.2 | |
| if "code" in solution.lower() or "api" in solution.lower(): | |
| score = min(0.95, score + 0.1) | |
| if score > best_score: | |
| best_score = score | |
| best_result = solution | |
| history.append({"iteration": i + 1, "solution": solution[:300], "score": round(score, 3)}) | |
| if score >= 0.85: | |
| log.info(f"[Fable] Seuil atteint à l'itération {i+1} ({score:.3f})") | |
| break | |
| current_task = ( | |
| f"{current_task}\nVoici une solution précédente :\n{solution[:500]}\n" | |
| f"Améliore-la en corrigeant les éventuelles faiblesses." | |
| ) | |
| elapsed = time.perf_counter() - start_time | |
| log.info(f"[Fable] Terminé en {elapsed:.2f}s, meilleur score {best_score:.3f}") | |
| try: | |
| from core.memory import MemoryTier | |
| self.memory.ingest( | |
| f"Fable: {task[:100]} → {best_score:.2f} ({len(history)} iter)", | |
| MemoryTier.EPISODIC, "fable", | |
| importance=best_score, confidence=best_score | |
| ) | |
| except Exception as e: | |
| log.warning(f"[Fable] Mémorisation échouée: {e}") | |
| return { | |
| "task": task, | |
| "method": "Fable (itératif)", | |
| "iterations": len(history), | |
| "elapsed_s": round(elapsed, 2), | |
| "best_score": round(best_score, 3), | |
| "best_result": best_result[:1500] if best_result else "Aucun résultat", | |
| "history": history | |
| } | |