An AI-powered agent that uses language models to generate intelligent responses.
using AutoGen;using AutoGen.OpenAI;var openAIKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");var gpt4Config = new OpenAIConfig(openAIKey, "gpt-4");var assistant = new AssistantAgent( name: "assistant", systemMessage: "You are a helpful AI assistant that can write code and solve problems.", llmConfig: new ConversableAgentConfig { Temperature = 0.7, ConfigList = [gpt4Config], MaxToken = 2000 });
Craft effective system messages to guide agent behavior:
var coder = new AssistantAgent( name: "coder", systemMessage: @" You are an expert C# developer. When writing code: - Use modern C# features and best practices - Include error handling - Add XML documentation comments - Format code between ```csharp and ``` markers ", llmConfig: config);
using AutoGen.Core;// Send a simple text messagevar response = await assistant.SendAsync("What is 2 + 2?");Console.WriteLine(response.GetContent());// Send with message historyvar history = new List<IMessage>{ new TextMessage(Role.User, "Hello"), new TextMessage(Role.Assistant, "Hi! How can I help?", from: "assistant")};var reply = await assistant.SendAsync( "Can you help me with math?", chatHistory: history);
Stream responses token-by-token for real-time output:
await foreach (var message in assistant.GenerateStreamingReplyAsync( new[] { new TextMessage(Role.User, "Write a story") })){ Console.Write(message.GetContent());}
var assistant = new AssistantAgent( name: "assistant", systemMessage: "You are a helpful assistant.", llmConfig: config) .RegisterPrintMessage();var user = new UserProxyAgent( name: "user", humanInputMode: HumanInputMode.ALWAYS) .RegisterPrintMessage();// User initiates the conversationawait user.InitiateChatAsync( receiver: assistant, message: "Help me write a sorting algorithm", maxRound: 10);
Lower-level method for custom conversation control:
var messages = new List<IMessage>{ new TextMessage(Role.User, "Hello")};// Generate a replyvar reply = await assistant.GenerateReplyAsync(messages);messages.Add(reply);// Continue the conversationmessages.Add(new TextMessage(Role.User, "Tell me a joke"));reply = await assistant.GenerateReplyAsync(messages);
using AutoGen.OpenAI.Extension;var agent = new OpenAIChatAgent(/*...*/) .RegisterMessageConnector() // Converts AutoGen messages to OpenAI format .RegisterPrintMessage();
using AutoGen.OpenAI;using AutoGen.OpenAI.Extension;using OpenAI;var openAIClient = new OpenAIClient(apiKey);var agent = new OpenAIChatAgent( chatClient: openAIClient.GetChatClient("gpt-4"), name: "assistant", systemMessage: "You are a helpful assistant") .RegisterMessageConnector() .RegisterPrintMessage();
using AutoGen.Anthropic;using AutoGen.Anthropic.Extensions;var anthropicClient = new AnthropicClient( new HttpClient(), AnthropicConstants.Endpoint, apiKey);var agent = new AnthropicClientAgent( anthropicClient, "assistant", AnthropicConstants.Claude3Haiku) .RegisterMessageConnector() .RegisterPrintMessage();
using AutoGen.AzureAIInference;using AutoGen.AzureAIInference.Extension;using Azure.AI.Inference;var client = new ChatCompletionsClient( new Uri(endpoint), new AzureKeyCredential(apiKey));var agent = new ChatCompletionsClientAgent( chatCompletionsClient: client, name: "assistant", modelName: "gpt-4", systemMessage: "You are a helpful assistant") .RegisterMessageConnector();