forked from microsoft/semantic-kernel-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKernel.java
More file actions
419 lines (374 loc) · 15.8 KB
/
Kernel.java
File metadata and controls
419 lines (374 loc) · 15.8 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel;
import com.microsoft.semantickernel.builders.SemanticKernelBuilder;
import com.microsoft.semantickernel.contextvariables.ContextVariableType;
import com.microsoft.semantickernel.hooks.KernelHooks;
import com.microsoft.semantickernel.orchestration.FunctionInvocation;
import com.microsoft.semantickernel.orchestration.FunctionResult;
import com.microsoft.semantickernel.orchestration.InvocationContext;
import com.microsoft.semantickernel.plugin.KernelPlugin;
import com.microsoft.semantickernel.semanticfunctions.KernelFunction;
import com.microsoft.semantickernel.semanticfunctions.KernelArguments;
import com.microsoft.semantickernel.services.AIService;
import com.microsoft.semantickernel.services.AIServiceCollection;
import com.microsoft.semantickernel.services.AIServiceSelection;
import com.microsoft.semantickernel.services.AIServiceSelector;
import com.microsoft.semantickernel.services.OrderedAIServiceSelector;
import com.microsoft.semantickernel.services.ServiceNotFoundException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Provides state for use throughout a Semantic Kernel workload.
* <p>
* An instance of {@code Kernel} is passed through to every function invocation and service call
* throughout the system, providing to each the ability to access shared state and services.
*/
public class Kernel {
private final AIServiceSelector serviceSelector;
private final KernelPluginCollection plugins;
private final KernelHooks globalKernelHooks;
// Only present so we can create a builder in copy method
private final AIServiceCollection services;
@Nullable
private final Function<AIServiceCollection, AIServiceSelector> serviceSelectorProvider;
/**
* Initializes a new instance of {@code Kernel}.
*
* @param services The collection of services available through the kernel.
* @param serviceSelectorProvider The service selector provider for the kernel. If {@code null},
* an ordered service selector will be used.
* @param plugins The collection of plugins available through the kernel. If
* {@code null}, an empty collection will be used.
* @param globalKernelHooks The global hooks to be used throughout the kernel. If
* {@code null}, an empty collection will be used.
*/
@SuppressFBWarnings("EI_EXPOSE_REP2")
public Kernel(
AIServiceCollection services,
@Nullable Function<AIServiceCollection, AIServiceSelector> serviceSelectorProvider,
@Nullable List<KernelPlugin> plugins,
@Nullable KernelHooks globalKernelHooks) {
this.services = services;
this.serviceSelectorProvider = serviceSelectorProvider;
AIServiceSelector serviceSelector;
if (serviceSelectorProvider == null) {
serviceSelector = new OrderedAIServiceSelector(services);
} else {
serviceSelector = serviceSelectorProvider.apply(services);
}
this.serviceSelector = serviceSelector;
if (plugins != null) {
this.plugins = new KernelPluginCollection(plugins);
} else {
this.plugins = new KernelPluginCollection();
}
this.globalKernelHooks = new KernelHooks(globalKernelHooks);
}
/**
* Get the fluent builder for creating a new instance of {@code Kernel}.
*
* @return The fluent builder for creating a new instance of {@code Kernel}.
*/
public static Builder builder() {
return new Kernel.Builder();
}
/**
* Creates a Builder that can create a copy of the {@code Kernel}. Use this method if you wish
* to modify the state of the kernel such as adding new plugins or services.
*
* @param kernel The kernel to copy.
* @return A Builder that can create a copy of the instance of {@code Kernel}.
*/
public static Builder from(Kernel kernel) {
return new Builder(
kernel.services,
kernel.serviceSelectorProvider,
kernel.plugins);
}
/**
* Creates a Builder that can create a copy of the current instance of {@code Kernel}. Use this
* method if you wish to modify the state of the kernel such as adding new plugins or services.
*
* @return A Builder that can create a copy of the current instance of {@code Kernel}.
*/
public Builder toBuilder() {
return new Builder(services, serviceSelectorProvider, plugins);
}
/**
* Invokes a {@code KernelFunction} function by name.
*
* @param <T> The return type of the function.
* @param pluginName The name of the plugin containing the function.
* @param functionName The name of the function to invoke.
* @return The result of the function invocation.
* @throws IllegalArgumentException if the plugin or function is not found.
* @see KernelFunction#invokeAsync(Kernel)
* @see KernelPluginCollection#getFunction(String, String)
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public <T> FunctionInvocation<T> invokeAsync(
String pluginName,
String functionName) {
KernelFunction function = getFunction(pluginName, functionName);
return invokeAsync(function);
}
/**
* Invokes a {@code KernelFunction} function by name.
*
* @param <T> The return type of the function.
* @param pluginName The name of the plugin containing the function.
* @param functionName The name of the function to invoke.
* @return The result of the function invocation.
* @throws IllegalArgumentException if the plugin or function is not found.
* @see KernelFunction#invokeAsync(Kernel)
* @see KernelPluginCollection#getFunction(String, String)
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public <T> FunctionResult<T> invoke(
String pluginName,
String functionName) {
return this.<T>invokeAsync(pluginName, functionName).block();
}
/**
* Invokes a Prompt.
*
* @param <T> The return type of the prompt.
* @param prompt The prompt to invoke.
* @return The result of the prompt invocation.
* @see KernelFunction#invokeAsync(Kernel)
*/
public <T> FunctionInvocation<T> invokePromptAsync(@Nonnull String prompt) {
return invokeAsync(KernelFunction.<T>createFromPrompt(prompt).build());
}
/**
* Invokes a Prompt.
*
* @param <T> The return type of the prompt.
* @param prompt The prompt to invoke.
* @param arguments The arguments to pass to the prompt.
* @return The result of the prompt invocation.
* @see KernelFunction#invokeAsync(Kernel)
*/
public <T> FunctionInvocation<T> invokePromptAsync(@Nonnull String prompt,
@Nonnull KernelArguments arguments) {
KernelFunction<T> function = KernelFunction.<T>createFromPrompt(prompt).build();
return function.invokeAsync(this)
.withArguments(arguments);
}
/**
* Invokes a Prompt.
*
* @param <T> The return type of the prompt.
* @param prompt The prompt to invoke.
* @param arguments The arguments to pass to the prompt.
* @param invocationContext Additional context to used when invoking the prompt.
* @return The result of the prompt invocation.
* @see KernelFunction#invokeAsync(Kernel)
*/
public <T> FunctionInvocation<T> invokePromptAsync(@Nonnull String prompt,
@Nonnull KernelArguments arguments, @Nonnull InvocationContext invocationContext) {
KernelFunction<T> function = KernelFunction.<T>createFromPrompt(prompt).build();
return function.invokeAsync(this)
.withArguments(arguments)
.withInvocationContext(invocationContext);
}
/**
* Invokes a {@code KernelFunction}.
*
* @param <T> The return type of the function.
* @param function The function to invoke.
* @return The result of the function invocation.
* @see KernelFunction#invokeAsync(Kernel)
*/
public <T> FunctionInvocation<T> invokeAsync(KernelFunction<T> function) {
return function.invokeAsync(this);
}
/**
* Invokes a {@code KernelFunction}.
*
* @param <T> The return type of the function.
* @param function The function to invoke.
* @return The result of the function invocation.
* @see KernelFunction#invokeAsync(Kernel)
*/
public <T> FunctionResult<T> invoke(KernelFunction<T> function) {
return invokeAsync(function).block();
}
/**
* Gets the plugin with the specified name.
*
* @param pluginName The name of the plugin to get.
* @return The plugin with the specified name, or {@code null} if no such plugin exists.
*/
@Nullable
public KernelPlugin getPlugin(String pluginName) {
return plugins.getPlugin(pluginName);
}
/**
* Gets the plugins that were added to the kernel.
*
* @return The plugins available through the kernel (unmodifiable list).
* @see Kernel#getPlugins()
*/
public Collection<KernelPlugin> getPlugins() {
return Collections.unmodifiableCollection(plugins.getPlugins());
}
/**
* Gets the function with the specified name from the plugin with the specified name.
*
* @param <T> The return type of the function.
* @param pluginName The name of the plugin containing the function.
* @param functionName The name of the function to get.
* @return The function with the specified name from the plugin with the specified name.
* @throws IllegalArgumentException if the plugin or function is not found.
* @see KernelPluginCollection#getFunction(String, String)
*/
@SuppressWarnings("unchecked")
public <T> KernelFunction<T> getFunction(String pluginName, String functionName) {
return (KernelFunction<T>) plugins.getFunction(pluginName, functionName);
}
/**
* Gets the functions available through the kernel. Functions are collected from all plugins
* available through the kernel.
*
* @return The functions available through the kernel.
* @see Kernel#getPlugins()
* @see Kernel.Builder#withPlugin(KernelPlugin)
*/
public List<KernelFunction<?>> getFunctions() {
return plugins.getFunctions();
}
/**
* Get the {@code KernelHooks} used throughout the kernel. These {@code KernelHooks} are used in
* addition to any hooks provided to a function.
*
* @return The {@code KernelHooks} used throughout the kernel.
* @see KernelFunction#invokeAsync(Kernel, KernelArguments, ContextVariableType,
* InvocationContext)
*/
@SuppressFBWarnings("EI_EXPOSE_REP")
public KernelHooks getGlobalKernelHooks() {
return globalKernelHooks;
}
/**
* Get the AIServiceSelector used to query for services available through the kernel.
*
* @return The AIServiceSelector used to query for services available through the kernel.
*/
public AIServiceSelector getServiceSelector() {
return serviceSelector;
}
/**
* Get the service of the specified type from the kernel.
*
* @param <T> The type of the service to get.
* @param clazz The class of the service to get.
* @return The service of the specified type from the kernel.
* @throws ServiceNotFoundException if the service is not found.
* @see com.microsoft.semantickernel.services.AIServiceSelector#trySelectAIService(Class, KernelArguments)
*/
public <T extends AIService> T getService(Class<T> clazz) throws ServiceNotFoundException {
AIServiceSelection<T> selector = serviceSelector
.trySelectAIService(
clazz,
null);
if (selector == null) {
throw new ServiceNotFoundException("Unable to find service of type " + clazz.getName());
}
return selector.getService();
}
/**
* Get the service of the specified type from the kernel.
*
* @param <T> The type of the service to get.
* @param clazz The class of the service to get.
* @param args The arguments to help select the service to get.
* @return The service of the specified type from the kernel.
* @throws ServiceNotFoundException if the service is not found.
* @see com.microsoft.semantickernel.services.AIServiceSelector#trySelectAIService(Class, KernelArguments)
*/
public <T extends AIService> T getService(Class<T> clazz, KernelArguments args) throws ServiceNotFoundException {
AIServiceSelection<T> selector = serviceSelector
.trySelectAIService(
clazz,
args);
if (selector == null) {
throw new ServiceNotFoundException("Unable to find service of type " + clazz.getName());
}
return selector.getService();
}
/**
* A fluent builder for creating a new instance of {@code Kernel}.
*/
public static class Builder implements SemanticKernelBuilder<Kernel> {
private final AIServiceCollection services = new AIServiceCollection();
private final List<KernelPlugin> plugins = new ArrayList<>();
@Nullable
private Function<AIServiceCollection, AIServiceSelector> serviceSelectorProvider;
/**
* Construct a Builder for creating a new instance of {@code Kernel}.
*/
public Builder() {
}
private Builder(
AIServiceCollection services,
@Nullable Function<AIServiceCollection, AIServiceSelector> serviceSelectorProvider,
KernelPluginCollection plugins) {
this.services.putAll(services);
this.serviceSelectorProvider = serviceSelectorProvider;
this.plugins.addAll(plugins.getPlugins());
}
/**
* Adds a service to the kernel.
*
* @param <T> The type of the service to add.
* @param clazz The class of the service to add.
* @param aiService The service to add.
* @return {@code this} builder with the service added.
*/
public <T extends AIService> Builder withAIService(Class<T> clazz, T aiService) {
services.put(clazz, aiService);
return this;
}
/**
* Adds a plugin to the kernel.
*
* @param plugin The plugin to add.
* @return {@code this} builder with the plugin added.
*/
public Kernel.Builder withPlugin(KernelPlugin plugin) {
plugins.add(plugin);
return this;
}
/**
* Sets the service selector provider for the kernel.
*
* @param serviceSelector The service selector provider for the kernel.
* @return {@code this} builder with the service selector provider set.
*/
public Kernel.Builder withServiceSelector(
Function<AIServiceCollection, AIServiceSelector> serviceSelector) {
this.serviceSelectorProvider = serviceSelector;
return this;
}
/**
* Builds a new instance of {@code Kernel} with the services and plugins provided.
*
* @return A new instance of {@code Kernel}.
*/
@Override
public Kernel build() {
return new Kernel(
services,
serviceSelectorProvider,
plugins,
null);
}
}
}