AI 모델이 함수 목록이 포함된 프롬프트를 받으면 호출을 위해 하나 이상의 함수를 선택하여 프롬프트를 완료할 수 있습니다. 모델에서 함수를 선택하는 경우 의미 체계 커널에서 호출해야 합니다.
의미 체계 커널에서 하위 시스템을 호출하는 함수에는 자동 및 수동이라는 두 가지 함수 호출 모드가 있습니다.
호출 모드에 따라 의미 체계 커널은 엔드 투 엔드 함수 호출을 수행하거나 호출자에게 함수 호출 프로세스를 제어합니다.
자동 함수 호출
자동 함수 호출은 의미 체계 커널 함수 호출 하위 시스템의 기본 모드입니다. AI 모델이 하나 이상의 함수를 선택하면 의미 체계 커널이 선택한 함수를 자동으로 호출합니다. 이러한 함수 호출의 결과는 채팅 기록에 추가되고 후속 요청에서 자동으로 모델로 전송됩니다. 그런 다음, 모델은 채팅 기록에 대한 이유를 설명하거나, 필요한 경우 추가 함수를 선택하거나, 최종 응답을 생성합니다. 이 방법은 완전히 자동화되어 있으며 호출자의 수동 개입이 필요하지 않습니다.
Tip
자동 함수 호출은 자동 함수 선택 동작과 다릅니다. 전자는 의미 체계 커널 함수를 자동으로 호출해야 하는지 여부를 지시하고, 후자는 AI 모델에서 함수를 자동으로 선택해야 하는지 여부를 결정합니다.
이 예제에서는 의미 체계 커널에서 자동 함수 호출을 사용하는 방법을 보여 줍니다. 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();
// By default, functions are set to be automatically invoked.
// If you want to explicitly enable this behavior, you can do so with the following code:
// PromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(autoInvoke: true) };
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));
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(
# By default, functions are set to be automatically invoked.
# If you want to explicitly enable this behavior, you can do so with the following code:
# function_choice_behavior=FunctionChoiceBehavior.Auto(auto_invoke=True),
function_choice_behavior=FunctionChoiceBehavior.Auto(),
)
)
response = await kernel.invoke_prompt(query, arguments=arguments)
Tip
Java SDK에 대한 추가 업데이트가 곧 제공될 예정입니다.
일부 AI 모델은 병렬 함수 호출을 지원하며, 여기서 모델은 호출을 위해 여러 함수를 선택합니다. 이는 선택한 함수를 호출하는 데 시간이 오래 걸리는 경우에 유용할 수 있습니다. 예를 들어 AI는 함수별로 왕복하는 대신 최신 뉴스와 현재 시간을 동시에 검색하도록 선택할 수 있습니다.
의미 체계 커널은 다음 두 가지 방법으로 이러한 함수를 호출할 수 있습니다.
- 순차적으로: 함수가 차례로 호출됩니다. 이 옵션은 기본 동작입니다.
-
동시: 함수가 동시에 호출됩니다. 아래 예와 같이
FunctionChoiceBehaviorOptions.AllowConcurrentInvocation속성을true(으)로 설정하여 이를 활성화할 수 있습니다.
using Microsoft.SemanticKernel;
IKernelBuilder builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<NewsUtils>();
builder.Plugins.AddFromType<DateTimeUtils>();
Kernel kernel = builder.Build();
// Enable concurrent invocation of functions to get the latest news and the current time.
FunctionChoiceBehaviorOptions options = new() { AllowConcurrentInvocation = true };
PromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(options: options) };
await kernel.InvokePromptAsync("Good morning! What is the current time and latest news headlines?", new(settings));
경우에 따라 모델에서 호출을 위해 여러 함수를 선택할 수 있습니다. 이를 병렬 함수 호출이라고도 합니다. AI 모델에서 여러 함수를 선택하면 의미 체계 커널 동시에 함수를 호출합니다.
Tip
OpenAI 또는 Azure OpenAI 커넥터를 사용하면 다음을 수행하여 병렬 함수 호출을 사용하지 않도록 설정할 수 있습니다.
from semantic_kernel.connectors.ai.open_ai import OpenAIChatPromptExecutionSettings
from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
settings = OpenAIChatPromptExecutionSettings(
function_choice_behavior=FunctionChoiceBehavior.Auto(),
parallel_tool_calls=False
)
수동 함수 호출
호출자가 함수 호출 프로세스를 더 잘 제어하려는 경우 수동 함수 호출을 사용할 수 있습니다.
수동 함수 호출을 사용하도록 설정하면 의미 체계 커널은 AI 모델에서 선택한 함수를 자동으로 호출하지 않습니다. 대신 선택한 함수 목록을 호출자에게 반환합니다. 그러면 호출할 함수를 결정하고 순차적으로 또는 병렬로 호출하고 예외를 처리하는 등의 작업을 수행할 수 있습니다. 함수 호출 결과를 채팅 기록에 추가하고 모델에 반환해야 합니다. 이로 인해 추가 함수를 선택할지 아니면 최종 응답을 생성할지 결정합니다.
아래 예제에서는 수동 함수 호출을 사용하는 방법을 보여 줍니다.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
IKernelBuilder builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<WeatherForecastUtils>();
builder.Plugins.AddFromType<DateTimeUtils>();
Kernel kernel = builder.Build();
IChatCompletionService chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
// Manual function invocation needs to be enabled explicitly by setting autoInvoke to false.
PromptExecutionSettings settings = new() { FunctionChoiceBehavior = Microsoft.SemanticKernel.FunctionChoiceBehavior.Auto(autoInvoke: false) };
ChatHistory chatHistory = [];
chatHistory.AddUserMessage("Given the current time of day and weather, what is the likely color of the sky in Boston?");
while (true)
{
ChatMessageContent result = await chatCompletionService.GetChatMessageContentAsync(chatHistory, settings, kernel);
// Check if the AI model has generated a response.
if (result.Content is not null)
{
Console.Write(result.Content);
// Sample output: "Considering the current weather conditions in Boston with a tornado watch in effect resulting in potential severe thunderstorms,
// the sky color is likely unusual such as green, yellow, or dark gray. Please stay safe and follow instructions from local authorities."
break;
}
// Adding AI model response containing chosen functions to chat history as it's required by the models to preserve the context.
chatHistory.Add(result);
// Check if the AI model has chosen any function for invocation.
IEnumerable<FunctionCallContent> functionCalls = FunctionCallContent.GetFunctionCalls(result);
if (!functionCalls.Any())
{
break;
}
// Sequentially iterating over each chosen function, invoke it, and add the result to the chat history.
foreach (FunctionCallContent functionCall in functionCalls)
{
try
{
// Invoking the function
FunctionResultContent resultContent = await functionCall.InvokeAsync(kernel);
// Adding the function result to the chat history
chatHistory.Add(resultContent.ToChatMessage());
}
catch (Exception ex)
{
// Adding function exception to the chat history.
chatHistory.Add(new FunctionResultContent(functionCall, ex).ToChatMessage());
// or
//chatHistory.Add(new FunctionResultContent(functionCall, "Error details that the AI model can reason about.").ToChatMessage());
}
}
}
참고
FunctionCallContent 및 FunctionResultContent 클래스는 각각 AI 모델 함수 호출 및 의미 체계 커널 함수 호출 결과를 나타내는 데 사용됩니다. 함수 ID, 이름 및 인수와 같은 선택한 함수에 대한 정보와 함수 호출 ID 및 결과와 같은 함수 호출 결과가 포함됩니다.
다음 예제에서는 스트리밍 채팅 완료 API와 함께 수동 함수 호출을 사용하는 방법을 보여 줍니다. 스트리밍 콘텐츠로부터 함수 호출을 생성하는 데 FunctionCallContentBuilder 클래스를 사용한다는 점에 유의하세요.
API의 스트리밍 특성으로 인해 함수 호출도 스트리밍됩니다. 즉, 호출자는 호출하기 전에 스트리밍 콘텐츠에서 함수 호출을 빌드해야 합니다.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
IKernelBuilder builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<WeatherForecastUtils>();
builder.Plugins.AddFromType<DateTimeUtils>();
Kernel kernel = builder.Build();
IChatCompletionService chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
// Manual function invocation needs to be enabled explicitly by setting autoInvoke to false.
PromptExecutionSettings settings = new() { FunctionChoiceBehavior = Microsoft.SemanticKernel.FunctionChoiceBehavior.Auto(autoInvoke: false) };
ChatHistory chatHistory = [];
chatHistory.AddUserMessage("Given the current time of day and weather, what is the likely color of the sky in Boston?");
while (true)
{
AuthorRole? authorRole = null;
FunctionCallContentBuilder fccBuilder = new ();
// Start or continue streaming chat based on the chat history
await foreach (StreamingChatMessageContent streamingContent in chatCompletionService.GetStreamingChatMessageContentsAsync(chatHistory, settings, kernel))
{
// Check if the AI model has generated a response.
if (streamingContent.Content is not null)
{
Console.Write(streamingContent.Content);
// Sample streamed output: "The color of the sky in Boston is likely to be gray due to the rainy weather."
}
authorRole ??= streamingContent.Role;
// Collect function calls details from the streaming content
fccBuilder.Append(streamingContent);
}
// Build the function calls from the streaming content and quit the chat loop if no function calls are found
IReadOnlyList<FunctionCallContent> functionCalls = fccBuilder.Build();
if (!functionCalls.Any())
{
break;
}
// Creating and adding chat message content to preserve the original function calls in the chat history.
// The function calls are added to the chat message a few lines below.
ChatMessageContent fcContent = new ChatMessageContent(role: authorRole ?? default, content: null);
chatHistory.Add(fcContent);
// Iterating over the requested function calls and invoking them.
// The code can easily be modified to invoke functions concurrently if needed.
foreach (FunctionCallContent functionCall in functionCalls)
{
// Adding the original function call to the chat message content
fcContent.Items.Add(functionCall);
// Invoking the function
FunctionResultContent functionResult = await functionCall.InvokeAsync(kernel);
// Adding the function result to the chat history
chatHistory.Add(functionResult.ToChatMessage());
}
}
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.contents.chat_history import ChatHistory
from semantic_kernel.contents.function_call_content import FunctionCallContent
from semantic_kernel.contents.function_result_content import FunctionResultContent
from semantic_kernel.kernel import Kernel
kernel = Kernel()
chat_completion_service = OpenAIChatCompletion()
# Assuming that WeatherPlugin is already implemented
kernel.add_plugin(WeatherPlugin(), "WeatherPlugin")
settings = PromptExecutionSettings(
function_choice_behavior=FunctionChoiceBehavior.Auto(auto_invoke=False),
)
chat_history = ChatHistory()
chat_history.add_user_message("What is the weather in Seattle on 10th of September 2024 at 11:29 AM?")
response = await chat_completion_service.get_chat_message_content(chat_history, settings, kernel=kernel)
function_call_content = response.items[0]
assert isinstance(function_call_content, FunctionCallContent)
# Need to add the response to the chat history to preserve the context
chat_history.add_message(response)
function = kernel.get_function(function_call_content.plugin_name, function_call_content.function_name)
function_result = await function(kernel, function_call_content.to_kernel_arguments())
function_result_content = FunctionResultContent.from_function_call_content_and_result(
function_call_content, function_result
)
# Adding the function result to the chat history
chat_history.add_message(function_result_content.to_chat_message_content())
# Invoke the model again with the function result
response = await chat_completion_service.get_chat_message_content(chat_history, settings, kernel=kernel)
print(response)
# The weather in Seattle on September 10th, 2024, is expected to be [weather condition].
참고
FunctionCallContent 및 FunctionResultContent 클래스는 각각 AI 모델 함수 호출 및 의미 체계 커널 함수 호출 결과를 나타내는 데 사용됩니다. 함수 ID, 이름 및 인수와 같은 선택한 함수에 대한 정보와 함수 호출 ID 및 결과와 같은 함수 호출 결과가 포함됩니다.
Tip
Java SDK에 대한 추가 업데이트가 곧 제공될 예정입니다.