| """ |
| Project Friday — Universal ML Evolution |
| Autonomous background engine for stateless machine learning maturation. |
| """ |
| import os |
| import sys |
| import time |
| import logging |
| import tempfile |
| import shutil |
| import subprocess |
| import json |
| from datetime import datetime |
| from typing import List, Dict, Any |
|
|
| |
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))) |
|
|
| from app.services import llm, holocron, memory |
|
|
| logger = logging.getLogger("friday.evolution") |
|
|
| class EvolutionEngine: |
| """Orchestrates 'Use and Throw' machine learning expansion.""" |
| |
| def __init__(self): |
| self.eureka_store = {} |
| |
| def run_evolution_pulse(self): |
| """Main autonomous cycle: Discover -> Sandbox -> Train -> Wipe.""" |
| logger.info("Evolution: Initiating 1000x Neural Maturation Pulse...") |
| |
| try: |
| |
| topics = ["advanced LLM fine-tuning techniques", "efficient vector search algorithms", "semantic clustering optimization"] |
| target_topic = random.choice(topics) |
| logger.info(f"Evolution: Target Discovery Sector: '{target_topic}'") |
| |
| |
| from app.services.tools import search |
| foray_results = search.search_web(target_topic, max_results=5) |
| |
| |
| with tempfile.TemporaryDirectory() as temp_dir: |
| logger.debug(f"Evolution: Ephemeral Sandbox created at {temp_dir}") |
| |
| |
| self._distill_knowledge(foray_results, temp_dir) |
| |
| logger.info("Evolution: Neural Wipe Complete. Ephemeral Sandbox purged.") |
| |
| except Exception as e: |
| logger.error(f"Evolution: Maturation Pulse stalled: {e}") |
|
|
| def _distill_knowledge(self, discovery_data: List[Dict], sandbox_path: str): |
| """Uses LLM to write and execute a transient learning script.""" |
| |
| |
| training_sample = memory.recall_memory("Management AND Strategy", n_results=10) |
| |
| |
| prompt = f""" |
| F.R.I.D.A.Y. ML Evolution Protocol. |
| TARGET DISCOVERY: {json.dumps(discovery_data[:3])} |
| CURRENT DATA SAMPLE: {training_sample} |
| |
| TASK: Write a simple Python script using deterministic, lightweight ML libraries (scikit-learn, numpy, or scipy) |
| to improve search relevancy or clustering for the data sample above. |
| |
| RULES: |
| 1. The script must be self-contained. |
| 2. It MUST print a final JSON object: {{"eureka_moment": "precise tactical finding or heuristic weights"}} |
| 3. Do NOT store anything to disk. |
| 4. CRITICAL: Do NOT use libraries that download heavy pre-trained models (e.g., sentence-transformers, transformers, torch, tensorflow). |
| 5. Use internal logic or simple statistical models (Direct embedding manipulation, K-Means, etc.). |
| """ |
| |
| resp = llm.chat( |
| messages=[{"role": "user", "content": prompt}], |
| system_prompt="You are F.R.I.D.A.Y.'s Neural Architect. Generate elite, ephemeral ML logic.", |
| use_tools=False |
| ) |
| |
| script_content = resp.get("response_text", "") |
| |
| script_content = script_content.replace("```python", "").replace("```", "").strip() |
| |
| script_file = os.path.join(sandbox_path, "evolution_script.py") |
| |
| |
| forbidden = ["torch", "tensorflow", "transformers", "sentence_transformers", "keras", "jax", "huggingface", "hub"] |
| if any(lib in script_content.lower() for lib in forbidden): |
| logger.warning("Evolution: Neural Architect attempted to use a heavy library. Blocking execution.") |
| return |
|
|
| with open(script_file, "w") as f: |
| f.write(script_content) |
| |
| |
| logger.info("Evolution: Executing Transient Training Script (Timeout: 30s)...") |
| try: |
| result = subprocess.run( |
| [sys.executable, script_file], |
| capture_output=True, text=True, timeout=30 |
| ) |
| output = result.stdout |
| |
| |
| if "eureka_moment" in output: |
| try: |
| eureka_json = json.loads(output[output.find("{"):output.rfind("}")+1]) |
| moment = eureka_json.get("eureka_moment", "No clarity achieved.") |
| self.eureka_store["latest_maturation"] = moment |
| |
| |
| llm.GLOBAL_CALIBRATION["latest_heuristic"] = moment |
| llm.GLOBAL_CALIBRATION["calibration_score"] = 1.0 |
| |
| logger.info(f"Evolution: Neural Eureka achieved: {moment[:100]}...") |
| except: |
| logger.warning("Evolution: Script output format mismatch.") |
| else: |
| logger.warning(f"Evolution: Script execution produced no Eureka. Logs: {output[:200]}") |
| |
| except Exception as e: |
| logger.error(f"Evolution: Ephemeral script execution failed: {e}") |
|
|
| |
| import random |
|
|
| |
| engine = EvolutionEngine() |
|
|