分步规划器迁移指南

本迁移指南演示如何从 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;

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