逐步規劃工具移轉指南
此移轉指南示範如何從 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()
的現有方案。 當已經存在(例如儲存在快取中)且應該再次重新執行,且最終結果應該由 AI 模型提供時,此方法 ChatHistory
非常有用。
舊方法:
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 使用自動函數呼叫的程式代碼。 深入瞭解 使用聊天完成進行函式呼叫。