iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐡

Trying out gpt-4o-mini with Semantic Kernel

に公開

Features of gpt-4o-mini

It's reasonably smart!
It's fast!
It's cheap!

This makes it easy to use for applications that perform a large number of API calls.

Code I Tried

I will basically be reusing the code used for a demo at the Global AI Bootcamp.
At that time, I was using gpt-4 in the Sweden region for the demo, and it took about as much time as a conversation before receiving an answer. I felt it took a little less than a minute.

Program.cs
using Azure.Identity;
using ConsoleApp16;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;

IConfigurationRoot config = new ConfigurationBuilder()
    .AddEnvironmentVariables()
    .AddUserSecrets<Program>()
    .Build();

string deploymentName = config["OpenAI:DeploymentName"] ?? throw new InvalidOperationException("OpenAI:DeploymentName is not set.");
string endpoint = config["OpenAI:Endpoint"] ?? throw new InvalidOperationException("OpenAI:BaseUrl is not set.");

var builder = Kernel.CreateBuilder();
builder.AddAzureOpenAIChatCompletion(
   deploymentName,
   endpoint,
   new DefaultAzureCredential()).Build();
builder.Services.AddLogging(c => c.AddDebug().SetMinimumLevel(LogLevel.Trace));
builder.Plugins.AddFromType<WeatherPlugin>();
Kernel kernel = builder.Build();

OpenAIPromptExecutionSettings? setting = new()
{
    ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,

};

while (true)
{
    Console.Write("User > ");
    string input = Console.ReadLine()!;
    if (input == "exit")
    {
        break;
    }
    else
    {
        var result = await kernel.InvokePromptAsync(input, new(setting));
        Console.WriteLine($"Assistant > {result}");
    }
}

WeatherPlugin.cs
using Microsoft.SemanticKernel;
using System.ComponentModel;

namespace ConsoleApp16;

public class WeatherPlugin
{
    private readonly HttpClient _client = new();

    [KernelFunction, Description("""
        Gets the location code for retrieving the weather.
        Supported location codes are as follows.
        If the location is not included below, please get the weather for a nearby location instead.
        Tokyo
        Yokohama
        Nagoya
        Kyoto
        Shizuoka
        Fukui
        Niigata
        Toyama
        Kanazawa
        Gifu
        Nagano
        Takayama
        Matsumoto
        Otsu
        Osaka
        Sapporo
        Sendai
        Fukuoka
        Naha
        """)]
    public static int GetPlaceId([Description("Location to get the weather for")] string place)
    {
        var res = place switch
        {
            "Tokyo" => 130000,
            "Yokohama" => 140000,
            "Nagoya" => 230000,
            "Kyoto" => 260000,
            "Shizuoka" => 220000,
            "Fukui" => 180000,
            "Niigata" => 150000,
            "Toyama" => 160000,
            "Kanazawa" => 170000,
            "Gifu" => 210000,
            "Nagano" => 200000,
            "Takayama" => 190000,
            "Matsumoto" => 200000,
            "Otsu" => 250000,
            "Osaka" => 270000,
            "Sapporo" => 016000,
            "Sendai" => 040000,
            "Fukuoka" => 400000,
            "Naha" => 471000,
            _ => throw new ArgumentException("Region not supported.")
        };
        return res;
    }

    [KernelFunction, Description("Returns the weather for the region with the location code")]
    public async Task<string> Weather([Description("Location code")] int place)
    {
        return (await _client.GetAsync($"https://www.jma.go.jp/bosai/forecast/data/forecast/{place}.json")).Content.ReadAsStringAsync().Result;
    }

}

Execution Results

https://youtu.be/RBUHSnWmT_w?si=pPXhMsXdLE0TmF4R

I recorded the execution results including logs.
You can see that it works quite fast, executing two functions, and even provides a proper answer in Japanese based on the weather data in JSON format obtained from the Japan Meteorological Agency.
Patterns requiring the execution of multiple functions like this have now become practical.

Discussion