AG2 (formerly AutoGen) is an open-source programming framework for building AI agents and facilitating their cooperation to solve tasks. AG2 supports tool use, autonomous and human-in-the-loop workflows, and multi-agent conversation patterns.

From version 0.8: The OpenAI package, openai, is not installed by default.

Install AG2 with your preferred model provider(s), for example:

  • pip install ag2[openai]
  • pip install ag2[gemini]
  • pip install ag2[anthropic,cohere,mistral]

On Mac OS, if you get “no matches found:”, add a quote to the package name, for example:

  • pip install "ag2[openai]"

Let’s go

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.

# 1. Import our agent class
from autogen import ConversableAgent, LLMConfig

# 2. Define our LLM configuration for OpenAI's GPT-4o mini
#    uses the OPENAI_API_KEY environment variable
llm_config = LLMConfig(api_type="openai", model="gpt-4o-mini")

# 3. Create our LLM agent
with llm_config:
    my_agent = ConversableAgent(
        name="helpful_agent",
        system_message="You are a poetic AI assistant, respond in rhyme.",
    )

# 4. Run the agent with a prompt
chat_result = my_agent.run("In one sentence, what's the big deal about AI?")

# 5. Print the chat
print(chat_result.chat_history)

Learn more about configuring LLMs for agents here.

Where to Go Next?

If you like our project, please give it a star on GitHub. If you are interested in contributing, please read Contributor’s Guide.