当前位置: 首页 > news >正文

网站制作人员php网站开发 课程介绍

网站制作人员,php网站开发 课程介绍,中铁建设集团门户登录网,网站首页的重要性目录 基础使用给大模型额外提供函数能力用Microsoft.Extensions.AI库实现用json格式回答 基础使用 https://siliconflow.cn/网站有些免费的大模型可以使用,去注册个账户,拿到apikey 引用 nuget Microsoft.Extensions.AI.OpenAI using Microsoft.Extensi…

目录

    • 基础使用
    • 给大模型额外提供函数能力
      • 用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.yayakq.cn/news/204534/

相关文章:

  • 中国电力建设股份有限公司官方网站宣威市住房和城乡建设局网站下载中心
  • 做百度联盟做什么类型网站福州室内设计公司排名
  • 如何做幸运28网站代理江苏高效网站制作公司
  • jsp网站专门找人做软件的网站
  • 山西大同企业做网站php做网站 价格
  • 网站图怎么做会高清网络服务提供者知道或者应当知道网络
  • 私人定制平台网站网站不备案可以用吗
  • 平面设计有几个软件广州软件系统开发seo推广
  • 网站模板 静态模版番禺有经验的网站建设
  • 辛集专业网站建设中文网址怎么注册
  • 电子商务网站开发形式选择拼多多网站首页
  • 做文库网站怎么赚钱吗网站备案 域名备案
  • 保定网站建设设计公司简单网页制作代码html
  • 网站开发如何使用API课程网站建设ppt模板下载
  • 信息网站 模板小企业网站建设5000块贵吗
  • 专业专业的网站开发百度网站收录提交入口全攻略
  • 建设部造价咨询企业网站临沂网站公司哪家好
  • 免费发布信息网站网站申请要多少钱
  • 宁波自适应网站建设特点品牌网站设计地址
  • 北京保障房建设项目网站佛山市做网站
  • 万网建网站流程网站的推广有哪些方式
  • 全网最低价业务网站移动分销系统代理
  • 极速网站制作江苏推广网站建设业务
  • wordpress源码整站单位建设网站申请信用卡
  • 都昌网站建设seo综合查询平台
  • php网站模板免费下载如何细分行业 做网站赚钱
  • 网站建设目的及功能定位开发公司各部门岗位职责
  • 三亚旅游网网站seo优缺点
  • 响应式装饰设计公司网站源码做搜狗网站优化首页
  • 创建吃的网站怎么做福建省住房和城乡建设厅网站