feat: aggiunge OllamaToolAgent con tool calling

- Nuovo modulo ollama_agent.py con parsing delle risposte
- Supporta chiamate tool per provider senza bind_tools
- Tool built-in: execute, read_file, write_file, ls, search, system_info
- Il modello risponde con [TOOL_CALL] che noi parsiamo ed eseguiamo
- Integrato in main.py per uso automatico con Ollama
This commit is contained in:
opencode committed 2026-03-23 16:14:37 +01:00
1 parent 2077618f52
commit a309c7e27c
2 files changed
+462 -11

No files matched your search

+52 -11
View File
@@ -414,19 +414,52 @@ def create_langcode_agent(system_prompt: Optional[str] = None, use_simple_agent:
"""
Create and return an agent instance with configurable model.
For Ollama and other providers without full tool-calling support,
uses a simple chat agent instead of deepagents.
For Ollama uses custom OllamaToolAgent with tool calling support.
For other providers with full tool support uses deepagents.
"""
# Detect if we need simple agent (for Ollama without tool support)
if not use_simple_agent and CONFIG_AVAILABLE:
# Try to get config info for Ollama detection
is_ollama = False
if CONFIG_AVAILABLE:
active_name = config_manager.get_active_configuration()
if active_name:
config = config_manager.get_configuration(active_name)
if config and config.get("provider") == "ollama":
# Ollama doesn't fully support bind_tools in deepagents
# Always use simple agent for Ollama
print("⚠️ Using simple chat agent (Ollama detected)")
use_simple_agent = True
is_ollama = True
# Use OllamaToolAgent for Ollama with tool calling
if is_ollama and not use_simple_agent:
try:
from ollama_agent import create_ollama_tool_agent
model = load_model_from_config()
custom_tools = get_custom_tools()
# Enhance with memory and planning tools if available
if ENHANCEMENTS_AVAILABLE:
try:
memory_tools = create_simple_memory_tools(simple_memory)
custom_tools.extend(memory_tools)
except Exception:
pass
try:
planning_tools = enhanced_planner.create_planning_tools()
custom_tools.extend(planning_tools)
except Exception:
pass
print("🔧 Using OllamaToolAgent with tool calling")
return create_ollama_tool_agent(
model=model,
tools=custom_tools,
system_prompt=system_prompt,
include_builtin_tools=True
)
except Exception as e:
print(f"⚠️ Could not load OllamaToolAgent: {e}")
print("⚠️ Falling back to simple agent")
use_simple_agent = True
elif is_ollama:
print("⚠️ Using simple chat agent (Ollama detected)")
use_simple_agent = True
if use_simple_agent:
return create_simple_agent(system_prompt)
@@ -1099,9 +1132,17 @@ def main():
# Otherwise, treat it as a chat message
try:
result = agent.invoke({"messages": [HumanMessage(content=user_input)]})
# Extract the AI's response
ai_message = result["messages"][-1]
print(f"\n🤖 LangCode: {ai_message.content}")
# Extract the AI's response (works for both simple and tool agent)
if "response" in result:
# OllamaToolAgent returns a separate response field
response_text = result["response"]
else:
# Simple agent returns the last message
ai_message = result["messages"][-1]
response_text = ai_message.content if hasattr(ai_message, 'content') else str(ai_message)
print(f"\n🤖 LangCode: {response_text}")
except Exception as e:
error_msg = str(e)
if not error_msg: