Podrobný průvodce migrací Planneru
Tento průvodce migrací ukazuje, jak migrovat z FunctionCallingStepwisePlanner
nového doporučeného přístupu pro plánování – automatické volání funkcí. Nový přístup vytváří výsledky spolehlivěji a používá méně tokenů v porovnání s FunctionCallingStepwisePlanner
.
Generování plánů
Následující kód ukazuje, jak vygenerovat nový plán s automatickým voláním funkcí pomocí FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
. Po odeslání požadavku na model AI bude plán umístěn v objektu, kde ChatHistory
zpráva s Assistant
rolí bude obsahovat seznam funkcí (kroků), které se mají volat.
Starý přístup:
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;
Nový přístup:
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;
Provádění nového plánu
Následující kód ukazuje, jak spustit nový plán s automatickým voláním funkce pomocí FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
. Tento přístup je užitečný v případě, že je potřeba jenom výsledek bez kroků plánu. V tomto případě Kernel
lze objekt použít k předání cíle metodě InvokePromptAsync
. Výsledek provádění plánu bude umístěn v FunctionResult
objektu.
Starý přístup:
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;
Nový přístup:
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();
Provádění stávajícího plánu
Následující kód ukazuje, jak spustit existující plán s automatickým voláním funkce pomocí FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
. Tento přístup je užitečný, pokud ChatHistory
už existuje (např. uložený v mezipaměti) a měl by se znovu spustit a konečný výsledek by měl poskytnout model AI.
Starý přístup:
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;
Nový přístup:
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;
Výše uvedené fragmenty kódu ukazují, jak migrovat kód, který používá Krokwise Planner k použití automatického volání funkcí. Přečtěte si další informace o volání funkcí s dokončením chatu.