Skip to content

🧠 Implement Multi-Step Function Calling Agent for Complex Reasoning Chains#8

Draft
Copilot wants to merge 7 commits into
mainfrom
copilot/fix-7
Draft

🧠 Implement Multi-Step Function Calling Agent for Complex Reasoning Chains#8
Copilot wants to merge 7 commits into
mainfrom
copilot/fix-7

Conversation

Copilot AI commented Jul 13, 2025

Copy link
Copy Markdown

This PR implements a comprehensive multi-step function calling agent that enables the LLM to reason through complex problems by chaining multiple function calls together, as detailed in issue #7.

🚀 Key Features Added

Multi-Step Agent Architecture

  • AgentContext: Maintains conversation history and function results across reasoning steps
  • Agent: Multi-step orchestrator that iteratively calls functions based on LLM decisions
  • Enhanced Response Handling: Supports both function_call and final_answer actions
  • Result Chaining: Functions can use outputs from previous function executions
  • Safety Controls: Maximum step limits prevent infinite loops

Example Multi-Step Workflow

// Create multi-step agent
agent := gopilot.NewAgent(gp, 5) // max 5 steps

// Multi-step execution
result, err := agent.ExecuteMultiStep("Based on today's weather, what should I wear in Istanbul?")

// Agent reasoning flow:
// Step 1: get-weather("Istanbul") → {temp: 12°C, cloudy, humid}
// Step 2: suggest-clothes(weatherData) → {light jacket, pants, umbrella}  
// Step 3: final_answer("Based on 12°C cloudy weather...")

Enhanced System Prompting

  • New agentSystemPrompt optimized for multi-step reasoning
  • Context-aware prompt building with conversation history
  • Support for both single-step and multi-step scenarios

🧩 Example Functions & Demo

Weather + Clothing Recommendation Chain

// Register example functions
weatherFn := gopilot.CreateWeatherFunction()    // Gets weather data
clothingFn := gopilot.CreateClothingFunction()  // Suggests clothes based on weather

gp.FunctionRegister(weatherFn)
gp.FunctionRegister(clothingFn)

// Agent automatically chains: weather → clothing → final answer

Interactive Demo

  • Complete CLI demo application (examples/demo/)
  • Works with both real Gemini API and mock LLM for testing
  • Shows step-by-step reasoning process

🧪 Testing & Development Tools

Comprehensive Test Suite

  • Single-step execution test
  • Multi-step execution test
  • Maximum steps limit enforcement test
  • Mock LLM client for deterministic testing

Public MockLLMClient

// Easy testing of multi-step scenarios
mockLLM := gopilot.NewMockLLMClient([]string{
    `{"action": "function_call", "agent": "get-weather", "parameters": {"city": "Istanbul"}}`,
    `{"action": "final_answer", "answer": "Weather is 12°C and cloudy..."}`,
})

📚 Documentation Updates

  • Updated README with multi-step usage examples
  • Clear distinction between single-step and multi-step patterns
  • Complete working examples for both approaches
  • Interactive demo instructions

🔧 Implementation Details

Minimal & Surgical Changes

  • Builds on existing function registry and LLM integration
  • Maintains full backward compatibility
  • No breaking changes to existing APIs
  • Added only necessary components for multi-step functionality

Key Components

  1. agent.go - Core multi-step agent implementation
  2. systemPrompt.go - Enhanced prompting for multi-step reasoning
  3. examples.go - Demo functions (GetWeather, SuggestClothes)
  4. mock.go - Public mock LLM client for testing
  5. agent_test.go - Comprehensive test suite

Context Management

  • Conversation history tracking
  • Function result storage and retrieval
  • Contextual prompt building with previous results
  • Step counting and limit enforcement

✅ Verification

  • ✅ All existing tests pass (backward compatibility maintained)
  • ✅ New multi-step tests pass (3 comprehensive test scenarios)
  • ✅ Demo application works with mock LLM
  • ✅ Code builds without errors
  • ✅ Documentation updated with examples

This implementation provides a solid foundation for complex multi-step reasoning while maintaining the simplicity and reliability of the existing single-step functionality.

Fixes #7.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 5 commits July 13, 2025 16:21
Co-authored-by: SadikSunbul <109942759+SadikSunbul@users.noreply.github.com>
…d tests

Co-authored-by: SadikSunbul <109942759+SadikSunbul@users.noreply.github.com>
Co-authored-by: SadikSunbul <109942759+SadikSunbul@users.noreply.github.com>
…p code

Co-authored-by: SadikSunbul <109942759+SadikSunbul@users.noreply.github.com>
…hains

Co-authored-by: SadikSunbul <109942759+SadikSunbul@users.noreply.github.com>
Copilot AI changed the title [WIP] 🧠 Multi-Step Function Calling Agent — Detailed Design 🧠 Implement Multi-Step Function Calling Agent for Complex Reasoning Chains Jul 13, 2025
Copilot AI requested a review from SadikSunbul July 13, 2025 16:33
@SadikSunbul

Copy link
Copy Markdown
Owner

@copilot , Can you explain the changes you made with examples? You added a simple usage. Give me a simulation example.

…ti-step agent

Co-authored-by: SadikSunbul <109942759+SadikSunbul@users.noreply.github.com>

Copilot AI commented Jul 13, 2025

Copy link
Copy Markdown
Author

@copilot , Can you explain the changes you made with examples? You added a simple usage. Give me a simulation example.

I've added comprehensive documentation and simulation examples to explain the multi-step function calling agent implementation. Here's what I built:

🚀 Multi-Step Agent Overview

Key Innovation: The agent can automatically chain function calls together, using results from previous functions as inputs for subsequent ones.

📋 Step-by-Step Simulation Example

// User Request: "Based on today's weather, what should I wear in Istanbul?"

// Agent's Reasoning Process:
Step 1: Agent decides"I need weather data first"
        Calls: get-weather("Istanbul") 
        Result: {temp: 12°C, condition: "cloudy", humidity: 75%}

Step 2: Agent decides"Now I can suggest clothes using this weather data"  
        Calls: suggest-clothes(weather_data_from_step_1)
        Result: {recommendation: "Dress in layers", items: ["light jacket", "pants"]}

Step 3: Agent decides"I have enough information for final answer"
        Action: final_answer("Based on 12°C cloudy weather in Istanbul...")

🔧 Architecture Components

  • Agent: Orchestrates multi-step reasoning with max step limits
  • AgentContext: Stores conversation history and function results
  • Enhanced Prompting: LLM instructions for intelligent function chaining
  • MockLLMClient: Deterministic testing with predefined responses

💻 Live Simulation

I've created /examples/simulation_example.go that shows the complete step-by-step process with detailed output. You can also run the interactive demo at /examples/demo/main.go.

New files added:

  • IMPLEMENTATION_GUIDE.md - Complete usage guide with examples
  • examples/simulation_example.go - Detailed step-by-step simulation

The agent transforms complex natural language requests into intelligent function call chains automatically!

Commit: 93e27cb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🧠 Multi-Step Function Calling Agent — Detailed Design

2 participants