百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

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

zhezhongyun 2025-06-09 07:23 29 浏览

前言

在AI编程时代,如果自己能够知道一些可行的解决方案,那么描述清楚交给AI,可以有很大的帮助。

但是我们往往不知道真正可行的解决方案是什么?

我自己有过这样的经历,遇到一个需求,我不知道有哪些解决方案,就去问AI,然后AI输出一大堆东西,我一个个去试,然后再换个AI问,又提出了不同的解决方案。

在换AI问与一个个试的过程中好像浪费了很多时间。

突然出现了一个想法,不是可以一下子把问题丢给多个AI,然后再总结一下出现最多的三个方案。那么这三个方案可行的概率会大一点。然后再丢给Cursor或者Cline等AI编程工具帮我们实现一下。

这样做的缺点是比起直接在网页上问,调用API需要耗费Token,但是硅基流动给我赠送了很多额度还没用完,随便玩一下。

实现效果:

image-20250507165621088
image-20250507165714732

实现方案

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

image-20250507165456604

先设计一下布局:

<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
xmlns:vm="using:AIE_Studio.ViewModels"
x:DataType="vm:DuoWenViewModel"
x:Class="AIE_Studio.Views.DuoWenView">
<StackPanel>
<TextBox Text="{Binding Question}"></TextBox>
<Button Content="提问" Command="{Binding DuoWenStreamingParallelCommand}" Margin="5"/>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!-- Row 1, Column 1 -->
<StackPanel Grid.Row="0" Grid.Column="0">
<TextBlock Text="{Binding Title1}" Margin="5"/>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBox Text="{Binding Result1}" AcceptsReturn="True" Margin="5" Height="300"/>
</ScrollViewer>
</StackPanel>
<!-- Row 1, Column 2 -->
<StackPanel Grid.Row="0" Grid.Column="1">
<TextBlock Text="{Binding Title2}" Margin="5"/>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBox Text="{Binding Result2}" AcceptsReturn="True" Margin="5" Height="300"/>
</ScrollViewer>
</StackPanel>
<!-- Row 1, Column 3 -->
<StackPanel Grid.Row="0" Grid.Column="2">
<TextBlock Text="{Binding Title3}" Margin="5"/>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBox Text="{Binding Result3}" AcceptsReturn="True" Margin="5" Height="300"/>
</ScrollViewer>
</StackPanel>
<!-- Row 2, Column 1 -->
<StackPanel Grid.Row="1" Grid.Column="0">
<TextBlock Text="{Binding Title4}" Margin="5"/>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBox Text="{Binding Result4}" AcceptsReturn="True" Margin="5" Height="300"/>
</ScrollViewer>
</StackPanel>
<!-- Row 2, Column 2 -->
<StackPanel Grid.Row="1" Grid.Column="1">
<TextBlock Text="{Binding Title5}" Margin="5"/>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBox Text="{Binding Result5}" AcceptsReturn="True" Margin="5" Height="300"/>
</ScrollViewer>
</StackPanel>
<!-- Row 2, Column 3 -->
<StackPanel Grid.Row="1" Grid.Column="2">
<TextBlock Text="{Binding Title6}" Margin="5"/>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBox Text="{Binding Result6}" AcceptsReturn="True" Margin="5" Height="300"/>
</ScrollViewer>
</StackPanel>
</Grid>
</ScrollViewer>
</StackPanel>
</UserControl>
image-20250508144609463

在ViewModel中先来看一下最原始的显示结果的方式:

 [RelayCommand]
private async Task DuoWen()
{
ApiKeyCredential apiKeyCredential = new ApiKeyCredential("your api key");

OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions();
openAIClientOptions.Endpoint = new Uri("https://api.siliconflow.cn/v1");

IChatClient client1 =
new OpenAI.Chat.ChatClient("Qwen/Qwen2.5-72B-Instruct", apiKeyCredential, openAIClientOptions).AsChatClient();

var result1 = await client1.GetResponseAsync(Question);

Result1 = result1.ToString();

IChatClient client2 =
new OpenAI.Chat.ChatClient("Qwen/Qwen3-235B-A22B", apiKeyCredential, openAIClientOptions).AsChatClient();

var result2 = await client2.GetResponseAsync(Question);

Result2 = result2.ToString();

IChatClient client3 =
new OpenAI.Chat.ChatClient("THUDM/GLM-Z1-32B-0414", apiKeyCredential, openAIClientOptions).AsChatClient();

var result3 = await client3.GetResponseAsync(Question);

Result3 = result3.ToString();

IChatClient client4 =
new OpenAI.Chat.ChatClient("THUDM/GLM-4-32B-0414", apiKeyCredential, openAIClientOptions).AsChatClient();

var result4 = await client4.GetResponseAsync(Question);

Result4 = result4.ToString();

IChatClient client5 =
new OpenAI.Chat.ChatClient("deepseek-ai/DeepSeek-R1", apiKeyCredential, openAIClientOptions).AsChatClient();

var result5 = await client5.GetResponseAsync(Question);

Result5 = result5.ToString();

IChatClient client6 =
new OpenAI.Chat.ChatClient("deepseek-ai/DeepSeek-V3", apiKeyCredential, openAIClientOptions).AsChatClient();

var result6 = await client6.GetResponseAsync(Question);

Result6 = result6.ToString();

这种最简单的方式是非流式的并且也不是并行的,你会发现一个结束了才会继续向下一个提问。

但至少已经成功显示结果了,现在想要实现的是有一个窗体进行总结。

窗体设计:

<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="450"
xmlns:vm="using:AIE_Studio.ViewModels"
x:Class="AIE_Studio.Views.ShowResultWindow"
x:DataType="vm:ShowResultWindowViewModel"
Title="ShowResultWindow">
<StackPanel>
<TextBlock Text="最终结果:" Margin="5" />
<ScrollViewer VerticalScrollBarVisibility="Auto">
<TextBox Text="{Binding ReceivedValue}" AcceptsReturn="True" Margin="5" Height="400"/>
</ScrollViewer>
</StackPanel>
</Window>

窗体的ViewModel:

public partial class ShowResultWindowViewModel : ViewModelBase
{
[ObservableProperty]
private string? receivedValue;
}

然后只要在全部都有结果之后,再进行一下总结即可。

IChatClient client7 =
new OpenAI.Chat.ChatClient("Qwen/Qwen2.5-72B-Instruct", apiKeyCredential, openAIClientOptions).AsChatClient();
List<Microsoft.Extensions.AI.ChatMessage> messages = new List<Microsoft.Extensions.AI.ChatMessage>();

string prompt = $"""
请分析以下各个助手给出的方案,选择其中提到最多的3种方案。
助手1:{result1}
助手2:{result2}
助手3:{result3}
助手4:{result4}
助手5:{result5}
助手6:{result6}
"
"";
messages.Add(new Microsoft.Extensions.AI.ChatMessage(ChatRole.User, prompt));
var result7 = await client7.GetResponseAsync(messages);

var showWindow = _serviceProvider.GetRequiredService<ShowResultWindow>();
var showWindowViewModel = _serviceProvider.GetRequiredService<ShowResultWindowViewModel>();
showWindowViewModel.ReceivedValue = result7.ToString();
showWindow.DataContext = showWindowViewModel;
showWindow.Show();

以上就成功实现了。

但是还是有可以改进的地方,首先是并行,一个一个问不如同时问。

 [RelayCommand]
private async Task DuoWenParallel()
{
ApiKeyCredential apiKeyCredential = new ApiKeyCredential("your api key");

OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions();
openAIClientOptions.Endpoint = new Uri("https://api.siliconflow.cn/v1");

// 创建一个列表来存储所有的任务
var tasks = new List<Task<string>>();

// 向每个助手发送请求并将任务添加到列表中
tasks.Add(GetResponseFromClient("Qwen/Qwen2.5-72B-Instruct", apiKeyCredential, openAIClientOptions));
tasks.Add(GetResponseFromClient("Qwen/Qwen3-235B-A22B", apiKeyCredential, openAIClientOptions));
tasks.Add(GetResponseFromClient("THUDM/GLM-Z1-32B-0414", apiKeyCredential, openAIClientOptions));
tasks.Add(GetResponseFromClient("THUDM/GLM-4-32B-0414", apiKeyCredential, openAIClientOptions));
tasks.Add(GetResponseFromClient("deepseek-ai/DeepSeek-R1", apiKeyCredential, openAIClientOptions));
tasks.Add(GetResponseFromClient("deepseek-ai/DeepSeek-V3", apiKeyCredential, openAIClientOptions));

// 等待所有任务完成
var results = await Task.WhenAll(tasks);

// 将结果分配给相应的属性
Result1 = results[0];
Result2 = results[1];
Result3 = results[2];
Result4 = results[3];
Result5 = results[4];
Result6 = results[5];
}

private async Task<string> GetResponseFromClient(string model, ApiKeyCredential apiKeyCredential, OpenAIClientOptions options)
{
IChatClient client = new OpenAI.Chat.ChatClient(model, apiKeyCredential, options).AsChatClient();
var result = await client.GetResponseAsync(Question);
return result.ToString();
}

现在虽然是并行了,但是只有等到所有助手都回答了之后,才会统一显示,用户体验也不好。

改成流式:

[RelayCommand]
private async Task DuoWenStreaming()
{
ApiKeyCredential apiKeyCredential = new ApiKeyCredential("your api key");

OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions();
openAIClientOptions.Endpoint = new Uri("https://api.siliconflow.cn/v1");

//string question = "C#如何获取鼠标滑动选中的值?请告诉我一些可能的方案,每个方案只需用一句话描述即可,不用展开说明。";

IChatClient client1 =
new OpenAI.Chat.ChatClient("Qwen/Qwen2.5-72B-Instruct", apiKeyCredential, openAIClientOptions).AsChatClient();

awaitforeach (var item in client1.GetStreamingResponseAsync(Question))
{
Result1 += item.ToString();
}
}

现在查看效果:

最后再改造成流式+并行就好了。

 [RelayCommand]
private async Task DuoWenStreamingParallel()
{
ApiKeyCredential apiKeyCredential = new ApiKeyCredential("your api key");

OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions();
openAIClientOptions.Endpoint = new Uri("https://api.siliconflow.cn/v1");

// Clear previous results
Result1 = Result2 = Result3 = Result4 = Result5 = Result6 = string.Empty;

// Create a list of tasks for parallel processing
var tasks = new List<Task>
{
ProcessStreamingResponse("Qwen/Qwen2.5-72B-Instruct", apiKeyCredential, openAIClientOptions, (text) => Result1 += text),
ProcessStreamingResponse("Qwen/Qwen3-235B-A22B", apiKeyCredential, openAIClientOptions, (text) => Result2 += text),
ProcessStreamingResponse("THUDM/GLM-Z1-32B-0414", apiKeyCredential, openAIClientOptions, (text) => Result3 += text),
ProcessStreamingResponse("THUDM/GLM-4-32B-0414", apiKeyCredential, openAIClientOptions, (text) => Result4 += text),
ProcessStreamingResponse("deepseek-ai/DeepSeek-R1", apiKeyCredential, openAIClientOptions, (text) => Result5 += text),
ProcessStreamingResponse("deepseek-ai/DeepSeek-V3", apiKeyCredential, openAIClientOptions, (text) => Result6 += text)
};

// Wait for all streaming responses to complete
await Task.WhenAll(tasks);

IChatClient client7 =
new OpenAI.Chat.ChatClient("Qwen/Qwen2.5-72B-Instruct", apiKeyCredential, openAIClientOptions).AsChatClient();
List<Microsoft.Extensions.AI.ChatMessage> messages = new List<Microsoft.Extensions.AI.ChatMessage>();

string prompt = $"""
请分析以下各个助手给出的方案,选择其中提到最多的3种方案。
助手1:{Result1}
助手2:{Result2}
助手3:{Result3}
助手4:{Result4}
助手5:{Result5}
助手6:{Result6}
"
"";
messages.Add(new Microsoft.Extensions.AI.ChatMessage(ChatRole.User, prompt));
var result7 = await client7.GetResponseAsync(messages);

var showWindow = _serviceProvider.GetRequiredService<ShowResultWindow>();
var showWindowViewModel = _serviceProvider.GetRequiredService<ShowResultWindowViewModel>();
showWindowViewModel.ReceivedValue = result7.ToString();
showWindow.DataContext = showWindowViewModel;
showWindow.Show();
}

private async Task ProcessStreamingResponse(string model, ApiKeyCredential apiKeyCredential, OpenAIClientOptions options, Action<string> updateResult)
{
IChatClient client = new OpenAI.Chat.ChatClient(model, apiKeyCredential, options).AsChatClient();

awaitforeach (var item in client.GetStreamingResponseAsync(Question))
{
updateResult(item.ToString());
}
}

这里使用了一个带有一个参数的委托来更新每个助手回复的结果。

现在再查看效果:

Qwen/Qwen3-235B-A22B、THUDM/GLM-Z1-32B-0414、deepseek-ai/DeepSeek-R1有思考过程,返回结果比较慢。

目前Microsoft.Extensions.AI.OpenAI好像还无法获取思考内容。

image-20250508151236981

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

image-20250508151421036

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

image-20250508151629385

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


相关推荐

Python入门学习记录之一:变量_python怎么用变量

写这个,主要是对自己学习python知识的一个总结,也是加深自己的印象。变量(英文:variable),也叫标识符。在python中,变量的命名规则有以下三点:>变量名只能包含字母、数字和下划线...

python变量命名规则——来自小白的总结

python是一个动态编译类编程语言,所以程序在运行前不需要如C语言的先行编译动作,因此也只有在程序运行过程中才能发现程序的问题。基于此,python的变量就有一定的命名规范。python作为当前热门...

Python入门学习教程:第 2 章 变量与数据类型

2.1什么是变量?在编程中,变量就像一个存放数据的容器,它可以存储各种信息,并且这些信息可以被读取和修改。想象一下,变量就如同我们生活中的盒子,你可以把东西放进去,也可以随时拿出来看看,甚至可以换成...

绘制学术论文中的“三线表”具体指导

在科研过程中,大家用到最多的可能就是“三线表”。“三线表”,一般主要由三条横线构成,当然在变量名栏里也可以拆分单元格,出现更多的线。更重要的是,“三线表”也是一种数据记录规范,以“三线表”形式记录的数...

Python基础语法知识--变量和数据类型

学习Python中的变量和数据类型至关重要,因为它们构成了Python编程的基石。以下是帮助您了解Python中的变量和数据类型的分步指南:1.变量:变量在Python中用于存储数据值。它们充...

一文搞懂 Python 中的所有标点符号

反引号`无任何作用。传说Python3中它被移除是因为和单引号字符'太相似。波浪号~(按位取反符号)~被称为取反或补码运算符。它放在我们想要取反的对象前面。如果放在一个整数n...

Python变量类型和运算符_python中变量的含义

别再被小名词坑哭了:Python新手常犯的那些隐蔽错误,我用同事的真实bug拆给你看我记得有一次和同事张姐一起追查一个看似随机崩溃的脚本,最后发现罪魁祸首竟然是她把变量命名成了list。说实话...

从零开始:深入剖析 Spring Boot3 中配置文件的加载顺序

在当今的互联网软件开发领域,SpringBoot无疑是最为热门和广泛应用的框架之一。它以其强大的功能、便捷的开发体验,极大地提升了开发效率,成为众多开发者构建Web应用程序的首选。而在Spr...

Python中下划线 ‘_’ 的用法,你知道几种

Python中下划线()是一个有特殊含义和用途的符号,它可以用来表示以下几种情况:1在解释器中,下划线(_)表示上一个表达式的值,可以用来进行快速计算或测试。例如:>>>2+...

解锁Shell编程:变量_shell $变量

引言:开启Shell编程大门Shell作为用户与Linux内核之间的桥梁,为我们提供了强大的命令行交互方式。它不仅能执行简单的文件操作、进程管理,还能通过编写脚本实现复杂的自动化任务。无论是...

一文学会Python的变量命名规则!_python的变量命名有哪些要求

目录1.变量的命名原则3.内置函数尽量不要做变量4.删除变量和垃圾回收机制5.结语1.变量的命名原则①由英文字母、_(下划线)、或中文开头②变量名称只能由英文字母、数字、下画线或中文字所组成。③英文字...

更可靠的Rust-语法篇-区分语句/表达式,略览if/loop/while/for

src/main.rs://函数定义fnadd(a:i32,b:i32)->i32{a+b//末尾表达式}fnmain(){leta:i3...

C++第五课:变量的命名规则_c++中变量的命名规则

变量的命名不是想怎么起就怎么起的,而是有一套固定的规则的。具体规则:1.名字要合法:变量名必须是由字母、数字或下划线组成。例如:a,a1,a_1。2.开头不能是数字。例如:可以a1,但不能起1a。3....

Rust编程-核心篇-不安全编程_rust安全性

Unsafe的必要性Rust的所有权系统和类型系统为我们提供了强大的安全保障,但在某些情况下,我们需要突破这些限制来:与C代码交互实现底层系统编程优化性能关键代码实现某些编译器无法验证的安全操作Rus...

探秘 Python 内存管理:背后的神奇机制

在编程的世界里,内存管理就如同幕后的精密操控者,确保程序的高效运行。Python作为一种广泛使用的编程语言,其内存管理机制既巧妙又复杂,为开发者们提供了便利的同时,也展现了强大的底层控制能力。一、P...