分步规划器迁移指南

本迁移指南演示如何从 FunctionCallingStepwisePlanner 新的规划功能建议方法迁移到自动 函数调用。 与上述方法相比 FunctionCallingStepwisePlanner,新方法可更可靠地生成结果,并使用更少的令牌。

计划生成

以下代码演示如何使用 FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()自动函数调用生成新计划。 向 AI 模型发送请求后,计划将位于 ChatHistory 包含 Assistant 角色的消息将包含要调用的函数列表(步骤)的对象中。

旧方法:

Kernel kernel = Kernel
    .CreateBuilder()
    .AddOpenAIChatCompletion("gpt-4", Environment.GetEnvironmentVariable("OpenAI__ApiKey"))
    .Build();

FunctionCallingStepwisePlanner planner = new();

FunctionCallingStepwisePlannerResult result = await planner.ExecuteAsync(kernel, "Check current UTC time and return current weather in Boston city.");

ChatHistory generatedPlan = result.ChatHistory;

新方法:

Kernel kernel = Kernel
    .CreateBuilder()
    .AddOpenAIChatCompletion("gpt-4", Environment.GetEnvironmentVariable("OpenAI__ApiKey"))
    .Build();

IChatCompletionService chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();

ChatHistory chatHistory = [];
chatHistory.AddUserMessage("Check current UTC time and return current weather in Boston city.");

OpenAIPromptExecutionSettings executionSettings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };

await chatCompletionService.GetChatMessageContentAsync(chatHistory, executionSettings, kernel);

ChatHistory generatedPlan = chatHistory;

执行新计划

以下代码演示如何使用 FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()自动函数调用执行新计划。 只有在不需要计划步骤的情况下才需要结果时,此方法非常有用。 在这种情况下, Kernel 可以使用对象将目标传递给 InvokePromptAsync 方法。 计划执行的结果将位于对象中 FunctionResult

旧方法:

Kernel kernel = Kernel
    .CreateBuilder()
    .AddOpenAIChatCompletion("gpt-4", Environment.GetEnvironmentVariable("OpenAI__ApiKey"))
    .Build();

FunctionCallingStepwisePlanner planner = new();

FunctionCallingStepwisePlannerResult result = await planner.ExecuteAsync(kernel, "Check current UTC time and return current weather in Boston city.");

string planResult = result.FinalAnswer;

新方法:

Kernel kernel = Kernel
    .CreateBuilder()
    .AddOpenAIChatCompletion("gpt-4", Environment.GetEnvironmentVariable("OpenAI__ApiKey"))
    .Build();

OpenAIPromptExecutionSettings executionSettings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };

FunctionResult result = await kernel.InvokePromptAsync("Check current UTC time and return current weather in Boston city.", new(executionSettings));

string planResult = result.ToString();

执行现有计划

以下代码演示如何使用 FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()自动函数调用执行现有计划。 此方法在已存在(例如存储在缓存中)时 ChatHistory 非常有用,应再次执行此方法,最终结果应由 AI 模型提供。

旧方法:

Kernel kernel = Kernel
    .CreateBuilder()
    .AddOpenAIChatCompletion("gpt-4", Environment.GetEnvironmentVariable("OpenAI__ApiKey"))
    .Build();

FunctionCallingStepwisePlanner planner = new();
ChatHistory existingPlan = GetExistingPlan(); // plan can be stored in database  or cache for reusability.

FunctionCallingStepwisePlannerResult result = await planner.ExecuteAsync(kernel, "Check current UTC time and return current weather in Boston city.", existingPlan);

string planResult = result.FinalAnswer;

新方法:

Kernel kernel = Kernel
    .CreateBuilder()
    .AddOpenAIChatCompletion("gpt-4", Environment.GetEnvironmentVariable("OpenAI__ApiKey"))
    .Build();

IChatCompletionService chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();

ChatHistory existingPlan = GetExistingPlan(); // plan can be stored in database or cache for reusability.

OpenAIPromptExecutionSettings executionSettings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };

ChatMessageContent result = await chatCompletionService.GetChatMessageContentAsync(existingPlan, executionSettings, kernel);

string planResult = result.Content;

以下代码演示如何使用 function_choice_behavior = FunctionChoiceBehavior.Auto()自动函数调用生成新计划。 向 AI 模型发送请求后,计划将位于 ChatHistory 包含 Assistant 角色的消息将包含要调用的函数列表(步骤)的对象中。

旧方法:

from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
from semantic_kernel.planners.function_calling_stepwise_planner import (
    FunctionCallingStepwisePlanner, 
    FunctionCallingStepwisePlannerResult,
)

kernel = Kernel()
kernel.add_service(AzureChatCompletion())

# Add any plugins to the kernel that the planner will leverage
kernel.add_plugins(...)

planner = FunctionCallingStepwisePlanner(service_id="service_id")

result: FunctionCallingStepwisePlannerResult = await planner.invoke(
    kernel=kernel, 
    question="Check current UTC time and return current weather in Boston city.",
)

generated_plan = result.chat_history

新方法:

from semantic_kernel import Kernel
from semantic_kernel.connectors.ai import FunctionChoiceBehavior
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, AzureChatPromptExecutionSettings
from semantic_kernel.contents import ChatHistory

chat_completion_service = AzureChatCompletion()

chat_history = ChatHistory()
chat_hitory.add_user_message("Check current UTC time and return current weather in Boston city.")

request_settings = AzureChatPromptExecutionSettings(function_choice_behavior=FunctionChoiceBehavior.Auto())

# Add any plugins to the kernel that the planner will leverage
kernel = Kernel()
kernel.add_plugins(...)

response = await chat_completion_service.get_chat_message_content(
    chat_history=chat_history,
    settings=request_settings,
    kernel=kernel,
)
print(response)

# The generated plan is now contained inside of `chat_history`.

执行新计划

以下代码演示如何使用 function_choice_behavior = FunctionChoiceBehavior.Auto()自动函数调用执行新计划。 仅仅需要结果而不需要计划步骤时,此方法非常有用。 在这种情况下, Kernel 该对象可用于将目标传递给 invoke_prompt 方法。 计划执行的结果将位于对象 FunctionResult 中。

旧方法:

from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
from semantic_kernel.planners.function_calling_stepwise_planner import (
    FunctionCallingStepwisePlanner, 
    FunctionCallingStepwisePlannerResult,
)

kernel = Kernel()
kernel.add_service(AzureChatCompletion())

# Add any plugins to the kernel that the planner will leverage
kernel.add_plugins(...)

planner = FunctionCallingStepwisePlanner(service_id="service_id")

result: FunctionCallingStepwisePlannerResult = await planner.invoke(
    kernel=kernel, 
    question="Check current UTC time and return current weather in Boston city.",
)

print(result.final_answer)

新方法:

from semantic_kernel import Kernel
from semantic_kernel.connectors.ai import FunctionChoiceBehavior
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion, AzureChatPromptExecutionSettings
from semantic_kernel.contents import ChatHistory
from semantic_kernel.functions import KernelArguments

kernel = Kernel()
kernel.add_service(AzureChatCompletion())
# Add any plugins to the kernel that the planner will leverage
kernel.add_plugins(...)

chat_history = ChatHistory()
chat_hitory.add_user_message("Check current UTC time and return current weather in Boston city.")

request_settings = AzureChatPromptExecutionSettings(function_choice_behavior=FunctionChoiceBehavior.Auto())

response = await kernel.invoke_prompt(
    "Check current UTC time and return current weather in Boston city.", 
    arguments=KernelArguments(settings=request_settings),
)
print(response)

SK Java 中没有可用的计划工具。 请直接使用函数调用。

上面的代码片段演示如何迁移使用 Stepwise Planner 使用自动函数调用的代码。 详细了解 通过聊天完成进行函数调用。