FunToolHandle.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using Microsoft.Extensions.AI;
  2. using OpenAI.Chat;
  3. using System.Diagnostics;
  4. using System.Reflection.Metadata.Ecma335;
  5. using System.Text.Json;
  6. using System.Threading;
  7. namespace DFS.AI.API.Helper
  8. {
  9. /// <summary>
  10. /// 大模型工具应用
  11. /// </summary>
  12. public class FunToolHandle
  13. {
  14. public async Task<string> Demo()
  15. {
  16. var chatClient = AIClientHelper.GetOllamaApiClient();
  17. // 创建模拟工具集
  18. var monitoringTools = new[]
  19. {
  20. AIFunctionFactory.Create((string city) =>
  21. {
  22. Thread.Sleep(500); // 模拟API延迟
  23. return new { Temperature = Random.Shared.Next(15, 35), Humidity = Random.Shared.Next(40, 80) };
  24. }, "get_weather", "获取指定城市的天气信息"),
  25. AIFunctionFactory.Create((string city) =>
  26. {
  27. Thread.Sleep(300);
  28. return new { Hotels = Random.Shared.Next(50, 200), AvgPrice = Random.Shared.Next(300, 1500) };
  29. }, "get_hotels", "查询指定城市的酒店数量和平均价格"),
  30. AIFunctionFactory.Create((int temperature) =>
  31. {
  32. return temperature switch
  33. {
  34. < 15 => "建议穿冬装,携带保暖衣物",
  35. < 25 => "建议穿春秋装,温度适宜",
  36. _ => "建议穿夏装,注意防晒"
  37. };
  38. }, "suggest_clothing", "根据温度推荐穿搭")
  39. };
  40. // 构建带监控的客户端
  41. int iterationCount = 0;
  42. int functionCallCount = 0;
  43. var monitoredClient = chatClient.AsBuilder()
  44. .UseFunctionInvocation(configure: options =>
  45. {
  46. options.AllowConcurrentInvocation = true;
  47. options.MaximumIterationsPerRequest = 10;
  48. options.FunctionInvoker = async (context, cancellationToken) =>
  49. {
  50. functionCallCount++;
  51. var sw = Stopwatch.StartNew();
  52. Console.WriteLine($"\n🔵 [函数调用 #{functionCallCount}]");
  53. Console.WriteLine($" 函数名: {context.Function.Name}");
  54. Console.WriteLine($" 参数: {JsonSerializer.Serialize(context.Arguments)}");
  55. var result = await context.Function.InvokeAsync(context.Arguments, cancellationToken);
  56. sw.Stop();
  57. Console.WriteLine($" 结果: {result}");
  58. Console.WriteLine($" 耗时: {sw.ElapsedMilliseconds}ms");
  59. return result;
  60. };
  61. })
  62. .Build();
  63. // 执行复杂查询
  64. Console.WriteLine("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
  65. Console.WriteLine("📋 用户查询:帮我查询北京和上海的天气,并根据北京的温度推荐穿搭\n");
  66. var monitoringOptions = new ChatOptions
  67. {
  68. ToolMode = ChatToolMode.Auto,
  69. AllowMultipleToolCalls = true,
  70. Tools = monitoringTools
  71. };
  72. var monitoringResult = await monitoredClient.GetResponseAsync(
  73. "帮我查询北京和上海的天气,并根据北京的温度推荐穿搭",
  74. monitoringOptions
  75. );
  76. Console.WriteLine("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
  77. Console.WriteLine("✅ 最终响应:");
  78. Console.WriteLine(monitoringResult.Text);
  79. Console.WriteLine($"\n📊 统计: 共 {iterationCount} 次迭代,{functionCallCount} 次函数调用");
  80. return monitoringResult.Text;
  81. }
  82. /// <summary>
  83. /// 自助加油识别计算总价,返回定量升数
  84. /// </summary>
  85. /// <returns></returns>
  86. public async Task FuelPrices()
  87. {
  88. var chatClient = AIClientHelper.GetOllamaApiClient();
  89. // 创建模拟工具集
  90. var monitoringTools = new[]
  91. {
  92. AIFunctionFactory.Create((decimal prices,int fuel) =>
  93. {
  94. //var L=0;
  95. //switch (switch_on)
  96. //{
  97. // default:
  98. //}
  99. }, "cal_liters", "根据金额计算油品升数"),
  100. };
  101. }
  102. }
  103. }