次の方法で共有


関数の選択の動作

関数の選択動作は、開発者が構成できる構成のビットです。

  1. AI モデルに提示される機能。
  2. モデルが呼び出しのためにそれらを選択する方法。
  3. セマンティック カーネルがこれらの関数を呼び出す方法。

現在、関数の選択動作は、 FunctionChoiceBehavior クラスの 3 つの静的メソッドによって表されています。

  • 自動: AI モデルが、指定された関数から 0 個以上の関数から呼び出し用に選択できるようにします。
  • 必須: 指定された関数から呼び出し用に 1 つ以上の関数を選択するように AI モデルに強制します。
  • なし: AI モデルに対して、関数を選択しないように指示します。

現在、関数の選択動作は、 FunctionChoiceBehavior クラスの 3 つのクラス メソッドによって表されています。

  • 自動: AI モデルが、指定された関数から 0 個以上の関数から呼び出し用に選択できるようにします。
  • 必須: 指定された関数から呼び出し用に 1 つ以上の関数を選択するように AI モデルに強制します。
  • NoneInvoke: AI モデルに対して、関数を選択しないように指示します。

注意

他の文献の None 行動に詳しいかもしれません。 python NoneInvoke キーワードとの混乱を避けるために、Noneを使用します。

  • 自動: AI モデルが、指定された関数から 0 個以上の関数から呼び出し用に選択できるようにします。
  • 必須: 指定された関数から呼び出し用に 1 つ以上の関数を選択するように AI モデルに強制します。
  • なし: AI モデルに対して、関数を選択しないように指示します。

注意

コードで ToolCallBehavior クラスで表される関数呼び出し機能を使用している場合は、 移行ガイド を参照して、コードを最新の関数呼び出しモデルに更新してください。

注意

関数呼び出し機能は、これまでにいくつかの AI コネクタでのみサポートされています。詳細については、後述の「 サポートされている AI コネクタ 」セクションを参照してください。

関数の広告

関数告知は、AIモデルに関数を提供し、それを呼び出して利用するプロセスです。 3 つの関数選択動作はすべて、 functions パラメーターとしてアドバタイズする関数の一覧を受け入れます。 既定では null です。つまり、カーネルに登録されているプラグインのすべての関数が AI モデルに提供されます。

using Microsoft.SemanticKernel;

IKernelBuilder builder = Kernel.CreateBuilder(); 
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<WeatherForecastUtils>();
builder.Plugins.AddFromType<DateTimeUtils>(); 

Kernel kernel = builder.Build();

// All functions from the DateTimeUtils and WeatherForecastUtils plugins will be sent to AI model together with the prompt.
PromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }; 

await kernel.InvokePromptAsync("Given the current time of day and weather, what is the likely color of the sky in Boston?", new(settings));

関数の一覧が提供されている場合、それらの関数のみが AI モデルに送信されます。

using Microsoft.SemanticKernel;

IKernelBuilder builder = Kernel.CreateBuilder(); 
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<WeatherForecastUtils>();
builder.Plugins.AddFromType<DateTimeUtils>(); 

Kernel kernel = builder.Build();

KernelFunction getWeatherForCity = kernel.Plugins.GetFunction("WeatherForecastUtils", "GetWeatherForCity");
KernelFunction getCurrentTime = kernel.Plugins.GetFunction("DateTimeUtils", "GetCurrentUtcDateTime");

// Only the specified getWeatherForCity and getCurrentTime functions will be sent to AI model alongside the prompt.
PromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(functions: [getWeatherForCity, getCurrentTime]) }; 

await kernel.InvokePromptAsync("Given the current time of day and weather, what is the likely color of the sky in Boston?", new(settings));

関数の空のリストは、AI モデルに関数が提供されていないことを意味します。これは、関数呼び出しを無効にすることと同じです。

using Microsoft.SemanticKernel;

IKernelBuilder builder = Kernel.CreateBuilder(); 
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<WeatherForecastUtils>();
builder.Plugins.AddFromType<DateTimeUtils>(); 

Kernel kernel = builder.Build();

// Disables function calling. Equivalent to var settings = new() { FunctionChoiceBehavior = null } or var settings = new() { }.
PromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(functions: []) }; 

await kernel.InvokePromptAsync("Given the current time of day and weather, what is the likely color of the sky in Boston?", new(settings));

関数告知は、AIモデルに関数を提供し、それを呼び出して利用するプロセスです。 既定では、 フィルター が指定されていない限り、カーネルに登録されているプラグインのすべての関数が AI モデルに提供されます。 フィルター は、次のキーを持つディクショナリです: excluded_pluginsincluded_pluginsexcluded_functionsincluded_functions。 これにより、AI モデルにアドバタイズする必要がある関数を指定できます。

重要

excluded_pluginsincluded_pluginsまたはexcluded_functionsincluded_functionsの両方を同時に指定することはできません。

from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings
from semantic_kernel.functions.kernel_arguments import KernelArguments
from semantic_kernel.kernel import Kernel

kernel = Kernel()
kernel.add_service(OpenAIChatCompletion())

# Assuming that WeatherPlugin, DateTimePlugin, and LocationPlugin are already implemented
kernel.add_plugin(WeatherPlugin(), "WeatherPlugin")
kernel.add_plugin(DateTimePlugin(), "DateTimePlugin")
kernel.add_plugin(LocationPlugin(), "LocationPlugin")

query = "What is the weather in my current location today?"
arguments = KernelArguments(
    settings=PromptExecutionSettings(
        # Advertise all functions from the WeatherPlugin, DateTimePlugin, and LocationPlugin plugins to the AI model.
        function_choice_behavior=FunctionChoiceBehavior.Auto(),
    )
)

response = await kernel.invoke_prompt(query, arguments=arguments)

フィルターが指定されている場合、フィルターに合格したフィルターのみが AI モデルに送信されます。

from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings
from semantic_kernel.functions.kernel_arguments import KernelArguments
from semantic_kernel.kernel import Kernel

kernel = Kernel()
kernel.add_service(OpenAIChatCompletion())

# Assuming that WeatherPlugin, DateTimePlugin, and LocationPlugin are already implemented
kernel.add_plugin(WeatherPlugin(), "WeatherPlugin")
kernel.add_plugin(DateTimePlugin(), "DateTimePlugin")
kernel.add_plugin(LocationPlugin(), "LocationPlugin")

query = "What is the weather in Seattle today?"
arguments = KernelArguments(
    settings=PromptExecutionSettings(
        # Advertise all functions from the WeatherPlugin and DateTimePlugin plugins to the AI model.
        function_choice_behavior=FunctionChoiceBehavior.Auto(filters={"included_plugins": ["WeatherPlugin", "DateTimePlugin"]}),
    )
)

response = await kernel.invoke_prompt(query, arguments=arguments)

重要

included_pluginsまたはincluded_functionsに空のリストを指定しても、何も有効になりません。 関数呼び出しを無効にする場合は、 function_choice_behaviorNoneInvoke に設定する必要があります。

関数告知は、AIモデルに関数を提供し、それを呼び出して利用するプロセスです。 3 つの関数選択動作はすべて、 functions パラメーターとしてアドバタイズする関数の一覧を受け入れます。 既定では null です。つまり、カーネルに登録されているプラグインのすべての関数が AI モデルに提供されます。

var chatCompletion = OpenAIChatCompletion.builder()
    .withModelId("<model-id>")
    .withOpenAIAsyncClient(new OpenAIClientBuilder()
            .credential(new AzureKeyCredential("<api-key>"))
            .endpoint("<endpoint>")
            .buildAsyncClient())
    .build();

Kernel kernel = Kernel.builder()
    .withAIService(ChatCompletionService.class, chatCompletion)
    .withPlugin(KernelPluginFactory.createFromObject(new WeatherForecastUtils(), "WeatherForecastUtils"))
    .withPlugin(KernelPluginFactory.createFromObject(new DateTimeUtils(), "DateTimeUtils"))
    .build();

InvocationContext invocationContext = InvocationContext.builder()
    // All functions from the DateTimeUtils and WeatherForecastUtils plugins will be sent to AI model together with the prompt.
    .withFunctionChoiceBehavior(FunctionChoiceBehavior.auto(true))
    .build();

var response = kernel.invokePromptAsync("Given the current time of day and weather, what is the likely color of the sky in Boston?",
    KernelArguments.builder().build(),
    invocationContext
).block();

関数の一覧が提供されている場合、それらの関数のみが AI モデルに送信されます。

var chatCompletion = OpenAIChatCompletion.builder()
    .withModelId("<model-id>")
    .withOpenAIAsyncClient(new OpenAIClientBuilder()
            .credential(new AzureKeyCredential("<api-key>"))
            .endpoint("<endpoint>")
            .buildAsyncClient())
    .build();

Kernel kernel = Kernel.builder()
    .withAIService(ChatCompletionService.class, chatCompletion)
    .withPlugin(KernelPluginFactory.createFromObject(new WeatherForecastUtils(), "WeatherForecastUtils"))
    .withPlugin(KernelPluginFactory.createFromObject(new DateTimeUtils(), "DateTimeUtils"))
    .build();

var getWeatherForCity = kernel.getFunction("WeatherPlugin", "getWeatherForCity");
var getCurrentTime = kernel.getFunction("WeatherPlugin", "getWeatherForCity");

InvocationContext invocationContext = InvocationContext.builder()
    // Only the specified getWeatherForCity and getCurrentTime functions will be sent to AI model alongside the prompt.
    .withFunctionChoiceBehavior(FunctionChoiceBehavior.auto(true, List.of(getWeatherForCity, getCurrentTime)))
    .build();

var response = kernel.invokePromptAsync("Given the current time of day and weather, what is the likely color of the sky in Boston?",
    KernelArguments.builder().build(),
    invocationContext
).block();

関数の空のリストは、AI モデルに関数が提供されていないことを意味します。これは、関数呼び出しを無効にすることと同じです。

var chatCompletion = OpenAIChatCompletion.builder()
    .withModelId("<model-id>")
    .withOpenAIAsyncClient(new OpenAIClientBuilder()
            .credential(new AzureKeyCredential("<api-key>"))
            .endpoint("<endpoint>")
            .buildAsyncClient())
    .build();

Kernel kernel = Kernel.builder()
    .withAIService(ChatCompletionService.class, chatCompletion)
    .withPlugin(KernelPluginFactory.createFromObject(new WeatherForecastUtils(), "WeatherForecastUtils"))
    .withPlugin(KernelPluginFactory.createFromObject(new DateTimeUtils(), "DateTimeUtils"))
    .build();

InvocationContext invocationContext = InvocationContext.builder()
    // Disables function calling. Equivalent to .withFunctionChoiceBehavior(null)
    .withFunctionChoiceBehavior(FunctionChoiceBehavior.auto(true, new ArrayList<>()))
    .build();

var response = kernel.invokePromptAsync("Given the current time of day and weather, what is the likely color of the sky in Boston?",
    KernelArguments.builder().build(),
    invocationContext
).block();

関数の自動選択動作の使用

Auto関数の選択動作は、指定された関数から 0 個以上の関数から呼び出しを選択するように AI モデルに指示します。

この例では、 DateTimeUtils プラグインと WeatherForecastUtils プラグインのすべての関数が、プロンプトと共に AI モデルに提供されます。 この情報はGetCurrentTime関数の入力として必要であるため、モデルは最初に呼び出しGetWeatherForCity関数を選択して現在の日時を取得します。 次に、呼び出し GetWeatherForCity 関数を選択して、取得した日時を使用してボストン市の天気予報を取得します。 この情報を使用すると、モデルはボストンの空の可能性のある色を決定できるようになります。

using Microsoft.SemanticKernel;

IKernelBuilder builder = Kernel.CreateBuilder(); 
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<WeatherForecastUtils>();
builder.Plugins.AddFromType<DateTimeUtils>(); 

Kernel kernel = builder.Build();

// All functions from the DateTimeUtils and WeatherForecastUtils plugins will be provided to AI model alongside the prompt.
PromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }; 

await kernel.InvokePromptAsync("Given the current time of day and weather, what is the likely color of the sky in Boston?", new(settings));

同じ例は、YAML プロンプト テンプレート構成で簡単にモデル化できます。

using Microsoft.SemanticKernel;

IKernelBuilder builder = Kernel.CreateBuilder(); 
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<WeatherForecastUtils>();
builder.Plugins.AddFromType<DateTimeUtils>(); 

Kernel kernel = builder.Build();

string promptTemplateConfig = """
    template_format: semantic-kernel
    template: Given the current time of day and weather, what is the likely color of the sky in Boston?
    execution_settings:
      default:
        function_choice_behavior:
          type: auto
    """;

KernelFunction promptFunction = KernelFunctionYaml.FromPromptYaml(promptTemplateConfig);

Console.WriteLine(await kernel.InvokeAsync(promptFunction));

この例では、 WeatherPlugin プラグインと DateTimePlugin プラグインのすべての関数が、プロンプトと共に AI モデルに提供されます。 この情報は、GetCurrentUtcDateTime プラグインからDateTimePlugin関数の入力として必要であるため、モデルは最初に呼び出し用のGetWeatherForCity プラグインからWeatherPlugin関数を選択して現在の日時を取得します。 次に、取得した日時を使用してシアトル市の天気予報を取得するために、呼び出しの GetWeatherForCity 関数を選択します。 この情報を使用すると、モデルは自然言語でユーザー クエリに応答できるようになります。

from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings
from semantic_kernel.functions.kernel_arguments import KernelArguments
from semantic_kernel.kernel import Kernel

kernel = Kernel()
kernel.add_service(OpenAIChatCompletion())

# Assuming that WeatherPlugin and DateTimePlugin are already implemented
kernel.add_plugin(WeatherPlugin(), "WeatherPlugin")
kernel.add_plugin(DateTimePlugin(), "DateTimePlugin")

query = "What is the weather in Seattle today?"
arguments = KernelArguments(
    settings=PromptExecutionSettings(
        # Advertise all functions from the WeatherPlugin and DateTimePlugin plugins to the AI model.
        function_choice_behavior=FunctionChoiceBehavior.Auto(),
    )
)

response = await kernel.invoke_prompt(query, arguments=arguments)

同じ例は、YAML プロンプト テンプレート構成で簡単にモデル化できます。

from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.functions.kernel_function_from_prompt import KernelFunctionFromPrompt
from semantic_kernel.kernel import Kernel

kernel = Kernel()
kernel.add_service(OpenAIChatCompletion())

# Assuming that WeatherPlugin and DateTimePlugin are already implemented
kernel.add_plugin(WeatherPlugin(), "WeatherPlugin")
kernel.add_plugin(DateTimePlugin(), "DateTimePlugin")

prompt_template_config = """
    name: Weather
    template_format: semantic-kernel
    template: What is the weather in Seattle today?
    execution_settings:
      default:
        function_choice_behavior:
          type: auto
"""
prompt_function = KernelFunctionFromPrompt.from_yaml(prompt_template_config)

response = await kernel.invoke(prompt_function)
var chatCompletion = OpenAIChatCompletion.builder()
    .withModelId("<model-id>")
    .withOpenAIAsyncClient(new OpenAIClientBuilder()
            .credential(new AzureKeyCredential("<api-key>"))
            .endpoint("<endpoint>")
            .buildAsyncClient())
    .build();

Kernel kernel = Kernel.builder()
    .withAIService(ChatCompletionService.class, chatCompletion)
    .withPlugin(KernelPluginFactory.createFromObject(new WeatherForecastUtils(), "WeatherForecastUtils"))
    .withPlugin(KernelPluginFactory.createFromObject(new DateTimeUtils(), "DateTimeUtils"))
    .build();

InvocationContext invocationContext = InvocationContext.builder()
    // All functions from the DateTimeUtils and WeatherForecastUtils plugins will be sent to AI model together with the prompt.
    .withFunctionChoiceBehavior(FunctionChoiceBehavior.auto(true))
    .build();

var response = kernel.invokePromptAsync("Given the current time of day and weather, what is the likely color of the sky in Boston?",
    KernelArguments.builder().build(),
    invocationContext
).block();

ヒント

その他の更新プログラムは、Java SDK に近日公開予定です。

必要な関数の選択動作の使用

Required動作により、モデルは、指定された関数から呼び出し用に 1 つ以上の関数を選択するように強制されます。 これは、AI モデルが独自の知識からではなく、指定された関数から必要な情報を取得する必要があるシナリオに役立ちます。

注意

この動作は、最初の要求の関数を AI モデルにのみアドバタイズし、後続の要求で関数の送信を停止して、モデルが繰り返し呼び出しのために同じ関数を選択し続ける無限ループを防ぎます。

ここでは、AI モデルが、独自の知識に基づいて推測するのではなく、ボストン市の天気予報を取得するために、呼び出しの GetWeatherForCity 関数を選択する必要があることを指定します。 モデルはまず、天気予報を取得するために呼び出し用の GetWeatherForCity 関数を選択します。 この情報を使用すると、モデルは、 GetWeatherForCityへの呼び出しからの応答を使用して、ボストンの空の可能性のある色を決定できます。

using Microsoft.SemanticKernel;

IKernelBuilder builder = Kernel.CreateBuilder(); 
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<WeatherForecastUtils>();

Kernel kernel = builder.Build();

KernelFunction getWeatherForCity = kernel.Plugins.GetFunction("WeatherForecastUtils", "GetWeatherForCity");

PromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Required(functions: [getWeatherFunction]) };

await kernel.InvokePromptAsync("Given that it is now the 10th of September 2024, 11:29 AM, what is the likely color of the sky in Boston?", new(settings));

YAML テンプレート構成の同じ例:

using Microsoft.SemanticKernel;

IKernelBuilder builder = Kernel.CreateBuilder(); 
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<WeatherForecastUtils>();

Kernel kernel = builder.Build();

string promptTemplateConfig = """
    template_format: semantic-kernel
    template: Given that it is now the 10th of September 2024, 11:29 AM, what is the likely color of the sky in Boston?
    execution_settings:
      default:
        function_choice_behavior:
          type: required
          functions:
            - WeatherForecastUtils.GetWeatherForCity
    """;

KernelFunction promptFunction = KernelFunctionYaml.FromPromptYaml(promptTemplateConfig);

Console.WriteLine(await kernel.InvokeAsync(promptFunction));

または、カーネルに登録されているすべての関数を、必要に応じて AI モデルに提供できます。 ただし、最初の要求の結果として AI モデルによって選択されたもののみがセマンティック カーネルによって呼び出されます。 前述のように、関数は、無限ループを防ぐために、後続の要求では AI モデルに送信されません。

using Microsoft.SemanticKernel;

IKernelBuilder builder = Kernel.CreateBuilder(); 
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<WeatherForecastUtils>();

Kernel kernel = builder.Build();

PromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Required() };

await kernel.InvokePromptAsync("Given that it is now the 10th of September 2024, 11:29 AM, what is the likely color of the sky in Boston?", new(settings));

ここでは、AI モデルに対して 1 つの関数 ( get_weather_for_city) のみを提供し、天気予報を取得するために呼び出し用にこの関数を選択するように強制します。

from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings
from semantic_kernel.functions.kernel_arguments import KernelArguments
from semantic_kernel.kernel import Kernel

kernel = Kernel()
kernel.add_service(OpenAIChatCompletion())

# Assuming that WeatherPlugin is already implemented with a
# get_weather_for_city function
kernel.add_plugin(WeatherPlugin(), "WeatherPlugin")

query = "What is the weather in Seattle on September 10, 2024, at 11:29 AM?"
arguments = KernelArguments(
    settings=PromptExecutionSettings(
        # Force the AI model to choose the get_weather_for_city function for invocation.
        function_choice_behavior=FunctionChoiceBehavior.Required(filters={"included_functions": ["get_weather_for_city"]}),
    )
)

response = await kernel.invoke_prompt(query, arguments=arguments)

YAML テンプレート構成の同じ例:

from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.functions.kernel_function_from_prompt import KernelFunctionFromPrompt
from semantic_kernel.kernel import Kernel

kernel = Kernel()
kernel.add_service(OpenAIChatCompletion())

# Assuming that WeatherPlugin is already implemented with a
# get_weather_for_city function
kernel.add_plugin(WeatherPlugin(), "WeatherPlugin")

prompt_template_config = """
    name: Weather
    template_format: semantic-kernel
    template: What is the weather in Seattle on September 10, 2024, at 11:29 AM?
    execution_settings:
      default:
        function_choice_behavior:
          type: auto
          filters:
            included_functions:
              - get_weather_for_city
"""
prompt_function = KernelFunctionFromPrompt.from_yaml(prompt_template_config)

response = await kernel.invoke(prompt_function)
var chatCompletion = OpenAIChatCompletion.builder()
    .withModelId("<model-id>")
    .withOpenAIAsyncClient(new OpenAIClientBuilder()
            .credential(new AzureKeyCredential("<api-key>"))
            .endpoint("<endpoint>")
            .buildAsyncClient())
    .build();

Kernel kernel = Kernel.builder()
    .withAIService(ChatCompletionService.class, chatCompletion)
    .withPlugin(KernelPluginFactory.createFromObject(new WeatherForecastUtils(), "WeatherForecastUtils"))
    .withPlugin(KernelPluginFactory.createFromObject(new DateTimeUtils(), "DateTimeUtils"))
    .build();

var getWeatherForCity = kernel.getFunction("WeatherPlugin", "getWeatherForCity");

InvocationContext invocationContext = InvocationContext.builder()
    // Force the AI model to choose the getWeatherForCity function for invocation.
    .withFunctionChoiceBehavior(FunctionChoiceBehavior.auto(true, List.of(getWeatherForCity)))
    .build();

var response = kernel.invokePromptAsync("Given that it is now the 10th of September 2024, 11:29 AM, what is the likely color of the sky in Boston?",
    KernelArguments.builder().build(),
    invocationContext
).block();

ヒント

その他の更新プログラムは、Java SDK に近日公開予定です。

None 関数の選択動作の使用

None動作では、呼び出し用に関数を選択せずに指定された関数を使用し、代わりにメッセージ応答を生成するように AI モデルに指示します。 これは、呼び出し元が実際に呼び出さずにモデルが選択する関数を確認したい場合に、ドライ ランに役立ちます。 たとえば、AI モデルの下のサンプルでは、ボストンの空の色を決定するために選択する関数が正しく一覧表示されています。


Here, we advertise all functions from the `DateTimeUtils` and `WeatherForecastUtils` plugins to the AI model but instruct it not to choose any of them.
Instead, the model will provide a response describing which functions it would choose to determine the color of the sky in Boston on a specified date.

```csharp
using Microsoft.SemanticKernel;

IKernelBuilder builder = Kernel.CreateBuilder(); 
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<WeatherForecastUtils>();
builder.Plugins.AddFromType<DateTimeUtils>(); 

Kernel kernel = builder.Build();

KernelFunction getWeatherForCity = kernel.Plugins.GetFunction("WeatherForecastUtils", "GetWeatherForCity");

PromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.None() };

await kernel.InvokePromptAsync("Specify which provided functions are needed to determine the color of the sky in Boston on a specified date.", new(settings))

// Sample response: To determine the color of the sky in Boston on a specified date, first call the DateTimeUtils-GetCurrentUtcDateTime function to obtain the 
// current date and time in UTC. Next, use the WeatherForecastUtils-GetWeatherForCity function, providing 'Boston' as the city name and the retrieved UTC date and time. 
// These functions do not directly provide the sky's color, but the GetWeatherForCity function offers weather data, which can be used to infer the general sky condition (e.g., clear, cloudy, rainy).

YAML プロンプト テンプレート構成の対応する例:

using Microsoft.SemanticKernel;

IKernelBuilder builder = Kernel.CreateBuilder(); 
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<WeatherForecastUtils>();
builder.Plugins.AddFromType<DateTimeUtils>(); 

Kernel kernel = builder.Build();

string promptTemplateConfig = """
    template_format: semantic-kernel
    template: Specify which provided functions are needed to determine the color of the sky in Boston on a specified date.
    execution_settings:
      default:
        function_choice_behavior:
          type: none
    """;

KernelFunction promptFunction = KernelFunctionYaml.FromPromptYaml(promptTemplateConfig);

Console.WriteLine(await kernel.InvokeAsync(promptFunction));

NoneInvoke動作では、呼び出し用に関数を選択せずに指定された関数を使用し、代わりにメッセージ応答を生成するように AI モデルに指示します。 これは、呼び出し元が実際に呼び出さずにモデルが選択する関数を確認したい場合に、ドライ ランに役立ちます。 たとえば、AI モデルの下のサンプルでは、ボストンの空の色を決定するために選択する関数が正しく一覧表示されています。

from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings
from semantic_kernel.functions.kernel_arguments import KernelArguments
from semantic_kernel.kernel import Kernel

kernel = Kernel()
kernel.add_service(OpenAIChatCompletion())

# Assuming that WeatherPlugin and DateTimePlugin are already implemented
kernel.add_plugin(WeatherPlugin(), "WeatherPlugin")
kernel.add_plugin(DateTimePlugin(), "DateTimePlugin")

query = "Specify which provided functions are needed to determine the color of the sky in Boston on the current date."
arguments = KernelArguments(
    settings=PromptExecutionSettings(
        # Force the AI model to choose the get_weather_for_city function for invocation.
        function_choice_behavior=FunctionChoiceBehavior.NoneInvoke(),
    )
)

response = await kernel.invoke_prompt(query, arguments=arguments)
# To determine the color of the sky in Boston on the current date, you would need the following functions:
# 1. **functions.DateTimePlugin-get_current_date**: This function is needed to get the current date.
# 2. **functions.WeatherPlugin-get_weather_for_city**: After obtaining the current date,
#    this function will allow you to get the weather for Boston, which will indicate the sky conditions
#    such as clear, cloudy, etc., helping you infer the color of the sky.

YAML テンプレート構成の同じ例:

from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.functions.kernel_function_from_prompt import KernelFunctionFromPrompt
from semantic_kernel.kernel import Kernel

kernel = Kernel()
kernel.add_service(OpenAIChatCompletion())

# Assuming that WeatherPlugin and DateTimePlugin are already implemented
kernel.add_plugin(WeatherPlugin(), "WeatherPlugin")
kernel.add_plugin(DateTimePlugin(), "DateTimePlugin")

prompt_template_config = """
    name: BostonSkyColor
    template_format: semantic-kernel
    template: Specify which provided functions are needed to determine the color of the sky in Boston on the current date.
    execution_settings:
      default:
        function_choice_behavior:
          type: none
"""
prompt_function = KernelFunctionFromPrompt.from_yaml(prompt_template_config)

response = await kernel.invoke(prompt_function)
# To determine the color of the sky in Boston on the current date, you would need the following functions:
# 1. **functions.DateTimePlugin-get_current_date**: This function is needed to get the current date.
# 2. **functions.WeatherPlugin-get_weather_for_city**: After obtaining the current date,
#    this function will allow you to get the weather for Boston, which will indicate the sky conditions
#    such as clear, cloudy, etc., helping you infer the color of the sky.

ここでは、 DateTimeUtils および WeatherForecastUtils プラグインからすべての機能を AI モデルにアドバタイズしますが、いずれかを選択しないように指示します。 代わりに、モデルは、指定された日付にボストンの空の色を決定するために選択する関数を説明する応答を提供します。

var chatCompletion = OpenAIChatCompletion.builder()
    .withModelId("<model-id>")
    .withOpenAIAsyncClient(new OpenAIClientBuilder()
            .credential(new AzureKeyCredential("<api-key>"))
            .endpoint("<endpoint>")
            .buildAsyncClient())
    .build();

Kernel kernel = Kernel.builder()
    .withAIService(ChatCompletionService.class, chatCompletion)
    .withPlugin(KernelPluginFactory.createFromObject(new WeatherForecastUtils(), "WeatherForecastUtils"))
    .withPlugin(KernelPluginFactory.createFromObject(new DateTimeUtils(), "DateTimeUtils"))
    .build();

InvocationContext invocationContext = InvocationContext.builder()
    // All functions from the WeatherForecastUtils and DateTimeUtils plugins will be sent to AI model together with the prompt.
    .withFunctionChoiceBehavior(FunctionChoiceBehavior.none())
    .build();

var response = kernel.invokePromptAsync("Specify which provided functions are needed to determine the color of the sky in Boston on a specified date.",
    KernelArguments.builder().build(),
    invocationContext
).block();
// Sample response: To determine the color of the sky in Boston on a specified date, first call the DateTimeUtils-GetCurrentUtcDateTime function to obtain the 
// current date and time in UTC. Next, use the WeatherForecastUtils-GetWeatherForCity function, providing 'Boston' as the city name and the retrieved UTC date and time. 
// These functions do not directly provide the sky's color, but the GetWeatherForCity function offers weather data, which can be used to infer the general sky condition (e.g., clear, cloudy, rainy).

ヒント

その他の更新プログラムは、Java SDK に近日公開予定です。

関数選択の動作オプション

関数の選択動作の特定の側面は、各関数選択動作クラスがoptions型の FunctionChoiceBehaviorOptions コンストラクター パラメーターを使用して受け入れるオプションを使用して構成できます。 次のオプションを使用できます。

  • AllowConcurrentInvocation: このオプションを使用すると、セマンティック カーネルによる関数の同時呼び出しが可能になります。 既定では false に設定されています。つまり、関数は順番に呼び出されます。 同時呼び出しは、AI モデルが 1 つの要求で呼び出しに複数の関数を選択できる場合にのみ可能です。それ以外の場合、順次呼び出しと同時呼び出しの区別はありません

  • AllowParallelCalls: このオプションを使用すると、AI モデルは 1 つの要求で複数の関数を選択できます。 一部の AI モデルでは、この機能がサポートされない場合があります。このような場合、このオプションは無効になります。 既定では、このオプションは null に設定され、AI モデルの既定の動作が使用されることを示します。

    The following table summarizes the effects of various combinations of the AllowParallelCalls and AllowConcurrentInvocation options:
    
    | AllowParallelCalls  | AllowConcurrentInvocation | # of functions chosen per AI roundtrip  | Concurrent Invocation by SK |
    |---------------------|---------------------------|-----------------------------------------|-----------------------|
    | false               | false                     | one                                     | false                 |
    | false               | true                      | one                                     | false*                |
    | true                | false                     | multiple                                | false                 |
    | true                | true                      | multiple                                | true                  |
    
    `*` There's only one function to invoke
    

関数の選択動作の特定の側面は、各関数選択動作クラスがoptions型の FunctionChoiceBehaviorOptions コンストラクター パラメーターを使用して受け入れるオプションを使用して構成できます。 次のオプションを使用できます。

  • AllowParallelCalls: このオプションを使用すると、AI モデルは 1 つの要求で複数の関数を選択できます。 一部の AI モデルでは、この機能がサポートされない場合があります。このような場合、このオプションは無効になります。 既定では、このオプションは null に設定され、AI モデルの既定の動作が使用されることを示します。

関数呼び出し

関数呼び出しは、セマンティック カーネルが AI モデルによって選択された関数を呼び出すプロセスです。 関数呼び出しの詳細については、関数呼び出しに関する記事 参照してください

サポートされている AI コネクタ

現在、セマンティック カーネルの次の AI コネクタでは、関数呼び出しモデルがサポートされています。

AI コネクタ 関数選択挙動 ToolCallBehavior
Anthropic 計画済み
AzureAIInference 間もなく利用できます
AzureOpenAI ✔️ ✔️
双子座 計画済み ✔️
HuggingFace 計画済み
ミストラル 計画済み ✔️
Ollama 間もなく利用できます
Onnx 間もなく利用できます
OpenAI ✔️ ✔️

現在、セマンティック カーネルの次の AI コネクタでは、関数呼び出しモデルがサポートされています。

AI コネクタ 関数選択挙動 ToolCallBehavior
Anthropic ✔️
AzureAIInference ✔️
岩盤 ✔️
Google AI ✔️
Vertex AI ✔️
HuggingFace 計画済み
Mistral AI(ミストラルAI) ✔️
Ollama ✔️
Onnx
OpenAI ✔️ ✔️
Azure OpenAI ✔️ ✔️

警告

すべてのモデルが関数呼び出しをサポートしているわけではありませんが、一部のモデルでは非ストリーミング モードでの関数呼び出しのみがサポートされています。 関数呼び出しを使用する前に、使用しているモデルの制限事項を理解してください。

現在、セマンティック カーネルの次の AI コネクタでは、関数呼び出しモデルがサポートされています。

AI コネクタ 関数選択挙動 ToolCallBehavior
AzureOpenAI ✔️ ✔️
双子座 計画済み ✔️
OpenAI ✔️ ✔️