diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 5119ce9..795a2b8 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -3,20 +3,20 @@ name: Maven Build on: push: branches: - - release/jdk-17 + - release/jdk-11 pull_request: branches: - - release/jdk-17 + - release/jdk-11 jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Set up JDK 17 + - name: Set up JDK 11 uses: actions/setup-java@v4 with: - java-version: '17' + java-version: '11' distribution: 'temurin' - name: JaCoCo Coverage run: mvn clean test jacoco:report -Pmvn-deploy diff --git a/README.md b/README.md index 5dc2a74..f054a8b 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@
-[![Maven Central](https://img.shields.io/maven-central/v/bg.codexio.ai/openai-api-sdk?versionSuffix=JDK17&color=EE5A9C)](https://central.sonatype.com/artifact/bg.codexio.ai/openai-api-sdk) +[![Maven Central](https://img.shields.io/maven-central/v/bg.codexio.ai/openai-api-sdk?versionSuffix=JDK11&color=EE5A9C)](https://central.sonatype.com/artifact/bg.codexio.ai/openai-api-sdk) [![Build](https://github.com/CodexioLtd/openai-api-sdk/actions/workflows/maven.yml/badge.svg)](https://github.com/CodexioLtd/openai-api-sdk/actions/workflows/maven.yml) [![Coverage](https://codecov.io/github/CodexioLtd/openai-api-sdk/graph/badge.svg?token=013OEUIYWI)](https://codecov.io/github/CodexioLtd/openai-api-sdk) [![License](https://img.shields.io/github/license/CodexioLtd/openai-api-sdk.svg)](https://github.com/CodexioLtd/openai-api-sdk/blob/master/LICENSE) @@ -21,7 +21,8 @@ ## Preambule -This library provides a Software Development Kit (SDK) for Java 17([see others](#supported-java-versions)) compliant runtimes +This library provides a Software Development Kit (SDK) for Java 11([see others](#supported-java-versions)) +compliant runtimes to connect to an OpenAI API and execute both synchronous and asynchronous (via callbacks or the Reactor Pattern) calls. @@ -117,10 +118,11 @@ In the next chapters you will see an explanation of all key concepts. 3. Add the library as a dependency in your project ```xml + bg.codexio.ai openai-api-sdk - 0.8.0.BETA-JDK17 + 0.8.0.BETA-JDK11 ``` @@ -131,14 +133,14 @@ In the next chapters you will see an explanation of all key concepts. Click here to see - - | Artifact | Description | - |-----------------------|---------------------------------------------------------------------------------------------------| - | `openai-api-models` | Contains info only for AI Model namings, such as GPT-4-Preview, DALL-E-2 and so on | - | `openai-api-payload` | Contains Request/Response DTO models and related information | - | `openai-api-http` | Contains HTTP Clients such as `ChatHttpExecutor`, `CreateImageHttpExecutor`, etc., ... | - | `openai-api-examples` | Usually you do not import this artfiactId, rather you can check the source code for some examples | - + + | Artifact | Description | + |-----------------------|---------------------------------------------------------------------------------------------------| + | `openai-api-models` | Contains info only for AI Model namings, such as GPT-4-Preview, DALL-E-2 and so on | + | `openai-api-payload` | Contains Request/Response DTO models and related information | + | `openai-api-http` | Contains HTTP Clients such as `ChatHttpExecutor`, `CreateImageHttpExecutor`, etc., ... | + | `openai-api-examples` | Usually you do not import this artfiactId, rather you can check the source code for some examples | + 4. Create in your `src/main/resources` folder a file named `openai-credentials.json` with the following content: @@ -264,7 +266,7 @@ Java, you make every byte worthwhile. ## Supported Java Versions -The `master` branch builds versions for JDK 21. This is the current Java version and the artifacts built from +The `master` branch builds versions for JDK 21. This is the current Java version and the artifacts built from `master` does not contain any JDK suffix. However, there are several other branches which build versions for different JDKs. See the table below: @@ -275,7 +277,6 @@ for different JDKs. See the table below: | [release/jdk-11](https://github.com/CodexioLtd/openai-api-sdk/tree/release/jdk-11) | 11 | X.Y.Z[.a]-JDK11 (e.g. 0.8.0.BETA-JDK11) | | [release/jdk-8](https://github.com/CodexioLtd/openai-api-sdk/tree/release/jdk-8) | 8 | X.Y.Z[.a]-JDK8 (e.g. 0.8.0.BETA-JDK8) | - ## Available SDKs ### List @@ -346,11 +347,15 @@ Credentials are passed to the SDK via instantiating the `openai-api-payload/ApiC object, which looks like: ```java -public record ApiCredentials( - String apiKey, - String organization, - String baseUrl -) {} +public final class ApiCredentials { + + public static final String BASE_URL = "https://api.openai.com/v1"; + private final String apiKey; + private final String organization; + private final String baseUrl; + + // constructor and getters here +} ``` Where `organization` and `baseUrl` are not mandatory, it will fall back to no specific @@ -552,7 +557,7 @@ public class Main { callbacks. Most of the APIs support two subscription promises - one for a stringified line of the response (not recommended, but gives a lot of control if necessary) and another for the whole response. Usually with `async().method().onEachLine(line -> { ... })` and - `async().method().thne(response -> { ... })`. + `async().method().thne(response -> { ... })`. - `reactive()` - mostly the same as `async()`, but utilizes the `Mono` and `Flux` patterns from Project Reactor. **This is only recommended if your codebase is already fully reactive, using something like `Reactor Netty`**. Otherwise, the overhead of @@ -924,13 +929,12 @@ public class GetNearbyPlaces ); } - public record GetNearbyPlacesFunctionChoice() + public static final class GetNearbyPlacesFunctionChoice implements FunctionChoice { - @Override - public String name() { - return GetNearbyPlaces.FUNCTION.function() - .getName(); + public GetNearbyPlacesFunctionChoice() { } + + // constructor and getters here } } ``` diff --git a/openai-api-coverage/pom.xml b/openai-api-coverage/pom.xml index 0099147..b6596b1 100644 --- a/openai-api-coverage/pom.xml +++ b/openai-api-coverage/pom.xml @@ -6,7 +6,7 @@ bg.codexio.ai openai-api - 0.8.0.BETA-JDK17 + 0.8.0.BETA-JDK11-SNAPSHOT openai-api-coverage @@ -15,7 +15,7 @@ Codexio Ltd. Open AI API SDK Test Coverage Aggregate Module - 0.8.0.BETA-JDK17 + 0.8.0.BETA-JDK11-SNAPSHOT false diff --git a/openai-api-examples/pom.xml b/openai-api-examples/pom.xml index b4b2644..715b39a 100644 --- a/openai-api-examples/pom.xml +++ b/openai-api-examples/pom.xml @@ -7,7 +7,7 @@ bg.codexio.ai openai-api - 0.8.0.BETA-JDK17 + 0.8.0.BETA-JDK11-SNAPSHOT openai-api-examples @@ -22,7 +22,7 @@ bg.codexio.ai openai-api-sdk - 0.8.0.BETA-JDK17 + 0.8.0.BETA-JDK11-SNAPSHOT diff --git a/openai-api-http/pom.xml b/openai-api-http/pom.xml index 256c780..9aa6007 100644 --- a/openai-api-http/pom.xml +++ b/openai-api-http/pom.xml @@ -7,7 +7,7 @@ bg.codexio.ai openai-api - 0.8.0.BETA-JDK17 + 0.8.0.BETA-JDK11-SNAPSHOT openai-api-http @@ -22,13 +22,13 @@ bg.codexio.ai openai-api-models - 0.8.0.BETA-JDK17 + 0.8.0.BETA-JDK11-SNAPSHOT bg.codexio.ai openai-api-payload - 0.8.0.BETA-JDK17 + 0.8.0.BETA-JDK11-SNAPSHOT diff --git a/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/HttpExecutorContext.java b/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/HttpExecutorContext.java index e3146c6..5f515ea 100644 --- a/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/HttpExecutorContext.java +++ b/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/HttpExecutorContext.java @@ -2,19 +2,28 @@ import bg.codexio.ai.openai.api.payload.credentials.ApiCredentials; +import java.util.Objects; import java.util.concurrent.TimeUnit; /** * Context object holding authentication * and timeout configuration - * - * @param credentials - * @param timeouts */ -public record HttpExecutorContext( - ApiCredentials credentials, - HttpTimeouts timeouts -) { +public final class HttpExecutorContext { + private final ApiCredentials credentials; + private final HttpTimeouts timeouts; + + /** + * @param credentials + * @param timeouts + */ + public HttpExecutorContext( + ApiCredentials credentials, + HttpTimeouts timeouts + ) { + this.credentials = credentials; + this.timeouts = timeouts; + } /** * Constructing the object with default timeouts. @@ -96,4 +105,45 @@ public HttpExecutorContext withReadTimeout( ) ); } + + public ApiCredentials credentials() { + return credentials; + } + + public HttpTimeouts timeouts() { + return timeouts; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (HttpExecutorContext) obj; + return Objects.equals( + this.credentials, + that.credentials + ) && Objects.equals( + this.timeouts, + that.timeouts + ); + } + + @Override + public int hashCode() { + return Objects.hash( + credentials, + timeouts + ); + } + + @Override + public String toString() { + return "HttpExecutorContext[" + "credentials=" + credentials + ", " + + "timeouts=" + timeouts + ']'; + } + } diff --git a/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/HttpTimeout.java b/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/HttpTimeout.java index 547288d..2242bcb 100644 --- a/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/HttpTimeout.java +++ b/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/HttpTimeout.java @@ -1,12 +1,58 @@ package bg.codexio.ai.openai.api.http; +import java.util.Objects; import java.util.concurrent.TimeUnit; /** * Representation of an HTTP Timeout. * Can be used for all call, connect and read. */ -public record HttpTimeout( - long period, - TimeUnit timeUnit -) {} +public final class HttpTimeout { + private final long period; + private final TimeUnit timeUnit; + + public HttpTimeout( + long period, + TimeUnit timeUnit + ) { + this.period = period; + this.timeUnit = timeUnit; + } + + public long period() { + return period; + } + + public TimeUnit timeUnit() { + return timeUnit; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (HttpTimeout) obj; + return this.period == that.period && Objects.equals( + this.timeUnit, + that.timeUnit + ); + } + + @Override + public int hashCode() { + return Objects.hash( + period, + timeUnit + ); + } + + @Override + public String toString() { + return "HttpTimeout[" + "period=" + period + ", " + "timeUnit=" + + timeUnit + ']'; + } +} diff --git a/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/HttpTimeouts.java b/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/HttpTimeouts.java index 4152742..e7aed58 100644 --- a/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/HttpTimeouts.java +++ b/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/HttpTimeouts.java @@ -1,10 +1,70 @@ package bg.codexio.ai.openai.api.http; +import java.util.Objects; + /** * Context object hinting the variety of timeouts */ -public record HttpTimeouts( - HttpTimeout call, - HttpTimeout connect, - HttpTimeout read -) {} +public final class HttpTimeouts { + private final HttpTimeout call; + private final HttpTimeout connect; + private final HttpTimeout read; + + public HttpTimeouts( + HttpTimeout call, + HttpTimeout connect, + HttpTimeout read + ) { + this.call = call; + this.connect = connect; + this.read = read; + } + + public HttpTimeout call() { + return call; + } + + public HttpTimeout connect() { + return connect; + } + + public HttpTimeout read() { + return read; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (HttpTimeouts) obj; + return Objects.equals( + this.call, + that.call + ) && Objects.equals( + this.connect, + that.connect + ) && Objects.equals( + this.read, + that.read + ); + } + + @Override + public int hashCode() { + return Objects.hash( + call, + connect, + read + ); + } + + @Override + public String toString() { + return "HttpTimeouts[" + "call=" + call + ", " + "connect=" + connect + + ", " + "read=" + read + ']'; + } +} diff --git a/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/OpenAIHttpExecutor.java b/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/OpenAIHttpExecutor.java index b201657..f46432d 100644 --- a/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/OpenAIHttpExecutor.java +++ b/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/OpenAIHttpExecutor.java @@ -8,14 +8,12 @@ import java.util.function.Consumer; /** - *

* Abstraction over HTTP execution to OpenAPI. * It takes a potentially {@link Streamable} request, * and sends HTTP request, which execution can be * immediately executed in a blocking manner, or * scheduled for execution in asynchronous * (reactive included) fashion. - *

* * @param The request (Input) model * @param The response (Output) model @@ -32,17 +30,13 @@ public interface OpenAIHttpExecutor * Executes HTTP request asynchronously. * Since response can be streamed, it can be * potentially beneficial for the developer, * to subscribe to each line, hence the callback * parameter. - *

- *

* It makes a little bit more sense to subscribe, * to the whole response, using the finalizer parameter. - *

* * @param request The request (Input) model * @param callBack A callback of type stringLine -> consume(stringLine) @@ -55,12 +49,10 @@ void executeAsync( ); /** - *

* Executes HTTP request in reactive fashion. * We strongly recommend to use this only if * a real reactive runtime is present, such as * Reactor netty. - *

* * @param request The request (Input) model * @return {@link ReactiveExecution} object holding a single observable @@ -71,12 +63,10 @@ void executeAsync( ReactiveExecution executeReactive(I request); /** - *

* Denotes whether the response can be streamed, * mostly taking into account the value of {@link Streamable#stream()} * which usually is implemented like inputModel -> inputModel.stream() && * specificApiLogic. - *

* * @param input The request (Input) model * @return boolean saying if you can stream the response or not. @@ -87,12 +77,26 @@ void executeAsync( /** * Data holder for the response of {@link #executeReactive(Streamable)} * - * @param lines Observable to all string lines of the response - * @param response Observable to single emit of the whole response object - * @param The response (Output) model + * @param The response (Output) model */ - record ReactiveExecution( - Flux lines, - Mono response - ) {} + class ReactiveExecution { + private final Flux lines; + private final Mono response; + + public ReactiveExecution( + Flux lines, + Mono response + ) { + this.lines = lines; + this.response = response; + } + + public Flux lines() { + return lines; + } + + public Mono response() { + return response; + } + } } diff --git a/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/exception/OpenAIRespondedNot2xxException.java b/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/exception/OpenAIRespondedNot2xxException.java index f11caca..6abbb8a 100644 --- a/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/exception/OpenAIRespondedNot2xxException.java +++ b/openai-api-http/src/main/java/bg/codexio/ai/openai/api/http/exception/OpenAIRespondedNot2xxException.java @@ -15,7 +15,8 @@ public OpenAIRespondedNot2xxException( super(String.format( "HTTP status: %d. Response: %s", httpStatusCode, - errorHolder + errorHolder.error() + .message() )); this.errorHolder = errorHolder; diff --git a/openai-api-http/src/test/java/bg/codexio/ai/openai/api/http/DefaultOpenAIHttpExecutorTest.java b/openai-api-http/src/test/java/bg/codexio/ai/openai/api/http/DefaultOpenAIHttpExecutorTest.java index 92b0431..b12e563 100644 --- a/openai-api-http/src/test/java/bg/codexio/ai/openai/api/http/DefaultOpenAIHttpExecutorTest.java +++ b/openai-api-http/src/test/java/bg/codexio/ai/openai/api/http/DefaultOpenAIHttpExecutorTest.java @@ -5,6 +5,7 @@ import bg.codexio.ai.openai.api.http.exception.UnparseableResponseException; import bg.codexio.ai.openai.api.payload.Mergeable; import bg.codexio.ai.openai.api.payload.Streamable; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; import okhttp3.OkHttpClient; import okhttp3.Response; @@ -21,6 +22,7 @@ import java.io.File; import java.io.IOException; +import java.util.Objects; import java.util.UUID; import java.util.function.Supplier; @@ -593,37 +595,26 @@ public void testToFormData_nestedObjects_shouldPopulateAll() ); assertEquals( - """ - --test-boundary - Content-Disposition: form-data; name="id" - - 50 - --test-boundary - Content-Disposition: form-data; name="img"; filename="fake-image.png" - Content-Type: application/octet-stream - - test - --test-boundary - Content-Disposition: form-data; name="name" - - test-name - --test-boundary - Content-Disposition: form-data; name="stream" - - false - --test-boundary - Content-Disposition: form-data; name="id" - - 50 - --test-boundary - Content-Disposition: form-data; name="img"; filename="fake-image.png" - Content-Type: application/octet-stream - - test - --test-boundary-- - """, + "--test-boundary\n" + + "Content-Disposition: form-data; name=\"id\"\n" + "\n" + + "50\n" + "--test-boundary\n" + + "Content-Disposition: form-data; name=\"img\"; " + + "filename=\"fake-image.png\"\n" + + "Content-Type: application/octet-stream\n" + "\n" + + "test\n" + "--test-boundary\n" + + "Content-Disposition: form-data; name=\"name\"\n" + + "\n" + "test-name\n" + "--test-boundary\n" + + "Content-Disposition: form-data; name=\"stream\"\n" + + "\n" + "false\n" + "--test-boundary\n" + + "Content-Disposition: form-data; name=\"id\"\n" + "\n" + + "50\n" + "--test-boundary\n" + + "Content-Disposition: form-data; name=\"img\"; " + + "filename=\"fake-image.png\"\n" + + "Content-Type: application/octet-stream\n" + "\n" + + "test\n" + "--test-boundary--\n", result ); + } private void initExecutor(boolean streamable) { @@ -639,11 +630,32 @@ private void initExecutor(boolean streamable) { } - record MockRequest( - String name, - boolean stream - ) - implements Streamable {} + static class MockRequest + implements Streamable { + private final String name; + private final boolean stream; + + public MockRequest( + String name, + boolean stream + ) { + this.name = name; + this.stream = stream; + } + + public String getName() { + return name; + } + + public boolean isStream() { + return stream; + } + + @Override + public boolean stream() { + return stream; + } + } static class MockFormData implements Streamable { @@ -654,7 +666,7 @@ static class MockFormData private final MockFormData self; private final MockFormData nullSelf; - MockFormData( + public MockFormData( int id, File img, MockRequest nested @@ -697,12 +709,35 @@ public boolean stream() { } } - - record MockResponse( - int id, - String name - ) + static class MockResponse implements Mergeable { + private final int id; + private final String name; + + public MockResponse() { + this( + 0, + null + ); + } + + public MockResponse( + int id, + String name + ) { + this.id = id; + this.name = name; + } + + @JsonProperty + public int id() { + return id; + } + + @JsonProperty + public String name() { + return name; + } @Override public MockResponse merge(MockResponse other) { @@ -714,6 +749,26 @@ public MockResponse merge(MockResponse other) { this.name + other.name ); } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + MockResponse that = (MockResponse) obj; + return id == that.id && name.equals(that.name); + } + + @Override + public int hashCode() { + return Objects.hash( + this.id, + this.name + ); + } } diff --git a/openai-api-http/src/test/java/bg/codexio/ai/openai/api/http/ExecutorTests.java b/openai-api-http/src/test/java/bg/codexio/ai/openai/api/http/ExecutorTests.java index 9ec5d1a..f537fa3 100644 --- a/openai-api-http/src/test/java/bg/codexio/ai/openai/api/http/ExecutorTests.java +++ b/openai-api-http/src/test/java/bg/codexio/ai/openai/api/http/ExecutorTests.java @@ -13,6 +13,7 @@ import org.mockito.stubbing.Answer; import java.io.IOException; +import java.util.Objects; import java.util.function.Consumer; import static org.junit.jupiter.api.Assertions.*; @@ -566,11 +567,18 @@ public static void setMultipartBoundary( executor.setMultipartBoundary(boundary); } - public record MockCall( - Response execute, - boolean shouldFail - ) + public static final class MockCall implements Call { + private final Response execute; + private final boolean shouldFail; + + public MockCall( + Response execute, + boolean shouldFail + ) { + this.execute = execute; + this.shouldFail = shouldFail; + } @Override @@ -625,6 +633,45 @@ public Timeout timeout() { public Request request() { return null; } + + @Override + public Response execute() { + return execute; + } + + public boolean shouldFail() { + return shouldFail; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (MockCall) obj; + return Objects.equals( + this.execute, + that.execute + ) && this.shouldFail == that.shouldFail; + } + + @Override + public int hashCode() { + return Objects.hash( + execute, + shouldFail + ); + } + + @Override + public String toString() { + return "MockCall[" + "execute=" + execute + ", " + "shouldFail=" + + shouldFail + ']'; + } + } } diff --git a/openai-api-models/pom.xml b/openai-api-models/pom.xml index b6b98fb..9d2266f 100644 --- a/openai-api-models/pom.xml +++ b/openai-api-models/pom.xml @@ -7,7 +7,7 @@ bg.codexio.ai openai-api - 0.8.0.BETA-JDK17 + 0.8.0.BETA-JDK11-SNAPSHOT openai-api-models diff --git a/openai-api-payload/pom.xml b/openai-api-payload/pom.xml index c8f6062..7cf8508 100644 --- a/openai-api-payload/pom.xml +++ b/openai-api-payload/pom.xml @@ -7,7 +7,7 @@ bg.codexio.ai openai-api - 0.8.0.BETA-JDK17 + 0.8.0.BETA-JDK11-SNAPSHOT openai-api-payload @@ -17,4 +17,12 @@ false + + + com.fasterxml.jackson.core + jackson-databind + 2.16.0 + + + diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/ChatMessage.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/ChatMessage.java index 0173203..f51e606 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/ChatMessage.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/ChatMessage.java @@ -1,21 +1,86 @@ package bg.codexio.ai.openai.api.payload.chat; import bg.codexio.ai.openai.api.payload.chat.response.ToolCallResponse; +import com.fasterxml.jackson.annotation.JsonProperty; import java.util.List; import java.util.Objects; -public record ChatMessage( - String role, - String content, - List toolCalls -) { +public final class ChatMessage { + private final String role; + private final String content; + private final List toolCalls; - @Override + public ChatMessage() { + this( + null, + null, + null + ); + } + + public ChatMessage( + String role, + String content, + List toolCalls + ) { + this.role = role; + this.content = content; + this.toolCalls = toolCalls; + } + + @JsonProperty public String role() { return Objects.requireNonNullElse( this.role, "user" ); } + + @JsonProperty + public String content() { + return content; + } + + @JsonProperty + public List toolCalls() { + return toolCalls; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (ChatMessage) obj; + return Objects.equals( + this.role, + that.role + ) && Objects.equals( + this.content, + that.content + ) && Objects.equals( + this.toolCalls, + that.toolCalls + ); + } + + @Override + public int hashCode() { + return Objects.hash( + role, + content, + toolCalls + ); + } + + @Override + public String toString() { + return "ChatMessage[" + "role=" + role + ", " + "content=" + content + + ", " + "toolCalls=" + toolCalls + ']'; + } + } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/functions/GetCurrentWeather.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/functions/GetCurrentWeather.java index 0ee595a..62e470e 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/functions/GetCurrentWeather.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/functions/GetCurrentWeather.java @@ -29,8 +29,7 @@ private GetCurrentWeather() { "string", "description", "The city and state, e.g. San " - + "Francisco," - + " CA" + + "Francisco," + " CA" ), "unit", Map.of( @@ -47,13 +46,33 @@ private GetCurrentWeather() { ); } - public record GetCurrentWeatherFunctionChoice() + public static final class GetCurrentWeatherFunctionChoice implements FunctionChoice { + public GetCurrentWeatherFunctionChoice() { + } + @Override public String name() { return GetCurrentWeather.FUNCTION.function() .getName(); } + + @Override + public boolean equals(Object obj) { + return obj == this + || obj != null && obj.getClass() == this.getClass(); + } + + @Override + public int hashCode() { + return 1; + } + + @Override + public String toString() { + return "GetCurrentWeatherFunctionChoice[]"; + } + } } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/functions/GetNearbyPlaces.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/functions/GetNearbyPlaces.java index 93572e5..4c50143 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/functions/GetNearbyPlaces.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/functions/GetNearbyPlaces.java @@ -30,8 +30,7 @@ private GetNearbyPlaces() { "string", "description", "Comma separated geo coordinates such" - + " as" - + " 45,6789,12.3456" + + " as" + " 45,6789,12.3456" ), "radius", Map.of( @@ -55,13 +54,33 @@ private GetNearbyPlaces() { ); } - public record GetNearbyPlacesFunctionChoice() + public static final class GetNearbyPlacesFunctionChoice implements FunctionChoice { + public GetNearbyPlacesFunctionChoice() { + } + @Override public String name() { return GetNearbyPlaces.FUNCTION.function() .getName(); } + + @Override + public boolean equals(Object obj) { + return obj == this + || obj != null && obj.getClass() == this.getClass(); + } + + @Override + public int hashCode() { + return 1; + } + + @Override + public String toString() { + return "GetNearbyPlacesFunctionChoice[]"; + } + } } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/request/ChatMessageRequest.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/request/ChatMessageRequest.java index b6e8756..0431ac0 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/request/ChatMessageRequest.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/request/ChatMessageRequest.java @@ -2,6 +2,7 @@ import bg.codexio.ai.openai.api.payload.Streamable; import bg.codexio.ai.openai.api.payload.chat.ChatMessage; +import com.fasterxml.jackson.annotation.JsonProperty; import java.util.ArrayList; import java.util.List; @@ -12,46 +13,45 @@ * Represents a * Create chat completion#Request body */ -public record ChatMessageRequest( - String model, - List messages, - Double frequencyPenalty, - Map logitBias, - Integer maxTokens, - Integer n, - Double presencePenalty, - Integer seed, - String[] stop, - boolean stream, - Double temperature, - Double topP, - List tools, - ChatTool.ChatToolChoice toolChoice, - String user -) +public final class ChatMessageRequest implements Streamable { + private final String model; + private final List messages; + private final Double frequencyPenalty; + private final Map logitBias; + private final Integer maxTokens; + private final Integer n; + private final Double presencePenalty; + private final Integer seed; + private final String[] stop; + private final boolean stream; + private final Double temperature; + private final Double topP; + private final List tools; + private final ChatTool.ChatToolChoice toolChoice; + private final String user; - public static Builder builder() { - return new Builder( - "", - new ArrayList<>(), - null, - null, - null, + public ChatMessageRequest() { + this( null, + new ArrayList<>(), + 0.0, null, + 150, + 1, + 0.0, null, null, false, - null, - null, - null, + 1.0, + 1.0, + new ArrayList<>(), null, null ); } - public record Builder( + public ChatMessageRequest( String model, List messages, Double frequencyPenalty, @@ -68,6 +68,169 @@ public record Builder( ChatTool.ChatToolChoice toolChoice, String user ) { + this.model = model; + this.messages = messages; + this.frequencyPenalty = frequencyPenalty; + this.logitBias = logitBias; + this.maxTokens = maxTokens; + this.n = n; + this.presencePenalty = presencePenalty; + this.seed = seed; + this.stop = stop; + this.stream = stream; + this.temperature = temperature; + this.topP = topP; + this.tools = tools; + this.toolChoice = toolChoice; + this.user = user; + } + + public static Builder builder() { + return new Builder( + "", + new ArrayList<>(), + null, + null, + null, + null, + null, + null, + null, + false, + null, + null, + null, + null, + null + ); + } + + @JsonProperty + public String model() { + return model; + } + + @JsonProperty + public List messages() { + return messages; + } + + @JsonProperty + public Double frequencyPenalty() { + return frequencyPenalty; + } + + @JsonProperty + public Map logitBias() { + return logitBias; + } + + @JsonProperty + public Integer maxTokens() { + return maxTokens; + } + + @JsonProperty + public Integer n() { + return n; + } + + @JsonProperty + public Double presencePenalty() { + return presencePenalty; + } + + @JsonProperty + public Integer seed() { + return seed; + } + + @JsonProperty + public String[] stop() { + return stop; + } + + @JsonProperty + public Double temperature() { + return temperature; + } + + @JsonProperty + public Double topP() { + return topP; + } + + @JsonProperty + public List tools() { + return tools; + } + + @JsonProperty + public ChatTool.ChatToolChoice toolChoice() { + return toolChoice; + } + + @JsonProperty + public String user() { + return user; + } + + @Override + @JsonProperty + public boolean stream() { + return stream; + } + + public static final class Builder { + private final String model; + private final List messages; + private final Double frequencyPenalty; + private final Map logitBias; + private final Integer maxTokens; + private final Integer n; + private final Double presencePenalty; + private final Integer seed; + private final String[] stop; + private final boolean stream; + private final Double temperature; + private final Double topP; + private final List tools; + private final ChatTool.ChatToolChoice toolChoice; + private final String user; + + public Builder( + String model, + List messages, + Double frequencyPenalty, + Map logitBias, + Integer maxTokens, + Integer n, + Double presencePenalty, + Integer seed, + String[] stop, + boolean stream, + Double temperature, + Double topP, + List tools, + ChatTool.ChatToolChoice toolChoice, + String user + ) { + this.model = model; + this.messages = messages; + this.frequencyPenalty = frequencyPenalty; + this.logitBias = logitBias; + this.maxTokens = maxTokens; + this.n = n; + this.presencePenalty = presencePenalty; + this.seed = seed; + this.stop = stop; + this.stream = stream; + this.temperature = temperature; + this.topP = topP; + this.tools = tools; + this.toolChoice = toolChoice; + this.user = user; + } public Builder withModel(String model) { return new Builder( @@ -279,7 +442,7 @@ public Builder shouldStream(boolean stream) { ); } - public Builder withTermperature(Double temperature) { + public Builder withTemperature(Double temperature) { return new Builder( model, messages, @@ -409,5 +572,154 @@ public ChatMessageRequest build() { user ); } + + public String model() { + return model; + } + + public List messages() { + return messages; + } + + public Double frequencyPenalty() { + return frequencyPenalty; + } + + public Map logitBias() { + return logitBias; + } + + public Integer maxTokens() { + return maxTokens; + } + + public Integer n() { + return n; + } + + public Double presencePenalty() { + return presencePenalty; + } + + public Integer seed() { + return seed; + } + + public String[] stop() { + return stop; + } + + public boolean stream() { + return stream; + } + + public Double temperature() { + return temperature; + } + + public Double topP() { + return topP; + } + + public List tools() { + return tools; + } + + public ChatTool.ChatToolChoice toolChoice() { + return toolChoice; + } + + public String user() { + return user; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (Builder) obj; + return Objects.equals( + this.model, + that.model + ) && Objects.equals( + this.messages, + that.messages + ) && Objects.equals( + this.frequencyPenalty, + that.frequencyPenalty + ) && Objects.equals( + this.logitBias, + that.logitBias + ) && Objects.equals( + this.maxTokens, + that.maxTokens + ) && Objects.equals( + this.n, + that.n + ) && Objects.equals( + this.presencePenalty, + that.presencePenalty + ) && Objects.equals( + this.seed, + that.seed + ) && Objects.equals( + this.stop, + that.stop + ) && this.stream == that.stream && Objects.equals( + this.temperature, + that.temperature + ) && Objects.equals( + this.topP, + that.topP + ) && Objects.equals( + this.tools, + that.tools + ) && Objects.equals( + this.toolChoice, + that.toolChoice + ) && Objects.equals( + this.user, + that.user + ); + } + + @Override + public int hashCode() { + return Objects.hash( + model, + messages, + frequencyPenalty, + logitBias, + maxTokens, + n, + presencePenalty, + seed, + stop, + stream, + temperature, + topP, + tools, + toolChoice, + user + ); + } + + @Override + public String toString() { + return "Builder[" + "model=" + model + ", " + "messages=" + messages + + ", " + "frequencyPenalty=" + frequencyPenalty + ", " + + "logitBias=" + logitBias + ", " + "maxTokens=" + maxTokens + + ", " + "n=" + n + ", " + "presencePenalty=" + + presencePenalty + ", " + "seed=" + seed + ", " + "stop=" + + stop + ", " + "stream=" + stream + ", " + "temperature=" + + temperature + ", " + "topP=" + topP + ", " + "tools=" + + tools + ", " + "toolChoice=" + toolChoice + ", " + "user=" + + user + ']'; + } + } } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/request/FunctionTool.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/request/FunctionTool.java index a5734d3..ccc94be 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/request/FunctionTool.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/request/FunctionTool.java @@ -2,10 +2,21 @@ import bg.codexio.ai.openai.api.payload.chat.ChatFunction; -public record FunctionTool( - ChatFunction function -) +import java.util.Objects; + +public final class FunctionTool implements ChatTool { + private final ChatFunction function; + + public FunctionTool() { + this(null); + } + + public FunctionTool( + ChatFunction function + ) { + this.function = function; + } @Override public String getType() { @@ -17,8 +28,73 @@ public ChatToolChoice asChoice() { return null; } - public record FunctionToolChoice(FunctionChoice function) + public ChatFunction function() { + return function; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (FunctionTool) obj; + return Objects.equals( + this.function, + that.function + ); + } + + @Override + public int hashCode() { + return Objects.hash(function); + } + + @Override + public String toString() { + return "FunctionTool[" + "function=" + function + ']'; + } + + + public static final class FunctionToolChoice implements ChatToolChoice { + private final FunctionChoice function; + + public FunctionToolChoice(FunctionChoice function) { + this.function = function; + } + + public FunctionChoice function() { + return function; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (FunctionToolChoice) obj; + return Objects.equals( + this.function, + that.function + ); + } + + @Override + public int hashCode() { + return Objects.hash(function); + } + + @Override + public String toString() { + return "FunctionToolChoice[" + "function=" + function + ']'; + } + @Override public String type() { return "function"; diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/response/ChatChoiceResponse.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/response/ChatChoiceResponse.java index 69ebae8..88078c7 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/response/ChatChoiceResponse.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/response/ChatChoiceResponse.java @@ -2,16 +2,38 @@ import bg.codexio.ai.openai.api.payload.Mergeable; import bg.codexio.ai.openai.api.payload.chat.ChatMessage; +import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Objects; -public record ChatChoiceResponse( - ChatMessage message, - ChatMessage delta, - String finishReason, - int index -) +public final class ChatChoiceResponse implements Mergeable { + private final ChatMessage message; + private final ChatMessage delta; + private final String finishReason; + private final int index; + + public ChatChoiceResponse( + ) { + this( + null, + null, + null, + 1 + ); + } + + public ChatChoiceResponse( + ChatMessage message, + ChatMessage delta, + String finishReason, + int index + ) { + this.message = message; + this.delta = delta; + this.finishReason = finishReason; + this.index = index; + } public static ChatChoiceResponse empty() { return new ChatChoiceResponse( @@ -73,4 +95,63 @@ public ChatChoiceResponse merge(ChatChoiceResponse other) { ) ); } + + @JsonProperty + public ChatMessage message() { + return message; + } + + @JsonProperty + public ChatMessage delta() { + return delta; + } + + @JsonProperty + public String finishReason() { + return finishReason; + } + + @JsonProperty + public int index() { + return index; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (ChatChoiceResponse) obj; + return Objects.equals( + this.message, + that.message + ) && Objects.equals( + this.delta, + that.delta + ) && Objects.equals( + this.finishReason, + that.finishReason + ) && this.index == that.index; + } + + @Override + public int hashCode() { + return Objects.hash( + message, + delta, + finishReason, + index + ); + } + + @Override + public String toString() { + return "ChatChoiceResponse[" + "message=" + message + ", " + "delta=" + + delta + ", " + "finishReason=" + finishReason + ", " + + "index=" + index + ']'; + } + } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/response/ChatMessageResponse.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/response/ChatMessageResponse.java index 36c6bf8..12f48cf 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/response/ChatMessageResponse.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/chat/response/ChatMessageResponse.java @@ -1,6 +1,7 @@ package bg.codexio.ai.openai.api.payload.chat.response; import bg.codexio.ai.openai.api.payload.Mergeable; +import com.fasterxml.jackson.annotation.JsonProperty; import java.util.List; import java.util.Objects; @@ -9,15 +10,41 @@ * Represents a * OpenAI user settings page
- * @param organization optional organization, default organization can be - * configured in - * OpenAI user settings page
- * @param baseUrl optional base url for the API calls, default one is - * https://api.openai.com/v1 */ -public record ApiCredentials( - String apiKey, - String organization, - String baseUrl -) { +public final class ApiCredentials { public static final String BASE_URL = "https://api.openai.com/v1"; + private final String apiKey; + private final String organization; + private final String baseUrl; + + /** + * @param apiKey generated in + * OpenAI user settings page
+ * @param organization optional organization, default organization can be + * configured in + * OpenAI user settings page
+ * @param baseUrl optional base url for the API calls, default one is + * https://api.openai.com/v1 + */ + public ApiCredentials( + String apiKey, + String organization, + String baseUrl + ) { + this.apiKey = apiKey; + this.organization = organization; + this.baseUrl = baseUrl; + } /** * Generates credential object with supplied apiKey, @@ -39,11 +50,55 @@ public ApiCredentials(String apiKey) { ); } - @Override public String baseUrl() { return Optional.ofNullable(this.baseUrl) .map(String::trim) .filter(Predicate.not(String::isBlank)) .orElse(BASE_URL); } + + public String apiKey() { + return apiKey; + } + + public String organization() { + return organization; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (ApiCredentials) obj; + return Objects.equals( + this.apiKey, + that.apiKey + ) && Objects.equals( + this.organization, + that.organization + ) && Objects.equals( + this.baseUrl, + that.baseUrl + ); + } + + @Override + public int hashCode() { + return Objects.hash( + apiKey, + organization, + baseUrl + ); + } + + @Override + public String toString() { + return "ApiCredentials[" + "apiKey=" + apiKey + ", " + "organization=" + + organization + ", " + "baseUrl=" + baseUrl + ']'; + } + } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/error/ErrorResponse.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/error/ErrorResponse.java index 7c980bc..618408c 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/error/ErrorResponse.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/error/ErrorResponse.java @@ -1,8 +1,94 @@ package bg.codexio.ai.openai.api.payload.error; -public record ErrorResponse( - String message, - String type, - String param, - String code -) {} +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Objects; + +public final class ErrorResponse { + private final String message; + private final String type; + private final String param; + private final String code; + + public ErrorResponse( + ) { + this( + null, + null, + null, + null + ); + } + + public ErrorResponse( + String message, + String type, + String param, + String code + ) { + this.message = message; + this.type = type; + this.param = param; + this.code = code; + } + + @JsonProperty + public String message() { + return message; + } + + @JsonProperty + public String type() { + return type; + } + + @JsonProperty + public String param() { + return param; + } + + @JsonProperty + public String code() { + return code; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (ErrorResponse) obj; + return Objects.equals( + this.message, + that.message + ) && Objects.equals( + this.type, + that.type + ) && Objects.equals( + this.param, + that.param + ) && Objects.equals( + this.code, + that.code + ); + } + + @Override + public int hashCode() { + return Objects.hash( + message, + type, + param, + code + ); + } + + @Override + public String toString() { + return "ErrorResponse[" + "message=" + message + ", " + "type=" + type + + ", " + "param=" + param + ", " + "code=" + code + ']'; + } +} diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/error/ErrorResponseHolder.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/error/ErrorResponseHolder.java index 75666c2..3d86fa2 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/error/ErrorResponseHolder.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/error/ErrorResponseHolder.java @@ -1,5 +1,50 @@ package bg.codexio.ai.openai.api.payload.error; -public record ErrorResponseHolder( - ErrorResponse error -) {} +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Objects; + +public final class ErrorResponseHolder { + private final ErrorResponse error; + + public ErrorResponseHolder( + ) { + this(null); + } + + public ErrorResponseHolder( + ErrorResponse error + ) { + this.error = error; + } + + @JsonProperty + public ErrorResponse error() { + return error; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (ErrorResponseHolder) obj; + return Objects.equals( + this.error, + that.error + ); + } + + @Override + public int hashCode() { + return Objects.hash(error); + } + + @Override + public String toString() { + return "ErrorResponseHolder[" + "error=" + error + ']'; + } +} diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/CreateImageRequest.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/CreateImageRequest.java index a48f1f8..232efbb 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/CreateImageRequest.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/CreateImageRequest.java @@ -1,21 +1,149 @@ package bg.codexio.ai.openai.api.payload.images.request; import bg.codexio.ai.openai.api.payload.Streamable; +import com.fasterxml.jackson.annotation.JsonProperty; -public record CreateImageRequest( - String prompt, - String model, - Integer n, - String quality, - String responseFormat, - String size, - String style, - String user -) +import java.util.Objects; + +public final class CreateImageRequest implements Streamable, ImageRequest { + private final String prompt; + private final String model; + private final Integer n; + private final String quality; + private final String responseFormat; + private final String size; + private final String style; + private final String user; + + public CreateImageRequest( + String prompt, + String model, + Integer n, + String quality, + String responseFormat, + String size, + String style, + String user + ) { + this.prompt = prompt; + this.model = model; + this.n = n; + this.quality = quality; + this.responseFormat = responseFormat; + this.size = size; + this.style = style; + this.user = user; + } + @Override public boolean stream() { return false; } + + @Override + @JsonProperty + public String prompt() { + return prompt; + } + + @Override + @JsonProperty + public String model() { + return model; + } + + @Override + @JsonProperty + public Integer n() { + return n; + } + + @JsonProperty + public String quality() { + return quality; + } + + @Override + @JsonProperty + public String responseFormat() { + return responseFormat; + } + + @Override + @JsonProperty + public String size() { + return size; + } + + @JsonProperty + public String style() { + return style; + } + + @Override + @JsonProperty + public String user() { + return user; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (CreateImageRequest) obj; + return Objects.equals( + this.prompt, + that.prompt + ) && Objects.equals( + this.model, + that.model + ) && Objects.equals( + this.n, + that.n + ) && Objects.equals( + this.quality, + that.quality + ) && Objects.equals( + this.responseFormat, + that.responseFormat + ) && Objects.equals( + this.size, + that.size + ) && Objects.equals( + this.style, + that.style + ) && Objects.equals( + this.user, + that.user + ); + } + + @Override + public int hashCode() { + return Objects.hash( + prompt, + model, + n, + quality, + responseFormat, + size, + style, + user + ); + } + + @Override + public String toString() { + return "CreateImageRequest[" + "prompt=" + prompt + ", " + "model=" + + model + ", " + "n=" + n + ", " + "quality=" + quality + ", " + + "responseFormat=" + responseFormat + ", " + "size=" + size + + ", " + "style=" + style + ", " + "user=" + user + ']'; + } + } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/EditImageRequest.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/EditImageRequest.java index 80b56fd..de996d5 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/EditImageRequest.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/EditImageRequest.java @@ -1,20 +1,149 @@ package bg.codexio.ai.openai.api.payload.images.request; +import com.fasterxml.jackson.annotation.JsonProperty; + import java.io.File; +import java.util.Objects; -public record EditImageRequest( - File image, - String prompt, - File mask, - String model, - Integer n, - String size, - String responseFormat, - String user -) +public final class EditImageRequest implements ImageRequest { + private final File image; + private final String prompt; + private final File mask; + private final String model; + private final Integer n; + private final String size; + private final String responseFormat; + private final String user; + + public EditImageRequest( + File image, + String prompt, + File mask, + String model, + Integer n, + String size, + String responseFormat, + String user + ) { + this.image = image; + this.prompt = prompt; + this.mask = mask; + this.model = model; + this.n = n; + this.size = size; + this.responseFormat = responseFormat; + this.user = user; + } + @Override public boolean stream() { return false; } + + @JsonProperty + public File image() { + return image; + } + + @Override + @JsonProperty + public String prompt() { + return prompt; + } + + @JsonProperty + public File mask() { + return mask; + } + + @Override + @JsonProperty + public String model() { + return model; + } + + @Override + @JsonProperty + public Integer n() { + return n; + } + + @Override + @JsonProperty + public String size() { + return size; + } + + @Override + @JsonProperty + public String responseFormat() { + return responseFormat; + } + + @Override + @JsonProperty + public String user() { + return user; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (EditImageRequest) obj; + return Objects.equals( + this.image, + that.image + ) && Objects.equals( + this.prompt, + that.prompt + ) && Objects.equals( + this.mask, + that.mask + ) && Objects.equals( + this.model, + that.model + ) && Objects.equals( + this.n, + that.n + ) && Objects.equals( + this.size, + that.size + ) && Objects.equals( + this.responseFormat, + that.responseFormat + ) && Objects.equals( + this.user, + that.user + ); + } + + @Override + public int hashCode() { + return Objects.hash( + image, + prompt, + mask, + model, + n, + size, + responseFormat, + user + ); + } + + @Override + public String toString() { + return "EditImageRequest[" + "image=" + image + ", " + "prompt=" + + prompt + ", " + "mask=" + mask + ", " + "model=" + model + + ", " + "n=" + n + ", " + "size=" + size + ", " + + "responseFormat=" + responseFormat + ", " + "user=" + user + + ']'; + } + } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/ImageRequestBuilder.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/ImageRequestBuilder.java index 6193239..6131503 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/ImageRequestBuilder.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/ImageRequestBuilder.java @@ -1,21 +1,48 @@ package bg.codexio.ai.openai.api.payload.images.request; import java.io.File; +import java.util.Objects; import java.util.function.Function; -public record ImageRequestBuilder( - Function, R> specificRequestCreator, - String prompt, - String model, - Integer n, - String quality, - String responseFormat, - String size, - String style, - String user, - File image, - File mask -) { +public final class ImageRequestBuilder { + private final Function, R> specificRequestCreator; + private final String prompt; + private final String model; + private final Integer n; + private final String quality; + private final String responseFormat; + private final String size; + private final String style; + private final String user; + private final File image; + private final File mask; + + public ImageRequestBuilder( + Function, R> specificRequestCreator, + String prompt, + String model, + Integer n, + String quality, + String responseFormat, + String size, + String style, + String user, + File image, + File mask + ) { + this.specificRequestCreator = specificRequestCreator; + this.prompt = prompt; + this.model = model; + this.n = n; + this.quality = quality; + this.responseFormat = responseFormat; + this.size = size; + this.style = style; + this.user = user; + this.image = image; + this.mask = mask; + } + public static ImageRequestBuilder builder() { return new ImageRequestBuilder<>( null, @@ -223,4 +250,121 @@ public ImageRequestBuilder build() { mask ); } + + public Function, R> specificRequestCreator() { + return specificRequestCreator; + } + + public String prompt() { + return prompt; + } + + public String model() { + return model; + } + + public Integer n() { + return n; + } + + public String quality() { + return quality; + } + + public String responseFormat() { + return responseFormat; + } + + public String size() { + return size; + } + + public String style() { + return style; + } + + public String user() { + return user; + } + + public File image() { + return image; + } + + public File mask() { + return mask; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (ImageRequestBuilder) obj; + return Objects.equals( + this.specificRequestCreator, + that.specificRequestCreator + ) && Objects.equals( + this.prompt, + that.prompt + ) && Objects.equals( + this.model, + that.model + ) && Objects.equals( + this.n, + that.n + ) && Objects.equals( + this.quality, + that.quality + ) && Objects.equals( + this.responseFormat, + that.responseFormat + ) && Objects.equals( + this.size, + that.size + ) && Objects.equals( + this.style, + that.style + ) && Objects.equals( + this.user, + that.user + ) && Objects.equals( + this.image, + that.image + ) && Objects.equals( + this.mask, + that.mask + ); + } + + @Override + public int hashCode() { + return Objects.hash( + specificRequestCreator, + prompt, + model, + n, + quality, + responseFormat, + size, + style, + user, + image, + mask + ); + } + + @Override + public String toString() { + return "ImageRequestBuilder[" + "specificRequestCreator=" + + specificRequestCreator + ", " + "prompt=" + prompt + ", " + + "model=" + model + ", " + "n=" + n + ", " + "quality=" + + quality + ", " + "responseFormat=" + responseFormat + ", " + + "size=" + size + ", " + "style=" + style + ", " + "user=" + + user + ", " + "image=" + image + ", " + "mask=" + mask + ']'; + } + } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/ImageVariationRequest.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/ImageVariationRequest.java index 71002d5..380adc7 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/ImageVariationRequest.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/request/ImageVariationRequest.java @@ -1,22 +1,138 @@ package bg.codexio.ai.openai.api.payload.images.request; import bg.codexio.ai.openai.api.payload.Streamable; +import com.fasterxml.jackson.annotation.JsonProperty; import java.io.File; +import java.util.Objects; -public record ImageVariationRequest( - File image, - String prompt, - String model, - Integer n, - String size, - String responseFormat, - String user -) +public final class ImageVariationRequest implements ImageRequest, Streamable { + private final File image; + private final String prompt; + private final String model; + private final Integer n; + private final String size; + private final String responseFormat; + private final String user; + + public ImageVariationRequest( + File image, + String prompt, + String model, + Integer n, + String size, + String responseFormat, + String user + ) { + this.image = image; + this.prompt = prompt; + this.model = model; + this.n = n; + this.size = size; + this.responseFormat = responseFormat; + this.user = user; + } + @Override public boolean stream() { return false; } + + @JsonProperty + public File image() { + return image; + } + + @Override + @JsonProperty + public String prompt() { + return prompt; + } + + @Override + @JsonProperty + public String model() { + return model; + } + + @Override + @JsonProperty + public Integer n() { + return n; + } + + @Override + @JsonProperty + public String size() { + return size; + } + + @Override + @JsonProperty + public String responseFormat() { + return responseFormat; + } + + @Override + @JsonProperty + public String user() { + return user; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (ImageVariationRequest) obj; + return Objects.equals( + this.image, + that.image + ) && Objects.equals( + this.prompt, + that.prompt + ) && Objects.equals( + this.model, + that.model + ) && Objects.equals( + this.n, + that.n + ) && Objects.equals( + this.size, + that.size + ) && Objects.equals( + this.responseFormat, + that.responseFormat + ) && Objects.equals( + this.user, + that.user + ); + } + + @Override + public int hashCode() { + return Objects.hash( + image, + prompt, + model, + n, + size, + responseFormat, + user + ); + } + + @Override + public String toString() { + return "ImageVariationRequest[" + "image=" + image + ", " + "prompt=" + + prompt + ", " + "model=" + model + ", " + "n=" + n + ", " + + "size=" + size + ", " + "responseFormat=" + responseFormat + + ", " + "user=" + user + ']'; + } + } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/response/ImageDataResponse.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/response/ImageDataResponse.java index 3b93e53..d5127a7 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/response/ImageDataResponse.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/response/ImageDataResponse.java @@ -1,16 +1,31 @@ package bg.codexio.ai.openai.api.payload.images.response; import bg.codexio.ai.openai.api.payload.Mergeable; +import com.fasterxml.jackson.annotation.JsonProperty; import java.util.ArrayList; import java.util.List; import java.util.Objects; -public record ImageDataResponse( - Long created, - List data -) +public final class ImageDataResponse implements Mergeable { + private final Long created; + private final List data; + + public ImageDataResponse() { + this( + null, + null + ); + } + + public ImageDataResponse( + Long created, + List data + ) { + this.created = created; + this.data = data; + } public static ImageDataResponse empty() { return new ImageDataResponse( @@ -46,4 +61,47 @@ public ImageDataResponse merge(ImageDataResponse other) { ); } + @JsonProperty + public Long created() { + return created; + } + + @JsonProperty + public List data() { + return data; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (ImageDataResponse) obj; + return Objects.equals( + this.created, + that.created + ) && Objects.equals( + this.data, + that.data + ); + } + + @Override + public int hashCode() { + return Objects.hash( + created, + data + ); + } + + @Override + public String toString() { + return "ImageDataResponse[" + "created=" + created + ", " + "data=" + + data + ']'; + } + + } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/response/ImageResponse.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/response/ImageResponse.java index 75e2330..c530dd8 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/response/ImageResponse.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/images/response/ImageResponse.java @@ -1,15 +1,34 @@ package bg.codexio.ai.openai.api.payload.images.response; import bg.codexio.ai.openai.api.payload.Mergeable; +import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Objects; -public record ImageResponse( - String b64Json, - String url, - String revisedPrompt -) +public final class ImageResponse implements Mergeable { + private final String b64Json; + private final String url; + private final String revisedPrompt; + + public ImageResponse() { + this( + null, + null, + null + ); + } + + public ImageResponse( + String b64Json, + String url, + String revisedPrompt + ) { + this.b64Json = b64Json; + this.url = url; + this.revisedPrompt = revisedPrompt; + } + public static ImageResponse empty() { return new ImageResponse( null, @@ -35,4 +54,56 @@ public ImageResponse merge(ImageResponse other) { ) ); } + + @JsonProperty + public String b64Json() { + return b64Json; + } + + @JsonProperty + public String url() { + return url; + } + + @JsonProperty + public String revisedPrompt() { + return revisedPrompt; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (ImageResponse) obj; + return Objects.equals( + this.b64Json, + that.b64Json + ) && Objects.equals( + this.url, + that.url + ) && Objects.equals( + this.revisedPrompt, + that.revisedPrompt + ); + } + + @Override + public int hashCode() { + return Objects.hash( + b64Json, + url, + revisedPrompt + ); + } + + @Override + public String toString() { + return "ImageResponse[" + "b64Json=" + b64Json + ", " + "url=" + url + + ", " + "revisedPrompt=" + revisedPrompt + ']'; + } + } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/ImageUrlMessageRequest.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/ImageUrlMessageRequest.java index ebdc7db..ca1e795 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/ImageUrlMessageRequest.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/ImageUrlMessageRequest.java @@ -1,11 +1,56 @@ package bg.codexio.ai.openai.api.payload.vision.request; -public record ImageUrlMessageRequest( - ImageUrlRequest imageUrl -) +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Objects; + +public final class ImageUrlMessageRequest implements VisionMessage { + private final ImageUrlRequest imageUrl; + + public ImageUrlMessageRequest() { + this(null); + } + + public ImageUrlMessageRequest( + ImageUrlRequest imageUrl + ) { + this.imageUrl = imageUrl; + } + @Override public String getType() { return "image_url"; } + + @JsonProperty + public ImageUrlRequest imageUrl() { + return imageUrl; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (ImageUrlMessageRequest) obj; + return Objects.equals( + this.imageUrl, + that.imageUrl + ); + } + + @Override + public int hashCode() { + return Objects.hash(imageUrl); + } + + @Override + public String toString() { + return "ImageUrlMessageRequest[" + "imageUrl=" + imageUrl + ']'; + } + } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/ImageUrlRequest.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/ImageUrlRequest.java index 1f50449..a220b79 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/ImageUrlRequest.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/ImageUrlRequest.java @@ -1,13 +1,30 @@ package bg.codexio.ai.openai.api.payload.vision.request; +import com.fasterxml.jackson.annotation.JsonProperty; + import java.io.File; import java.nio.file.Files; import java.util.Base64; +import java.util.Objects; + +public final class ImageUrlRequest { + private final String url; + private final String detail; + + public ImageUrlRequest() { + this( + null, + null + ); + } -public record ImageUrlRequest( - String url, - String detail -) { + public ImageUrlRequest( + String url, + String detail + ) { + this.url = url; + this.detail = detail; + } public static ImageUrlRequest fromLocalFile(File image) { try { @@ -48,4 +65,47 @@ public ImageUrlRequest withDetail(String detail) { detail ); } + + @JsonProperty + public String url() { + return url; + } + + @JsonProperty + public String detail() { + return detail; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (ImageUrlRequest) obj; + return Objects.equals( + this.url, + that.url + ) && Objects.equals( + this.detail, + that.detail + ); + } + + @Override + public int hashCode() { + return Objects.hash( + url, + detail + ); + } + + @Override + public String toString() { + return "ImageUrlRequest[" + "url=" + url + ", " + "detail=" + detail + + ']'; + } + } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/MessageContentHolder.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/MessageContentHolder.java index 3f20f4e..74b396b 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/MessageContentHolder.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/MessageContentHolder.java @@ -1,11 +1,30 @@ package bg.codexio.ai.openai.api.payload.vision.request; +import com.fasterxml.jackson.annotation.JsonProperty; + import java.util.ArrayList; import java.util.List; +import java.util.Objects; + +public final class MessageContentHolder { + private final List content; + + public MessageContentHolder( + ) { + this(null); + } + + public MessageContentHolder( + List content + ) { + this.content = content; + } + + @JsonProperty + public List content() { + return content; + } -public record MessageContentHolder( - List content -) { public String getRole() { return "user"; } @@ -29,4 +48,30 @@ public MessageContentHolder withContentOn( return new MessageContentHolder(content); } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (MessageContentHolder) obj; + return Objects.equals( + this.content, + that.content + ); + } + + @Override + public int hashCode() { + return Objects.hash(content); + } + + @Override + public String toString() { + return "MessageContentHolder[" + "content=" + content + ']'; + } + } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/QuestionVisionRequest.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/QuestionVisionRequest.java index f2bacfa..f84a5dd 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/QuestionVisionRequest.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/QuestionVisionRequest.java @@ -1,9 +1,54 @@ package bg.codexio.ai.openai.api.payload.vision.request; -public record QuestionVisionRequest(String text) +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Objects; + +public final class QuestionVisionRequest implements VisionMessage { + private final String text; + + public QuestionVisionRequest() { + this(null); + } + + public QuestionVisionRequest(String text) { + this.text = text; + } + @Override public String getType() { return "text"; } + + @JsonProperty + public String text() { + return text; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (QuestionVisionRequest) obj; + return Objects.equals( + this.text, + that.text + ); + } + + @Override + public int hashCode() { + return Objects.hash(text); + } + + @Override + public String toString() { + return "QuestionVisionRequest[" + "text=" + text + ']'; + } + } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/VisionRequest.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/VisionRequest.java index 008821f..059b59b 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/VisionRequest.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/vision/request/VisionRequest.java @@ -1,20 +1,39 @@ package bg.codexio.ai.openai.api.payload.vision.request; import bg.codexio.ai.openai.api.payload.Streamable; +import com.fasterxml.jackson.annotation.JsonProperty; import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * Represents a * Vision request */ -public record VisionRequest( - String model, - List messages, - Integer maxTokens -) +public final class VisionRequest implements Streamable { + private final String model; + private final List messages; + private final Integer maxTokens; + + public VisionRequest() { + this( + null, + null, + null + ); + } + + public VisionRequest( + String model, + List messages, + Integer maxTokens + ) { + this.model = model; + this.messages = messages; + this.maxTokens = maxTokens; + } public static VisionRequest empty() { return new VisionRequest( @@ -29,6 +48,23 @@ public boolean stream() { return false; } + + @JsonProperty + public String model() { + return model; + } + + @JsonProperty + public List messages() { + return messages; + } + + @JsonProperty + public Integer maxTokens() { + return maxTokens; + } + + public VisionRequest withModel(String model) { return new VisionRequest( model, @@ -72,4 +108,41 @@ public VisionRequest withTokens(Integer maxTokens) { maxTokens ); } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (VisionRequest) obj; + return Objects.equals( + this.model, + that.model + ) && Objects.equals( + this.messages, + that.messages + ) && Objects.equals( + this.maxTokens, + that.maxTokens + ); + } + + @Override + public int hashCode() { + return Objects.hash( + model, + messages, + maxTokens + ); + } + + @Override + public String toString() { + return "VisionRequest[" + "model=" + model + ", " + "messages=" + + messages + ", " + "maxTokens=" + maxTokens + ']'; + } + } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/request/SpeechRequest.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/request/SpeechRequest.java index a42fddb..6ab59c4 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/request/SpeechRequest.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/request/SpeechRequest.java @@ -1,19 +1,46 @@ package bg.codexio.ai.openai.api.payload.voice.request; import bg.codexio.ai.openai.api.payload.Streamable; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Objects; +import java.util.stream.Stream; /** * Represents a * Create speech#Request body */ -public record SpeechRequest( - String model, - String input, - String voice, - String responseFormat, - Double speed -) +public final class SpeechRequest implements Streamable { + private final String model; + private final String input; + private final String voice; + private final String responseFormat; + private final Double speed; + + public SpeechRequest() { + this( + null, + null, + null, + null, + null + ); + } + + public SpeechRequest( + String model, + String input, + String voice, + String responseFormat, + Double speed + ) { + this.model = model; + this.input = input; + this.voice = voice; + this.responseFormat = responseFormat; + this.speed = speed; + } public static Builder builder() { return new Builder( @@ -30,13 +57,97 @@ public boolean stream() { return false; } - public record Builder( - String model, - String input, - String voice, - String responseFormat, - Double speed - ) { + @JsonProperty + public String model() { + return model; + } + + @JsonProperty + public String input() { + return input; + } + + @JsonProperty + public String voice() { + return voice; + } + + @JsonProperty + public String responseFormat() { + return responseFormat; + } + + @JsonProperty + public Double speed() { + return speed; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (SpeechRequest) obj; + return Objects.equals( + this.model, + that.model + ) && Objects.equals( + this.input, + that.input + ) && Objects.equals( + this.voice, + that.voice + ) && Objects.equals( + this.responseFormat, + that.responseFormat + ) && Objects.equals( + this.speed, + that.speed + ); + } + + @Override + public int hashCode() { + return Objects.hash( + model, + input, + voice, + responseFormat, + speed + ); + } + + @Override + public String toString() { + return "SpeechRequest[" + "model=" + model + ", " + "input=" + input + + ", " + "voice=" + voice + ", " + "responseFormat=" + + responseFormat + ", " + "speed=" + speed + ']'; + } + + + public static final class Builder { + private final String model; + private final String input; + private final String voice; + private final String responseFormat; + private final Double speed; + + public Builder( + String model, + String input, + String voice, + String responseFormat, + Double speed + ) { + this.model = model; + this.input = input; + this.voice = voice; + this.responseFormat = responseFormat; + this.speed = speed; + } public Builder withModel(String model) { return new Builder( @@ -97,5 +208,71 @@ public SpeechRequest build() { speed ); } + + public String model() { + return model; + } + + public String input() { + return input; + } + + public String voice() { + return voice; + } + + public String responseFormat() { + return responseFormat; + } + + public Double speed() { + return speed; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (Builder) obj; + return Objects.equals( + this.model, + that.model + ) && Objects.equals( + this.input, + that.input + ) && Objects.equals( + this.voice, + that.voice + ) && Objects.equals( + this.responseFormat, + that.responseFormat + ) && Objects.equals( + this.speed, + that.speed + ); + } + + @Override + public int hashCode() { + return Objects.hash( + model, + input, + voice, + responseFormat, + speed + ); + } + + @Override + public String toString() { + return "Builder[" + "model=" + model + ", " + "input=" + input + + ", " + "voice=" + voice + ", " + "responseFormat=" + + responseFormat + ", " + "speed=" + speed + ']'; + } + } } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/request/TranscriptionRequest.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/request/TranscriptionRequest.java index 8407d5d..d9991e4 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/request/TranscriptionRequest.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/request/TranscriptionRequest.java @@ -1,22 +1,54 @@ package bg.codexio.ai.openai.api.payload.voice.request; import bg.codexio.ai.openai.api.payload.Streamable; +import com.fasterxml.jackson.annotation.JsonProperty; import java.io.File; +import java.util.Objects; /** * Represents a * Create transcription#Request body */ -public record TranscriptionRequest( - File file, - String model, - String language, - String prompt, - String responseFormat, - Double temperature -) +public final class TranscriptionRequest implements Streamable { + private final File file; + private final String model; + private final String language; + private final String prompt; + private final String responseFormat; + private final Double temperature; + private final Boolean stream; + + public TranscriptionRequest() { + this( + null, + null, + null, + null, + null, + null, + null + ); + } + + public TranscriptionRequest( + File file, + String model, + String language, + String prompt, + String responseFormat, + Double temperature, + Boolean stream + ) { + this.file = file; + this.model = model; + this.language = language; + this.prompt = prompt; + this.responseFormat = responseFormat; + this.temperature = temperature; + this.stream = stream; + } public static Builder builder() { return new Builder( @@ -25,6 +57,7 @@ public static Builder builder() { null, null, null, + null, null ); } @@ -34,14 +67,116 @@ public boolean stream() { return false; } - public record Builder( - File file, - String model, - String language, - String prompt, - String responseFormat, - Double temperature - ) { + @JsonProperty + public File file() { + return file; + } + + @JsonProperty + public String model() { + return model; + } + + @JsonProperty + public String language() { + return language; + } + + @JsonProperty + public String prompt() { + return prompt; + } + + @JsonProperty + public String responseFormat() { + return responseFormat; + } + + @JsonProperty + public Double temperature() { + return temperature; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (TranscriptionRequest) obj; + return Objects.equals( + this.file, + that.file + ) && Objects.equals( + this.model, + that.model + ) && Objects.equals( + this.language, + that.language + ) && Objects.equals( + this.prompt, + that.prompt + ) && Objects.equals( + this.responseFormat, + that.responseFormat + ) && Objects.equals( + this.temperature, + that.temperature + ); + } + + @Override + public int hashCode() { + return Objects.hash( + file, + model, + language, + prompt, + responseFormat, + temperature + ); + } + + @Override + public String toString() { + return "TranscriptionRequest[" + "file=" + file + ", " + "model=" + + model + ", " + "language=" + language + ", " + "prompt=" + + prompt + ", " + "responseFormat=" + responseFormat + ", " + + "temperature=" + temperature + ']'; + } + + + public static final class Builder { + private final File file; + private final String model; + private final String language; + private final String prompt; + private final String responseFormat; + private final Double temperature; + + private final Boolean stream; + + public Builder( + File file, + String model, + String language, + String prompt, + String responseFormat, + Double temperature, + Boolean stream + + ) { + this.file = file; + this.model = model; + this.language = language; + this.prompt = prompt; + this.responseFormat = responseFormat; + this.temperature = temperature; + this.stream = stream; + } + public Builder withFile(File file) { return new Builder( file, @@ -49,7 +184,8 @@ public Builder withFile(File file) { language, prompt, responseFormat, - temperature + temperature, + stream ); } @@ -60,7 +196,8 @@ public Builder withModel(String model) { language, prompt, responseFormat, - temperature + temperature, + stream ); } @@ -71,7 +208,8 @@ public Builder withLanguage(String language) { language, prompt, responseFormat, - temperature + temperature, + stream ); } @@ -82,7 +220,8 @@ public Builder withPrompt(String prompt) { language, prompt, responseFormat, - temperature + temperature, + stream ); } @@ -94,7 +233,8 @@ public Builder withFormat(String responseFormat) { language, prompt, responseFormat, - temperature + temperature, + stream ); } @@ -105,7 +245,8 @@ public Builder withTemperature(Double temperature) { language, prompt, responseFormat, - temperature + temperature, + stream ); } @@ -116,8 +257,92 @@ public TranscriptionRequest build() { language, prompt, responseFormat, - temperature + temperature, + stream + ); + } + + public File file() { + return file; + } + + public String model() { + return model; + } + + public String language() { + return language; + } + + public String prompt() { + return prompt; + } + + public String responseFormat() { + return responseFormat; + } + + public Double temperature() { + return temperature; + } + + public Boolean stream() { + return stream; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (Builder) obj; + return Objects.equals( + this.file, + that.file + ) && Objects.equals( + this.model, + that.model + ) && Objects.equals( + this.language, + that.language + ) && Objects.equals( + this.prompt, + that.prompt + ) && Objects.equals( + this.responseFormat, + that.responseFormat + ) && Objects.equals( + this.temperature, + that.temperature + ) && Objects.equals( + this.stream, + that.stream + ); + } + + @Override + public int hashCode() { + return Objects.hash( + file, + model, + language, + prompt, + responseFormat, + temperature, + stream ); } + + @Override + public String toString() { + return "Builder[" + "file=" + file + ", " + "model=" + model + ", " + + "language=" + language + ", " + "prompt=" + prompt + ", " + + "responseFormat=" + responseFormat + ", " + "temperature=" + + temperature + "stream=" + stream + ']'; + } + } } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/request/TranslationRequest.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/request/TranslationRequest.java index bee06e9..60243db 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/request/TranslationRequest.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/request/TranslationRequest.java @@ -1,21 +1,47 @@ package bg.codexio.ai.openai.api.payload.voice.request; import bg.codexio.ai.openai.api.payload.Streamable; +import com.fasterxml.jackson.annotation.JsonProperty; import java.io.File; +import java.util.Objects; +import java.util.stream.Stream; /** * Represents a * Create translation#Request body */ -public record TranslationRequest( - File file, - String model, - String prompt, - String responseFormat, - Double temperature -) +public final class TranslationRequest implements Streamable { + private final File file; + private final String model; + private final String prompt; + private final String responseFormat; + private final Double temperature; + + public TranslationRequest() { + this( + null, + null, + null, + null, + null + ); + } + + public TranslationRequest( + File file, + String model, + String prompt, + String responseFormat, + Double temperature + ) { + this.file = file; + this.model = model; + this.prompt = prompt; + this.responseFormat = responseFormat; + this.temperature = temperature; + } public static Builder builder() { return new Builder( @@ -32,13 +58,97 @@ public boolean stream() { return false; } - public record Builder( - File file, - String model, - String prompt, - String responseFormat, - Double temperature - ) { + @JsonProperty + public File file() { + return file; + } + + @JsonProperty + public String model() { + return model; + } + + @JsonProperty + public String prompt() { + return prompt; + } + + @JsonProperty + public String responseFormat() { + return responseFormat; + } + + @JsonProperty + public Double temperature() { + return temperature; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (TranslationRequest) obj; + return Objects.equals( + this.file, + that.file + ) && Objects.equals( + this.model, + that.model + ) && Objects.equals( + this.prompt, + that.prompt + ) && Objects.equals( + this.responseFormat, + that.responseFormat + ) && Objects.equals( + this.temperature, + that.temperature + ); + } + + @Override + public int hashCode() { + return Objects.hash( + file, + model, + prompt, + responseFormat, + temperature + ); + } + + @Override + public String toString() { + return "TranslationRequest[" + "file=" + file + ", " + "model=" + model + + ", " + "prompt=" + prompt + ", " + "responseFormat=" + + responseFormat + ", " + "temperature=" + temperature + ']'; + } + + + public static final class Builder { + private final File file; + private final String model; + private final String prompt; + private final String responseFormat; + private final Double temperature; + + public Builder( + File file, + String model, + String prompt, + String responseFormat, + Double temperature + ) { + this.file = file; + this.model = model; + this.prompt = prompt; + this.responseFormat = responseFormat; + this.temperature = temperature; + } public Builder withFile(File file) { return new Builder( @@ -99,5 +209,73 @@ public TranslationRequest build() { temperature ); } + + public File file() { + return file; + } + + public String model() { + return model; + } + + public String prompt() { + return prompt; + } + + public String responseFormat() { + return responseFormat; + } + + public Double temperature() { + return temperature; + } + + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (Builder) obj; + return Objects.equals( + this.file, + that.file + ) && Objects.equals( + this.model, + that.model + ) && Objects.equals( + this.prompt, + that.prompt + ) && Objects.equals( + this.responseFormat, + that.responseFormat + ) && Objects.equals( + this.temperature, + that.temperature + ); + } + + @Override + public int hashCode() { + return Objects.hash( + file, + model, + prompt, + responseFormat, + temperature + ); + } + + @Override + public String toString() { + return "Builder[" + "file=" + file + ", " + "model=" + model + ", " + + "prompt=" + prompt + ", " + "responseFormat=" + + responseFormat + ", " + "temperature=" + temperature + + ']'; + } + } } diff --git a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/response/AudioBinaryResponse.java b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/response/AudioBinaryResponse.java index 5b2ba36..2224888 100644 --- a/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/response/AudioBinaryResponse.java +++ b/openai-api-payload/src/main/java/bg/codexio/ai/openai/api/payload/voice/response/AudioBinaryResponse.java @@ -1,6 +1,7 @@ package bg.codexio.ai.openai.api.payload.voice.response; import bg.codexio.ai.openai.api.payload.Mergeable; +import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Arrays; import java.util.Objects; @@ -9,10 +10,25 @@ * Represents an * Audio#response */ -public record SpeechTextResponse( - String text -) +public final class SpeechTextResponse implements Mergeable { + private final String text; + + public SpeechTextResponse() { + this(null); + } + + public SpeechTextResponse( + String text + ) { + this.text = text; + } + + @JsonProperty + public String text() { + return text; + } + @Override public SpeechTextResponse merge(SpeechTextResponse other) { return new SpeechTextResponse(Objects.requireNonNullElse( @@ -22,4 +38,30 @@ public SpeechTextResponse merge(SpeechTextResponse other) { "" )); } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + var that = (SpeechTextResponse) obj; + return Objects.equals( + this.text, + that.text + ); + } + + @Override + public int hashCode() { + return Objects.hash(text); + } + + @Override + public String toString() { + return "SpeechTextResponse[" + "text=" + text + ']'; + } + } diff --git a/openai-api-sdk/pom.xml b/openai-api-sdk/pom.xml index d56f1c3..69c81e9 100644 --- a/openai-api-sdk/pom.xml +++ b/openai-api-sdk/pom.xml @@ -7,7 +7,7 @@ bg.codexio.ai openai-api - 0.8.0.BETA-JDK17 + 0.8.0.BETA-JDK11-SNAPSHOT openai-api-sdk @@ -22,7 +22,7 @@ bg.codexio.ai openai-api-http - 0.8.0.BETA-JDK17 + 0.8.0.BETA-JDK11-SNAPSHOT diff --git a/openai-api-sdk/src/main/java/bg/codexio/ai/openai/api/sdk/chat/AccuracyStage.java b/openai-api-sdk/src/main/java/bg/codexio/ai/openai/api/sdk/chat/AccuracyStage.java index ba3d966..8cac4b4 100644 --- a/openai-api-sdk/src/main/java/bg/codexio/ai/openai/api/sdk/chat/AccuracyStage.java +++ b/openai-api-sdk/src/main/java/bg/codexio/ai/openai/api/sdk/chat/AccuracyStage.java @@ -30,7 +30,7 @@ public class AccuracyStage public AccuracyStage withTemperature(Creativity temperature) { return new AccuracyStage( this.executor, - this.requestBuilder.withTermperature(temperature.val()) + this.requestBuilder.withTemperature(temperature.val()) ); } diff --git a/openai-api-sdk/src/main/java/bg/codexio/ai/openai/api/sdk/chat/TemperatureStage.java b/openai-api-sdk/src/main/java/bg/codexio/ai/openai/api/sdk/chat/TemperatureStage.java index e181c0a..0b36786 100644 --- a/openai-api-sdk/src/main/java/bg/codexio/ai/openai/api/sdk/chat/TemperatureStage.java +++ b/openai-api-sdk/src/main/java/bg/codexio/ai/openai/api/sdk/chat/TemperatureStage.java @@ -34,7 +34,7 @@ protected TemperatureStage( public MessageStage creativeAs(Creativity creativity) { return new MessageStage( this.executor, - this.requestBuilder.withTermperature(creativity.val()) + this.requestBuilder.withTemperature(creativity.val()) ); } @@ -50,7 +50,7 @@ public MessageStage creativeAs(Creativity creativity) { public MessageStage scaleRepetitionToCreativity(Creativity creativity) { return new MessageStage( this.executor, - this.requestBuilder.withTermperature(creativity.val()) + this.requestBuilder.withTemperature(creativity.val()) .withTopP(creativity.val() == Creativity.BALANCE_BETWEEN_NOVELTY_AND_PREDICTABILITY.val() ? diff --git a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/HttpBuilderTest.java b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/HttpBuilderTest.java index 09ddb13..bc00421 100644 --- a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/HttpBuilderTest.java +++ b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/HttpBuilderTest.java @@ -32,10 +32,10 @@ public void testAnd_shouldProvideNextStageWithCtxAndMapper() { var nextStage = this.httpBuilder.and(); assertAll( - () -> assertNotNull(nextStage.mapper), + () -> assertNotNull(nextStage.mapper()), () -> assertEquals( CTX, - nextStage.ctx + nextStage.ctx() ) ); } @@ -48,11 +48,11 @@ public void testUnderstand_shouldProvideNextStageWithCtxAndMapper() { assertAll( () -> assertEquals( mapper, - nextStage.mapper + nextStage.mapper() ), () -> assertEquals( CTX, - nextStage.ctx + nextStage.ctx() ) ); } @@ -66,7 +66,7 @@ public void testWithCallTimeout_expectNewTimeout() { .and(); assertAll( - () -> assertNotNull(nextStage.mapper), + () -> assertNotNull(nextStage.mapper()), () -> assertEquals( new HttpExecutorContext( new ApiCredentials("test-key"), @@ -85,7 +85,7 @@ public void testWithCallTimeout_expectNewTimeout() { ) ) ), - nextStage.ctx + nextStage.ctx() ) ); } @@ -99,7 +99,7 @@ public void testWithConnectTimeout_expectNewTimeout() { .and(); assertAll( - () -> assertNotNull(nextStage.mapper), + () -> assertNotNull(nextStage.mapper()), () -> assertEquals( new HttpExecutorContext( new ApiCredentials("test-key"), @@ -118,7 +118,7 @@ public void testWithConnectTimeout_expectNewTimeout() { ) ) ), - nextStage.ctx + nextStage.ctx() ) ); } @@ -132,7 +132,7 @@ public void testWithReadTimeout_expectNewTimeout() { .and(); assertAll( - () -> assertNotNull(nextStage.mapper), + () -> assertNotNull(nextStage.mapper()), () -> assertEquals( new HttpExecutorContext( new ApiCredentials("test-key"), @@ -151,15 +151,29 @@ public void testWithReadTimeout_expectNewTimeout() { ) ) ), - nextStage.ctx + nextStage.ctx() ) ); } - record NextStage( - HttpExecutorContext ctx, - ObjectMapper mapper - ) { - + static class NextStage { + private final HttpExecutorContext ctx; + private final ObjectMapper mapper; + + public NextStage( + HttpExecutorContext ctx, + ObjectMapper mapper + ) { + this.ctx = ctx; + this.mapper = mapper; + } + + public HttpExecutorContext ctx() { + return ctx; + } + + public ObjectMapper mapper() { + return mapper; + } } } diff --git a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/AsyncContextStageTest.java b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/AsyncContextStageTest.java index 0b09955..f9e0bad 100644 --- a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/AsyncContextStageTest.java +++ b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/AsyncContextStageTest.java @@ -18,7 +18,7 @@ public void setUp() { CHAT_EXECUTOR, ChatMessageRequest.builder() .withModel(MODEL_TYPE.name()) - .withTermperature(CREATIVITY.val()) + .withTemperature(CREATIVITY.val()) .withTopP(CREATIVITY.val()) .withFrequencyPenalty(REPETITION_PENALTY.val()) .withPresencePenalty(REPETITION_PENALTY.val()) diff --git a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ChatRuntimeSelectionStageTest.java b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ChatRuntimeSelectionStageTest.java index 432be08..acb1b7d 100644 --- a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ChatRuntimeSelectionStageTest.java +++ b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ChatRuntimeSelectionStageTest.java @@ -16,7 +16,7 @@ public void setUp() { null, ChatMessageRequest.builder() .withModel(MODEL_TYPE.name()) - .withTermperature(CREATIVITY.val()) + .withTemperature(CREATIVITY.val()) .withTopP(CREATIVITY.val()) .withFrequencyPenalty(REPETITION_PENALTY.val()) .withPresencePenalty(REPETITION_PENALTY.val()) diff --git a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ImmediateContextStageTest.java b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ImmediateContextStageTest.java index 2b47f2e..9a69ff4 100644 --- a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ImmediateContextStageTest.java +++ b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ImmediateContextStageTest.java @@ -18,7 +18,7 @@ public void setUp() { CHAT_EXECUTOR, ChatMessageRequest.builder() .withModel(MODEL_TYPE.name()) - .withTermperature(CREATIVITY.val()) + .withTemperature(CREATIVITY.val()) .withTopP(CREATIVITY.val()) .withFrequencyPenalty(REPETITION_PENALTY.val()) .withPresencePenalty(REPETITION_PENALTY.val()) diff --git a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/MessageStageTest.java b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/MessageStageTest.java index c7deeb9..a58a5be 100644 --- a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/MessageStageTest.java +++ b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/MessageStageTest.java @@ -18,7 +18,7 @@ public void setUp() { null, ChatMessageRequest.builder() .withModel(MODEL_TYPE.name()) - .withTermperature(CREATIVITY.val()) + .withTemperature(CREATIVITY.val()) .withTopP(CREATIVITY.val()) .withFrequencyPenalty(REPETITION_PENALTY.val()) .withPresencePenalty(REPETITION_PENALTY.val()) @@ -43,9 +43,20 @@ public void testWithRoledMessage_manualRoleChoice_expectMessageAsChoice() { "Test role", "Test message", null - ), + ).role(), + stage.requestBuilder.messages() + .get(0) + .role() + ), + () -> assertEquals( + new ChatMessage( + "Test role", + "Test message", + null + ).role(), stage.requestBuilder.messages() .get(0) + .role() ) ); } diff --git a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ReactiveContextStageTest.java b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ReactiveContextStageTest.java index d12a9fc..9a3f2bb 100644 --- a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ReactiveContextStageTest.java +++ b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ReactiveContextStageTest.java @@ -26,7 +26,7 @@ public void setUp() { CHAT_EXECUTOR, ChatMessageRequest.builder() .withModel(MODEL_TYPE.name()) - .withTermperature(CREATIVITY.val()) + .withTemperature(CREATIVITY.val()) .withTopP(CREATIVITY.val()) .withFrequencyPenalty(REPETITION_PENALTY.val()) .withPresencePenalty(REPETITION_PENALTY.val()) diff --git a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/TokenStageTest.java b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/TokenStageTest.java index 069a854..6810125 100644 --- a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/TokenStageTest.java +++ b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/TokenStageTest.java @@ -17,7 +17,7 @@ public void setUp() { null, ChatMessageRequest.builder() .withModel(MODEL_TYPE.name()) - .withTermperature(CREATIVITY.val()) + .withTemperature(CREATIVITY.val()) .withTopP(CREATIVITY.val()) .withFrequencyPenalty(REPETITION_PENALTY.val()) .withPresencePenalty(REPETITION_PENALTY.val()) diff --git a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ToolStageTest.java b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ToolStageTest.java index fd08f46..ca380c0 100644 --- a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ToolStageTest.java +++ b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/chat/ToolStageTest.java @@ -19,7 +19,7 @@ public void setUp() { null, ChatMessageRequest.builder() .withModel(MODEL_TYPE.name()) - .withTermperature(CREATIVITY.val()) + .withTemperature(CREATIVITY.val()) .withTopP(CREATIVITY.val()) .withFrequencyPenalty(REPETITION_PENALTY.val()) .withPresencePenalty(REPETITION_PENALTY.val()) diff --git a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/speech/ReactiveDownloadStageTest.java b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/speech/ReactiveDownloadStageTest.java index c89636c..a97d951 100644 --- a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/speech/ReactiveDownloadStageTest.java +++ b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/speech/ReactiveDownloadStageTest.java @@ -17,8 +17,10 @@ import static bg.codexio.ai.openai.api.sdk.voice.speech.InternalAssertions.*; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.when; public class ReactiveDownloadStageTest { @@ -26,7 +28,6 @@ public class ReactiveDownloadStageTest { private ReactiveDownloadStage reactiveDownloadStage; - @BeforeEach public void setUp() { this.reactiveDownloadStage = new ReactiveDownloadStage( @@ -42,7 +43,7 @@ public void setUp() { @Test public void testDownloadTo_withImaginaryFolder_shouldUseCorrectMediaTypeAndResponse() { - try (var mockData = this.startMocking()) { + try (var mockData = startMocking()) { var filePath = "var/files/resultFile"; mockData.executorMock() @@ -63,7 +64,7 @@ public void testDownloadTo_withImaginaryFolder_shouldUseCorrectMediaTypeAndRespo @Test public void testDownloadTo_withErrorWhileDownloading_shouldThrowException() { - try (var mockData = this.startMocking()) { + try (var mockData = startMocking()) { mockData.executorMock() .thenThrow(new RuntimeException("Cannot download")); @@ -104,16 +105,38 @@ private MockData startMocking() { ); } - record MockData( - OngoingStubbing executorMock, - File targetFolder, - MockedStatic utils - ) + class MockData implements AutoCloseable { + private final OngoingStubbing executorMock; + private final File targetFolder; + private final MockedStatic utils; + + public MockData( + OngoingStubbing executorMock, + File targetFolder, + MockedStatic utils + ) { + this.executorMock = executorMock; + this.targetFolder = targetFolder; + this.utils = utils; + } + + public OngoingStubbing executorMock() { + return executorMock; + } + + public File targetFolder() { + return targetFolder; + } + + public MockedStatic utils() { + return utils; + } + @Override public void close() { - this.utils() - .close(); + utils().close(); } } } + diff --git a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/speech/SynchronousDownloadStageTest.java b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/speech/SynchronousDownloadStageTest.java index 0666853..8be5e26 100644 --- a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/speech/SynchronousDownloadStageTest.java +++ b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/speech/SynchronousDownloadStageTest.java @@ -24,7 +24,6 @@ public class SynchronousDownloadStageTest { private SynchronousDownloadStage synchronousDownloadStage; - @BeforeEach public void setUp() { this.synchronousDownloadStage = new SynchronousDownloadStage( @@ -41,7 +40,7 @@ public void setUp() { @Test public void testDownloadTo_withImaginaryFolder_shouldUseCorrectMediaTypeAndResponse() throws IOException { - try (var mockData = this.startMocking()) { + try (var mockData = startMocking()) { var filePath = "var/files/resultFile"; mockData.executorMock() .thenReturn(new File(filePath)); @@ -60,7 +59,7 @@ public void testDownloadTo_withImaginaryFolder_shouldUseCorrectMediaTypeAndRespo @Test public void testDownloadTo_withErrorWhileDownloading_shouldThrowException() { - try (var mockData = this.startMocking()) { + try (var mockData = startMocking()) { mockData.executorMock() .thenThrow(new RuntimeException("Cannot download")); @@ -75,7 +74,6 @@ public void testDownloadTo_withErrorWhileDownloading_shouldThrowException() { } } - private MockData startMocking() { var targetFolder = new File("imaginaryFolder"); var response = new AudioBinaryResponse(new byte[]{1, 2, 3}); @@ -95,16 +93,37 @@ private MockData startMocking() { ); } - record MockData( - OngoingStubbing executorMock, - File targetFolder, - MockedStatic utils - ) + static class MockData implements AutoCloseable { + private final OngoingStubbing executorMock; + private final File targetFolder; + private final MockedStatic utils; + + public MockData( + OngoingStubbing executorMock, + File targetFolder, + MockedStatic utils + ) { + this.executorMock = executorMock; + this.targetFolder = targetFolder; + this.utils = utils; + } + + public OngoingStubbing executorMock() { + return executorMock; + } + + public File targetFolder() { + return targetFolder; + } + + public MockedStatic utils() { + return utils; + } + @Override public void close() { - this.utils() - .close(); + utils().close(); } } } diff --git a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/transcription/PreSimplifiedStageTest.java b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/transcription/PreSimplifiedStageTest.java index c347e43..41910b4 100644 --- a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/transcription/PreSimplifiedStageTest.java +++ b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/transcription/PreSimplifiedStageTest.java @@ -5,7 +5,6 @@ import org.junit.jupiter.api.Test; import static bg.codexio.ai.openai.api.sdk.voice.transcription.InternalAssertions.TEST_FILE; -import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; public class PreSimplifiedStageTest { diff --git a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/translation/PreSimplifiedStageTest.java b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/translation/PreSimplifiedStageTest.java index d71a12c..515f965 100644 --- a/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/translation/PreSimplifiedStageTest.java +++ b/openai-api-sdk/src/test/java/bg/codexio/ai/openai/api/sdk/voice/translation/PreSimplifiedStageTest.java @@ -5,7 +5,6 @@ import org.junit.jupiter.api.Test; import static bg.codexio.ai.openai.api.sdk.voice.translation.InternalAssertions.TEST_FILE; -import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; public class PreSimplifiedStageTest { diff --git a/pom.xml b/pom.xml index 2080dde..37ad2c9 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ bg.codexio.ai openai-api - 0.8.0.BETA-JDK17 + 0.8.0.BETA-JDK11-SNAPSHOT pom Codexio Ltd. OpenAI API @@ -55,7 +55,7 @@ - 17 + 11 ${java.version} ${java.version}