OnFunctionResult
OnFunctionResult는 Semantic Kernel Process Framework에서 하나의 Step 안에서 실행된 Kernel Function이 성공적으로 결과를 반환했을 때 동작하는 일종의 '이벤트 연결 지점'이다. 쉽게 말해, "이 함수가 끝나면 그다음에 무엇을 할지"를 미리 정의해 놓는 장치이다.
동작원리
Process Framework의 Step은 하나 이상의 Kernel Function을 가질 수 있다. 이 Kernel Function이 실행을 마치고 결과를 반환하면, 그 심점에서 OnFunctionResult가 호출된다.
이때 OnFunctionResult는 단순히 "다음 Step 으로 넘어간다"는 의미를 넘어서, 어떤 Step의 어떤 함수를 호출할지 그리고 어떤 데이터를 전달할지를 지정하는 역할을 한다.
예를 들어, StepA가 문자열 "Hello"를 반환하면, OnFunctionResult에서 "이 데이터를 StepB로 보내고, 거기서 Greet라는 함수를 실행해"라고 지정할 수 있다. 이 과정은 코드 한 줄로 연결되지만, 내부적으로는 이벤트 트리거 → 대상 Step 탐색 → 결과 데이터 전달 → 대상 함수 실행이라는 순서를 거친다.
OnFunctionResult는 모든 함수 실행 결과에 반응하도록 설정할 수도 있고, 특정 함수의 결과에만 반응하도록 제한할 수도 있다.
예를 들어, StepA 안에 SayHello와 SayGoodbye 라는 두 함수가 있다면, OnFunctionResult(nameof(SayHello))처럼 특정 함수 이름을 명시하면, SayHello가 끝났을 때만 다음 Step을 실행하게 된다. 이렇게 하면 불필요한 호출을 방지하고, 워크플로우의 흐름을 더 정밀하게 제어할 수 있다.
예시
Step Class
public class StepA : KernelProcessStep
{
[KernelFunction]
public string Greet(string name)
{
return $"Hello, {name}";
}
}
public class StepB : KernelProcessStep
{
[KernelFunction("AddExclamation")]
public string AddExclamation(string text)
{
return text + "!";
}
}
public class StepC : KernelProcessStep
{
[KernelFunction]
public void Print(string text)
{
Console.WriteLine(text);
}
}
Process
// StepA 정의
var stepA = builder.AddStepFromType<StepA>();
var stepB = builder.AddStepFromType<StepB>();
var stepC = builder.AddStepFromType<StepC>();
// StepA의 모든 함수 실행 결과를 StepB로 전달
stepA.OnFunctionResult()
.SendEventTo(new ProcessFunctionTargetBuilder(stepB));
// StepB의 특정 함수(AddExclamation)가 끝나면 StepC로 전달
stepB.OnFunctionResult(nameof(StepB.AddExclamation))
.SendEventTo(new ProcessFunctionTargetBuilder(stepC));
Flow
[StepA.Greet]
↓ (OnFunctionResult - 모든 함수 결과)
[StepB.AddExclamation]
↓ (OnFunctionResult - AddExclamation 결과)
[StepC.Print]
- StepA.Greet가 "Hello, John"을 반환하면 → OnFunctionResult()가 트리거 된다.
- SendEventTo 설정에 따라 StepB.AddExclamation이 호출됨
- StepB.AddExclamationdl "Hello, John!"을 반환하면 → OnFunctionResult(nameof(AddExclamation))이 트리거 된다.
- StepC.Print가 호출되어 최종 "hello, John!" 출력
OnFunctionResult(): 해당 Step의 모든 함수 결과에 반응
OnFunctionResult(nameof(FunctionName)): 특정 함수 실행이 끝날을 때만 반응
이 메커니즘 덕분에 단계 간 연결을 유연하게 설정 가능하며 함수의 반환값이 자동으로 다음 Step의 입력으로 전달됨
'Programming > Semantic Kernel' 카테고리의 다른 글
[Semantic Kernel] Process Framework 란? (1) | 2025.08.14 |
---|---|
[Semantic Kernel] Kernel 생성 방법 (0) | 2024.11.22 |
[Semantic Kernel] Kernel 이란? (1) | 2024.11.21 |
[Microsoft] Semantic Kernel 이란? (2) | 2024.11.15 |
댓글