OpenAI’s API and models are fully supported in AG2. You will need an OpenAI account and to create an API key. See their website for further details.

Installation

If you are using a version before 0.8 the OpenAI package will already be installed and ready to use.

If you are installing a newer version of AG2, you need to install the AG2 package with the OpenAI extra.

pip install ag2[openai]

If you have been using autogen or pyautogen, all you need to do is upgrade it using:

pip install -U autogen[openai]

or

pip install -U pyautogen[openai]

as pyautogen, autogen, and ag2 are aliases for the same PyPI package.

LLM Configurations

See the LLM Configuration for further details on setting an creating an LLM configuration.

The below example of an OAI_CONFIG_LIST shows how the OpenAI client class is used by specifying the api_type as openai.

[
    {
        "api_type": "openai",
        "model": "o1",
        "api_key": "your OpenAI Key goes here",
    },
    {
        "api_type": "openai",
        "model": "gpt-4o-mini",
        "api_key": "your OpenAI Key goes here",
    },
    {
        "api_type": "openai",
        "model": "gpt-4o-mini",
        "api_key": "your OpenAI Key goes here",
    }
]

As an alternative to the api_key key and value in the config, you can set the environment variable OPENAI_API_KEY to your OpenAI key.

Linux/Mac:

export OPENAI_API_KEY="your_openai_api_key_here"

Windows:

set OPENAI_API_KEY=your_openai_api_key_here

Examples

See the ConversableAgent example in our Basic Concepts documentation to see OpenAI’s LLMs in use.