Share via


KernelHooks Class

  • java.lang.Object
    • com.microsoft.semantickernel.hooks.KernelHooks

public class KernelHooks

Represents a collection of hooks that can be used to intercept and modify events in the kernel.

Constructor Summary

Constructor Description
KernelHooks()

Creates a new instance of the KernelHooks class.

KernelHooks(KernelHooks kernelHooks)

Creates a copy of the KernelHooks.

KernelHooks(Map<String,KernelHook<?>> hooks)

Creates a new instance of the KernelHooks class from the given hooks.

Method Summary

Modifier and Type Method and Description
T executeHooks(T event)

Executes the hooks in this collection that accept the event.

java.lang.String addFunctionInvokedHook(Function<FunctionInvokedEvent<?>,FunctionInvokedEvent<?>> function)

Add a FunctionInvokedHook to the collection of hooks.

java.lang.String addFunctionInvokingHook(Function<FunctionInvokingEvent<?>,FunctionInvokingEvent<?>> function)

Add a FunctionInvokingHook to the collection of hooks.

java.lang.String addHook(KernelHook<?> hook)

Add a KernelHook<T> to the collection of hooks.

java.lang.String addHook(String hookName, KernelHook<?> hook)

Add a KernelHook<T> to the collection of hooks.

KernelHooks addHooks(KernelHooks kernelHooks)

Appends the given hooks to this collection.

java.lang.String addPostChatCompletionHook(Function<PostChatCompletionEvent,PostChatCompletionEvent> function)

Add a PostChatCompletionEvent to the collection of hooks.

java.lang.String addPreChatCompletionHook(Function<PreChatCompletionEvent,PreChatCompletionEvent> function)

Add a PreChatCompletionHook to the collection of hooks.

java.lang.String addPreToolCallHook(Function<PreToolCallEvent,PreToolCallEvent> function)

Add a PreToolCallHook to the collection of hooks.

java.lang.String addPromptRenderedHook(Function<PromptRenderedEvent,PromptRenderedEvent> function)

Add a PromptRenderedHook to the collection of hooks.

java.lang.String addPromptRenderingHook(Function<PromptRenderingEvent,PromptRenderingEvent> function)

Add a PromptRenderingHook to the collection of hooks.

protected java.util.Map<java.lang.String,KernelHook<?>> getHooks()

Gets the hooks in this collection.

boolean isEmpty()

Determines if this collection of hooks is empty.

static KernelHooks merge(KernelHooks a, 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.

KernelHook<?> removeHook(String hookName)

Remove a hook from the collection of hooks.

UnmodifiableKernelHooks unmodifiableClone()

Creates an unmodifiable copy of this KernelHooks.

Methods inherited from java.lang.Object

java.lang.Object.clone java.lang.Object.equals java.lang.Object.finalize java.lang.Object.getClass java.lang.Object.hashCode java.lang.Object.notify java.lang.Object.notifyAll java.lang.Object.toString java.lang.Object.wait java.lang.Object.wait java.lang.Object.wait

Constructor Details

KernelHooks

public KernelHooks()

Creates a new instance of the KernelHooks class.

KernelHooks

public KernelHooks(KernelHooks kernelHooks)

Creates a copy of the KernelHooks.

Parameters:

kernelHooks - the hooks to copy

KernelHooks

public KernelHooks(Map> hooks)

Creates a new instance of the KernelHooks class from the given hooks.

Parameters:

hooks - the hooks to add

Method Details

executeHooks

public T executeHooks(T event)

Executes the hooks in this collection that accept the event.

Parameters:

event - the event to execute the hooks on

Returns:

the event after the hooks have been executed

addFunctionInvokedHook

public String addFunctionInvokedHook(Function,FunctionInvokedEvent> function)

Add a FunctionInvokedHook to the collection of hooks.

Parameters:

function - the function to add

Returns:

the key of the hook in the collection

addFunctionInvokingHook

public String addFunctionInvokingHook(Function,FunctionInvokingEvent> function)

Add a FunctionInvokingHook to the collection of hooks.

Parameters:

function - the function to add

Returns:

the key of the hook in the collection

addHook

public String addHook(KernelHook hook)

Add a KernelHook<T> to 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 a KernelHook<T> to the collection of hooks.

Parameters:

hookName - the key of the hook in the collection
hook - the hook to add

Returns:

the key of the hook in the collection

addHooks

public KernelHooks addHooks(KernelHooks kernelHooks)

Appends the given hooks to this collection.

Parameters:

kernelHooks - the hooks to append

Returns:

this instance of the KernelHooks class

addPostChatCompletionHook

public String addPostChatCompletionHook(Function function)

Add a PostChatCompletionEvent to the collection of hooks.

Parameters:

function - the function to add

Returns:

the key of the hook in the collection

addPreChatCompletionHook

public String addPreChatCompletionHook(Function function)

Add a PreChatCompletionHook to the collection of hooks.

Parameters:

function - the function to add

Returns:

the key of the hook in the collection

addPreToolCallHook

public String addPreToolCallHook(Function function)

Add a PreToolCallHook to the collection of hooks.

Parameters:

function - the function to add

Returns:

the key of the hook in the collection

addPromptRenderedHook

public String addPromptRenderedHook(Function function)

Add a PromptRenderedHook to the collection of hooks.

Parameters:

function - the function to add

Returns:

the key of the hook in the collection

addPromptRenderingHook

public String addPromptRenderingHook(Function function)

Add a PromptRenderingHook to the collection of hooks.

Parameters:

function - the function to add

Returns:

the key of the hook in the collection

getHooks

protected Map> getHooks()

Gets the hooks in this collection.

Returns:

an unmodifiable map of the hooks

isEmpty

public boolean isEmpty()

Determines if this collection of hooks is empty.

Returns:

true if the collection is empty, otherwise false

merge

public static KernelHooks merge(KernelHooks a, 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 merge
b - hooks to merge

Returns:

the list of hooks to be invoked

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 null if the hook was not found

unmodifiableClone

public KernelHooks.UnmodifiableKernelHooks unmodifiableClone()

Creates an unmodifiable copy of this KernelHooks.

Returns:

an unmodifiable copy of this KernelHooks

Applies to