Integration guide
AgentGen + Microsoft AutoGen
AutoGen agents can reason and collaborate, but their outputs are text. Integrate AgentGen as a registered function and any AutoGen agent can produce a shareable PDF or image — invoices, reports, certificates — and hand the download link back to the user.
Install
pip install pyautogen requests python-dotenv
Step 1 — Define the AgentGen functions
import os, requests
AGENTGEN_KEY = os.environ["AGENTGEN_API_KEY"]
def generate_pdf(html: str, page_size: str = "A4") -> dict:
"""
Render an HTML string to a PDF document using AgentGen.
Returns a dict with 'url' (CDN download link) and 'tokens_used'.
Cost: 2 tokens per page.
"""
r = requests.post(
"https://www.agent-gen.com/api/v1/generate/pdf",
headers={"X-API-Key": AGENTGEN_KEY, "Content-Type": "application/json"},
json={"html": html, "format": page_size},
timeout=30,
)
if r.status_code == 402:
return {"error": "Insufficient tokens. Top up at agent-gen.com/pricing."}
r.raise_for_status()
return r.json()
def generate_image(html: str, width: int = 1200, height: int = 630) -> dict:
"""
Render an HTML string to a PNG image using AgentGen.
Returns a dict with 'url' (CDN link). Cost: 1 token.
"""
r = requests.post(
"https://www.agent-gen.com/api/v1/generate/image",
headers={"X-API-Key": AGENTGEN_KEY, "Content-Type": "application/json"},
json={"html": html, "width": width, "height": height, "format": "png"},
timeout=30,
)
r.raise_for_status()
return r.json()Step 2 — Register with AutoGen agents
import autogen
config_list = [{"model": "gpt-4o", "api_key": os.environ["OPENAI_API_KEY"]}]
# The assistant agent that writes HTML and calls tools
assistant = autogen.AssistantAgent(
name="DocumentAgent",
system_message=(
"You are a document generation specialist. When asked to create a PDF or image, "
"write complete, styled HTML with inline CSS, then call generate_pdf or generate_image. "
"Always share the returned URL with the user."
),
llm_config={
"config_list": config_list,
"functions": [
{
"name": "generate_pdf",
"description": "Render HTML to a PDF. Returns a CDN download URL. Cost: 2 tokens/page.",
"parameters": {
"type": "object",
"properties": {
"html": {"type": "string", "description": "Complete HTML to render"},
"page_size": {"type": "string", "enum": ["A4", "Letter"], "default": "A4"},
},
"required": ["html"],
},
},
{
"name": "generate_image",
"description": "Render HTML to a PNG image. Returns a CDN URL. Cost: 1 token.",
"parameters": {
"type": "object",
"properties": {
"html": {"type": "string"},
"width": {"type": "integer", "default": 1200},
"height": {"type": "integer", "default": 630},
},
"required": ["html"],
},
},
],
},
)
# The user proxy that executes function calls
user_proxy = autogen.UserProxyAgent(
name="UserProxy",
human_input_mode="NEVER",
max_consecutive_auto_reply=5,
function_map={
"generate_pdf": generate_pdf,
"generate_image": generate_image,
},
)Step 3 — Run the agent
user_proxy.initiate_chat(
assistant,
message=(
"Create a PDF invoice for Acme Corp. "
"Services: API integration consulting — $4,500. "
"Due: April 15, 2026."
),
)
# DocumentAgent writes HTML, calls generate_pdf, returns:
# → "Your invoice is ready: https://cdn.agent-gen.com/output/abc123.pdf"Multi-agent workflow
AgentGen works equally well in multi-agent AutoGen setups. A DataAgent can fetch and summarise metrics while a DocumentAgent renders the final report PDF — with the function registered on the executor proxy shared between both:
groupchat = autogen.GroupChat(
agents=[data_agent, document_agent, user_proxy],
messages=[],
max_round=10,
)
manager = autogen.GroupChatManager(groupchat=groupchat, llm_config={"config_list": config_list})
user_proxy.initiate_chat(
manager,
message="Analyse last week's sales data and generate a PDF summary report.",
)Give your AutoGen agents a download link
50 free tokens on signup — no credit card required.