Let’s create a simple conversation between a user and an AI assistant.
1
Create a new console application
dotnet new console -n AutoGenQuickStartcd AutoGenQuickStartdotnet add package AutoGen
2
Set up your OpenAI API key
Set your OpenAI API key as an environment variable:
$env:OPENAI_API_KEY="your-api-key-here"
3
Create the application
Replace the contents of Program.cs with:
Program.cs
using AutoGen;using AutoGen.OpenAI;var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") ?? throw new Exception("Please set OPENAI_API_KEY environment variable.");var gpt35Config = new OpenAIConfig(openAIKey, "gpt-3.5-turbo");// Create an assistant agentvar assistantAgent = new AssistantAgent( name: "assistant", systemMessage: "You are a helpful assistant that can answer questions and help with tasks.", llmConfig: new ConversableAgentConfig { Temperature = 0, ConfigList = [gpt35Config], }) .RegisterPrintMessage(); // Print messages to console// Create a user proxy agentvar userProxyAgent = new UserProxyAgent( name: "user", humanInputMode: HumanInputMode.ALWAYS) // Always ask for user input .RegisterPrintMessage();// Start the conversationawait userProxyAgent.InitiateChatAsync( receiver: assistantAgent, message: "Hey assistant, please help me write a C# function to calculate factorial.", maxRound: 10);
4
Run the application
dotnet run
The application will start a conversation where you can interact with the AI assistant. Type your messages and press Enter. The conversation continues until you reach the maximum rounds or terminate it.
The AssistantAgent is an AI-powered agent that uses a language model to generate responses:
var assistantAgent = new AssistantAgent( name: "assistant", systemMessage: "You are a helpful assistant.", llmConfig: new ConversableAgentConfig { Temperature = 0, ConfigList = [gpt35Config], });
name: Unique identifier for the agent
systemMessage: Instructions that define the agent’s behavior
llmConfig: Configuration including model settings and API keys
For a fully autonomous conversation without human input:
using AutoGen;using AutoGen.OpenAI;var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");var gpt35Config = new OpenAIConfig(openAIKey, "gpt-3.5-turbo");var assistant = new AssistantAgent( name: "assistant", systemMessage: "You are a helpful coding assistant.", llmConfig: new ConversableAgentConfig { Temperature = 0, ConfigList = [gpt35Config], }) .RegisterPrintMessage();var user = new UserProxyAgent( name: "user", humanInputMode: HumanInputMode.NEVER, defaultReply: "Thank you!") .RegisterPrintMessage();await user.InitiateChatAsync( receiver: assistant, message: "Write a C# function to check if a number is prime.", maxRound: 3);