Nuta
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować się zalogować lub zmienić katalog.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
W tym przewodniku migracji pokazano, jak przeprowadzić migrację z FunctionCallingStepwisePlanner do nowego zalecanego podejścia do planowania możliwości — automatyczne wywoływanie funkcji. Nowe podejście daje wyniki bardziej niezawodnie i używa mniejszej liczby tokenów w porównaniu z FunctionCallingStepwisePlanner.
Generowanie planu
Poniższy kod pokazuje, jak wygenerować nowy plan za pomocą funkcji automatycznego wywoływania przy użyciu polecenia FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(). Po wysłaniu żądania do modelu sztucznej inteligencji plan będzie znajdować się w obiekcie, w ChatHistory którym komunikat z Assistant rolą będzie zawierać listę funkcji (kroków) do wywołania.
Stare podejście:
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;
Nowe podejście:
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;
Wykonanie nowego planu
Poniższy kod pokazuje, jak wykonać nowy plan za pomocą funkcji automatycznego wywoływania przy użyciu polecenia FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(). Takie podejście jest przydatne, gdy tylko wynik jest potrzebny bez kroków planu. W takim przypadku Kernel obiekt może służyć do przekazania celu do InvokePromptAsync metody . Wynik wykonania planu będzie znajdować się w FunctionResult obiekcie.
Stare podejście:
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;
Nowe podejście:
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();
Wykonanie istniejącego planu
Poniższy kod pokazuje, jak wykonać istniejący plan za pomocą funkcji automatycznego wywoływania przy użyciu polecenia FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(). Takie podejście jest przydatne, gdy ChatHistory jest już obecne (np. przechowywane w pamięci podręcznej) i powinno zostać ponownie wykonane, a końcowy wynik powinien zostać dostarczony przez model sztucznej inteligencji.
Stare podejście:
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;
Nowe podejście:
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;
Poniższy kod pokazuje, jak wygenerować nowy plan za pomocą funkcji automatycznego wywoływania przy użyciu polecenia function_choice_behavior = FunctionChoiceBehavior.Auto(). Po wysłaniu żądania do modelu sztucznej inteligencji plan będzie znajdować się w obiekcie, w ChatHistory którym komunikat z Assistant rolą będzie zawierać listę funkcji (kroków) do wywołania.
Stare podejście:
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
Nowe podejście:
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`.
Wykonanie nowego planu
Poniższy kod pokazuje, jak wykonać nowy plan za pomocą funkcji automatycznego wywoływania przy użyciu polecenia function_choice_behavior = FunctionChoiceBehavior.Auto(). Takie podejście jest przydatne, gdy tylko wynik jest potrzebny bez kroków planu. W takim przypadku obiekt Kernel może służyć do przekazania celu do metody invoke_prompt. Wynik wykonania planu będzie znajdować się w FunctionResult obiekcie.
Stare podejście:
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)
Nowe podejście:
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)
Planery nie były dostępne w środowisku SK Java. Użyj funkcji wywołującej bezpośrednio.
Powyższe fragmenty kodu pokazują, jak przeprowadzić migrację kodu, który używa narzędzia Stepwise Planner do używania funkcji automatycznego wywoływania. Dowiedz się więcej na temat wywoływania funkcji z ukończeniem czatu.