Skip to content

Commit 44b27cd

Browse files
author
Milder Hernandez Cagua
committed
Add AIServiceSelector.trySelectAIService(Class<T>,KernelArguments) and related updates
1 parent de947e2 commit 44b27cd

File tree

6 files changed

+107
-13
lines changed

6 files changed

+107
-13
lines changed

semantickernel-api/src/main/java/com/microsoft/semantickernel/Kernel.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,12 @@ public AIServiceSelector getServiceSelector() {
302302
* @param clazz The class of the service to get.
303303
* @return The service of the specified type from the kernel.
304304
* @throws ServiceNotFoundException if the service is not found.
305-
* @see com.microsoft.semantickernel.services.AIServiceSelector#trySelectAIService(Class,
306-
* KernelFunction, KernelArguments)
305+
* @see com.microsoft.semantickernel.services.AIServiceSelector#trySelectAIService(Class, KernelArguments)
307306
*/
308307
public <T extends AIService> T getService(Class<T> clazz) throws ServiceNotFoundException {
309308
AIServiceSelection<T> selector = serviceSelector
310309
.trySelectAIService(
311310
clazz,
312-
null,
313311
null);
314312

315313
if (selector == null) {
@@ -319,6 +317,29 @@ public <T extends AIService> T getService(Class<T> clazz) throws ServiceNotFound
319317
return selector.getService();
320318
}
321319

320+
/**
321+
* Get the service of the specified type from the kernel.
322+
*
323+
* @param <T> The type of the service to get.
324+
* @param clazz The class of the service to get.
325+
* @param args The arguments to help select the service to get.
326+
* @return The service of the specified type from the kernel.
327+
* @throws ServiceNotFoundException if the service is not found.
328+
* @see com.microsoft.semantickernel.services.AIServiceSelector#trySelectAIService(Class, KernelArguments)
329+
*/
330+
public <T extends AIService> T getService(Class<T> clazz, KernelArguments args) throws ServiceNotFoundException {
331+
AIServiceSelection<T> selector = serviceSelector
332+
.trySelectAIService(
333+
clazz,
334+
args);
335+
336+
if (selector == null) {
337+
throw new ServiceNotFoundException("Unable to find service of type " + clazz.getName());
338+
}
339+
340+
return selector.getService();
341+
}
342+
322343
/**
323344
* A fluent builder for creating a new instance of {@code Kernel}.
324345
*/

semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/KernelArguments.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,16 @@ public <T> Builder<U> withVariable(String key, T value,
360360
value));
361361
}
362362

363+
/**
364+
* Set prompt execution settings
365+
*
366+
* @param executionSettings Execution settings
367+
* @return {$code this} Builder for fluent coding
368+
*/
369+
public Builder<U> withExecutionSettings(PromptExecutionSettings executionSettings) {
370+
return withExecutionSettings(Collections.singletonList(executionSettings));
371+
}
372+
363373
/**
364374
* Set prompt execution settings
365375
*

semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/KernelFunctionFromPrompt.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,17 @@ private Flux<FunctionResult<T>> invokeInternalAsync(
133133

134134
FunctionInvokingEvent updateArguments = kernelHooks
135135
.executeHooks(new FunctionInvokingEvent(this, args));
136-
args = updateArguments.getArguments();
136+
137+
args = KernelArguments.builder()
138+
.withVariables(updateArguments.getArguments())
139+
.withExecutionSettings(
140+
this.getExecutionSettings())
141+
.build();
137142

138143
AIServiceSelection<?> aiServiceSelection = kernel
139144
.getServiceSelector()
140145
.trySelectAIService(
141146
TextAIService.class,
142-
this,
143147
args);
144148

145149
AIService client = aiServiceSelection != null ? aiServiceSelection.getService()

semantickernel-api/src/main/java/com/microsoft/semantickernel/services/AIServiceSelector.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,39 @@ public interface AIServiceSelector {
2020
* @param serviceType The type of service to select. This must be the same type with which the
2121
* service was registered in the {@link AIServiceSelection}
2222
* @param function The KernelFunction to use to select the service, or {@code null}.
23-
* @param arguments The KernelFunctionArguments to use to select the service, or
23+
* @param arguments The KernelArguments to use to select the service, or
2424
* {@code null}.
2525
* @param <T> The type of service to select.
2626
* @return An {@code AIServiceSelection} containing the selected service and associated
2727
* PromptExecutionSettings.
28+
*
29+
* @deprecated Use {@link #trySelectAIService(Class, KernelArguments)} instead.
2830
*/
31+
@Deprecated
2932
@Nullable
3033
<T extends AIService> AIServiceSelection<T> trySelectAIService(
3134
Class<T> serviceType,
3235
@Nullable KernelFunction<?> function,
3336
@Nullable KernelArguments arguments);
37+
38+
/**
39+
* Resolves an {@link AIService} and associated and
40+
* {@link com.microsoft.semantickernel.orchestration.PromptExecutionSettings} based on the
41+
* associated {@link KernelFunction} and {@link KernelArguments}.
42+
*
43+
* @param serviceType The type of service to select. This must be the same type with which the
44+
* service was registered in the {@link AIServiceSelection}
45+
* @param arguments The KernelArguments to use to select the service, or
46+
* {@code null}.
47+
* @param <T> The type of service to select.
48+
* @return An {@code AIServiceSelection} containing the selected service and associated
49+
* PromptExecutionSettings.
50+
*/
51+
@Nullable
52+
default <T extends AIService> AIServiceSelection<T> trySelectAIService(
53+
Class<T> serviceType,
54+
@Nullable KernelArguments arguments) {
55+
throw new UnsupportedOperationException(
56+
"This method is not implemented.");
57+
}
3458
}

semantickernel-api/src/main/java/com/microsoft/semantickernel/services/BaseAIServiceSelector.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public <T extends AIService> AIServiceSelection<T> trySelectAIService(
3535
return trySelectAIService(serviceType, function, arguments, services);
3636
}
3737

38+
@Override
39+
@Nullable
40+
public <T extends AIService> AIServiceSelection<T> trySelectAIService(
41+
Class<T> serviceType,
42+
@Nullable KernelArguments arguments) {
43+
return trySelectAIService(serviceType, arguments, services);
44+
}
45+
3846
/**
3947
* Resolves an {@link AIService} from the {@code services} argument using the specified
4048
* {@code function} and {@code arguments} for selection.
@@ -47,11 +55,36 @@ public <T extends AIService> AIServiceSelection<T> trySelectAIService(
4755
* @param services The services to select from.
4856
* @param <T> The type of service to select.
4957
* @return The selected service, or {@code null} if no service could be selected.
58+
*
59+
* @deprecated Implement {@link #trySelectAIService(Class, KernelArguments)}
5060
*/
61+
@Deprecated
5162
@Nullable
5263
protected abstract <T extends AIService> AIServiceSelection<T> trySelectAIService(
5364
Class<T> serviceType,
5465
@Nullable KernelFunction<?> function,
5566
@Nullable KernelArguments arguments,
5667
Map<Class<? extends AIService>, AIService> services);
68+
69+
70+
/**
71+
* Resolves an {@link AIService} from the {@code services} argument using the specified
72+
* {@code function} and {@code arguments} for selection.
73+
*
74+
* @param serviceType The type of service to select. This must be the same type with which the
75+
* service was registered in the {@link AIServiceSelection}
76+
* @param arguments The KernelArguments to use to select the service, or
77+
* {@code null}.
78+
* @param services The services to select from.
79+
* @param <T> The type of service to select.
80+
* @return The selected service, or {@code null} if no service could be selected.
81+
*/
82+
@Nullable
83+
protected <T extends AIService> AIServiceSelection<T> trySelectAIService(
84+
Class<T> serviceType,
85+
@Nullable KernelArguments arguments,
86+
Map<Class<? extends AIService>, AIService> services) {
87+
throw new UnsupportedOperationException(
88+
"This method is not implemented.");
89+
}
5790
}

semantickernel-api/src/main/java/com/microsoft/semantickernel/services/OrderedAIServiceSelector.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ private static Map<String, PromptExecutionSettings> settingsFromFunctionSettings
6666
return null;
6767
}
6868

69-
// @Nullable
70-
// @Override
71-
// public <T extends AIService> AIServiceSelection<T> trySelectAIService(
72-
// Class<T> serviceType,
73-
// @Nullable KernelArguments arguments) {
74-
// return selectAIService(serviceType, arguments.getPromptExecutionSettings());
75-
// }
69+
@Nullable
70+
@Override
71+
public <T extends AIService> AIServiceSelection<T> trySelectAIService(
72+
Class<T> serviceType,
73+
@Nullable KernelArguments arguments,
74+
Map<Class<? extends AIService>, AIService> services) {
75+
76+
return selectAIService(serviceType, arguments != null ? arguments.getExecutionSettings() : null);
77+
}
7678

7779
@Nullable
7880
@Override

0 commit comments

Comments
 (0)