Remarque
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de vous connecter ou de modifier des répertoires.
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de modifier des répertoires.
Ce guide de migration montre comment migrer à partir d’une FunctionCallingStepwisePlanner nouvelle approche recommandée pour la planification de la fonctionnalité - Appel automatique de fonction. La nouvelle approche produit les résultats de manière plus fiable et utilise moins de jetons par rapport à FunctionCallingStepwisePlanner.
Génération de plan
Le code suivant montre comment générer un nouveau plan avec l’appel automatique de fonction à l’aide FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()de . Après avoir envoyé une requête au modèle IA, le plan se trouve dans l’objet ChatHistory où un message avec Assistant rôle contient une liste de fonctions (étapes) à appeler.
Ancienne approche :
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;
Nouvelle approche :
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;
Exécution du nouveau plan
Le code suivant montre comment exécuter un nouveau plan avec appel automatique de fonction à l’aide FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()de . Cette approche est utile lorsque seul un résultat est nécessaire sans étapes de plan. Dans ce cas, Kernel l’objet peut être utilisé pour passer un objectif à InvokePromptAsync la méthode. Le résultat de l’exécution du plan se trouve dans l’objet FunctionResult .
Ancienne approche :
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;
Nouvelle approche :
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();
Exécution du plan existant
Le code suivant montre comment exécuter un plan existant avec appel automatique de fonction à l’aide FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()de . Cette approche est utile lorsqu’elle ChatHistory est déjà présente (par exemple, stockée dans le cache) et qu’elle doit être réexécutée et le résultat final doit être fourni par le modèle IA.
Ancienne approche :
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;
Nouvelle approche :
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;
Le code suivant montre comment générer un nouveau plan avec l'appel automatique de fonction, en utilisant function_choice_behavior = FunctionChoiceBehavior.Auto(). Après avoir envoyé une requête au modèle IA, le plan se trouve dans l’objet ChatHistory où un message avec Assistant rôle contient une liste de fonctions (étapes) à appeler.
Ancienne approche :
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
Nouvelle approche :
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`.
Exécution du nouveau plan
Le code suivant montre comment exécuter un nouveau plan avec appel automatique de fonction à l’aide function_choice_behavior = FunctionChoiceBehavior.Auto()de . Cette approche est utile lorsque seul le résultat est nécessaire sans étapes de plan. Dans ce cas, l’objet Kernel peut être utilisé pour passer un objectif à la invoke_prompt méthode. Le résultat de l’exécution du plan se trouve dans un FunctionResult objet.
Ancienne approche :
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)
Nouvelle approche :
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)
Les planificateurs n’étaient pas disponibles dans SK Java. Utilisez directement l’appel de fonction.
Les extraits de code ci-dessus montrent comment migrer votre code qui utilise stepwise Planner pour utiliser l’appel automatique de fonction. En savoir plus sur l’appel de fonction avec la saisie semi-automatique de conversation.