Spaces:
Configuration error
Configuration error
Upload stratego\web\config\agent_builder.py with huggingface_hub
Browse files
stratego//web//config//agent_builder.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Agent factory for web UI"""
|
| 2 |
+
|
| 3 |
+
from typing import Optional, Tuple
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class AgentBuilderError(Exception):
|
| 8 |
+
"""Custom error for agent building"""
|
| 9 |
+
pass
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def build_agent(backend: str, model: str, prompt: str = "base") -> Tuple[Optional[object], Optional[str]]:
|
| 13 |
+
"""
|
| 14 |
+
Build an AI agent for the web UI. Currently supports Ollama.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
backend: "ollama" (for MVP)
|
| 18 |
+
model: Model name (e.g., "mistral:7b")
|
| 19 |
+
prompt: Prompt preset (e.g., "base", "concise", "adaptive")
|
| 20 |
+
|
| 21 |
+
Returns:
|
| 22 |
+
(agent, error_message) - agent is None if error, error_message is None if success
|
| 23 |
+
"""
|
| 24 |
+
try:
|
| 25 |
+
if backend == "ollama":
|
| 26 |
+
from stratego.models.ollama_model import OllamaAgent
|
| 27 |
+
agent = OllamaAgent(model_name=model, prompt_name=prompt)
|
| 28 |
+
return agent, None
|
| 29 |
+
|
| 30 |
+
else:
|
| 31 |
+
return None, f"Unknown backend: {backend}. Currently only 'ollama' is supported."
|
| 32 |
+
|
| 33 |
+
except ImportError as e:
|
| 34 |
+
error_msg = f"Backend '{backend}' not installed: {str(e)}"
|
| 35 |
+
return None, error_msg
|
| 36 |
+
except Exception as e:
|
| 37 |
+
error_msg = f"Failed to build agent: {str(e)}"
|
| 38 |
+
return None, error_msg
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def build_mock_agent() -> object:
|
| 42 |
+
"""
|
| 43 |
+
Build a mock agent for testing (always returns first legal move).
|
| 44 |
+
Useful for testing UI without real LLM.
|
| 45 |
+
"""
|
| 46 |
+
class MockAgent:
|
| 47 |
+
def __init__(self):
|
| 48 |
+
self.model_name = "mock"
|
| 49 |
+
|
| 50 |
+
def __call__(self, observation: str) -> str:
|
| 51 |
+
"""Extract and return first legal move from observation"""
|
| 52 |
+
from stratego.utils.parsing import extract_legal_moves
|
| 53 |
+
|
| 54 |
+
legal_moves = extract_legal_moves(observation)
|
| 55 |
+
if legal_moves:
|
| 56 |
+
return legal_moves[0]
|
| 57 |
+
return "[A0 B0]" # Fallback
|
| 58 |
+
|
| 59 |
+
return MockAgent()
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def validate_agent_config(backend: str, model: str, prompt: str) -> Optional[str]:
|
| 63 |
+
"""
|
| 64 |
+
Validate agent configuration without building the full agent.
|
| 65 |
+
Returns error message if invalid, None if valid.
|
| 66 |
+
"""
|
| 67 |
+
if not backend:
|
| 68 |
+
return "Backend is required"
|
| 69 |
+
if not model:
|
| 70 |
+
return "Model name is required"
|
| 71 |
+
if not prompt:
|
| 72 |
+
return "Prompt preset is required"
|
| 73 |
+
|
| 74 |
+
if backend != "ollama":
|
| 75 |
+
return f"Only 'ollama' backend is currently supported. Got: {backend}"
|
| 76 |
+
|
| 77 |
+
if prompt not in ["base", "concise", "adaptive"]:
|
| 78 |
+
return f"Unknown prompt: {prompt}"
|
| 79 |
+
|
| 80 |
+
return None
|