Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,18 @@ private static void verifyCalled(OpenAIAsyncClient client, String expected) {
new JsonOptions()
);
JsonWriter format = chatCompletionsOptions.getResponseFormat()
.toJson(jsonWriter);
.toJson(jsonWriter);
jsonWriter.flush();
writer.flush();

String json = String.valueOf(writer.getBuffer())
.replaceAll("\n", "")
.replaceAll("\r", "")
.replaceAll(" +", "");
String expectedClean = expected
.stripIndent()
.replaceAll("\n", "")
.replaceAll("\r", "")
.replaceAll(" +", "");

return json.equals(expectedClean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,12 @@ public AIServiceSelector getServiceSelector() {
* @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,
* KernelFunction, KernelArguments)
* @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,
null);

if (selector == null) {
Expand All @@ -319,6 +317,29 @@ public <T extends AIService> T getService(Class<T> clazz) throws ServiceNotFound
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}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,16 @@ public <T> Builder<U> withVariable(String key, T value,
value));
}

/**
* Set prompt execution settings
*
* @param executionSettings Execution settings
* @return {$code this} Builder for fluent coding
*/
public Builder<U> withExecutionSettings(PromptExecutionSettings executionSettings) {
return withExecutionSettings(Collections.singletonList(executionSettings));
}

/**
* Set prompt execution settings
*
Expand Down Expand Up @@ -399,6 +409,8 @@ public Builder<U> withExecutionSettings(List<PromptExecutionSettings> executionS
serviceId)
);
}

this.executionSettings.put(serviceId, settings);
}

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,18 @@ private Flux<FunctionResult<T>> invokeInternalAsync(

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

FunctionInvokingEvent updateArguments = kernelHooks
FunctionInvokingEvent invokingEvent = kernelHooks
.executeHooks(new FunctionInvokingEvent(this, args));
args = updateArguments.getArguments();

args = KernelArguments.builder()
.withVariables(invokingEvent.getArguments())
.withExecutionSettings(this.getExecutionSettings())
.build();

AIServiceSelection<?> aiServiceSelection = kernel
.getServiceSelector()
.trySelectAIService(
TextAIService.class,
this,
args);

AIService client = aiServiceSelection != null ? aiServiceSelection.getService()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface AIServiceSelector {
* @param serviceType The type of service to select. This must be the same type with which the
* service was registered in the {@link AIServiceSelection}
* @param function The KernelFunction to use to select the service, or {@code null}.
* @param arguments The KernelFunctionArguments to use to select the service, or
* @param arguments The KernelArguments to use to select the service, or
* {@code null}.
* @param <T> The type of service to select.
* @return An {@code AIServiceSelection} containing the selected service and associated
Expand All @@ -31,4 +31,25 @@ <T extends AIService> AIServiceSelection<T> trySelectAIService(
Class<T> serviceType,
@Nullable KernelFunction<?> function,
@Nullable KernelArguments arguments);

/**
* Resolves an {@link AIService} and associated and
* {@link com.microsoft.semantickernel.orchestration.PromptExecutionSettings} based on the
* associated {@link KernelFunction} and {@link KernelArguments}.
*
* @param serviceType The type of service to select. This must be the same type with which the
* service was registered in the {@link AIServiceSelection}
* @param arguments The KernelArguments to use to select the service, or
* {@code null}.
* @param <T> The type of service to select.
* @return An {@code AIServiceSelection} containing the selected service and associated
* PromptExecutionSettings.
*/
@Nullable
default <T extends AIService> AIServiceSelection<T> trySelectAIService(
Class<T> serviceType,
@Nullable KernelArguments arguments) {
throw new UnsupportedOperationException(
"This method is not implemented.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public <T extends AIService> AIServiceSelection<T> trySelectAIService(
return trySelectAIService(serviceType, function, arguments, services);
}

@Override
@Nullable
public <T extends AIService> AIServiceSelection<T> trySelectAIService(
Class<T> serviceType,
@Nullable KernelArguments arguments) {
return trySelectAIService(serviceType, arguments, services);
}

/**
* Resolves an {@link AIService} from the {@code services} argument using the specified
* {@code function} and {@code arguments} for selection.
Expand All @@ -47,11 +55,33 @@ public <T extends AIService> AIServiceSelection<T> trySelectAIService(
* @param services The services to select from.
* @param <T> The type of service to select.
* @return The selected service, or {@code null} if no service could be selected.
*
*/
@Nullable
protected abstract <T extends AIService> AIServiceSelection<T> trySelectAIService(
Class<T> serviceType,
@Nullable KernelFunction<?> function,
@Nullable KernelArguments arguments,
Map<Class<? extends AIService>, AIService> services);


/**
* Resolves an {@link AIService} from the {@code services} argument using the specified
* {@code function} and {@code arguments} for selection.
*
* @param serviceType The type of service to select. This must be the same type with which the
* service was registered in the {@link AIServiceSelection}
* @param arguments The KernelArguments to use to select the service, or
* {@code null}.
* @param services The services to select from.
* @param <T> The type of service to select.
* @return The selected service, or {@code null} if no service could be selected.
*/
@Nullable
protected <T extends AIService> AIServiceSelection<T> trySelectAIService(
Class<T> serviceType,
@Nullable KernelArguments arguments,
Map<Class<? extends AIService>, AIService> services) {
return trySelectAIService(serviceType, null, arguments, services);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,6 @@ private static <T extends AIService> AIServiceSelection<T> castServiceSelection(
}
}

@Nullable
private static Map<String, PromptExecutionSettings> settingsFromFunctionSettings(
@Nullable KernelFunction function) {
if (function != null) {
return function.getExecutionSettings();
}
return null;
}

// @Nullable
// @Override
// public <T extends AIService> AIServiceSelection<T> trySelectAIService(
// Class<T> serviceType,
// @Nullable KernelArguments arguments) {
// return selectAIService(serviceType, arguments.getPromptExecutionSettings());
// }

@Nullable
@Override
public <T extends AIService> AIServiceSelection<T> trySelectAIService(
Expand All @@ -82,11 +65,11 @@ public <T extends AIService> AIServiceSelection<T> trySelectAIService(
@Nullable KernelArguments arguments,
Map<Class<? extends AIService>, AIService> services) {

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

return selectAIService(serviceType, executionSettings);
return selectAIService(serviceType, function.getExecutionSettings());
}


Expand Down