From 85e27ec74c4f680db8e2c5c06435f7502c6d9cf1 Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Sun, 5 Oct 2025 21:54:04 +0200 Subject: [PATCH] feat: add agent options to model config (#6383) Signed-off-by: Ettore Di Giacinto --- core/config/model_config.go | 10 +++++++- core/http/endpoints/openai/mcp.go | 34 +++++++++++++++++++------- docs/content/docs/features/mcp.md | 40 +++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 10 deletions(-) diff --git a/core/config/model_config.go b/core/config/model_config.go index 84edd0fde..e372febb7 100644 --- a/core/config/model_config.go +++ b/core/config/model_config.go @@ -74,7 +74,8 @@ type ModelConfig struct { Options []string `yaml:"options" json:"options"` Overrides []string `yaml:"overrides" json:"overrides"` - MCP MCPConfig `yaml:"mcp" json:"mcp"` + MCP MCPConfig `yaml:"mcp" json:"mcp"` + Agent AgentConfig `yaml:"agent" json:"agent"` } type MCPConfig struct { @@ -82,6 +83,13 @@ type MCPConfig struct { Stdio string `yaml:"stdio" json:"stdio"` } +type AgentConfig struct { + MaxAttempts int `yaml:"max_attempts" json:"max_attempts"` + MaxIterations int `yaml:"max_iterations" json:"max_iterations"` + EnableReasoning bool `yaml:"enable_reasoning" json:"enable_reasoning"` + EnableReEvaluation bool `yaml:"enable_re_evaluation" json:"enable_re_evaluation"` +} + func (c *MCPConfig) MCPConfigFromYAML() (MCPGenericConfig[MCPRemoteServers], MCPGenericConfig[MCPSTDIOServers]) { var remote MCPGenericConfig[MCPRemoteServers] var stdio MCPGenericConfig[MCPSTDIOServers] diff --git a/core/http/endpoints/openai/mcp.go b/core/http/endpoints/openai/mcp.go index ee8e3a11c..11897ae55 100644 --- a/core/http/endpoints/openai/mcp.go +++ b/core/http/endpoints/openai/mcp.go @@ -95,19 +95,35 @@ func MCPCompletionEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, // we satisfy to just call internally ComputeChoices defaultLLM := cogito.NewOpenAILLM(config.Name, apiKey, "http://127.0.0.1:"+port) - f, err := cogito.ExecuteTools( - defaultLLM, fragment, + cogitoOpts := []cogito.Option{ cogito.WithStatusCallback(func(s string) { log.Debug().Msgf("[model agent] [model: %s] Status: %s", config.Name, s) }), cogito.WithContext(ctx), - // TODO: move these to configs - cogito.EnableToolReEvaluator, - cogito.WithIterations(3), - cogito.WithMaxAttempts(3), - cogito.WithTools( - cogitoTools..., - ), + cogito.WithTools(cogitoTools...), + cogito.WithIterations(3), // default to 3 iterations + cogito.WithMaxAttempts(3), // default to 3 attempts + } + + if config.Agent.EnableReasoning { + cogitoOpts = append(cogitoOpts, cogito.EnableToolReasoner) + } + + if config.Agent.EnableReEvaluation { + cogitoOpts = append(cogitoOpts, cogito.EnableToolReEvaluator) + } + + if config.Agent.MaxIterations != 0 { + cogitoOpts = append(cogitoOpts, cogito.WithIterations(config.Agent.MaxIterations)) + } + + if config.Agent.MaxAttempts != 0 { + cogitoOpts = append(cogitoOpts, cogito.WithMaxAttempts(config.Agent.MaxAttempts)) + } + + f, err := cogito.ExecuteTools( + defaultLLM, fragment, + cogitoOpts..., ) if err != nil && !errors.Is(err, cogito.ErrNoToolSelected) { return err diff --git a/docs/content/docs/features/mcp.md b/docs/content/docs/features/mcp.md index 339c29c0a..5963aa092 100644 --- a/docs/content/docs/features/mcp.md +++ b/docs/content/docs/features/mcp.md @@ -27,6 +27,8 @@ The Model Context Protocol is a standard for connecting AI models to external to - **⚡ Cached Connections**: Efficient tool caching for better performance - **🔒 Secure Authentication**: Support for bearer token authentication - **🎯 OpenAI Compatible**: Uses the familiar `/mcp/v1/chat/completions` endpoint +- **🧠 Advanced Reasoning**: Configurable reasoning and re-evaluation capabilities +- **⚙️ Flexible Agent Control**: Customizable execution limits and retry behavior ## Configuration @@ -73,6 +75,13 @@ mcp: } } } + +# Agent Configuration +agent: + max_attempts: 3 # Maximum number of tool execution attempts + max_iterations: 3 # Maximum number of reasoning iterations + enable_reasoning: true # Enable tool reasoning capabilities + enable_re_evaluation: false # Enable tool re-evaluation ``` ### Configuration Options @@ -90,6 +99,14 @@ Configure local command-based MCP servers: - **`args`**: Array of command-line arguments - **`env`**: Environment variables (optional) +#### Agent Configuration (`agent`) +Configure agent behavior and tool execution: + +- **`max_attempts`**: Maximum number of tool execution attempts (default: 3) +- **`max_iterations`**: Maximum number of reasoning iterations (default: 3) +- **`enable_reasoning`**: Enable tool reasoning capabilities (default: false) +- **`enable_re_evaluation`**: Enable tool re-evaluation (default: false) + ## Usage ### API Endpoint @@ -148,8 +165,31 @@ mcp: } } } + +agent: + max_attempts: 5 + max_iterations: 5 + enable_reasoning: true + enable_re_evaluation: true ``` +## Agent Configuration Details + +The `agent` section controls how the AI model interacts with MCP tools: + +### Execution Control +- **`max_attempts`**: Limits how many times a tool can be retried if it fails. Higher values provide more resilience but may increase response time. +- **`max_iterations`**: Controls the maximum number of reasoning cycles the agent can perform. More iterations allow for complex multi-step problem solving. + +### Reasoning Capabilities +- **`enable_reasoning`**: When enabled, the agent uses advanced reasoning to better understand tool results and plan next steps. +- **`enable_re_evaluation`**: When enabled, the agent can re-evaluate previous tool results and decisions, allowing for self-correction and improved accuracy. + +### Recommended Settings +- **Simple tasks**: `max_attempts: 2`, `max_iterations: 2`, `enable_reasoning: false` +- **Complex tasks**: `max_attempts: 5`, `max_iterations: 5`, `enable_reasoning: true`, `enable_re_evaluation: true` +- **Development/Debugging**: `max_attempts: 1`, `max_iterations: 1`, `enable_reasoning: true`, `enable_re_evaluation: true` + ## How It Works 1. **Tool Discovery**: LocalAI connects to configured MCP servers and discovers available tools