| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- using Microsoft.Extensions.AI;
- using OpenAI.Chat;
- using System.Diagnostics;
- using System.Reflection.Metadata.Ecma335;
- using System.Text.Json;
- using System.Threading;
- namespace DFS.AI.API.Helper
- {
- /// <summary>
- /// 大模型工具应用
- /// </summary>
- public class FunToolHandle
- {
- public async Task<string> Demo()
- {
- var chatClient = AIClientHelper.GetOllamaApiClient();
- // 创建模拟工具集
- var monitoringTools = new[]
- {
- AIFunctionFactory.Create((string city) =>
- {
- Thread.Sleep(500); // 模拟API延迟
- return new { Temperature = Random.Shared.Next(15, 35), Humidity = Random.Shared.Next(40, 80) };
- }, "get_weather", "获取指定城市的天气信息"),
- AIFunctionFactory.Create((string city) =>
- {
- Thread.Sleep(300);
- return new { Hotels = Random.Shared.Next(50, 200), AvgPrice = Random.Shared.Next(300, 1500) };
- }, "get_hotels", "查询指定城市的酒店数量和平均价格"),
- AIFunctionFactory.Create((int temperature) =>
- {
- return temperature switch
- {
- < 15 => "建议穿冬装,携带保暖衣物",
- < 25 => "建议穿春秋装,温度适宜",
- _ => "建议穿夏装,注意防晒"
- };
- }, "suggest_clothing", "根据温度推荐穿搭")
- };
- // 构建带监控的客户端
- int iterationCount = 0;
- int functionCallCount = 0;
- var monitoredClient = chatClient.AsBuilder()
- .UseFunctionInvocation(configure: options =>
- {
- options.AllowConcurrentInvocation = true;
- options.MaximumIterationsPerRequest = 10;
- options.FunctionInvoker = async (context, cancellationToken) =>
- {
- functionCallCount++;
- var sw = Stopwatch.StartNew();
- Console.WriteLine($"\n🔵 [函数调用 #{functionCallCount}]");
- Console.WriteLine($" 函数名: {context.Function.Name}");
- Console.WriteLine($" 参数: {JsonSerializer.Serialize(context.Arguments)}");
- var result = await context.Function.InvokeAsync(context.Arguments, cancellationToken);
- sw.Stop();
- Console.WriteLine($" 结果: {result}");
- Console.WriteLine($" 耗时: {sw.ElapsedMilliseconds}ms");
- return result;
- };
- })
- .Build();
- // 执行复杂查询
- Console.WriteLine("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
- Console.WriteLine("📋 用户查询:帮我查询北京和上海的天气,并根据北京的温度推荐穿搭\n");
- var monitoringOptions = new ChatOptions
- {
- ToolMode = ChatToolMode.Auto,
- AllowMultipleToolCalls = true,
- Tools = monitoringTools
- };
- var monitoringResult = await monitoredClient.GetResponseAsync(
- "帮我查询北京和上海的天气,并根据北京的温度推荐穿搭",
- monitoringOptions
- );
- Console.WriteLine("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
- Console.WriteLine("✅ 最终响应:");
- Console.WriteLine(monitoringResult.Text);
- Console.WriteLine($"\n📊 统计: 共 {iterationCount} 次迭代,{functionCallCount} 次函数调用");
- return monitoringResult.Text;
- }
- /// <summary>
- /// 自助加油识别计算总价,返回定量升数
- /// </summary>
- /// <returns></returns>
- public async Task FuelPrices()
- {
- var chatClient = AIClientHelper.GetOllamaApiClient();
- // 创建模拟工具集
- var monitoringTools = new[]
- {
- AIFunctionFactory.Create((decimal prices,int fuel) =>
- {
- //var L=0;
- //switch (switch_on)
- //{
- // default:
- //}
- }, "cal_liters", "根据金额计算油品升数"),
- };
- }
- }
- }
|