AutoGen / AG2
The classic autogen package (ConversableAgent, LLMConfig). The redesigned ag2 1.x API has no ConversableAgent; this page doesn't apply to it.
Connect
BASE is http://127.0.0.1:4010/v1 and KEY is your plugboard key. Install autogen[openai,anthropic].
BASE = os.environ.get("PLUGBOARD_BASE_URL", "http://127.0.0.1:4010/v1")
KEY = os.environ["PLUGBOARD_KEY"]
def llm_config(model: str = "fast", stream: bool = False) -> LLMConfig:
return LLMConfig(
{
"api_type": "openai",
"model": model,
"base_url": BASE,
"api_key": KEY,
"max_tokens": 1024,
"stream": stream,
}
)bot = ConversableAgent("bot", llm_config=cfg, human_input_mode="NEVER")
reply = bot.generate_reply(messages=[{"role": "user", "content": "Reply with exactly: pong"}])Pass the endpoint in the entry dict; keyword arguments to LLMConfig don't set it. llm_config(stream=True) streams. Here cfg is llm_config().
A tool round trip is two agents: the assistant registers the function for the model, the user agent executes it.
assistant = ConversableAgent(
"assistant",
llm_config=cfg,
human_input_mode="NEVER",
system_message="Answer weather questions by calling get_weather.",
)
user = ConversableAgent(
"user",
llm_config=False,
human_input_mode="NEVER",
default_auto_reply="",
is_termination_msg=lambda m: False,
)
assistant.register_for_llm(description="Get the current weather for a city.")(get_weather)
user.register_for_execution()(get_weather)
result = user.initiate_chat(
assistant,
message="What is the weather in Paris? Use the tool.",
max_turns=4,
silent=True,
)Anthropic route
With api_type: "anthropic" AutoGen calls the Messages API. Its Anthropic client adds /v1/messages itself, so base_url is the plugboard address without /v1. Chat and the tool round trip above run unchanged with cfg = anthropic_config().
def anthropic_config(model: str = "fast") -> LLMConfig:
return LLMConfig(
{
"api_type": "anthropic",
"model": model,
# The Anthropic SDK adds /v1/messages itself: pass the address without /v1.
"base_url": BASE.removesuffix("/v1"),
"api_key": KEY,
"max_tokens": 1024,
}
)The code is in examples/autogen/:
cd examples/autogen
PLUGBOARD_KEY=<app key> uv run chat.pyWhat works
Chat Completions and Messages, each with chat and function tools (register_for_llm and register_for_execution). Chat Completions also streams.
Limits
- autogen logs
Model fast is not found. The cost will be 0.for an alias. Add apricefield to the entry to silence it. - A tool round takes four turns (prompt, call, result, answer). Set
max_turnsto at least 4 and don't terminate on the first message. - The Anthropic client of AutoGen doesn't stream.