Skip to main content

Overview

The AutoGen.OpenAI package provides seamless integration with OpenAI’s GPT models, including GPT-4, GPT-4 Turbo, GPT-3.5, and Azure OpenAI Service.

Installation

OpenAI Setup

Connect to OpenAI’s API:
1

Get your API key

Obtain an API key from OpenAI Platform.
2

Set environment variable

3

Create an agent

OpenAIChatAgent

The main agent class for OpenAI models:

Constructor Parameters

ChatClient
required
OpenAI ChatClient instance for the specific model
string
required
Unique identifier for the agent
string
default:"You are a helpful assistant"
Instructions defining the agent’s behavior
int?
Random seed for deterministic outputs (when supported by model)
float
default:"0.7"
Sampling temperature (0.0 = deterministic, 2.0 = very creative)
int
default:"1024"
Maximum tokens to generate in response
ChatResponseFormat?
Response format (e.g., JSON mode)

Available Models

Azure OpenAI

Connect to models deployed on Azure:
1

Set up credentials

2

Create Azure OpenAI agent

Streaming Responses

Stream responses token-by-token for real-time output:

JSON Mode

Force responses in JSON format:

Structured Output

Use JSON schema for strongly-typed responses:

Function Calling

Combine with AutoGen’s function calling:

Vision (GPT-4 Vision)

Process images with GPT-4 Vision models:

Message Connector

The message connector converts between AutoGen and OpenAI message formats:

Configuration Options

ConversableAgentConfig (Legacy)

For agents using the older configuration style:

Connecting to Ollama

Use OpenAI-compatible endpoints:

Best Practices

  • GPT-4o / GPT-4 Turbo: Best for complex reasoning, function calling
  • GPT-4o Mini: Fast, cost-effective for simple tasks
  • GPT-3.5 Turbo: Budget-friendly for high-volume applications
  • O1 Models: Advanced reasoning for complex problems
  • Reuse OpenAIClient instances
  • Use streaming for long responses
  • Set appropriate maxTokens to control costs
  • Cache responses when possible
  • Use seed parameter for reproducible outputs

Environment Variables

string
required
Your OpenAI API key from platform.openai.com
string
Your Azure OpenAI API key
string
Your Azure OpenAI endpoint URL
string
Your Azure OpenAI deployment name

Next Steps

Anthropic

Use Claude models with AutoGen

Function Calling

Add tools to OpenAI agents

Group Chat

Create multi-agent workflows

Examples

See complete examples