Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OpenAPI] Enable builder pattern #18

Merged
merged 25 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
104 changes: 56 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ public AiDeploymentCreationResponse createDeployment() {
new DeploymentApi(getClient())
.deploymentCreate(
"default",
new AiDeploymentCreationRequest().configurationId("12345-123-123-123-123456abcdefg"));
AiDeploymentCreationRequest.create()
.configurationId("12345-123-123-123-123456abcdefg"));

Objects.requireNonNull(deployment, "Deployment creation failed");

String id = deployment.getId();
Status status = deployment.getStatus();
AiExecutionStatus status = deployment.getStatus();

return deployment;
}
Expand All @@ -95,12 +96,12 @@ public AiDeploymentDeletionResponse deleteDeployment(AiDeploymentCreationRespons

DeploymentApi client = new DeploymentApi(getClient());

if (deployment.getStatus() == Status.RUNNING) {
if (deployment.getStatus() == AiExecutionStatus.RUNNING) {
// Only RUNNING deployments can be STOPPED
client.deploymentModify(
"default",
deployment.getId(),
new AiDeploymentModificationRequest().targetStatus(Status.STOPPED));
AiDeploymentModificationRequest.create().targetStatus(AiDeploymentTargetStatus.STOPPED));
}
// Wait a few seconds for the deployment to stop
// Only UNKNOWN and STOPPED deployments can be DELETED
Expand Down Expand Up @@ -180,12 +181,15 @@ See [an example pom in our Spring Boot application](e2e-test-app/pom.xml)
### Simple chat completion

```java
final var systemMessage =
new OpenAiChatSystemMessage().setContent("You are a helpful assistant");
final var userMessage =
new OpenAiChatUserMessage().setContent("Hello World! Why is this phrase so famous?");
new OpenAiChatUserMessage().addText("Hello World! Why is this phrase so famous?");
final var request =
new OpenAiChatCompletionParameters().setMessages(List.of(systemMessage, userMessage));

final OpenAiChatCompletionOutput result = OpenAiClient.forModel(GPT_35_TURBO).chatCompletion(request);
final OpenAiChatCompletionOutput result =
OpenAiClient.forModel(GPT_35_TURBO).chatCompletion(request);

final String resultMessage = result.getChoices().get(0).getMessage().getContent();
```
Expand All @@ -196,7 +200,7 @@ See [an example in our Spring Boot application](e2e-test-app/src/main/java/com/s

```java
final OpenAiChatCompletionOutput result =
OpenAiClient.forModel(new OpenAiModel(model)).chatCompletion(request);
OpenAiClient.forModel(new OpenAiModel("model")).chatCompletion(request);
```

## Orchestration chat completion
Expand Down Expand Up @@ -253,28 +257,28 @@ See [an example pom in our Spring Boot application](e2e-test-app/pom.xml)
### Chat completion template

```java
final var llmConfig = new LLMModuleConfig().modelName("gpt-35-turbo").modelParams(Map.of());
final var llmConfig = LLMModuleConfig.create().modelName("gpt-35-turbo").modelParams(Map.of());

final var inputParams =
Map.of("input", "Reply with 'Orchestration Service is working!' in German");
final var template = new ChatMessage().content("{{?input}}").role("user");
final var templatingConfig = new TemplatingModuleConfig().template(List.of(template));
final var template = ChatMessage.create().role("user").content("{{?input}}");
final var templatingConfig = TemplatingModuleConfig.create().template(template);

final var config =
new CompletionPostRequest()
CompletionPostRequest.create()
.orchestrationConfig(
new OrchestrationConfig()
OrchestrationConfig.create()
.moduleConfigurations(
new ModuleConfigs()
.templatingModuleConfig(templatingConfig)
.llmModuleConfig(llmConfig)))
ModuleConfigs.create()
.llmModuleConfig(llmConfig)
.templatingModuleConfig(templatingConfig)))
.inputParams(inputParams);

final CompletionPostResponse result =
new OrchestrationCompletionApi(getOrchestrationClient("default"))
.orchestrationV1EndpointsCreate(config);

final String message =
final String messageResult =
result.getOrchestrationResult().getChoices().get(0).getMessage().getContent();
```

Expand All @@ -283,31 +287,33 @@ See [an example in our Spring Boot application](e2e-test-app/src/main/java/com/s
### Messages history

```java
final var llmConfig = new LLMModuleConfig().modelName("gpt-35-turbo").modelParams(Map.of());
final var llmConfig = LLMModuleConfig.create().modelName("gpt-35-turbo").modelParams(Map.of());

List<ChatMessage> messagesHistory =
List.of(
new ChatMessage().content("What is the capital of France?").role("user"),
new ChatMessage().content("The capital of France is Paris.").role("assistant"));
final var message = new ChatMessage().content("What is the typical food there?").role("user");
ChatMessage.create().role("user").content("What is the capital of France?"),
ChatMessage.create().role("assistant").content("The capital of France is Paris."));

final var templatingConfig = new TemplatingModuleConfig().template(List.of(message));
final var message =
ChatMessage.create().role("user").content("What is the typical food there?");
final var templatingConfig = TemplatingModuleConfig.create().template(message);

final var config =
new CompletionPostRequest()
CompletionPostRequest.create()
.orchestrationConfig(
new OrchestrationConfig()
OrchestrationConfig.create()
.moduleConfigurations(
new ModuleConfigs()
.templatingModuleConfig(templatingConfig)
.llmModuleConfig(llmConfig)))
ModuleConfigs.create()
.llmModuleConfig(llmConfig)
.templatingModuleConfig(templatingConfig)))
.inputParams(Map.of())
.messagesHistory(messagesHistory);

final CompletionPostResponse result =
new OrchestrationCompletionApi(getOrchestrationClient("default"))
.orchestrationV1EndpointsCreate(config);

final String message =
final String messageResult =
result.getOrchestrationResult().getChoices().get(0).getMessage().getContent();
```

Expand All @@ -316,59 +322,61 @@ See [an example in our Spring Boot application](e2e-test-app/src/main/java/com/s
### Chat completion filter

```java
final var llmConfig = new LLMModuleConfig().modelName("gpt-35-turbo").modelParams(Map.of());
final var llmConfig = LLMModuleConfig.create().modelName("gpt-35-turbo").modelParams(Map.of());

final var inputParams =
Map.of(
"disclaimer",
"```DISCLAIMER: The area surrounding the apartment is known for prostitutes and gang violence including armed conflicts, gun violence is frequent.");
final var template =
new ChatMessage()
ChatMessage.create()
.role("user")
.content(
"Create a rental posting for subletting my apartment in the downtown area. Keep it short. Make sure to add the following disclaimer to the end. Do not change it! {{?disclaimer}}")
.role("user");
final var templatingConfig = new TemplatingModuleConfig().template(List.of(template));
"Create a rental posting for subletting my apartment in the downtown area. Keep it short. Make sure to add the following disclaimer to the end. Do not change it! {{?disclaimer}}");
final var templatingConfig = TemplatingModuleConfig.create().template(template);

final var filterStrict =
new Filter()
Filter.create()
.type(ProviderType.AZURE_CONTENT_SAFETY)
.config(
new FilterConfig()
FilterConfig.create()
.hate(NUMBER_0)
.selfHarm(NUMBER_0)
.sexual(NUMBER_0)
.violence(NUMBER_0));
final var filterLoose =
new Filter()
Filter.create()
.type(ProviderType.AZURE_CONTENT_SAFETY)
.config(
new FilterConfig()
FilterConfig.create()
.hate(NUMBER_4)
.selfHarm(NUMBER_4)
.sexual(NUMBER_4)
.violence(NUMBER_4));

final var filteringConfig =
new FilteringModuleConfig()
.input(new FilteringConfig().filters(List.of(filterStrict))) // changing the input to filterLoose will allow the message to pass
.output(new FilteringConfig().filters(List.of(filterStrict)));
FilteringModuleConfig.create()
// changing the input to filterLoose will allow the message to pass
.input(FilteringConfig.create().filters(filterStrict))
.output(FilteringConfig.create().filters(filterStrict));

final var config =
new CompletionPostRequest()
CompletionPostRequest.create()
.orchestrationConfig(
new OrchestrationConfig()
OrchestrationConfig.create()
.moduleConfigurations(
new ModuleConfigs()
ModuleConfigs.create()
.llmModuleConfig(llmConfig)
.templatingModuleConfig(templatingConfig)
.filteringModuleConfig(filteringConfig)
.llmModuleConfig(llmConfig)))
.filteringModuleConfig(filteringConfig)))
.inputParams(inputParams);

final CompletionPostResponse result =
new OrchestrationCompletionApi(getOrchestrationClient("default"))
.orchestrationV1EndpointsCreate(config); // this fails with Bad Request because the strict filter prohibits the input message
// this fails with Bad Request because the strict filter prohibits the input message
.orchestrationV1EndpointsCreate(config);

final String message =
final String messageResult =
result.getOrchestrationResult().getChoices().get(0).getMessage().getContent();
```

Expand All @@ -380,8 +388,8 @@ See [an example in our Spring Boot application](e2e-test-app/src/main/java/com/s
Change your LLM module configuration to add model parameters:

```java
var llmModuleConfig =
new LLMModuleConfig()
var llmConfig =
LLMModuleConfig.create()
.modelName("gpt-35-turbo")
.modelParams(
Map.of(
Expand Down
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
<inputSpec>${project.basedir}/src/main/resources/spec/aicore.yaml</inputSpec>
<apiPackage>com.sap.ai.sdk.core.client</apiPackage>
<modelPackage>com.sap.ai.sdk.core.client.model</modelPackage>
<additionalProperties>
<pojoBuilderMethodName>create</pojoBuilderMethodName>
<pojoBuildMethodName/>
<pojoConstructorVisibility>protected</pojoConstructorVisibility>
</additionalProperties>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class AiApiError
@JsonAnySetter
@JsonAnyGetter
private final Map<String, Object> cloudSdkCustomFields = new LinkedHashMap<>();
protected AiApiError() { }

/**
* Set the code of this {@link AiApiError} instance and return the same instance.
Expand Down Expand Up @@ -285,6 +286,36 @@ private String toIndentedString(final java.lang.Object o) {
return o.toString().replace("\n", "\n ");
}

/**
* Create a type-safe, fluent-api builder object to construct a new {@link AiApiError} instance with all required arguments.
*/
public static Builder create() {
return (code) -> (message) -> new AiApiError().code(code).message(message);
}
/**
* Builder helper class.
*/
public interface Builder {
/**
* Set the code of this {@link AiApiError} instance.
*
* @param code Descriptive error code (not http status code)
* @return The AiApiError builder.
*/
Builder1 code( @Nonnull final String code);
}
/**
* Builder helper class.
*/
public interface Builder1 {
/**
* Set the message of this {@link AiApiError} instance.
*
* @param message Plaintext error description
* @return The AiApiError instance.
*/
AiApiError message( @Nonnull final String message);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class AiApiErrorWithId
@JsonAnySetter
@JsonAnyGetter
private final Map<String, Object> cloudSdkCustomFields = new LinkedHashMap<>();
protected AiApiErrorWithId() { }

/**
* Set the id of this {@link AiApiErrorWithId} instance and return the same instance.
Expand Down Expand Up @@ -187,6 +188,36 @@ private String toIndentedString(final java.lang.Object o) {
return o.toString().replace("\n", "\n ");
}

/**
* Create a type-safe, fluent-api builder object to construct a new {@link AiApiErrorWithId} instance with all required arguments.
*/
public static Builder create() {
return (id) -> (error) -> new AiApiErrorWithId().id(id).error(error);
}
/**
* Builder helper class.
*/
public interface Builder {
/**
* Set the id of this {@link AiApiErrorWithId} instance.
*
* @param id Generic ID
* @return The AiApiErrorWithId builder.
*/
Builder1 id( @Nonnull final String id);
}
/**
* Builder helper class.
*/
public interface Builder1 {
/**
* Set the error of this {@link AiApiErrorWithId} instance.
*
* @param error The error of this {@link AiApiErrorWithId}
* @return The AiApiErrorWithId instance.
*/
AiApiErrorWithId error( @Nonnull final AiApiError error);
}

}

Loading