Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,17 @@ private Flux<FunctionResult<T>> invokeInternalAsync(

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

args = KernelArguments.builder()
.withVariables(updateArguments.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,15 +20,39 @@ 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
* PromptExecutionSettings.
*
* @deprecated Use {@link #trySelectAIService(Class, KernelArguments)} instead.
*/
@Deprecated
@Nullable
<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,36 @@ 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.
*
* @deprecated Implement {@link #trySelectAIService(Class, KernelArguments)}
*/
@Deprecated
@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) {
throw new UnsupportedOperationException(
"This method is not implemented.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ private static Map<String, PromptExecutionSettings> settingsFromFunctionSettings
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(
Class<T> serviceType,
@Nullable KernelArguments arguments,
Map<Class<? extends AIService>, AIService> services) {

return selectAIService(serviceType, arguments != null ? arguments.getExecutionSettings() : null);
}

@Nullable
@Override
Expand Down
Loading