c#通过网上AI大模型实现对话功能

ops/2024/11/25 15:58:52/

目录

    • 基础使用
    • 给大模型额外提供函数能力
      • 用Microsoft.Extensions.AI库实现
      • 用json格式回答

基础使用

https://siliconflow.cn/网站有些免费的大模型可以使用,去注册个账户,拿到apikey
引用 nuget

Microsoft.Extensions.AI.OpenAI

using Microsoft.Extensions.AI;
using OpenAI;
using System.ClientModel;namespace ConsoleApp2
{internal class Program{static async Task Main(string[] args){OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions();openAIClientOptions.Endpoint = new Uri("https://api.siliconflow.cn/v1");// SiliconCloud API Keystring mySiliconCloudAPIKey = "你的api key";OpenAIClient client = new OpenAIClient(new ApiKeyCredential(mySiliconCloudAPIKey), openAIClientOptions);IChatClient chatClient = client.AsChatClient("Qwen/Qwen2.5-7B-Instruct");while (true){var response = chatClient.CompleteStreamingAsync(Console.ReadLine());//手动输入问题await foreach (var item in response){Console.Write(item.Text);}Console.Write("\r\n\r\n");}}}
}

给大模型额外提供函数能力


using OpenAI;
using OpenAI.Chat;
using System.ClientModel;namespace ConsoleApp2
{internal class Program{static async Task Main(string[] args){OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions();openAIClientOptions.Endpoint = new Uri("https://api.siliconflow.cn/v1");// SiliconCloud API Keystring mySiliconCloudAPIKey = "你的api key";OpenAIClient client = new OpenAIClient(new ApiKeyCredential(mySiliconCloudAPIKey), openAIClientOptions);var assistantClient = client.GetChatClient("Qwen/Qwen2.5-7B-Instruct");await FunctionCallPlayground(assistantClient, "how is the weather today in beijing?");Console.ReadKey();}private static async Task FunctionCallPlayground(ChatClient chatClient, string prompt){Console.WriteLine(prompt);var messages = new[]{new UserChatMessage(  prompt)};BinaryData functionParameters = BinaryData.FromBytes("""{"type": "object","properties": {"city": {"type": "string","description": "The name of the city to query weather for."}},"required": ["city"],"additionalProperties": false}"""u8.ToArray());var chatTool = ChatTool.CreateFunctionTool("get_weather", "Get the current weather for a given city.", functionParameters);var options = new ChatCompletionOptions{Temperature = 0.01f,TopP = 0.95f,};options.Tools.Add(chatTool);var response = chatClient.CompleteChat(messages, options);// 假设我们只处理第一个工具调用请求var func1Name = response.Value.ToolCalls[0].FunctionName;var func1Args = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string,string>>( response.Value.ToolCalls[0].FunctionArguments.ToString());var func1Out = await GetWeather(func1Args["city"] );options = new ChatCompletionOptions{Temperature = 0.01f,TopP = 0.95f,};response = chatClient.CompleteChat(new ChatMessage[] { messages[0] , new ToolChatMessage(response.Value.ToolCalls[0].Id, func1Out) }, options);Console.WriteLine(response.Value.Content.FirstOrDefault()?.Text);}private static async Task<string> GetWeather(string city){return $"23摄氏度";}}}

用Microsoft.Extensions.AI库实现


using Microsoft.Extensions.AI;
using OpenAI;
using OpenAI.Chat;
using System.ClientModel;namespace ConsoleApp2
{internal class Program{const string APIKEY = "你的apikey";static async Task Main(string[] args){OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions();openAIClientOptions.Endpoint = new Uri("https://api.siliconflow.cn/v1");// SiliconCloud API Keystring mySiliconCloudAPIKey = APIKEY;var weathfunc = new GetWeatherFunction();var humidtyfunc = new GetHumidityFunction();ChatOptions chatOptions = new ChatOptions(){Temperature = 0.01f,TopP = 0.95f,Tools = new AIFunction[] { weathfunc, humidtyfunc }};OpenAIClient client = new OpenAIClient(new ApiKeyCredential(mySiliconCloudAPIKey), openAIClientOptions);IChatClient chatClient = client.AsChatClient("Qwen/Qwen2.5-7B-Instruct");while (true){var question = Console.ReadLine();//手动输入问题var response = await chatClient.CompleteAsync(question, chatOptions);_check:if(response.FinishReason == Microsoft.Extensions.AI.ChatFinishReason.ToolCalls){List<AIContent> callRets = new List<AIContent>();foreach( var aiContent in response.Choices[0].Contents){if(aiContent is FunctionCallContent callContent){var callid = callContent.CallId;var func = (AIFunction)chatOptions.Tools.FirstOrDefault(m=>((AIFunction)m).Metadata.Name == callContent.Name);var ret  =await func!.InvokeAsync(callContent.Arguments);callRets.Add(new FunctionResultContent(callid , callContent.Name , ret));}}List<Microsoft.Extensions.AI.ChatMessage> list = new List<Microsoft.Extensions.AI.ChatMessage>();list.Add(new Microsoft.Extensions.AI.ChatMessage(Microsoft.Extensions.AI.ChatRole.User, question));list.Add(new Microsoft.Extensions.AI.ChatMessage(Microsoft.Extensions.AI.ChatRole.Tool, callRets));response = await chatClient.CompleteAsync(list, chatOptions);goto _check;}else{Console.WriteLine(response.Choices[0].Contents.FirstOrDefault()?.ToString());}Console.Write("\r\n");}}}public class GetWeatherFunction : AIFunction{Microsoft.Extensions.AI.AIFunctionMetadata _Metadata;public override AIFunctionMetadata Metadata => _Metadata;public GetWeatherFunction() {_Metadata = new Microsoft.Extensions.AI.AIFunctionMetadata("get_weather"){Description = "Get the current weather for a given city.",Parameters = new[] { new AIFunctionParameterMetadata("city") { ParameterType = typeof(string),Description = "The name of the city to query weather for.",IsRequired = true,} },  };}protected override async Task<object?> InvokeCoreAsync(IEnumerable<KeyValuePair<string, object?>> arguments, CancellationToken cancellationToken){return $"23摄氏度";}}public class GetHumidityFunction : AIFunction{Microsoft.Extensions.AI.AIFunctionMetadata _Metadata;public override AIFunctionMetadata Metadata => _Metadata;public GetHumidityFunction(){_Metadata = new Microsoft.Extensions.AI.AIFunctionMetadata("get_humidity"){Description = "获取指定城市的湿度",Parameters = new[] { new AIFunctionParameterMetadata("city") {ParameterType = typeof(string),Description = "要获取湿度的城市名称",IsRequired = true,} },};}protected override async Task<object?> InvokeCoreAsync(IEnumerable<KeyValuePair<string, object?>> arguments, CancellationToken cancellationToken){return $"70%";}}}

用json格式回答

通过设置ResponseFormat 为json,可以让大模型以json格式输出

上面代码修改为:

                    List<Microsoft.Extensions.AI.ChatMessage> list = new List<Microsoft.Extensions.AI.ChatMessage>();list.Add(new Microsoft.Extensions.AI.ChatMessage(Microsoft.Extensions.AI.ChatRole.User, question));list.Add(new Microsoft.Extensions.AI.ChatMessage(Microsoft.Extensions.AI.ChatRole.Tool, callRets));ChatOptions chatOptions2 = new ChatOptions(){Temperature = 0.01f,TopP = 0.95f,MaxOutputTokens = int.MaxValue,ResponseFormat = Microsoft.Extensions.AI.ChatResponseFormat.Json,};response = await chatClient.CompleteAsync(list, chatOptions2);

注意:如果你的提问需要调用你自己定义的函数去计算,那么,提问时不要设置ResponseFormat ,否则回答类型不会是FinishReason == Microsoft.Extensions.AI.ChatFinishReason.ToolCalls,也就无法触发你的程序去调用函数了,只有计算完结果后,回传结果给大模型时才设置 ResponseFormat = Microsoft.Extensions.AI.ChatResponseFormat.Json

提问需要这么提问:
北京现在的温度和湿度是多少?请用这种格式回答:[“温度值”,“湿度值”]


http://www.ppmy.cn/ops/136612.html

相关文章

移动端自动化环境搭建_Android

adb的安装与使用 adb安装adb环境变量adb使用adb常用命令adb简介adb作用 adb安装 选择对应系统进入下载界面&#xff0c;选中版本下载即可&#xff1a; Windows版本&#xff1a;Windows Mac版本&#xff1a;Mac Linux版本&#xff1a;Linux 安装完成后&#xff0c;进行压缩&…

.Net框架以及桌面UI时间线

依托于.net框架&#xff0c;按照时间线可分为以下三种。 桌面应用的UI可分为以下三种。 2024.10.20

工业相机视场角计算

现在常用的是海康威视品牌的相机&#xff0c;打开官网查找现在相机型号&#xff0c;在详细信息里面找到像元尺寸、和分辨率 通过计算得到传感器尺寸 (1.85/1000*40247.4444)mm (1.85*3036/10005.6166)mm 当前使用镜头焦距8mm&#xff0c;可以通过计算得出视场角&#xff08;…

Redhat 10.0 beta (Red Hat Enterprise Linux)安装教程

Red Hat Enterprise Linux10.0 beta安装教程 一、镜像下载地址二、RHEL 10.0 beta 中的主要变化1、 安全2、动态编程语言、Web 和数据库服务器3、 编译器和开发工具4、身份管理5、Web 控制台 三、RHEL 10.0 beta 安装步骤1、配置虚拟机2、操作系统安装 一、镜像下载地址 1、点…

离散数学---概率, 期望

本文根据 MIT 计算机科学离散数学课程整理&#xff08;Lecture 22 ~ Lecture 24&#xff09;。 1 非负整数期望性质 用 N 表示非负整数集合&#xff0c;R 是 N 上的随机变量&#xff0c;则 R 的期望可以表示成&#xff1a; 证明&#xff1a; 换一个形式&#xff0c;把每一列…

LLM的原理理解6-10:6、前馈步骤7、使用向量运算进行前馈网络的推理8、注意力层和前馈层有不同的功能9、语言模型的训练方式10、GPT-3的惊人性能

目录 LLM的原理理解6-10: 6、前馈步骤 7、使用向量运算进行前馈网络的推理 8、注意力层和前馈层有不同的功能 注意力:特征提取 前馈层:数据库 9、语言模型的训练方式 10、GPT-3的惊人性能 一个原因是规模 大模型GPT-1。它使用了768维的词向量,共有12层,总共有1.…

idea 程序打包 jar 发布

配置 No1&#xff1a;打开程序 No2&#xff1a;进入 File-> Project Structure No3: 修改Artifacts 配置信息 No4&#xff1a;选择MainClass&#xff0c;其他选择默认 No5: 选择Ok进入下一步&#xff0c;修输出路径。点击Ok 开始构建jar包 No1&#xff1a;执行 Build-…

认识c++(c++入门)

1. C关键字 C关键字是语言本身的一部分&#xff0c;它们有特定的含义&#xff0c;并被用作程序的基础结构。以下是C标准中定义的关键字列表&#xff1a; 2. 命名空间 在C中&#xff0c;命名空间&#xff08;Namespace&#xff09;是一种用来组织代码的方法&#xff0c;它可以…