-
Notifications
You must be signed in to change notification settings - Fork 54
Add FunctionChoiceBehavior for OpenAI #310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
03bd7ed
Remove authorName from ChatMessageContent
d380bb5
Merge branch 'main' of https://github.com/microsoft/semantic-kernel-j…
9b821db
Add FunctionChoiceBehavior implementation
bc61886
return Collections.unmodifiableList(functions)
8cd72af
Fixes
a222ccb
Fix retry logic
3c39538
Add suggestions
66976a9
Avoid notify thread when auto-invoke is not active
cbb37c6
Add @Nullable annotations
b568818
Add additional constructor for ChatHistoryAgentThread
fd33c21
Merge pull request #1 from milderhc/completion-agent
milderhc 81abcbe
Fix keys of execution settings
394367f
Apply format
c535e61
Small fix
730af6c
Add Github plugin to learn resources
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
241 changes: 174 additions & 67 deletions
241
...a/com/microsoft/semantickernel/aiservices/openai/chatcompletion/OpenAIChatCompletion.java
Large diffs are not rendered by default.
Oops, something went wrong.
70 changes: 70 additions & 0 deletions
70
...a/com/microsoft/semantickernel/aiservices/openai/chatcompletion/OpenAIToolCallConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package com.microsoft.semantickernel.aiservices.openai.chatcompletion; | ||
|
|
||
| import com.azure.ai.openai.models.ChatCompletionsToolDefinition; | ||
| import com.azure.ai.openai.models.ChatCompletionsToolSelection; | ||
| import com.microsoft.semantickernel.functionchoice.FunctionChoiceBehaviorOptions; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.util.List; | ||
|
|
||
| public class OpenAIToolCallConfig { | ||
| private final List<ChatCompletionsToolDefinition> tools; | ||
| private final ChatCompletionsToolSelection toolChoice; | ||
| private final boolean autoInvoke; | ||
| private final FunctionChoiceBehaviorOptions options; | ||
|
|
||
| /** | ||
| * Creates a new instance of the {@link OpenAIToolCallConfig} class. | ||
| * | ||
| * @param tools The list of tools available for the call. | ||
| * @param toolChoice The tool selection strategy. | ||
| * @param autoInvoke Indicates whether to automatically invoke the tool. | ||
| * @param options Additional options for function choice behavior. | ||
| */ | ||
| public OpenAIToolCallConfig( | ||
| List<ChatCompletionsToolDefinition> tools, | ||
| ChatCompletionsToolSelection toolChoice, | ||
| boolean autoInvoke, | ||
| @Nullable FunctionChoiceBehaviorOptions options) { | ||
| this.tools = tools; | ||
| this.toolChoice = toolChoice; | ||
| this.autoInvoke = autoInvoke; | ||
| this.options = options; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the list of tools available for the call. | ||
| * | ||
| * @return The list of tools. | ||
| */ | ||
| public List<ChatCompletionsToolDefinition> getTools() { | ||
| return tools; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the tool selection strategy. | ||
| * | ||
| * @return The tool selection strategy. | ||
| */ | ||
| public ChatCompletionsToolSelection getToolChoice() { | ||
| return toolChoice; | ||
| } | ||
|
|
||
| /** | ||
| * Indicates whether to automatically invoke the tool. | ||
| * | ||
| * @return True if auto-invocation is enabled; otherwise, false. | ||
| */ | ||
| public boolean isAutoInvoke() { | ||
| return autoInvoke; | ||
| } | ||
|
|
||
| /** | ||
| * Gets additional options for function choice behavior. | ||
| * | ||
| * @return The function choice behavior options. | ||
| */ | ||
| public FunctionChoiceBehaviorOptions getOptions() { | ||
| return options; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...src/main/java/com/microsoft/semantickernel/functionchoice/AutoFunctionChoiceBehavior.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.microsoft.semantickernel.functionchoice; | ||
|
|
||
| import com.microsoft.semantickernel.semanticfunctions.KernelFunction; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * A set of allowed kernel functions. All kernel functions are allowed if allKernelFunctionsAllowed is true. | ||
| * Otherwise, only the functions in allowedFunctions are allowed. | ||
| * <p> | ||
| * If a function is allowed, it may be called. If it is not allowed, it will not be called. | ||
| */ | ||
| public class AutoFunctionChoiceBehavior extends FunctionChoiceBehavior { | ||
| private final boolean autoInvoke; | ||
|
|
||
| /** | ||
| * Create a new instance of AutoFunctionChoiceBehavior. | ||
| * | ||
| * @param autoInvoke Whether auto-invocation is enabled. | ||
| * @param functions A set of functions to advertise to the model. | ||
| * @param options Options for the function choice behavior. | ||
| */ | ||
| public AutoFunctionChoiceBehavior(boolean autoInvoke, | ||
| @Nullable List<KernelFunction<?>> functions, | ||
| @Nullable FunctionChoiceBehaviorOptions options) { | ||
| super(functions, options); | ||
| this.autoInvoke = autoInvoke; | ||
| } | ||
|
|
||
| /** | ||
| * Check whether the given function is allowed. | ||
| * | ||
| * @return Whether the function is allowed. | ||
| */ | ||
| public boolean isAutoInvoke() { | ||
| return autoInvoke; | ||
| } | ||
| } |
162 changes: 162 additions & 0 deletions
162
...api/src/main/java/com/microsoft/semantickernel/functionchoice/FunctionChoiceBehavior.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| package com.microsoft.semantickernel.functionchoice; | ||
|
|
||
| import com.microsoft.semantickernel.semanticfunctions.KernelFunction; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Defines the behavior of a tool call. Currently, the only tool available is function calling. | ||
| */ | ||
| public abstract class FunctionChoiceBehavior { | ||
| private final Set<String> fullFunctionNames; | ||
|
|
||
| protected final List<KernelFunction<?>> functions; | ||
| protected final FunctionChoiceBehaviorOptions options; | ||
|
|
||
| protected FunctionChoiceBehavior(List<KernelFunction<?>> functions, | ||
| @Nullable FunctionChoiceBehaviorOptions options) { | ||
| this.functions = functions; | ||
| this.fullFunctionNames = new HashSet<>(); | ||
|
|
||
| if (functions != null) { | ||
| functions.stream().filter(Objects::nonNull).forEach( | ||
| f -> this.fullFunctionNames | ||
| .add(formFullFunctionName(f.getPluginName(), f.getName()))); | ||
| } | ||
|
|
||
| if (options != null) { | ||
| this.options = options; | ||
| } else { | ||
| this.options = FunctionChoiceBehaviorOptions.builder().build(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets the functions that are allowed. | ||
| * | ||
| * @return The functions that are allowed. | ||
| */ | ||
| public List<KernelFunction<?>> getFunctions() { | ||
| return functions; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the options for the function choice behavior. | ||
| * | ||
| * @return The options for the function choice behavior. | ||
| */ | ||
| public FunctionChoiceBehaviorOptions getOptions() { | ||
| return options; | ||
| } | ||
|
|
||
| /** | ||
| * Gets an instance of the FunctionChoiceBehavior that provides all the Kernel's plugins functions to the AI model to call. | ||
| * | ||
| * @param autoInvoke Indicates whether the functions should be automatically invoked by AI connectors | ||
| * | ||
| * @return A new ToolCallBehavior instance with all kernel functions allowed. | ||
| */ | ||
| public static FunctionChoiceBehavior auto(boolean autoInvoke) { | ||
| return new AutoFunctionChoiceBehavior(autoInvoke, null, null); | ||
| } | ||
|
|
||
| /** | ||
| * Gets an instance of the FunctionChoiceBehavior that provides either all the Kernel's plugins functions to the AI model to call or specific functions. | ||
| * | ||
| * @param autoInvoke Enable or disable auto-invocation. | ||
| * If auto-invocation is enabled, the model may request that the Semantic Kernel | ||
| * invoke the kernel functions and return the value to the model. | ||
| * @param functions Functions to provide to the model. If null, all the Kernel's plugins' functions are provided to the model. | ||
| * If empty, no functions are provided to the model, which is equivalent to disabling function calling. | ||
| * @param options Options for the function choice behavior. | ||
| * | ||
| * @return A new FunctionChoiceBehavior instance with all kernel functions allowed. | ||
| */ | ||
| public static FunctionChoiceBehavior auto(boolean autoInvoke, | ||
| List<KernelFunction<?>> functions, | ||
| @Nullable FunctionChoiceBehaviorOptions options) { | ||
| return new AutoFunctionChoiceBehavior(autoInvoke, functions, options); | ||
| } | ||
|
|
||
| /** | ||
| * Gets an instance of the FunctionChoiceBehavior that provides either all the Kernel's plugins functions to the AI model to call or specific functions. | ||
| * <p> | ||
| * This behavior forces the model to call the provided functions. | ||
| * SK connectors will invoke a requested function or multiple requested functions if the model requests multiple ones in one request, | ||
| * 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). | ||
| * | ||
| * @param functions Functions to provide to the model. If null, all the Kernel's plugins' functions are provided to the model. | ||
| * If empty, no functions are provided to the model, which is equivalent to disabling function calling. | ||
| * @return A new FunctionChoiceBehavior instance with the required function. | ||
| */ | ||
| public static FunctionChoiceBehavior required(boolean autoInvoke, | ||
| List<KernelFunction<?>> functions, | ||
| @Nullable FunctionChoiceBehaviorOptions options) { | ||
| return new RequiredFunctionChoiceBehavior(autoInvoke, functions, options); | ||
| } | ||
|
|
||
| /** | ||
| * Gets an instance of the FunctionChoiceBehavior that provides either all the Kernel's plugins functions to the AI model to call or specific functions. | ||
| * <p> | ||
| * This behavior is useful if the user should first validate what functions the model will use. | ||
| * | ||
| * @param functions Functions to provide to the model. If null, all the Kernel's plugins' functions are provided to the model. | ||
| * If empty, no functions are provided to the model, which is equivalent to disabling function calling. | ||
| */ | ||
| public static FunctionChoiceBehavior none(List<KernelFunction<?>> functions, | ||
| @Nullable FunctionChoiceBehaviorOptions options) { | ||
| return new NoneFunctionChoiceBehavior(functions, options); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * The separator between the plugin name and the function name. | ||
| */ | ||
| public static final String FUNCTION_NAME_SEPARATOR = "-"; | ||
|
|
||
| /** | ||
| * Form the full function name. | ||
| * | ||
| * @param pluginName The name of the plugin that the function is in. | ||
| * @param functionName The name of the function. | ||
| * @return The key for the function. | ||
| */ | ||
| public static String formFullFunctionName(@Nullable String pluginName, String functionName) { | ||
| if (pluginName == null) { | ||
| pluginName = ""; | ||
| } | ||
| return String.format("%s%s%s", pluginName, FUNCTION_NAME_SEPARATOR, functionName); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether the given function is allowed. | ||
| * | ||
| * @param function The function to check. | ||
| * @return Whether the function is allowed. | ||
| */ | ||
| public boolean isFunctionAllowed(KernelFunction<?> function) { | ||
| return isFunctionAllowed(function.getPluginName(), function.getName()); | ||
| } | ||
|
|
||
| /** | ||
| * Check whether the given function is allowed. | ||
| * | ||
| * @param pluginName The name of the plugin that the function is in. | ||
| * @param functionName The name of the function. | ||
| * @return Whether the function is allowed. | ||
| */ | ||
| public boolean isFunctionAllowed(@Nullable String pluginName, String functionName) { | ||
| // If no functions are provided, all functions are allowed. | ||
| if (functions == null || functions.isEmpty()) { | ||
| return true; | ||
| } | ||
|
|
||
| String key = formFullFunctionName(pluginName, functionName); | ||
| return fullFunctionNames.contains(key); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.