Skip to content

Commit 69af29b

Browse files
author
Milder Hernandez
authored
Merge pull request #301 from milderhc/service-selector
Add AIServiceSelector.trySelectAIService(Class<T>,KernelArguments)
2 parents de947e2 + 17a920e commit 69af29b

File tree

7 files changed

+101
-29
lines changed

7 files changed

+101
-29
lines changed

api-test/integration-tests/src/test/java/com/microsoft/semantickernel/tests/ResponseSchemaTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,18 @@ private static void verifyCalled(OpenAIAsyncClient client, String expected) {
161161
new JsonOptions()
162162
);
163163
JsonWriter format = chatCompletionsOptions.getResponseFormat()
164-
.toJson(jsonWriter);
164+
.toJson(jsonWriter);
165165
jsonWriter.flush();
166166
writer.flush();
167167

168168
String json = String.valueOf(writer.getBuffer())
169169
.replaceAll("\n", "")
170+
.replaceAll("\r", "")
170171
.replaceAll(" +", "");
171172
String expectedClean = expected
172173
.stripIndent()
173174
.replaceAll("\n", "")
175+
.replaceAll("\r", "")
174176
.replaceAll(" +", "");
175177

176178
return json.equals(expectedClean);

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: 12 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
*
@@ -399,6 +409,8 @@ public Builder<U> withExecutionSettings(List<PromptExecutionSettings> executionS
399409
serviceId)
400410
);
401411
}
412+
413+
this.executionSettings.put(serviceId, settings);
402414
}
403415

404416
return this;

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,18 @@ private Flux<FunctionResult<T>> invokeInternalAsync(
131131

132132
LOGGER.info(SemanticKernelResources.getString("rendered.prompt"), prompt);
133133

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

138142
AIServiceSelection<?> aiServiceSelection = kernel
139143
.getServiceSelector()
140144
.trySelectAIService(
141145
TextAIService.class,
142-
this,
143146
args);
144147

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

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ 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
@@ -31,4 +31,25 @@ <T extends AIService> AIServiceSelection<T> trySelectAIService(
3131
Class<T> serviceType,
3232
@Nullable KernelFunction<?> function,
3333
@Nullable KernelArguments arguments);
34+
35+
/**
36+
* Resolves an {@link AIService} and associated and
37+
* {@link com.microsoft.semantickernel.orchestration.PromptExecutionSettings} based on the
38+
* associated {@link KernelFunction} and {@link KernelArguments}.
39+
*
40+
* @param serviceType The type of service to select. This must be the same type with which the
41+
* service was registered in the {@link AIServiceSelection}
42+
* @param arguments The KernelArguments to use to select the service, or
43+
* {@code null}.
44+
* @param <T> The type of service to select.
45+
* @return An {@code AIServiceSelection} containing the selected service and associated
46+
* PromptExecutionSettings.
47+
*/
48+
@Nullable
49+
default <T extends AIService> AIServiceSelection<T> trySelectAIService(
50+
Class<T> serviceType,
51+
@Nullable KernelArguments arguments) {
52+
throw new UnsupportedOperationException(
53+
"This method is not implemented.");
54+
}
3455
}

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

Lines changed: 30 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,33 @@ 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+
*
5059
*/
5160
@Nullable
5261
protected abstract <T extends AIService> AIServiceSelection<T> trySelectAIService(
5362
Class<T> serviceType,
5463
@Nullable KernelFunction<?> function,
5564
@Nullable KernelArguments arguments,
5665
Map<Class<? extends AIService>, AIService> services);
66+
67+
68+
/**
69+
* Resolves an {@link AIService} from the {@code services} argument using the specified
70+
* {@code function} and {@code arguments} for selection.
71+
*
72+
* @param serviceType The type of service to select. This must be the same type with which the
73+
* service was registered in the {@link AIServiceSelection}
74+
* @param arguments The KernelArguments to use to select the service, or
75+
* {@code null}.
76+
* @param services The services to select from.
77+
* @param <T> The type of service to select.
78+
* @return The selected service, or {@code null} if no service could be selected.
79+
*/
80+
@Nullable
81+
protected <T extends AIService> AIServiceSelection<T> trySelectAIService(
82+
Class<T> serviceType,
83+
@Nullable KernelArguments arguments,
84+
Map<Class<? extends AIService>, AIService> services) {
85+
return trySelectAIService(serviceType, null, arguments, services);
86+
}
5787
}

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

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,6 @@ private static <T extends AIService> AIServiceSelection<T> castServiceSelection(
5757
}
5858
}
5959

60-
@Nullable
61-
private static Map<String, PromptExecutionSettings> settingsFromFunctionSettings(
62-
@Nullable KernelFunction function) {
63-
if (function != null) {
64-
return function.getExecutionSettings();
65-
}
66-
return null;
67-
}
68-
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-
// }
76-
7760
@Nullable
7861
@Override
7962
public <T extends AIService> AIServiceSelection<T> trySelectAIService(
@@ -82,11 +65,11 @@ public <T extends AIService> AIServiceSelection<T> trySelectAIService(
8265
@Nullable KernelArguments arguments,
8366
Map<Class<? extends AIService>, AIService> services) {
8467

85-
// Allow the execution settings from the kernel arguments to take precedence
86-
Map<String, PromptExecutionSettings> executionSettings = settingsFromFunctionSettings(
87-
function);
68+
if (function == null) {
69+
return selectAIService(serviceType, arguments != null ? arguments.getExecutionSettings() : null);
70+
}
8871

89-
return selectAIService(serviceType, executionSettings);
72+
return selectAIService(serviceType, function.getExecutionSettings());
9073
}
9174

9275

0 commit comments

Comments
 (0)