|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +package com.microsoft.semantickernel.functionchoice; |
| 3 | + |
| 4 | +import com.microsoft.semantickernel.semanticfunctions.KernelFunction; |
| 5 | + |
| 6 | +import javax.annotation.Nullable; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Objects; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +/** |
| 13 | + * Defines the behavior of a tool call. Currently, the only tool available is function calling. |
| 14 | + */ |
| 15 | +public abstract class FunctionChoiceBehavior { |
| 16 | + private final Set<String> fullFunctionNames; |
| 17 | + |
| 18 | + protected final List<KernelFunction<?>> functions; |
| 19 | + protected final FunctionChoiceBehaviorOptions options; |
| 20 | + |
| 21 | + protected FunctionChoiceBehavior(List<KernelFunction<?>> functions, |
| 22 | + @Nullable FunctionChoiceBehaviorOptions options) { |
| 23 | + this.functions = functions; |
| 24 | + this.fullFunctionNames = new HashSet<>(); |
| 25 | + |
| 26 | + if (functions != null) { |
| 27 | + functions.stream().filter(Objects::nonNull).forEach( |
| 28 | + f -> this.fullFunctionNames |
| 29 | + .add(formFullFunctionName(f.getPluginName(), f.getName()))); |
| 30 | + } |
| 31 | + |
| 32 | + if (options != null) { |
| 33 | + this.options = options; |
| 34 | + } else { |
| 35 | + this.options = FunctionChoiceBehaviorOptions.builder().build(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Gets the functions that are allowed. |
| 41 | + * |
| 42 | + * @return The functions that are allowed. |
| 43 | + */ |
| 44 | + public List<KernelFunction<?>> getFunctions() { |
| 45 | + return functions; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Gets the options for the function choice behavior. |
| 50 | + * |
| 51 | + * @return The options for the function choice behavior. |
| 52 | + */ |
| 53 | + public FunctionChoiceBehaviorOptions getOptions() { |
| 54 | + return options; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Gets an instance of the FunctionChoiceBehavior that provides all the Kernel's plugins functions to the AI model to call. |
| 59 | + * |
| 60 | + * @param autoInvoke Indicates whether the functions should be automatically invoked by AI connectors |
| 61 | + * |
| 62 | + * @return A new ToolCallBehavior instance with all kernel functions allowed. |
| 63 | + */ |
| 64 | + public static FunctionChoiceBehavior auto(boolean autoInvoke) { |
| 65 | + return new AutoFunctionChoiceBehavior(autoInvoke, null, null); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Gets an instance of the FunctionChoiceBehavior that provides either all the Kernel's plugins functions to the AI model to call or specific functions. |
| 70 | + * |
| 71 | + * @param autoInvoke Enable or disable auto-invocation. |
| 72 | + * If auto-invocation is enabled, the model may request that the Semantic Kernel |
| 73 | + * invoke the kernel functions and return the value to the model. |
| 74 | + * @param functions Functions to provide to the model. If null, all the Kernel's plugins' functions are provided to the model. |
| 75 | + * If empty, no functions are provided to the model, which is equivalent to disabling function calling. |
| 76 | + * @param options Options for the function choice behavior. |
| 77 | + * |
| 78 | + * @return A new FunctionChoiceBehavior instance with all kernel functions allowed. |
| 79 | + */ |
| 80 | + public static FunctionChoiceBehavior auto(boolean autoInvoke, |
| 81 | + List<KernelFunction<?>> functions, |
| 82 | + @Nullable FunctionChoiceBehaviorOptions options) { |
| 83 | + return new AutoFunctionChoiceBehavior(autoInvoke, functions, options); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Gets an instance of the FunctionChoiceBehavior that provides either all the Kernel's plugins functions to the AI model to call or specific functions. |
| 88 | + * <p> |
| 89 | + * This behavior forces the model to call the provided functions. |
| 90 | + * SK connectors will invoke a requested function or multiple requested functions if the model requests multiple ones in one request, |
| 91 | + * while handling the first request, and stop advertising the functions for the following requests to prevent the model from repeatedly calling the same function(s). |
| 92 | + * |
| 93 | + * @param functions Functions to provide to the model. If null, all the Kernel's plugins' functions are provided to the model. |
| 94 | + * If empty, no functions are provided to the model, which is equivalent to disabling function calling. |
| 95 | + * @return A new FunctionChoiceBehavior instance with the required function. |
| 96 | + */ |
| 97 | + public static FunctionChoiceBehavior required(boolean autoInvoke, |
| 98 | + List<KernelFunction<?>> functions, |
| 99 | + @Nullable FunctionChoiceBehaviorOptions options) { |
| 100 | + return new RequiredFunctionChoiceBehavior(autoInvoke, functions, options); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Gets an instance of the FunctionChoiceBehavior that provides either all the Kernel's plugins functions to the AI model to call or specific functions. |
| 105 | + * <p> |
| 106 | + * This behavior is useful if the user should first validate what functions the model will use. |
| 107 | + * |
| 108 | + * @param functions Functions to provide to the model. If null, all the Kernel's plugins' functions are provided to the model. |
| 109 | + * If empty, no functions are provided to the model, which is equivalent to disabling function calling. |
| 110 | + */ |
| 111 | + public static FunctionChoiceBehavior none(List<KernelFunction<?>> functions, |
| 112 | + @Nullable FunctionChoiceBehaviorOptions options) { |
| 113 | + return new NoneFunctionChoiceBehavior(functions, options); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + /** |
| 118 | + * The separator between the plugin name and the function name. |
| 119 | + */ |
| 120 | + public static final String FUNCTION_NAME_SEPARATOR = "-"; |
| 121 | + |
| 122 | + /** |
| 123 | + * Form the full function name. |
| 124 | + * |
| 125 | + * @param pluginName The name of the plugin that the function is in. |
| 126 | + * @param functionName The name of the function. |
| 127 | + * @return The key for the function. |
| 128 | + */ |
| 129 | + public static String formFullFunctionName(@Nullable String pluginName, String functionName) { |
| 130 | + if (pluginName == null) { |
| 131 | + pluginName = ""; |
| 132 | + } |
| 133 | + return String.format("%s%s%s", pluginName, FUNCTION_NAME_SEPARATOR, functionName); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Check whether the given function is allowed. |
| 138 | + * |
| 139 | + * @param function The function to check. |
| 140 | + * @return Whether the function is allowed. |
| 141 | + */ |
| 142 | + public boolean isFunctionAllowed(KernelFunction<?> function) { |
| 143 | + return isFunctionAllowed(function.getPluginName(), function.getName()); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Check whether the given function is allowed. |
| 148 | + * |
| 149 | + * @param pluginName The name of the plugin that the function is in. |
| 150 | + * @param functionName The name of the function. |
| 151 | + * @return Whether the function is allowed. |
| 152 | + */ |
| 153 | + public boolean isFunctionAllowed(@Nullable String pluginName, String functionName) { |
| 154 | + // If no functions are provided, all functions are allowed. |
| 155 | + if (functions == null || functions.isEmpty()) { |
| 156 | + return true; |
| 157 | + } |
| 158 | + |
| 159 | + String key = formFullFunctionName(pluginName, functionName); |
| 160 | + return fullFunctionNames.contains(key); |
| 161 | + } |
| 162 | +} |
0 commit comments