이 마이그레이션 가이드에서는 계획 기능에 권장되는 새로운 접근 방식인 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()기존 계획을 실행하는 방법을 보여 주는 코드입니다. 이 방법은 이미 있는 경우 ChatHistory (예: 캐시에 저장됨) 유용하며 다시 실행해야 하며 AI 모델에서 최종 결과를 제공해야 합니다.
이전 접근 방식:
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;
다음 코드는 function_choice_behavior = FunctionChoiceBehavior.Auto()를 사용하여 자동 함수 호출로 새로운 계획을 생성하는 방법을 보여줍니다. AI 모델에 요청을 보낸 후에는 역할이 있는 메시지에 ChatHistory 호출할 함수(단계) 목록이 포함된 개체에 계획이 배치 Assistant 됩니다.
이전 접근 방식:
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
새로운 접근 방식:
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`.
새 계획 실행
다음 코드에서는 을 사용하여 자동 함수 호출을 사용하여 function_choice_behavior = FunctionChoiceBehavior.Auto()새 계획을 실행하는 방법을 보여 있습니다. 이 방법은 계획 단계 없이 결과만 필요한 경우에 유용합니다. 이 경우 Kernel 사용하여 목표를 invoke_prompt 메서드에 전달할 수 있습니다. 계획 실행 결과는 FunctionResult 개체에 들어갑니다.
이전 접근 방식:
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)
새로운 접근 방식:
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)
플래너는 SK Java에서 사용할 수 없습니다. 함수 호출을 직접 사용하세요.
위의 코드 조각은 Stepwise Planner를 사용하여 자동 함수 호출을 사용하는 코드를 마이그레이션하는 방법을 보여 줍니다. 채팅 완료를 사용하여 함수 호출에 대해 자세히 알아봅니다.