Skip to content

Commit

Permalink
Replace wrapper with interface method
Browse files Browse the repository at this point in the history
  • Loading branch information
MatKuhr committed Oct 15, 2024
1 parent 088d44d commit 0bc695c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,95 +26,46 @@ public class DefaultOrchestrationConfig<T extends OrchestrationConfig<T>>
@Nonnull private Option<FilterConfig> inputContentFilter = Option.none();
@Nonnull private Option<FilterConfig> outputContentFilter = Option.none();

@Nonnull private final T wrapper;

/**
* Create a new instance of {@link DefaultOrchestrationConfig} to delegate to. This is useful when
* exposing the {@link OrchestrationConfig} in other objects, without re-implementing it. To
* maintain fluent API usage, the given wrapper object will be returned by the fluent methods,
* instead of this instance.
*
* @param wrapper The wrapper that delegates to this object.
* @param <T> The type of the wrapper object.
* @return The new instance.
* @see #standalone()
*/
public static <T extends OrchestrationConfig<T>> DefaultOrchestrationConfig<T> asDelegateFor(
@Nonnull final T wrapper) {
return new DefaultOrchestrationConfig<>(wrapper);
}

/**
* Create an implementation without any object delegating to it. The fluent API will return this
* object itself.
*
* @return The new instance.
* @see #asDelegateFor(OrchestrationConfig)
*/
static DefaultOrchestrationConfig<?> standalone() {
return new DefaultOrchestrationConfig<>();
}

@Nonnull
@Override
@SuppressWarnings("unchecked")
private DefaultOrchestrationConfig() {
this.wrapper = (T) this;
}

private DefaultOrchestrationConfig(@Nonnull final T wrapper) {
this.wrapper = wrapper;
public T instance() {
return (T) this;
}

@Nonnull
@Override
public T withLlmConfig(@Nonnull final LLMModuleConfig llm) {
this.llmConfig = Option.some(llm);
return wrapper;
return instance();
}

@Nonnull
@Override
public T withTemplate(@Nonnull final TemplatingModuleConfig template) {
this.template = Option.some(template);
return wrapper;
return instance();
}

@Nonnull
@Override
public T withMaskingConfig(@Nonnull final DpiMaskingConfig maskingConfig) {
this.maskingConfig = Option.some(maskingConfig);
return wrapper;
return instance();
}

@Nonnull
@Override
public T withInputContentFilter(@Nonnull final FilterConfig filter) {
this.inputContentFilter = Option.some(filter);
return wrapper;
return instance();
}

@Nonnull
@Override
public T withOutputContentFilter(@Nonnull final FilterConfig filter) {
this.outputContentFilter = Option.some(filter);
return wrapper;
}

@Nonnull
static OrchestrationConfig<?> fromConfigAndDefaults(
@Nonnull final OrchestrationConfig<?> config,
@Nonnull final OrchestrationConfig<?> defaults) {

var copy = DefaultOrchestrationConfig.standalone();

copy.llmConfig = config.getLlmConfig().orElse(defaults::getLlmConfig);
copy.template = config.getTemplate().orElse(defaults::getTemplate);
copy.maskingConfig = config.getMaskingConfig().orElse(defaults::getMaskingConfig);
copy.inputContentFilter =
config.getInputContentFilter().orElse(defaults::getInputContentFilter);
copy.outputContentFilter =
config.getOutputContentFilter().orElse(defaults::getOutputContentFilter);

return copy;
return instance();
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class OrchestrationClient implements OrchestrationConfig<OrchestrationCli

@Delegate @Nonnull
private final DefaultOrchestrationConfig<OrchestrationClient> clientConfig =
DefaultOrchestrationConfig.asDelegateFor(this);
new DefaultOrchestrationConfig<>();

@Nonnull private final HttpDestination destination;

Expand All @@ -47,6 +47,12 @@ public OrchestrationClient() {
this.destination = Core.getDestinationForDeployment("db1d64d9f06be467", "default").asHttp();
}

@Nonnull
@Override
public OrchestrationClient instance() {
return this;
}

/**
* Generate a completion for the given user prompt.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* @param <T> Type of the specific implementation to make a fluent API possible.
*/
public interface OrchestrationConfig<T extends OrchestrationConfig<T>> {
@Nonnull
T instance();

@Nonnull
Option<LLMModuleConfig> getLlmConfig();

Expand Down Expand Up @@ -42,4 +45,30 @@ public interface OrchestrationConfig<T extends OrchestrationConfig<T>> {

@Nonnull
T withOutputContentFilter(@Nonnull final FilterConfig filter);

/**
* Copy the configuration into the given target configuration. The copy is
* <strong>shallow</strong> and does <strong>not override</strong> any existing configuration.
*
* <p>This has two main use cases:
*
* <ol>
* <li>Duplicating a config
* <li>Applying defaults to a config
* </ol>
*
* @param source The source configuration to copy from.
*/
default T copyFrom(@Nonnull final OrchestrationConfig<?> source) {
getLlmConfig().orElse(source::getLlmConfig).forEach(this::withLlmConfig);
getTemplate().orElse(source::getTemplate).forEach(this::withTemplate);
getMaskingConfig().orElse(source::getMaskingConfig).forEach(this::withMaskingConfig);
getInputContentFilter()
.orElse(source::getInputContentFilter)
.forEach(this::withInputContentFilter);
getOutputContentFilter()
.orElse(source::getOutputContentFilter)
.forEach(this::withOutputContentFilter);
return instance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ public class OrchestrationPrompt implements OrchestrationConfig<OrchestrationPro
@Getter(AccessLevel.NONE)
@Delegate
@Nonnull
DefaultOrchestrationConfig<OrchestrationPrompt> delegate =
DefaultOrchestrationConfig.asDelegateFor(this);
DefaultOrchestrationConfig<OrchestrationPrompt> delegate = new DefaultOrchestrationConfig<>();

@Nonnull
@Override
public OrchestrationPrompt instance() {
return this;
}

public OrchestrationPrompt(@Nonnull final String message) {
this(List.of(ChatMessage.create().role("user").content(message)), Map.of());
Expand All @@ -37,7 +42,8 @@ public OrchestrationPrompt(@Nonnull final Map<String, String> inputParams) {

@Nonnull
ModuleConfigs toModuleConfigDTO(@Nonnull final OrchestrationConfig<?> defaults) {
var config = DefaultOrchestrationConfig.fromConfigAndDefaults(delegate, defaults);
// duplicate the prompt config so it isn't modified, to make sure this prompt can be reused
var config = new DefaultOrchestrationConfig<>().copyFrom(this).copyFrom(defaults);
return DefaultOrchestrationConfig.toModuleConfigDTO(config, messages);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@
/** Configuration to be used for orchestration requests. */
@Data
@Getter(AccessLevel.PACKAGE)
@Setter(AccessLevel.NONE)
@Setter(AccessLevel.PRIVATE)
public class OrchestrationChatOptions
implements ChatOptions, OrchestrationConfig<OrchestrationChatOptions> {
@Delegate @Nonnull
private final DefaultOrchestrationConfig<OrchestrationChatOptions> delegate =
DefaultOrchestrationConfig.asDelegateFor(this);
new DefaultOrchestrationConfig<>();

@Nonnull
@Override
public OrchestrationChatOptions instance() {
return this;
}

@Getter(AccessLevel.PUBLIC)
@Nonnull
Expand All @@ -44,6 +50,7 @@ public OrchestrationChatOptions withTemplate(@Nonnull final List<Message> templa
return this;
}

@Nonnull
static List<ChatMessage> toChatMessages(@Nonnull final List<Message> messages) {
return messages.stream()
.map(m -> ChatMessage.create().role(m.getMessageType().getValue()).content(m.getContent()))
Expand Down Expand Up @@ -106,6 +113,8 @@ public Double getTopP() {

@Override
public OrchestrationChatOptions copy() {
var copy = new OrchestrationChatOptions();

// TODO: implement, needed for chat memory apparently
throw new RuntimeException("Not implemented");
}
Expand Down

0 comments on commit 0bc695c

Please sign in to comment.