Class KernelHooks
- java.lang.Object
-
- com.microsoft.semantickernel.hooks.KernelHooks
-
- Direct Known Subclasses:
KernelHooks.UnmodifiableKernelHooks
public class KernelHooks extends Object
Represents a collection of hooks that can be used to intercept and modify events in the kernel.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKernelHooks.UnmodifiableKernelHooksA wrapper for KernelHooks that disables mutating methods.
-
Constructor Summary
Constructors Constructor Description KernelHooks()Creates a new instance of theKernelHooksclass.KernelHooks(KernelHooks kernelHooks)Creates a copy of theKernelHooks.KernelHooks(Map<String,KernelHook<?>> hooks)Creates a new instance of theKernelHooksclass from the given hooks.
-
Method Summary
-
-
-
Constructor Detail
-
KernelHooks
public KernelHooks()
Creates a new instance of theKernelHooksclass.
-
KernelHooks
public KernelHooks(@Nullable KernelHooks kernelHooks)
Creates a copy of theKernelHooks.- Parameters:
kernelHooks- the hooks to copy
-
KernelHooks
public KernelHooks(Map<String,KernelHook<?>> hooks)
Creates a new instance of theKernelHooksclass from the given hooks.- Parameters:
hooks- the hooks to add
-
-
Method Detail
-
unmodifiableClone
public KernelHooks.UnmodifiableKernelHooks unmodifiableClone()
Creates an unmodifiable copy of thisKernelHooks.- Returns:
- an unmodifiable copy of this
KernelHooks
-
addFunctionInvokingHook
public String addFunctionInvokingHook(Function<FunctionInvokingEvent<?>,FunctionInvokingEvent<?>> function)
Add aKernelHook.FunctionInvokingHookto the collection of hooks.- Parameters:
function- the function to add- Returns:
- the key of the hook in the collection
-
addFunctionInvokedHook
public String addFunctionInvokedHook(Function<FunctionInvokedEvent<?>,FunctionInvokedEvent<?>> function)
Add aKernelHook.FunctionInvokedHookto the collection of hooks.- Parameters:
function- the function to add- Returns:
- the key of the hook in the collection
-
addPreChatCompletionHook
public String addPreChatCompletionHook(Function<PreChatCompletionEvent,PreChatCompletionEvent> function)
Add aKernelHook.PreChatCompletionHookto the collection of hooks.- Parameters:
function- the function to add- Returns:
- the key of the hook in the collection
-
addPreToolCallHook
public String addPreToolCallHook(Function<PreToolCallEvent,PreToolCallEvent> function)
Add aKernelHook.PreToolCallHookto the collection of hooks.- Parameters:
function- the function to add- Returns:
- the key of the hook in the collection
-
addPostChatCompletionHook
public String addPostChatCompletionHook(Function<PostChatCompletionEvent,PostChatCompletionEvent> function)
Add aPostChatCompletionEventto the collection of hooks.- Parameters:
function- the function to add- Returns:
- the key of the hook in the collection
-
addPromptRenderedHook
public String addPromptRenderedHook(Function<PromptRenderedEvent,PromptRenderedEvent> function)
Add aKernelHook.PromptRenderedHookto the collection of hooks.- Parameters:
function- the function to add- Returns:
- the key of the hook in the collection
-
addPromptRenderingHook
public String addPromptRenderingHook(Function<PromptRenderingEvent,PromptRenderingEvent> function)
Add aKernelHook.PromptRenderingHookto the collection of hooks.- Parameters:
function- the function to add- Returns:
- the key of the hook in the collection
-
executeHooks
public <T extends KernelHookEvent> T executeHooks(T event)
Executes the hooks in this collection that accept the event.- Type Parameters:
T- the type of the event- Parameters:
event- the event to execute the hooks on- Returns:
- the event after the hooks have been executed
-
addHook
public String addHook(KernelHook<?> hook)
Add aKernelHookto the collection of hooks.- Parameters:
hook- the hook to add- Returns:
- the key of the hook in the collection
-
addHook
public String addHook(String hookName, KernelHook<?> hook)
Add aKernelHookto the collection of hooks.- Parameters:
hookName- the key of the hook in the collectionhook- the hook to add- Returns:
- the key of the hook in the collection
-
removeHook
public KernelHook<?> removeHook(String hookName)
Remove a hook from the collection of hooks.- Parameters:
hookName- the key of the hook in the collection- Returns:
- the removed hook, or
nullif the hook was not found
-
addHooks
public KernelHooks addHooks(@Nullable KernelHooks kernelHooks)
Appends the given hooks to this collection.- Parameters:
kernelHooks- the hooks to append- Returns:
- this instance of the
KernelHooksclass
-
isEmpty
public boolean isEmpty()
Determines if this collection of hooks is empty.- Returns:
trueif the collection is empty, otherwisefalse
-
merge
public static KernelHooks merge(@Nullable KernelHooks a, @Nullable KernelHooks b)
Builds the list of hooks to be invoked for the given context, by merging the hooks in this collection with the hooks in the context. Duplicate hooks in b will override hooks in a.- Parameters:
a- hooks to mergeb- hooks to merge- Returns:
- the list of hooks to be invoked
-
-