Convert AutoGen agents to Agent Spec#

This usage example showcases how an AutoGen Agent can be converted into an Agent Spec configuration in JSON format.

# Create an AutoGen Agent
import os
os.environ["OPENAI_API_KEY"] = "YOUR_API_KEY"
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient

async def add_tool(a: int, b: int) -> int:
    """Adds a to b and returns the result"""
    return a + b

autogen_tools = {"add_tool": add_tool}

model_client = OpenAIChatCompletionClient(
    model="gpt-4.1",
)

autogen_agent = AssistantAgent(
    name="assistant",
    model_client=model_client,
    tools=list(autogen_tools.values()),
    system_message="Use tools to solve tasks, and reformulate the answers that you get.",
    reflect_on_tool_use=True,
)

# Convert to Agent Spec
from pyagentspec.adapters.autogen import AgentSpecExporter

agentspec_config = AgentSpecExporter().to_json(autogen_agent)