找回密码
 立即注册
首页 业界区 业界 使用C#构建一个同时问多个LLM并总结的小工具 ...

使用C#构建一个同时问多个LLM并总结的小工具

钦娅芬 2025-6-2 23:51:39
前言

在AI编程时代,如果自己能够知道一些可行的解决方案,那么描述清楚交给AI,可以有很大的帮助。
但是我们往往不知道真正可行的解决方案是什么?
我自己有过这样的经历,遇到一个需求,我不知道有哪些解决方案,就去问AI,然后AI输出一大堆东西,我一个个去试,然后再换个AI问,又提出了不同的解决方案。
在换AI问与一个个试的过程中好像浪费了很多时间。
突然出现了一个想法,不是可以一下子把问题丢给多个AI,然后再总结一下出现最多的三个方案。那么这三个方案可行的概率会大一点。然后再丢给Cursor或者Cline等AI编程工具帮我们实现一下。
这样做的缺点是比起直接在网页上问,调用API需要耗费Token,但是硅基流动给我赠送了很多额度还没用完,随便玩一下。
实现效果:
1.png

2.png

实现方案

实现方案也很简单,如下图所示:
3.png

先设计一下布局:
  1. <UserControl xmlns="https://github.com/avaloniaui"
  2.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  4.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5.              mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
  6.                          xmlns:vm="using:AIE_Studio.ViewModels"
  7.                          x:DataType="vm:DuoWenViewModel"
  8.              x:>
  9.         <StackPanel>
  10.                 <TextBox Text="{Binding Question}"></TextBox>
  11.                 <Button Content="提问" Command="{Binding DuoWenStreamingParallelCommand}" Margin="5"/>
  12.                 <ScrollViewer VerticalScrollBarVisibility="Auto">
  13.                 <Grid>
  14.                         <Grid.RowDefinitions>
  15.                                 <RowDefinition Height="*"/>
  16.                                 <RowDefinition Height="*"/>
  17.                         </Grid.RowDefinitions>
  18.                         <Grid.ColumnDefinitions>
  19.                                 <ColumnDefinition Width="*"/>
  20.                                 <ColumnDefinition Width="*"/>
  21.                                 <ColumnDefinition Width="*"/>
  22.                         </Grid.ColumnDefinitions>
  23.                        
  24.                         <StackPanel Grid.Row="0" Grid.Column="0">
  25.                                 <TextBlock Text="{Binding Title1}" Margin="5"/>
  26.                                 <ScrollViewer VerticalScrollBarVisibility="Auto">
  27.                                         <TextBox Text="{Binding Result1}" AcceptsReturn="True" Margin="5" Height="300"/>
  28.                                 </ScrollViewer>
  29.                         </StackPanel>
  30.                        
  31.                         <StackPanel Grid.Row="0" Grid.Column="1">
  32.                                 <TextBlock Text="{Binding Title2}" Margin="5"/>
  33.                                 <ScrollViewer VerticalScrollBarVisibility="Auto">
  34.                                         <TextBox Text="{Binding Result2}" AcceptsReturn="True" Margin="5" Height="300"/>
  35.                                 </ScrollViewer>
  36.                         </StackPanel>
  37.                        
  38.                         <StackPanel Grid.Row="0" Grid.Column="2">
  39.                                 <TextBlock Text="{Binding Title3}" Margin="5"/>
  40.                                 <ScrollViewer VerticalScrollBarVisibility="Auto">
  41.                                         <TextBox Text="{Binding Result3}" AcceptsReturn="True" Margin="5" Height="300"/>
  42.                                 </ScrollViewer>
  43.                         </StackPanel>
  44.                        
  45.                         <StackPanel Grid.Row="1" Grid.Column="0">
  46.                                 <TextBlock Text="{Binding Title4}" Margin="5"/>
  47.                                 <ScrollViewer VerticalScrollBarVisibility="Auto">
  48.                                         <TextBox Text="{Binding Result4}" AcceptsReturn="True" Margin="5" Height="300"/>
  49.                                 </ScrollViewer>
  50.                         </StackPanel>
  51.                        
  52.                         <StackPanel Grid.Row="1" Grid.Column="1">
  53.                                 <TextBlock Text="{Binding Title5}" Margin="5"/>
  54.                                 <ScrollViewer VerticalScrollBarVisibility="Auto">
  55.                                         <TextBox Text="{Binding Result5}" AcceptsReturn="True" Margin="5" Height="300"/>
  56.                                 </ScrollViewer>
  57.                         </StackPanel>
  58.                        
  59.                         <StackPanel Grid.Row="1" Grid.Column="2">
  60.                                 <TextBlock Text="{Binding Title6}" Margin="5"/>
  61.                                 <ScrollViewer VerticalScrollBarVisibility="Auto">
  62.                                         <TextBox Text="{Binding Result6}" AcceptsReturn="True" Margin="5" Height="300"/>
  63.                                 </ScrollViewer>
  64.                         </StackPanel>
  65.                 </Grid>
  66.                 </ScrollViewer>
  67.         </StackPanel>
  68. </UserControl>
复制代码
4.png

在ViewModel中先来看一下最原始的显示结果的方式:
  1. [RelayCommand]
  2. private async Task DuoWen()
  3. {
  4.      ApiKeyCredential apiKeyCredential = new ApiKeyCredential("your api key");
  5.      OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions();
  6.      openAIClientOptions.Endpoint = new Uri("https://api.siliconflow.cn/v1");
  7.    
  8.      IChatClient client1 =
  9.      new OpenAI.Chat.ChatClient("Qwen/Qwen2.5-72B-Instruct", apiKeyCredential, openAIClientOptions).AsChatClient();
  10.      var result1 = await client1.GetResponseAsync(Question);
  11.      Result1 = result1.ToString();
  12.      IChatClient client2 =
  13.      new OpenAI.Chat.ChatClient("Qwen/Qwen3-235B-A22B", apiKeyCredential, openAIClientOptions).AsChatClient();
  14.      var result2 = await client2.GetResponseAsync(Question);
  15.      Result2 = result2.ToString();
  16.      IChatClient client3 =
  17.      new OpenAI.Chat.ChatClient("THUDM/GLM-Z1-32B-0414", apiKeyCredential, openAIClientOptions).AsChatClient();
  18.      var result3 = await client3.GetResponseAsync(Question);
  19.      Result3 = result3.ToString();
  20.      IChatClient client4 =
  21.      new OpenAI.Chat.ChatClient("THUDM/GLM-4-32B-0414", apiKeyCredential, openAIClientOptions).AsChatClient();
  22.      var result4 = await client4.GetResponseAsync(Question);
  23.      Result4 = result4.ToString();
  24.      IChatClient client5 =
  25.     new OpenAI.Chat.ChatClient("deepseek-ai/DeepSeek-R1", apiKeyCredential, openAIClientOptions).AsChatClient();
  26.      var result5 = await client5.GetResponseAsync(Question);
  27.      Result5 = result5.ToString();
  28.      IChatClient client6 =
  29.      new OpenAI.Chat.ChatClient("deepseek-ai/DeepSeek-V3", apiKeyCredential, openAIClientOptions).AsChatClient();
  30.      var result6 = await client6.GetResponseAsync(Question);
  31.      Result6 = result6.ToString();
复制代码
这种最简单的方式是非流式的并且也不是并行的,你会发现一个结束了才会继续向下一个提问。
但至少已经成功显示结果了,现在想要实现的是有一个窗体进行总结。
窗体设计:
  1. <Window xmlns="https://github.com/avaloniaui"
  2.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  4.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5.         mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="450"
  6.                 xmlns:vm="using:AIE_Studio.ViewModels"
  7.         x:
  8.                 x:DataType="vm:ShowResultWindowViewModel"
  9.         Title="ShowResultWindow">
  10.         <StackPanel>
  11.                 <TextBlock Text="最终结果:" Margin="5" />
  12.                 <ScrollViewer VerticalScrollBarVisibility="Auto">
  13.                         <TextBox Text="{Binding ReceivedValue}" AcceptsReturn="True" Margin="5" Height="400"/>
  14.                 </ScrollViewer>
  15.         </StackPanel>
  16. </Window>
复制代码
窗体的ViewModel:
  1. public partial class ShowResultWindowViewModel : ViewModelBase
  2. {
  3.     [ObservableProperty]
  4.     private string? receivedValue;      
  5. }
复制代码
然后只要在全部都有结果之后,再进行一下总结即可。
  1. IChatClient client7 =
  2. new OpenAI.Chat.ChatClient("Qwen/Qwen2.5-72B-Instruct", apiKeyCredential, openAIClientOptions).AsChatClient();
  3. List<Microsoft.Extensions.AI.ChatMessage> messages = new List<Microsoft.Extensions.AI.ChatMessage>();
  4. string prompt = $"""
  5.       请分析以下各个助手给出的方案,选择其中提到最多的3种方案。
  6.       助手1:{result1}
  7.       助手2:{result2}
  8.       助手3:{result3}
  9.       助手4:{result4}
  10.       助手5:{result5}
  11.       助手6:{result6}
  12.       """;
  13. messages.Add(new Microsoft.Extensions.AI.ChatMessage(ChatRole.User, prompt));
  14. var result7 = await client7.GetResponseAsync(messages);
  15. var showWindow = _serviceProvider.GetRequiredService<ShowResultWindow>();
  16. var showWindowViewModel = _serviceProvider.GetRequiredService<ShowResultWindowViewModel>();
  17. showWindowViewModel.ReceivedValue = result7.ToString();
  18. showWindow.DataContext = showWindowViewModel;
  19. showWindow.Show();
复制代码
以上就成功实现了。
但是还是有可以改进的地方,首先是并行,一个一个问不如同时问。
  1. [RelayCommand]
  2. private async Task DuoWenParallel()
  3. {
  4.      ApiKeyCredential apiKeyCredential = new ApiKeyCredential("your api key");
  5.      OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions();
  6.      openAIClientOptions.Endpoint = new Uri("https://api.siliconflow.cn/v1");
  7.      // 创建一个列表来存储所有的任务
  8.      var tasks = new List<Task<string>>();
  9.      // 向每个助手发送请求并将任务添加到列表中
  10.      tasks.Add(GetResponseFromClient("Qwen/Qwen2.5-72B-Instruct", apiKeyCredential, openAIClientOptions));
  11.      tasks.Add(GetResponseFromClient("Qwen/Qwen3-235B-A22B", apiKeyCredential, openAIClientOptions));
  12.      tasks.Add(GetResponseFromClient("THUDM/GLM-Z1-32B-0414", apiKeyCredential, openAIClientOptions));
  13.      tasks.Add(GetResponseFromClient("THUDM/GLM-4-32B-0414", apiKeyCredential, openAIClientOptions));
  14.      tasks.Add(GetResponseFromClient("deepseek-ai/DeepSeek-R1", apiKeyCredential, openAIClientOptions));
  15.      tasks.Add(GetResponseFromClient("deepseek-ai/DeepSeek-V3", apiKeyCredential, openAIClientOptions));
  16.      // 等待所有任务完成
  17.      var results = await Task.WhenAll(tasks);
  18.      // 将结果分配给相应的属性
  19.      Result1 = results[0];
  20.      Result2 = results[1];
  21.      Result3 = results[2];
  22.      Result4 = results[3];
  23.      Result5 = results[4];
  24.      Result6 = results[5];
  25. }
  26.   private async Task<string> GetResponseFromClient(string model, ApiKeyCredential apiKeyCredential, OpenAIClientOptions options)
  27.   {
  28.       IChatClient client = new OpenAI.Chat.ChatClient(model, apiKeyCredential, options).AsChatClient();
  29.       var result = await client.GetResponseAsync(Question);
  30.       return result.ToString();
  31.   }
复制代码
现在虽然是并行了,但是只有等到所有助手都回答了之后,才会统一显示,用户体验也不好。
改成流式:
  1. [RelayCommand]
  2. private async Task DuoWenStreaming()
  3. {
  4.     ApiKeyCredential apiKeyCredential = new ApiKeyCredential("your api key");
  5.     OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions();
  6.     openAIClientOptions.Endpoint = new Uri("https://api.siliconflow.cn/v1");
  7.     //string question = "C#如何获取鼠标滑动选中的值?请告诉我一些可能的方案,每个方案只需用一句话描述即可,不用展开说明。";
  8.     IChatClient client1 =
  9.     new OpenAI.Chat.ChatClient("Qwen/Qwen2.5-72B-Instruct", apiKeyCredential, openAIClientOptions).AsChatClient();
  10.     await foreach (var item in client1.GetStreamingResponseAsync(Question))
  11.     {
  12.        Result1 += item.ToString();
  13.     }         
  14. }
复制代码
现在查看效果:
5.gif

最后再改造成流式+并行就好了。
  1. [RelayCommand]
  2. private async Task DuoWenStreamingParallel()
  3. {
  4.      ApiKeyCredential apiKeyCredential = new ApiKeyCredential("your api key");
  5.      OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions();
  6.      openAIClientOptions.Endpoint = new Uri("https://api.siliconflow.cn/v1");
  7.      // Clear previous results
  8.      Result1 = Result2 = Result3 = Result4 = Result5 = Result6 = string.Empty;
  9.      // Create a list of tasks for parallel processing
  10.      var tasks = new List<Task>
  11.      {
  12.          ProcessStreamingResponse("Qwen/Qwen2.5-72B-Instruct", apiKeyCredential, openAIClientOptions, (text) => Result1 += text),
  13.          ProcessStreamingResponse("Qwen/Qwen3-235B-A22B", apiKeyCredential, openAIClientOptions, (text) => Result2 += text),
  14.          ProcessStreamingResponse("THUDM/GLM-Z1-32B-0414", apiKeyCredential, openAIClientOptions, (text) => Result3 += text),
  15.          ProcessStreamingResponse("THUDM/GLM-4-32B-0414", apiKeyCredential, openAIClientOptions, (text) => Result4 += text),
  16.          ProcessStreamingResponse("deepseek-ai/DeepSeek-R1", apiKeyCredential, openAIClientOptions, (text) => Result5 += text),
  17.          ProcessStreamingResponse("deepseek-ai/DeepSeek-V3", apiKeyCredential, openAIClientOptions, (text) => Result6 += text)
  18.      };
  19.      // Wait for all streaming responses to complete
  20.      await Task.WhenAll(tasks);
  21.      IChatClient client7 =
  22.      new OpenAI.Chat.ChatClient("Qwen/Qwen2.5-72B-Instruct", apiKeyCredential, openAIClientOptions).AsChatClient();
  23.      List<Microsoft.Extensions.AI.ChatMessage> messages = new List<Microsoft.Extensions.AI.ChatMessage>();
  24.      string prompt = $"""
  25.            请分析以下各个助手给出的方案,选择其中提到最多的3种方案。
  26.            助手1:{Result1}
  27.            助手2:{Result2}
  28.            助手3:{Result3}
  29.            助手4:{Result4}
  30.            助手5:{Result5}
  31.            助手6:{Result6}
  32.            """;
  33.      messages.Add(new Microsoft.Extensions.AI.ChatMessage(ChatRole.User, prompt));
  34.      var result7 = await client7.GetResponseAsync(messages);
  35.      var showWindow = _serviceProvider.GetRequiredService<ShowResultWindow>();
  36.      var showWindowViewModel = _serviceProvider.GetRequiredService<ShowResultWindowViewModel>();
  37.      showWindowViewModel.ReceivedValue = result7.ToString();
  38.      showWindow.DataContext = showWindowViewModel;
  39.      showWindow.Show();
  40. }
  41. private async Task ProcessStreamingResponse(string model, ApiKeyCredential apiKeyCredential, OpenAIClientOptions options, Action<string> updateResult)
  42. {
  43.     IChatClient client = new OpenAI.Chat.ChatClient(model, apiKeyCredential, options).AsChatClient();
  44.    
  45.     await foreach (var item in client.GetStreamingResponseAsync(Question))
  46.     {
  47.         updateResult(item.ToString());
  48.     }
  49. }
复制代码
这里使用了一个带有一个参数的委托来更新每个助手回复的结果。
现在再查看效果:
6.gif

Qwen/Qwen3-235B-A22B、THUDM/GLM-Z1-32B-0414、deepseek-ai/DeepSeek-R1有思考过程,返回结果比较慢。
目前Microsoft.Extensions.AI.OpenAI好像还无法获取思考内容。
7.png

等待久一会之后,可以看到结果都出来了:
8.png

然后总结窗口会显示最终的总结内容:
9.png

确定方案之后可以让Cursor或者Cline帮我们写一下试试。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册