Java: add a priority order to hook invocations. Remove withInvocationContext (#5004)

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
This commit is contained in:
John Oliver
2024-02-13 21:21:48 +00:00
committed by GitHub
parent 7d374d12a5
commit 6df0f5517c
3 changed files with 39 additions and 30 deletions
@@ -6,7 +6,19 @@ import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
public interface KernelHook<T extends KernelHookEvent> extends Predicate<KernelHookEvent>, Function<T, T> {
public interface KernelHook<T extends KernelHookEvent> extends Predicate<KernelHookEvent>,
Function<T, T> {
/**
* The priority of the hook. The default priority is 50. The priority is used to determine the
* order in which hooks that accept the same event type are executed, lower priorities are executed first. No ordering is
* guaranteed for hooks with the same priority.
*
* @return the priority of the hook
*/
default int getPriority() {
return 50;
}
interface FunctionInvokingHook extends KernelHook<FunctionInvokingEvent> {
@@ -1,18 +1,19 @@
package com.microsoft.semantickernel.hooks;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;
import javax.annotation.Nullable;
import com.microsoft.semantickernel.hooks.KernelHook.FunctionInvokedHook;
import com.microsoft.semantickernel.hooks.KernelHook.FunctionInvokingHook;
import com.microsoft.semantickernel.hooks.KernelHook.PreChatCompletionHook;
import com.microsoft.semantickernel.hooks.KernelHook.PromptRenderedHook;
import com.microsoft.semantickernel.hooks.KernelHook.PromptRenderingHook;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.UUID;
import java.util.function.Function;
import javax.annotation.Nullable;
public class KernelHooks {
@@ -37,7 +38,7 @@ public class KernelHooks {
public UnmodifiableKernelHooks unmodifiableClone() {
return new UnmodifiableKernelHooks(this);
}
private Map<String, KernelHook<?>> getHooks() {
return Collections.unmodifiableMap(hooks);
}
@@ -69,7 +70,11 @@ public class KernelHooks {
@SuppressWarnings("unchecked")
public <T extends KernelHookEvent> T executeHooks(T event) {
for (KernelHook<?> hook : hooks.values()) {
SortedSet<KernelHook<?>> hooks = new TreeSet<>(Comparator.comparingInt(KernelHook::getPriority));
hooks.addAll(this.hooks.values());
for (KernelHook<?> hook : hooks) {
if (hook.test(event)) {
// unchecked cast
event = ((KernelHook<T>) hook).apply(event);
@@ -110,33 +115,38 @@ public class KernelHooks {
}
public static class UnmodifiableKernelHooks extends KernelHooks {
private UnmodifiableKernelHooks(KernelHooks kernelHooks) {
super(kernelHooks);
}
@Override
public String addFunctionInvokingHook(Function<FunctionInvokingEvent, FunctionInvokingEvent> function) {
public String addFunctionInvokingHook(
Function<FunctionInvokingEvent, FunctionInvokingEvent> function) {
throw new UnsupportedOperationException("unmodifiable instance of KernelHooks");
}
@Override
public String addFunctionInvokedHook(Function<FunctionInvokedEvent<?>, FunctionInvokedEvent<?>> function) {
public String addFunctionInvokedHook(
Function<FunctionInvokedEvent<?>, FunctionInvokedEvent<?>> function) {
throw new UnsupportedOperationException("unmodifiable instance of KernelHooks");
}
@Override
public String addPreChatCompletionHook(Function<PreChatCompletionEvent, PreChatCompletionEvent> function) {
public String addPreChatCompletionHook(
Function<PreChatCompletionEvent, PreChatCompletionEvent> function) {
throw new UnsupportedOperationException("unmodifiable instance of KernelHooks");
}
@Override
public String addPromptRenderedHook(Function<PromptRenderedEvent, PromptRenderedEvent> function) {
public String addPromptRenderedHook(
Function<PromptRenderedEvent, PromptRenderedEvent> function) {
throw new UnsupportedOperationException("unmodifiable instance of KernelHooks");
}
@Override
public String addPromptRenderingHook(Function<PromptRenderingEvent, PromptRenderingEvent> function) {
public String addPromptRenderingHook(
Function<PromptRenderingEvent, PromptRenderingEvent> function) {
throw new UnsupportedOperationException("unmodifiable instance of KernelHooks");
}
@@ -129,19 +129,6 @@ public class FunctionInvocation<T> extends Mono<FunctionResult<T>> {
return this;
}
public FunctionInvocation<T> withInvocationContext(
@Nullable InvocationContext invocationContext) {
if (invocationContext == null) {
return this;
}
withTypes(invocationContext.getContextVariableTypes());
withToolCallBehavior(invocationContext.getToolCallBehavior());
withPromptExecutionSettings(invocationContext.getPromptExecutionSettings());
addKernelHooks(invocationContext.getKernelHooks());
return this;
}
@Override
public void subscribe(CoreSubscriber<? super FunctionResult<T>> coreSubscriber) {
performSubscribe(