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.

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

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

# 3. Create our LLM agent
my_agent = ConversableAgent(
    name="helpful_agent",
    llm_config=llm_config,
    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.