0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Semantic Kernel: Phi-4 mini のツール

Posted at

Phi4-mini はツールのサポートを開始しましたが、最初の試用では理想的ではありませんでした。kenfey が最近、この問題を解決するための記事を公開しました。詳細はこちらをご覧ください: https://techcommunity.microsoft.com/blog/educatordeveloperblog/building-ai-agents-on-edge-devices-using-ollama--phi-4-mini-function-calling/4391029。この記事では、SKを使った具体的な例を詳しく説明します。

まず、ollama をインストールし、次に phi4-mini をインストールします。コマンドは以下の通りです:

ollama pull phi4-mini

もともと生成された phi4-mini にはツールのサポートに問題がありました。ここでは modelfile を交換する必要があります。以下のコマンドで元の modelfile を確認し、FROM のアドレス情報を取得します:

ollama show phi4-mini --modelfile

次に、修正済みの Modefile を保存するディレクトリを見つけてください。このファイルには拡張子がありません。

FROM C:\Users\axzxs\.ollama\models\blobs\sha256-3c168af1dea0a414299c7d9077e100ac763370e5a98b3c53801a958a47f0a5db
TEMPLATE """{{- if or .System .Tools }}<|system|>{{ if .System }}{{ .System }}{{ end }}{{- if .Tools }}{{- if not .System }}You may call one or more functions to assist with the user query. You are provided with function signatures.{{- end }}<|tool|>[{{- range .Tools }}{{ .Function }}{{ end }}]<|/tool|><|end|>{{- end }}{{- end }}{{- range $i, $_ := .Messages }}{{- $last := eq (len (slice $.Messages $i)) 1 -}}{{- if ne .Role "system" }}<|{{ .Role }}|>{{ .Content }}{{- if .ToolCalls }}<|tool_call|>[{{ range .ToolCalls }}{"name":"{{ .Function.Name }}","arguments":{{ .Function.Arguments }}}{{ end }}]<|/tool_call|>{{- end }}{{- if not $last }}<|end|>{{- end }}{{- if and (ne .Role "assistant") $last }}<|end|><|assistant|>{{ end }}{{- end }}{{- end }}"""

次に、以下のコマンドを使用して Modelfile を書き換えます:

ollama create phi4-mini -f "あなたのModelfileのパス"

SK が Phi4-mini ツールを呼び出すコードは以下の通りです:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.Ollama;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using OllamaSharp;
using OllamaSharp.Models;
using OpenAI.RealtimeConversation;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
#pragma warning disable

await Call();
Console.ReadLine();

async Task Call()
{
    var builder = Kernel.CreateBuilder();
    var modelId = "phi4-mini";
    var endpoint = new Uri("http://localhost:11434");
    builder.Services.AddOllamaChatCompletion(modelId, endpoint);
    builder.Plugins.AddFromType<OrderPlugin>();
    var kernel = builder.Build();
    var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
    var settings = new OllamaPromptExecutionSettings { FunctionChoiceBehavior = FunctionChoiceBehavior.None() };
    Console.Write(">>> ");
    string? input = "受注番号SN0000111の注文総額を取得しますか?";
    Console.WriteLine(input);
    try
    {
        ChatMessageContent chatResult = await chatCompletionService.GetChatMessageContentAsync(input, settings, kernel);
        Console.Write($"\n>>> Result: {chatResult}\n\n> ");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}\n\n> ");
    }
}

public class OrderPlugin
{
    [KernelFunction]
    [Description("受注総額を取得する")]
    public decimal GetOrderAmount([Description("受注番号")] string orderNo)
    {
        Console.WriteLine($"受注番号:{orderNo},受注金額:12345.67");
        return 12345.67m;
    }
}

実行結果は以下の通りです:

画像

試用結果を見ても、毎回期待通りにはいかず、ツールの呼び出し成功率はまだ理想的とはいえません。公式のさらなる改善と成功率向上を期待したいと思います。

(Translated by GPT)
元のリンク:https://mp.weixin.qq.com/s/fWEaem_bxCTjxxWrRoYnDA?token=1135395277&lang=zh_CN&wt.mc_id=MVP_325642

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?