动态

详情 返回 返回

探索Semantic Plugins:開啓大模型的技能之門 - 动态 详情

前言

在之前的章節中我們或多或少的已經接觸到了 Semantic KernelPlugins,本章我們講詳細介紹如何使用插件。

Semantic Kernel 的一大特點是擁有強大的插件,通過結合自定義/預定義的插件解決智能業務的問題。讓傳統的代碼和智能插件一起工作靈活地接入到應用場景簡化傳統應用向智能化轉型的過程。

什麼是Plugins

image

我們知道LLMs(大模型)的訓練數據和我們使用之間有時間差,還有一個問題 LLMs 對自己企業化內的知識認知有缺陷。OpenAI通過插件將ChatGPT和第三方的應用程序之間進行連接,這些插件使 ChatGPT 能夠與開發人員定義的 API 進行交互,從而增強 ChatGPT 的功能並允許有更廣泛的操作,如:

  • 檢索實時信息,例如,體育賽事比分、股票價格、最新新聞等。
  • 檢索知識庫信息, 例如,公司文檔、個人筆記等。
  • 協助用户進行相關操作,例如,預訂航班、公司內預定會議、訂餐等。

Semantic Kernel 遵循 OpenAI 的插件的插件規範,可以很方便地接入和導出插件(如基於 Bing, Microsoft 365, OpenAI 的插件),這樣可以讓開發人員很簡單地調用不同的插件服務。除了兼容 OpenAI 的插件外,Semantic Kernel 內也有屬於自己插件定義的方式。不僅可以在規定模版格式上定義 Plugins, 更可以在函數內定義 Plugins.

從高層次上理解插件是一組可以公開給 AI 應用程序和服務的功能。

插件要提供在語義上描述其行為方式的詳細信息,從函數的輸入、輸出到副作用,一切都需要以 AI 可以理解的方式進行描述.

定義插件

在 Semantic Kernel 中定義 Plugins 插件有兩種方式,第一種是通過模版定義插件也叫Semantic Plugins(語義插件),第二種是通過函數創建插件也叫 Native Plugins(本地插件)

Sermantic Plugins

通過模版定義插件
我們知道可以通過Prompts(提示詞工程)可以和LLMs進行對話,我們在處理一系列特定業務過程中,可能不止一個Prompts,可能是一組Prompts的集合。我們可以把這些針對業務能力的Prompts集合放到Semantic Kernel的插件集合內。

模版格式

Semantic Kernel 模版定義格式有固定的格式,Prompts(提示詞)都放在 skprompt.txt 文件內,而相關參數設置都放在 config.json 文件內,文件結構參考如下圖

     const string ConfigFile = "config.json";
     const string PromptFile = "skprompt.txt";

這些都是在 SK 寫死的配置,所以插件內的命名一定要遵循這個規則!

|-plugins
    |-Prompts
        |-Translator
            |-skprompt.txt
            |-config.json
    |-WriterPlugins
        |-Joke
            |-skprompt.txt
            |-config.json
        |-ShortPoem
            |-skprompt.txt
            |-config.json

skprompt.txt

我們先來看看 skprompt.txt 的定義,這裏一般是放置和業務相關的 Prompt,可以支持多個參數,每個參數都放置在 {{$參數名}} 內,如以下格式:

Translate {{$input}} into {{$language}}

在之前的章節我們介紹過這是SKTemplateFormat的默認格式"semantic-kernel"

config.json

這是配置相關的內容,隨了設置和 LLMs 相關的參數外,你也可以設定輸入的參數以及相關描述

{
  "schema": 1,
  "description": "Translate sentenses into a language of your choice",
  "execution_settings": {
    "default": {
      "max_tokens": 2000,
      "temperature": 0.7,
      "top_p": 0.0,
      "presence_penalty": 0.0,
      "frequency_penalty": 0.0,
      "stop_sequences": ["[done]"]
    }
  },
  "input_variables": [
    {
      "name": "input",
      "description": "sentense to translate",
      "default": ""
    },
    {
      "name": "language",
      "description": "Language to translate to",
      "default": ""
    }
  ]
}

這其實就是對PromptTemplateConfig提示詞模版配置類的 json 數據,最後在 SK 內會被反序列化到對象內。

   // Load prompt configuration. Note: the configuration is optional.
   var configPath = Path.Combine(functionDirectory, ConfigFile);
   var promptConfig = File.Exists(configPath) ?
                PromptTemplateConfig.FromJson(File.ReadAllText(configPath)) :
                new PromptTemplateConfig();

之前我們對PromptTemplateConfig類進行過詳細的講解,不熟悉的可以看看深入學習 Semantic Kernel:創建和配置 prompts functions。

從解決方案的角度看一下配置的目錄圖

image

註冊 Semantic Plugins

要從 Semantic Kernel 中要實現Semantic Plugins模板化插件的註冊,需要KernelExtensions類中的CreatePluginFromPromptDirectory擴展方法。

再開始之前在我們代碼的解決方案Plugins文件夾下對每一個skprompt.txtconfig.json進行生成設置

image

核心代碼

var kernel = Kernel.CreateBuilder()
    .AddAzureOpenAIChatCompletion(config.ModelId, endpoint: config.Endpoint, apiKey: config.ApiKey)
    .Build();
//註冊插件
string folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
kernel.ImportPluginFromPromptDirectory(folder);

string[] pluginNames = ["Prompts", "WriterPlugins"];

foreach (var pluginName in pluginNames)
{
    kernel.ImportPluginFromPromptDirectory(Path.Combine(folder, pluginName));
}

//測試從插件獲得funciton
var jokeKernelFunction = kernel.Plugins.GetFunction("Prompts", "Translator");
Console.WriteLine("System: 請輸入要翻譯的內容");
var userResuest = Console.ReadLine();
Console.WriteLine("System: 請輸入要翻譯的語言語種");
var language = Console.ReadLine();

var results = await jokeKernelFunction.InvokeAsync(kernel, new KernelArguments()
{
  {"input", userResuest},
  {"language", language}
});

Console.WriteLine($"Assistant: {results.ToString()}");

插件名稱約定

ImportPluginFromPromptDirectory這個方法在註冊插件過程中如果沒有指定插件名字會默認用文件夾名稱

   pluginName ??= new DirectoryInfo(pluginDirectory).Name;

輸出

System: 請輸入要翻譯的內容
User: 那麼近那麼美週末去河北
System: 請輸入要翻譯的語言語種
User: 英文
Assistant: So close, so beautiful, go to Hebei for the weekend.

最後

本章我們詳細介紹瞭如何使用 Semantic Kernel 的插件功能,包括插件的概念、定義插件的兩種方式(Semantic Plugins 和 Native Plugins)、以及如何註冊和調用 Semantic Plugins。通過插件,我們可以擴展 ChatGPT 的功能,使其能夠與第三方應用程序進行連接,實現更廣泛的操作和服務。

通過註冊插件並調用相應函數,我們可以實現諸如翻譯、笑話生成等功能。在下一篇中,我們將關注 Native Plugins 原生函數插件的介紹。

參考文獻

  • 開啓大模型的技能之門 - Plugins
  • Understanding AI plugins in Semantic Kernel

示例代碼

本文源代碼

Add a new 评论

Some HTML is okay.