Login Sign Up

Setting Up and Running AutoGen

AutoGen is an advanced AI framework designed for creating LLM (Large Language Model) applications. It simplifies multi-agent collaboration and automation in AI workflows. In this guide, we’ll walk through setting up AutoGen and running a basic example.

Step 1: Installing AutoGen

To get started, install AutoGen using pip:

pip install pyautogen

You may also need additional dependencies depending on your use case. For full compatibility, refer to the official documentation.

Step 2: Setting Up Environment Variables

AutoGen requires API keys for models like OpenAI’s GPT. Set up your environment variables:

export OPENAI_API_KEY='your_api_key_here'

On Windows (Command Prompt):

set OPENAI_API_KEY=your_api_key_here

Alternatively, you can store the key in a .env file and use dotenv to load it into your script.

Step 3: Running a Basic Example

Here’s a simple example where two AI agents collaborate on a task:

from autogen import AssistantAgent, UserProxyAgent

# Create two agents: an assistant and a user proxy

assistant = AssistantAgent(name="AI_Assistant")

user = UserProxyAgent(name="User", human_input_mode="ALWAYS")

# Start an interaction

user.initiate_chat(assistant, message="How do I set up AutoGen?")

This script initializes two agents, one acting as an AI assistant and the other as a user proxy. The user sends a query, and the AI responds.

Step 4: Customizing AutoGen Agents

You can customize agents to specialize in different domains. For example, setting an agent with specific instructions:

expert = AssistantAgent(

    name="Python_Expert",

    system_message="You are an expert in Python and AI development. Answer coding-related queries precisely."

)

Now, when interacting with Python_Expert, responses will be tailored to coding and AI.

Step 5: Running Multi-Agent Collaboration

AutoGen enables multiple agents to collaborate. Here’s an example:

researcher = AssistantAgent(name="Researcher", system_message="Provide well-researched responses.")

writer = AssistantAgent(name="Writer", system_message="Summarize findings concisely.")

user.initiate_chat(researcher, message="Find recent AI trends.")

researcher.initiate_chat(writer, message="Summarize AI trends into a short report.")

This setup allows AI agents to work together on tasks, mimicking a real-world workflow.