Jegyzet
Az oldalhoz való hozzáférés engedélyezést igényel. Próbálhatod be jelentkezni vagy könyvtárat váltani.
Az oldalhoz való hozzáférés engedélyezést igényel. Megpróbálhatod a könyvtár váltását.
Ez a migrálási útmutató bemutatja, hogyan migrálhat egy FunctionCallingStepwisePlanner új, ajánlott tervezési módszerre – automatikus függvényhívásra. Az új megközelítés megbízhatóbb eredményeket hoz létre, és kevesebb jogkivonatot használ, mint a FunctionCallingStepwisePlanner.
Tervezés létrehozása
Az alábbi kód bemutatja, hogyan hozhat létre új csomagot automatikus függvényhívással a használatával FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(). Miután elküldte a kérelmet az AI-modellnek, a terv egy objektumban ChatHistory lesz, ahol egy szerepkörrel rendelkező Assistant üzenet tartalmazza a meghívandó függvények (lépések) listáját.
Régi megközelítés:
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;
Új megközelítés:
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;
Az új terv végrehajtása
Az alábbi kód bemutatja, hogyan hajthat végre egy új csomagot automatikus függvényhívással a használatával FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(). Ez a megközelítés akkor hasznos, ha csak az eredményre van szükség tervlépések nélkül. Ebben az esetben Kernel az objektum egy cél metódusnak való átadására InvokePromptAsync használható. A terv végrehajtásának eredménye objektumban FunctionResult lesz.
Régi megközelítés:
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;
Új megközelítés:
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();
A meglévő terv végrehajtása
Az alábbi kód bemutatja, hogyan hajthat végre egy meglévő csomagot automatikus függvényhívással a használatával FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(). Ez a megközelítés akkor hasznos, ha ChatHistory már létezik (például gyorsítótárban tárolva), és újra végre kell hajtani, és a végeredményt az AI-modellnek kell megadnia.
Régi megközelítés:
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;
Új megközelítés:
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;
Az alábbi kód bemutatja, hogyan hozhat létre új tervet automatikus függvényhívással a function_choice_behavior = FunctionChoiceBehavior.Auto() használatával. Miután elküldte a kérelmet az AI-modellnek, a terv egy objektumban ChatHistory lesz, ahol egy szerepkörrel rendelkező Assistant üzenet tartalmazza a meghívandó függvények (lépések) listáját.
Régi megközelítés:
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
Új megközelítés:
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`.
Az új terv végrehajtása
Az alábbi kód bemutatja, hogyan hajthat végre egy új csomagot automatikus függvényhívással a használatával function_choice_behavior = FunctionChoiceBehavior.Auto(). Ez a megközelítés akkor hasznos, ha csak az eredményre van szükség tervlépések nélkül. Ebben az esetben az Kernel objektum használható egy célnak a invoke_prompt metódusnak való átadására. A terv végrehajtásának eredménye egy FunctionResult objektumban lesz.
Régi megközelítés:
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)
Új megközelítés:
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)
A Planners nem volt elérhető az SK Java-ban. Használjon függvényhívást közvetlenül.
A fenti kódrészletek bemutatják, hogyan migrálhatja a Stepwise Plannert használó kódot az automatikus függvényhívás használatához. További információ a függvényhívásról a csevegés befejezésével.