Microsoft.Extensions.AI を使って JSON スキーマを生成する

に公開

以前 .NET 9 の JSON Schema 生成機能の挙動確認 という記事で System.Text.Json の JSON スキーマ生成機能を試してみましたが、今回は Microsoft.Extensions.AI を使って JSON スキーマを生成してみます。

というのも今朝見てみたら長らくプレビューバージョンだった Microsoft.Extensions.AI からプレビューが取れていたので、そろそろ本番でも使えるのかなと思ったからです。(まだ色々プレビューなものもあるのであと一歩)

この Microsoft.Extensions.AI.Abstractions には JSON スキーマを生成するための AIJsonUtilities クラスが用意されています。これを使うと JSON スキーマを生成することができます。このクラスは AI に渡すための様々な形の JSON を生成する機能があります。そのため System.Text.Json ではデフォルトで対応していない Description 属性を最初から考慮してくれます。便利…!

使ってみよう

ということで早速使っていきます。コンソールアプリのプロジェクトを作って Microsoft.Extensions.AI.Abstractions v9.5.0 を NuGet からインストールします。

試しに record を JSON スキーマに変換してみます。

using Microsoft.Extensions.AI;
using System.ComponentModel;

var jsonSchema = AIJsonUtilities.CreateJsonSchema(typeof(Person));
Console.WriteLine(jsonSchema);

[Description("This is a person class")]
record Person(
    [property: Description("This is the name of the person")]
    string Name,
    [property: Description("This is the age of the person")]
    int Age);

実行すると以下のような結果になりました。

{
    "description": "This is a person class",
    "type": "object",
    "properties": {
        "name": {
            "description": "This is the name of the person",
            "type": "string"
        },
        "age": {
            "description": "This is the age of the person",
            "type": "integer"
        }
    },
    "required": [
        "name",
        "age"
    ]
}

ちゃんと Description 属性の内容が反映されています。AI に渡すための JSON スキーマを生成する必要がある場合は、これからは、こちらを使ってもいいかもしれませんね。この他にも AIJsonUtilities はメソッドから JSON スキーマを生成することもできます。やり方は MethodBase を渡すだけです。やってみましょう。

using Microsoft.Extensions.AI;
using System.ComponentModel;
using System.Reflection;

var jsonSchema = AIJsonUtilities.CreateFunctionJsonSchema(
    typeof(Sample).GetMethod(nameof(Sample.GetSomethingAsync),
    BindingFlags.Public | BindingFlags.Static)!);
Console.WriteLine(jsonSchema);


class Sample
{
    [Description("This is a sample method.")]
    public static async Task<string> GetSomethingAsync(
        [Description("This is a sample parameter.")]
    string input,
        CancellationToken cancellationToken = default)
    {
        throw new NotImplementedException();
    }
}

実行すると以下のような結果になります。(フォーマットはこちらでおこないました。)

{
    "title": "GetSomethingAsync",
    "description": "This is a sample method.",
    "type": "object",
    "properties": {
        "input": {
            "description": "This is a sample parameter.",
            "type": "string"
        }
    },
    "required": [
        "input"
    ]
}

Description 属性の内容が反映されているのがわかります。

まとめ

Microsoft.Extensions.AI.Abstractions には JSON スキーマを生成するための AIJsonUtilities クラスが用意されています。これを使うと JSON スキーマを生成することができます。System.Text.Json ではデフォルトで対応していない Description 属性を考慮してくれるので、AI に渡すための JSON スキーマを生成する必要がある場合は、こちらを使うのも検討してみてもいいと思います。

使い方も単純なので AI に渡すための JSON スキーマ生成したい!という場合は試してみてくださいと思いつつ、JSON スキーマ生成するためだけに Microsoft.Extensions.AI を使うのはオーバーキル感もあります。ということで AI を使うときは基本的に Microsoft.Extensions.AI を使えば問題なしですね…!

Microsoft (有志)

Discussion