Skip to content

Implement Agent Optimizer interface and DSPy backend #6686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 17, 2025

This PR implements a unified agent optimization interface for AutoGen as requested in the issue, providing a clean API for optimizing agent prompts using various backends.

Overview

The implementation adds a new autogen_agentchat.optimize module that serves as a unified interface for agent optimization, with DSPy as the first optimization backend in autogen_ext.optimize.dspy.

Key Features

Unified Optimization Interface

from autogen_agentchat.optimize import compile

best_agent, report = compile(
    agent=my_autogen_agent,
    trainset=train_examples,
    metric=exact_match,
    backend="dspy",
    optimizer_name="MIPROv2",
    optimizer_kwargs=dict(max_steps=16)
)

Backend Registry System

  • Extensible architecture allowing new optimization backends to be easily added
  • Automatic registration when backend modules are imported
  • list_backends() function to discover available backends

DSPy Backend Implementation

  • Wraps AutoGen agents as DSPy modules for optimization
  • Makes system messages and tool descriptions learnable prompts
  • Handles AutoGen ↔ DSPy model client adaptation
  • Graceful error handling when DSPy is not installed

Package Structure

autogen_agentchat/
├─ optimize/
│   ├─ __init__.py        # Public API (compile / list_backends)
│   ├─ _backend.py        # Abstract base class & registry
│   └─ _utils.py          # AutoGen → DSPy conversion utilities

autogen_ext/
└─ optimize/
    └─ dspy.py            # DSPy backend implementation

Error Handling

The implementation includes robust error handling:

  • Clear error messages when DSPy is not installed
  • Validation of agent model clients
  • Proper exception handling for missing optimizers

Documentation & Examples

  • Complete example in examples/optimization_demo.py
  • Documentation in docs/optimization.md
  • Comprehensive test suite covering all functionality

Testing

The implementation includes:

  • Unit tests for the backend registry
  • Integration tests for the DSPy backend
  • Error handling validation
  • Interface compliance verification

Fixes #6685.


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

@ekzhu
Copy link
Collaborator

ekzhu commented Jun 17, 2025

@copilot Instead of having the util module inside the agentchat package, move it to the extension package for DSPy.

The agentchat package should not have dependency on DSPy.

Copilot AI and others added 2 commits June 17, 2025 02:39
Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Agent Optimizer interface and DSPy backend Implement Agent Optimizer interface and DSPy backend Jun 17, 2025
@Copilot Copilot AI requested a review from ekzhu June 17, 2025 02:48
Copilot finished work on behalf of ekzhu June 17, 2025 02:48
Move _utils.py containing DSPy-specific utilities from autogen_agentchat.optimize to autogen_ext.optimize to avoid DSPy dependency in the core agentchat package. Update import in dspy.py backend accordingly.

Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 17, 2025

@copilot Instead of having the util module inside the agentchat package, move it to the extension package for DSPy.

The agentchat package should not have dependency on DSPy.

Done! I've moved the _utils.py module from autogen_agentchat.optimize to autogen_ext.optimize and updated the import in dspy.py accordingly. The agentchat package no longer has any DSPy dependencies. Commit d1db7c8.

Copilot finished work on behalf of ekzhu June 17, 2025 02:55
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.

Agent Optimizer interface and DSPy backend
2 participants