Prerequisites
Before you begin, make sure you have:- .NET 6.0 or later installed
- An OpenAI API key
- Visual Studio, VS Code, or your preferred C# IDE
Installation
Install the AutoGen package, which includes all core features:Your First Agent Conversation
Let’s create a simple conversation between a user and an AI assistant.Understanding the Code
AssistantAgent
TheAssistantAgent is an AI-powered agent that uses a language model to generate responses:
- name: Unique identifier for the agent
- systemMessage: Instructions that define the agent’s behavior
- llmConfig: Configuration including model settings and API keys
- Temperature: Controls randomness (0 = deterministic, 1 = creative)
UserProxyAgent
TheUserProxyAgent represents a human user in the conversation:
- HumanInputMode.ALWAYS: Prompts for user input before each message
- HumanInputMode.NEVER: Fully autonomous (uses default replies)
- HumanInputMode.TERMINATE: Only prompts when termination is suggested
Message Printing
TheRegisterPrintMessage() middleware nicely formats and prints messages to the console:
InitiateChatAsync
Starts the conversation between agents:Autonomous Mode Example
For a fully autonomous conversation without human input:Next Steps
Agents
Learn more about agent types and configurations
Function Calling
Add custom functions to your agents
Group Chat
Create multi-agent conversations
Code Execution
Execute code snippets dynamically
Troubleshooting
API Key Not Found
API Key Not Found
Make sure you’ve set the
OPENAI_API_KEY environment variable correctly. In Windows, you may need to restart your terminal or IDE after setting it.Package Not Found
Package Not Found
Ensure you’re using .NET 6.0 or later and that you’ve restored packages:
Connection Errors
Connection Errors
Check your internet connection and verify that your OpenAI API key is valid and has sufficient credits.