forked from microsoft/semantic-kernel-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionChoiceBehavior.java
More file actions
201 lines (180 loc) · 9.22 KB
/
FunctionChoiceBehavior.java
File metadata and controls
201 lines (180 loc) · 9.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.functionchoice;
import com.microsoft.semantickernel.semanticfunctions.KernelFunction;
import javax.annotation.Nullable;
import java.util.Collections;
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(@Nullable List<KernelFunction<?>> functions,
@Nullable FunctionChoiceBehaviorOptions options) {
this.functions = functions != null ? Collections.unmodifiableList(functions) : null;
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 Collections.unmodifiableList(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.
*
* @return A new FunctionChoiceBehavior instance with all kernel functions allowed.
*/
public static FunctionChoiceBehavior auto(boolean autoInvoke,
@Nullable List<KernelFunction<?>> functions) {
return new AutoFunctionChoiceBehavior(autoInvoke, functions, 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,
@Nullable 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).
*
* @return A new FunctionChoiceBehavior instance with the required function.
*/
public static FunctionChoiceBehavior required(boolean autoInvoke,
@Nullable List<KernelFunction<?>> functions) {
return new RequiredFunctionChoiceBehavior(autoInvoke, functions, 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.
* <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,
@Nullable 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.
*/
public static FunctionChoiceBehavior none() {
return new NoneFunctionChoiceBehavior(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.
* <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(@Nullable 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);
}
}