<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Loading

      使用 SK Plugin 給 LLM 添加能力

      前幾篇我們介紹了如何使用 SK + ollama 跟 LLM 進行基本的對話。如果只是對話的話其實不用什么 SK 也是可以的。今天讓我們給 LLM 整點活,讓它真的給我們干點啥。

      What is Plugin?

      Plugins are a key component of Semantic Kernel. If you have already used plugins from ChatGPT or Copilot extensions in Microsoft 365, you’re already familiar with them. With plugins, you can encapsulate your existing APIs into a collection that can be used by an AI. This allows you to give your AI the ability to perform actions that it wouldn’t be able to do otherwise.
      Behind the scenes, Semantic Kernel leverages function calling, a native feature of most of the latest LLMs to allow LLMs, to perform planning and to invoke your APIs. With function calling, LLMs can request (i.e., call) a particular function. Semantic Kernel then marshals the request to the appropriate function in your codebase and returns the results back to the LLM so the LLM can generate a final response.

      以上是微軟文檔的原話。說人話:
      Plugins 是 SK 的關鍵組件?;?Plugins 你可以封裝已有的 API 給 AI 使用。這給了 AI 執行動作的能力。在背后,SK 利用了大多數最新大語言模型 (LLMs) 的本地功能調用功能,使LLMs能夠進行規劃并調用您的API。通過功能調用,LLMs可以請求(即調用)特定的函數。然后,Semantic Kernel 將請求傳遞給代碼庫中的相應函數,并將結果返回給LLM,以便LLM生成最終的響應。
      說的更直白一點,Plugins 給 LLM 提供了方法調用的能力。這就比較有意思了。我們知道 LLM 是基于過往的內容訓練出來的,也就是說 LLM 并不能回答當前的一些信息,因為它不知道。比如你問它今天有什么新聞,它肯定不知道,因為這不在它的訓練集里面?;蛘吣銌査裉焯鞖庠趺礃铀膊恢馈M瑯铀矝]有辦法給你做一些特定領域的事情,比如你讓他們給某某發一條短信,它做不到,因為它沒有這個能力。但是現在有了 Plugin,這一切就有了可能。

      以下讓我們使用 SK + Plugin 給 LLM 添加感知天氣的能力。當我們問 LLM 某個城市的天氣的時候,它能精確的給出回答。
      在我們開始之前還是先試試直接問 LLM 天氣問題會得到什么結果:

      可以看到當我問 What is the weather now of SuZhou ? 后 LLM 直接說它不能獲得實時數據。

      定義 WeatherPlugin

      using System.ComponentModel;
      using Microsoft.SemanticKernel;
      
      namespace SKLearning
      {
          public sealed class WeatherPlugin
          {
              [KernelFunction, Description("Gets the weather details of a given location")]
              [return: Description("Weather details")]        
              public static async Task<string> GetWeatherByLocation([Description("name of the location")]string location)
              {
                  var key = "...";
                  var url = @$"http://api.weatherapi.com/v1/current.json?key={key}&q={location}";
                  
                  using var client = new HttpClient();
                  var response = await client.GetAsync(url);
                  var content = await response.Content.ReadAsStringAsync();
                  
                  Console.WriteLine(content);
      
                  return content;
              }
          }
      }
      

      一個 plugin 就是一個 C# 的類。在這個類里面承載了一個或N個方法。我們給方法添加描述,給入參,出參添加描述好讓 LLM 認識這個方法的作用。這個描述非常重要,請使用盡量簡潔明了的語言。
      我們可以看到 WeatherPlugin 里的 GetWeatherByLocation 是通過要給 API 實時獲取某個城市的天氣信息。這個返回值是 JSON 格式的,并不需要進行特殊的處理,LLM 可以直接識別里面的內容。

      添加 Plugin 到 kernel

      var httpClient = new HttpClient();
      var builder = Kernel.CreateBuilder()
          .AddOpenAIChatCompletion(modelId: modelId!, apiKey: null, endpoint: endpoint, httpClient: httpClient);
      var kernel = builder.Build();
      kernel.Plugins.AddFromType<WeatherPlugin>();
      

      以上代碼片段演示了如何添加 OpenAI 的 Chat 服務以及如何添加 Plugin 。

      與 AI 對話

      var settings = new OpenAIPromptExecutionSettings()
          { Temperature = 0.0, ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
      var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
      var history = new ChatHistory();
      var initMessage =
          "I am a weatherman. I can tell you the weather of any location. Try asking me about the weather of a location.";
      history.AddSystemMessage(initMessage);
      Console.WriteLine(initMessage);
      
      while (true)
      {
          Console.BackgroundColor = ConsoleColor.Black;
          Console.Write("You:");
      
          var input = Console.ReadLine();
      
          if (string.IsNullOrWhiteSpace(input))
          {
              break;
          }
      
          history.AddUserMessage(input);
          // Get the response from the AI
          var contents = await chatCompletionService.GetChatMessageContentsAsync(history, settings, kernel);
      
          foreach (var chatMessageContent in contents)
          {
              var content = chatMessageContent.Content;
              Console.BackgroundColor = ConsoleColor.DarkGreen;
              Console.WriteLine($"AI: {content}");
              history.AddMessage(chatMessageContent.Role, content ?? "");
          }
      }
      

      以上內容跟上次演示的內容沒啥特別大的區別,無非就是讀取用戶的輸入,等待 LLM 的回答。

      試一下

      讓我們運行程序,然后再次問 What is the weather now of SuZhou?
      可以看到 LLM 精確的回答出了當前蘇州的天氣:

      I am a weatherman. I can tell you the weather of any location. Try asking me about the weather of a location.

      You: What is the weather now of SuZhou?

      AI: The current weather in Suzhou, China is patchy light drizzle with a temperature of 17.2°C (62.9°F). The wind is blowing at 3.6 mph (5.8 kph) from the NNE direction. The humidity is 86% and the visibility is 5 km (3 miles).

      總結

      通過以上演示,我們自定義了一個實時獲取天氣信息的 plugin 給 LLM 使用。當我們問到天氣信息的時候 LLM 會實時調用這個方法,然后使用方法結果構造一個可讀性非常高的回答。有了 plugin 之后 LLM 真正的可以觸發一些動作,執行一些任務,獲取實時信息了。

      希望此文對你有所幫助,謝謝。

      posted @ 2025-01-04 00:22  Agile.Zhou  閱讀(199)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 久久久久无码国产精品不卡| 日本熟妇浓毛| 国产精品一区二区传媒蜜臀| 韩国三级网一区二区三区| 丰满人妻被黑人猛烈进入| 国产女人在线视频| 美女裸体视频永久免费| 日韩一区二区三区女优丝袜| 精品无码国产污污污免费| 国产午夜精品久久一二区| 真人无码作爱免费视频| 精品一区二区免费不卡| 国产福利酱国产一区二区| 白嫩少妇激情无码| 精品一二三四区在线观看| 狠狠色噜噜狠狠狠狠av不卡| 真实国产老熟女无套中出| 亚洲人成色77777在线观看 | 久久精品视频一二三四区| 国产精品久久毛片av大全日韩| 亚洲精品久久久久久婷婷| 欧美精品亚洲精品日韩专区| 中文字幕一区二区人妻| 中国熟妇毛多多裸交视频| 自贡市| 精品国产乱码久久久久久浪潮| 亚洲精品成人福利网站| av中文字幕在线二区| 国产成人无码免费视频在线| 亚洲精品麻豆一二三区| 天天色综网| 亚洲国产综合一区二区精品| 加勒比无码人妻东京热| 亚洲国产成人久久综合人| 无码av最新无码av专区| 自慰无码一区二区三区| 少妇上班人妻精品偷人| 在线视频中文字幕二区 | 亚洲精品一品二品av| 在线日韩一区二区| 国产精自产拍久久久久久蜜|