AutoGen -- HelloWord --根据微软教程,使用AutoGen框架开启多Agent协同
微软官方教程:https://microsoft.github.io/autogen-for-net/index.html
一个简单的helloword项目
using Applier; using AutoGen; using AutoGen.Core; using AutoGen.OpenAI; using AutoGen.OpenAI.Extension; using OpenAI; using System.ClientModel; namespace ConsoleApp1 { internal class Program { // ① Main 方法改为 async Task static async Task Main(string[] args) { // ② 创建 OpenAI 客户端 var openAIClient = new OpenAIClient( new ApiKeyCredential(ConstParm.apiKey), new OpenAIClientOptions { Endpoint = new Uri(ConstParm.endpoint) }); // ③ 创建助手 Agent var assistantAgent = new OpenAIChatAgent( name: "小助手", systemMessage: "你是一个热情友好的 AI 助手,用中文回答用户问题", chatClient: openAIClient.GetChatClient(ConstParm.modelId)) .RegisterMessageConnector() .RegisterPrintMessage(); // ④ 创建用户代理 var userProxyAgent = new UserProxyAgent( name: "六爷", humanInputMode: HumanInputMode.ALWAYS) .RegisterPrintMessage(); // ⑤ 开始对话 Console.WriteLine("🤖 AutoGen Hello World 启动...\n"); Console.WriteLine($"使用模型:{ConstParm.modelId}"); Console.WriteLine("输入 'quit' 退出对话\n"); Console.WriteLine("========================================\n"); // ⑥ AutoGen 0.2.x 的正确用法 await assistantAgent.InitiateChatAsync( receiver: userProxyAgent, // ← 反过来! message: "你好,请介绍一下你自己", maxRound: 10); } } }
NuGet.Config 配置
<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <clear /> <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" /> </packageSources> <disabledPackageSources> <add key="Microsoft Visual Studio Offline Packages" value="true" /> </disabledPackageSources> </configuration>
项目引用:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net8.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <Compile Include="..\..\openClawTest\ConsoleApp1\ConstParm.cs" Link="ConstParm.cs" /> </ItemGroup> <ItemGroup> <PackageReference Include="AutoGen" Version="0.2.3" /> <PackageReference Include="AutoGen.OpenAI" Version="0.2.3" /> </ItemGroup> </Project>
运行

为什么会有红色框的提示?
🎯 这个提示的含义
"Please give feedback: Press enter or type 'exit' to stop the conversation." 翻译: "请给出反馈:按回车键或输入 'exit' 停止对话"
意思: UserProxyAgent 在等你输入 按 回车键 = 继续对话(不输入内容) 输入 exit = 结束对话
📖 为什么会出现这个提示?
var userProxyAgent = new UserProxyAgent( name: "六爷", humanInputMode: HumanInputMode.ALWAYS); // ← 这个模式导致的
HumanInputMode.ALWAYS 的行为:
每轮对话 → 等待用户输入 → 用户按回车或输入内容 → 继续
✅ 如果你想改变这个行为
方案 1:改成自动继续(不按回车)
// 改成 NEVER 模式 - 不等待人工输入 var userProxyAgent = new UserProxyAgent( name: "六爷", humanInputMode: HumanInputMode.NEVER, // ← 改这里 defaultReply: "") // 默认回复为空(直接继续) .RegisterPrintMessage();
方案 2:改成只在终止时等待
var userProxyAgent = new UserProxyAgent( name: "六爷", humanInputMode: HumanInputMode.TERMINATE, // ← 只在终止时等待 isTermination: async (messages, ct) => { var lastMessage = messages.LastOrDefault(); return lastMessage?.GetContent()?.ToLower().Contains("quit") == true; }) .RegisterPrintMessage();
推荐你用这个!
保持 ALWAYS 模式,这样可以:
- ✅ 每轮都可以控制是否继续
- ✅ 可以随时输入
exit结束 - ✅ 可以中途插入自己的话
如果觉得提示语不好看,可以忽略它,直接:
- 按 回车 = 继续
- 输入
exit= 结束

浙公网安备 33010602011771号