此移轉指南示範如何從 FunctionCallingStepwisePlanner 移轉至新的規劃功能 - 自動函數呼叫的建議方法。 相較於 FunctionCallingStepwisePlanner,新方法能更可靠地產生結果,且使用較少的 token。
計畫生成
以下程式碼示範如何使用 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 使用自動函數呼叫的程式代碼。 深入瞭解 使用聊天補全進行函式呼叫。