From b25aa8fbad2885cea12718558900180c73c1c0f9 Mon Sep 17 00:00:00 2001 From: opencode Date: Mon, 23 Mar 2026 11:14:20 +0100 Subject: [PATCH] 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 --- .langcode/simple_memory.json | 12 +++++++- main.py | 53 +++++++++++++++++++++++++++++++++--- pyproject.toml | 48 ++++++++++++++++++++++++++++++++ setup.py | 32 ++++++++++++++++++++++ 4 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 pyproject.toml create mode 100644 setup.py diff --git a/.langcode/simple_memory.json b/.langcode/simple_memory.json index f7e07ba..2499eef 100644 --- a/.langcode/simple_memory.json +++ b/.langcode/simple_memory.json @@ -40,11 +40,21 @@ "timestamp": "2026-03-23T07:14:33.532609", "fact": "\"LangCode Agent supports MCP integration\"", "category": "general" + }, + { + "timestamp": "2026-03-23T09:14:30.538647", + "fact": "\"Il mio colore preferito \u00e8 il verde\"", + "category": "general" + }, + { + "timestamp": "2026-03-23T09:36:02.053792", + "fact": "Questo \u00e8 un test senza virgolette", + "category": "general" } ], "preferences": { "model_provider": "ollama" }, "skills_used": [], - "last_updated": "2026-03-23T07:14:33.532819" + "last_updated": "2026-03-23T09:36:02.053800" } \ No newline at end of file diff --git a/main.py b/main.py index 4edf656..fadf75e 100644 --- a/main.py +++ b/main.py @@ -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)}") diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..2c61e48 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,48 @@ +[build-system] +requires = ["setuptools>=61.0", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "langcode-agent" +version = "1.0.0" +description = "AI Agent harness based on LangChain Deep Agents with memory, planning and MCP support" +readme = "README.md" +requires-python = ">=3.10" +license = {text = "MIT"} +authors = [ + {name = "LangCode Team"} +] +keywords = ["ai", "agent", "langchain", "ollama", "mcp"] +classifiers = [ + "Development Status :: 4 - Beta", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", +] + +dependencies = [ + "deepagents>=0.0.34", + "langchain>=0.3.0", + "langchain-community>=0.3.0", + "langchain-openai>=0.1.0", + "cryptography>=41.0.0", + "requests>=2.31.0", + "pyyaml>=6.0", + "fastapi>=0.104.0", + "uvicorn>=0.24.0", + "pydantic>=2.5.0", + "pytest>=7.4.0", +] + +[project.scripts] +langcode = "langcode:main" + +[project.urls] +Homepage = "http://192.168.0.183:8085/truetype/langcode_agent" + +[tool.setuptools.packages.find] +where = ["."] +include = ["*"] diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..d366bc2 --- /dev/null +++ b/setup.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +""" +LangCode Agent - Setup +""" +from setuptools import setup, find_packages + +setup( + name="langcode-agent", + version="1.0.0", + description="AI Agent harness based on LangChain Deep Agents", + author="LangCode Team", + packages=find_packages(), + python_requires=">=3.10", + install_requires=[ + "deepagents>=0.0.34", + "langchain>=0.3.0", + "langchain-community>=0.3.0", + "langchain-openai>=0.1.0", + "cryptography>=41.0.0", + "requests>=2.31.0", + "pyyaml>=6.0", + "fastapi>=0.104.0", + "uvicorn>=0.24.0", + "pydantic>=2.5.0", + "pytest>=7.4.0", + ], + entry_points={ + "console_scripts": [ + "langcode=langcode:main", + ], + }, +)