feat: migliora gestione errori con suggerimenti contextuali
- Aggiunge config_manager a load_model_from_config() per usare config attive - Migliora error handling con tipo eccezione e suggerimenti - Aggiunge pyproject.toml e setup.py per installazione pip
This commit is contained in:
1 parent
4de50a1712
commit
b25aa8fbad
4 files changed
+140
-5
No files matched your search
@@ -54,18 +54,49 @@ import json
|
||||
def load_model_from_config():
|
||||
"""
|
||||
Load the LLM model based on configuration.
|
||||
Configuration can come from environment variables or a config file.
|
||||
For simplicity, we'll use environment variables.
|
||||
Priority: 1) config_manager active config, 2) environment variables
|
||||
|
||||
Expected environment variables:
|
||||
Expected environment variables (fallback):
|
||||
- OPENAI_API_KEY: For OpenAI models
|
||||
- OLLAMA_BASE_URL: For Ollama (e.g., https://ollama.solarpi.net)
|
||||
- MODEL_PROVIDER: One of "openai", "ollama", "openrouter", etc.
|
||||
- MODEL_NAME: The specific model name (e.g., "gpt-4o", "gemma2:9b")
|
||||
"""
|
||||
# Try to use config_manager first
|
||||
if CONFIG_AVAILABLE:
|
||||
active_name = config_manager.get_active_configuration()
|
||||
if active_name:
|
||||
config = config_manager.get_configuration(active_name)
|
||||
if config:
|
||||
provider = config.get("provider", "ollama").lower()
|
||||
model_name = config.get("model_name", "gemma2:9b")
|
||||
base_url = config.get("base_url", "http://localhost:11434")
|
||||
api_key = config.get("api_key")
|
||||
|
||||
print(f"🔗 Caricamento modello da config_manager: {active_name}")
|
||||
print(f" Provider: {provider}")
|
||||
print(f" Modello: {model_name}")
|
||||
print(f" URL: {base_url}")
|
||||
|
||||
if provider == "ollama":
|
||||
return init_chat_model(f"ollama:{model_name}", base_url=base_url)
|
||||
elif provider == "openai":
|
||||
return init_chat_model(f"openai:{model_name}", api_key=api_key)
|
||||
elif provider == "openrouter":
|
||||
return init_chat_model(
|
||||
f"openai:{model_name}",
|
||||
base_url=base_url,
|
||||
api_key=api_key
|
||||
)
|
||||
|
||||
# Fallback to environment variables
|
||||
provider = os.getenv("MODEL_PROVIDER", "openai").lower()
|
||||
model_name = os.getenv("MODEL_NAME", "gpt-4o")
|
||||
|
||||
print(f"⚠️ Usando variabili d'ambiente (nessuna config attiva)")
|
||||
print(f" Provider: {provider}")
|
||||
print(f" Modello: {model_name}")
|
||||
|
||||
# Configure based on provider
|
||||
if provider == "openai":
|
||||
# Uses OPENAI_API_KEY from environment
|
||||
@@ -999,7 +1030,21 @@ def main():
|
||||
ai_message = result["messages"][-1]
|
||||
print(f"\n🤖 LangCode: {ai_message.content}")
|
||||
except Exception as e:
|
||||
print(f"\n❌ Error: {str(e)}")
|
||||
error_msg = str(e)
|
||||
if not error_msg:
|
||||
error_msg = f"{type(e).__name__} (nessun messaggio)"
|
||||
print(f"\n❌ Errore: {error_msg}")
|
||||
|
||||
# Suggerimenti basati sul tipo di errore
|
||||
exc_type = type(e).__name__
|
||||
if "Connection" in exc_type or "Connect" in str(e):
|
||||
print("💡 Suggerimento: Verifica che il server Ollama sia raggiungibile.")
|
||||
print(f" URL: {os.getenv('OLLAMA_BASE_URL', 'http://localhost:11434')}")
|
||||
elif "Timeout" in exc_type or "timeout" in str(e).lower():
|
||||
print("💡 Suggerimento: Il server è lento. Prova con un modello più piccolo.")
|
||||
elif "model" in str(e).lower() or "Model" in exc_type:
|
||||
print("💡 Suggerimento: Il modello specificato potrebbe non esistere.")
|
||||
print(f" Modello: {os.getenv('MODEL_NAME', 'non impostato')}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to initialize agent: {str(e)}")
|
||||
|
||||
Reference in new issue
Block a user