Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

refactor(client)!: switch query params objects to use QueryParams #64

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ class OpenAIOkHttpClient private constructor() {

fun apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }

fun organization(organization: String?) = apply { clientOptions.organization(organization) }
fun organization(organization: String) = apply { clientOptions.organization(organization) }

fun project(project: String?) = apply { clientOptions.project(project) }
fun project(project: String) = apply { clientOptions.project(project) }

fun fromEnv() = apply { clientOptions.fromEnv() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ class OpenAIOkHttpClientAsync private constructor() {

fun apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }

fun organization(organization: String?) = apply { clientOptions.organization(organization) }
fun organization(organization: String) = apply { clientOptions.organization(organization) }

fun project(project: String?) = apply { clientOptions.project(project) }
fun project(project: String) = apply { clientOptions.project(project) }

fun fromEnv() = apply { clientOptions.fromEnv() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ private constructor(

fun apiKey(apiKey: String) = apply { this.apiKey = apiKey }

fun organization(organization: String?) = apply { this.organization = organization }
fun organization(organization: String) = apply { this.organization = organization }

fun project(project: String?) = apply { this.project = project }
fun project(project: String) = apply { this.project = project }

fun fromEnv() = apply {
System.getenv("OPENAI_API_KEY")?.let { apiKey(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package com.openai.errors
import com.fasterxml.jackson.annotation.JsonAnyGetter
import com.fasterxml.jackson.annotation.JsonAnySetter
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.openai.core.ExcludeMissing
import com.openai.core.JsonValue
import com.openai.core.NoAutoDetect
import com.openai.core.toImmutable
Expand All @@ -15,6 +16,7 @@ import java.util.Objects
class OpenAIError
private constructor(
@JsonAnyGetter
@ExcludeMissing
@get:JvmName("additionalProperties")
val additionalProperties: Map<String, JsonValue>,
) {
Expand Down
76 changes: 44 additions & 32 deletions openai-java-core/src/main/kotlin/com/openai/models/Batch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ private constructor(
private val additionalProperties: Map<String, JsonValue>,
) {

private var validated: Boolean = false

fun id(): String = id.getRequired("id")

/** The object type, which is always `batch`. */
Expand Down Expand Up @@ -174,6 +172,8 @@ private constructor(
@ExcludeMissing
fun _additionalProperties(): Map<String, JsonValue> = additionalProperties

private var validated: Boolean = false

fun validate(): Batch = apply {
if (!validated) {
id()
Expand Down Expand Up @@ -232,27 +232,27 @@ private constructor(

@JvmSynthetic
internal fun from(batch: Batch) = apply {
this.id = batch.id
this.object_ = batch.object_
this.endpoint = batch.endpoint
this.errors = batch.errors
this.inputFileId = batch.inputFileId
this.completionWindow = batch.completionWindow
this.status = batch.status
this.outputFileId = batch.outputFileId
this.errorFileId = batch.errorFileId
this.createdAt = batch.createdAt
this.inProgressAt = batch.inProgressAt
this.expiresAt = batch.expiresAt
this.finalizingAt = batch.finalizingAt
this.completedAt = batch.completedAt
this.failedAt = batch.failedAt
this.expiredAt = batch.expiredAt
this.cancellingAt = batch.cancellingAt
this.cancelledAt = batch.cancelledAt
this.requestCounts = batch.requestCounts
this.metadata = batch.metadata
additionalProperties(batch.additionalProperties)
id = batch.id
object_ = batch.object_
endpoint = batch.endpoint
errors = batch.errors
inputFileId = batch.inputFileId
completionWindow = batch.completionWindow
status = batch.status
outputFileId = batch.outputFileId
errorFileId = batch.errorFileId
createdAt = batch.createdAt
inProgressAt = batch.inProgressAt
expiresAt = batch.expiresAt
finalizingAt = batch.finalizingAt
completedAt = batch.completedAt
failedAt = batch.failedAt
expiredAt = batch.expiredAt
cancellingAt = batch.cancellingAt
cancelledAt = batch.cancelledAt
requestCounts = batch.requestCounts
metadata = batch.metadata
additionalProperties = batch.additionalProperties.toMutableMap()
}

fun id(id: String) = id(JsonField.of(id))
Expand Down Expand Up @@ -420,18 +420,24 @@ private constructor(

fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
this.additionalProperties.clear()
this.additionalProperties.putAll(additionalProperties)
putAllAdditionalProperties(additionalProperties)
}

@JsonAnySetter
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
this.additionalProperties.put(key, value)
additionalProperties.put(key, value)
}

fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
this.additionalProperties.putAll(additionalProperties)
}

fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }

fun removeAllAdditionalProperties(keys: Set<String>) = apply {
keys.forEach(::removeAdditionalProperty)
}

fun build(): Batch =
Batch(
id,
Expand Down Expand Up @@ -611,8 +617,6 @@ private constructor(
private val additionalProperties: Map<String, JsonValue>,
) {

private var validated: Boolean = false

/** The object type, which is always `list`. */
fun object_(): Optional<String> = Optional.ofNullable(object_.getNullable("object"))

Expand All @@ -627,6 +631,8 @@ private constructor(
@ExcludeMissing
fun _additionalProperties(): Map<String, JsonValue> = additionalProperties

private var validated: Boolean = false

fun validate(): Errors = apply {
if (!validated) {
object_()
Expand All @@ -650,9 +656,9 @@ private constructor(

@JvmSynthetic
internal fun from(errors: Errors) = apply {
this.object_ = errors.object_
this.data = errors.data
additionalProperties(errors.additionalProperties)
object_ = errors.object_
data = errors.data
additionalProperties = errors.additionalProperties.toMutableMap()
}

/** The object type, which is always `list`. */
Expand All @@ -671,18 +677,24 @@ private constructor(

fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
this.additionalProperties.clear()
this.additionalProperties.putAll(additionalProperties)
putAllAdditionalProperties(additionalProperties)
}

@JsonAnySetter
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
this.additionalProperties.put(key, value)
additionalProperties.put(key, value)
}

fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
this.additionalProperties.putAll(additionalProperties)
}

fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }

fun removeAllAdditionalProperties(keys: Set<String>) = apply {
keys.forEach(::removeAdditionalProperty)
}

fun build(): Errors =
Errors(
object_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ constructor(
@NoAutoDetect
class BatchCreateBody
internal constructor(
private val completionWindow: CompletionWindow?,
private val endpoint: Endpoint?,
private val inputFileId: String?,
private val completionWindow: CompletionWindow,
private val endpoint: Endpoint,
private val inputFileId: String,
private val metadata: Metadata?,
private val additionalProperties: Map<String, JsonValue>,
) {
Expand All @@ -75,15 +75,15 @@ constructor(
* supported.
*/
@JsonProperty("completion_window")
fun completionWindow(): CompletionWindow? = completionWindow
fun completionWindow(): CompletionWindow = completionWindow

/**
* The endpoint to be used for all requests in the batch. Currently `/v1/chat/completions`,
* `/v1/embeddings`, and `/v1/completions` are supported. Note that `/v1/embeddings` batches
* are also restricted to a maximum of 50,000 embedding inputs across all requests in the
* batch.
*/
@JsonProperty("endpoint") fun endpoint(): Endpoint? = endpoint
@JsonProperty("endpoint") fun endpoint(): Endpoint = endpoint

/**
* The ID of an uploaded file that contains requests for the new batch.
Expand All @@ -96,10 +96,10 @@ constructor(
* must be uploaded with the purpose `batch`. The file can contain up to 50,000 requests,
* and can be up to 200 MB in size.
*/
@JsonProperty("input_file_id") fun inputFileId(): String? = inputFileId
@JsonProperty("input_file_id") fun inputFileId(): String = inputFileId

/** Optional custom metadata for the batch. */
@JsonProperty("metadata") fun metadata(): Metadata? = metadata
@JsonProperty("metadata") fun metadata(): Optional<Metadata> = Optional.ofNullable(metadata)

@JsonAnyGetter
@ExcludeMissing
Expand All @@ -122,11 +122,11 @@ constructor(

@JvmSynthetic
internal fun from(batchCreateBody: BatchCreateBody) = apply {
this.completionWindow = batchCreateBody.completionWindow
this.endpoint = batchCreateBody.endpoint
this.inputFileId = batchCreateBody.inputFileId
this.metadata = batchCreateBody.metadata
additionalProperties(batchCreateBody.additionalProperties)
completionWindow = batchCreateBody.completionWindow
endpoint = batchCreateBody.endpoint
inputFileId = batchCreateBody.inputFileId
metadata = batchCreateBody.metadata
additionalProperties = batchCreateBody.additionalProperties.toMutableMap()
}

/**
Expand Down Expand Up @@ -167,18 +167,24 @@ constructor(

fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
this.additionalProperties.clear()
this.additionalProperties.putAll(additionalProperties)
putAllAdditionalProperties(additionalProperties)
}

@JsonAnySetter
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
this.additionalProperties.put(key, value)
additionalProperties.put(key, value)
}

fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
this.additionalProperties.putAll(additionalProperties)
}

fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }

fun removeAllAdditionalProperties(keys: Set<String>) = apply {
keys.forEach(::removeAdditionalProperty)
}

fun build(): BatchCreateBody =
BatchCreateBody(
checkNotNull(completionWindow) {
Expand Down Expand Up @@ -541,23 +547,29 @@ constructor(

@JvmSynthetic
internal fun from(metadata: Metadata) = apply {
additionalProperties(metadata.additionalProperties)
additionalProperties = metadata.additionalProperties.toMutableMap()
}

fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
this.additionalProperties.clear()
this.additionalProperties.putAll(additionalProperties)
putAllAdditionalProperties(additionalProperties)
}

@JsonAnySetter
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
this.additionalProperties.put(key, value)
additionalProperties.put(key, value)
}

fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
this.additionalProperties.putAll(additionalProperties)
}

fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }

fun removeAllAdditionalProperties(keys: Set<String>) = apply {
keys.forEach(::removeAdditionalProperty)
}

fun build(): Metadata = Metadata(additionalProperties.toImmutable())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ private constructor(
private val additionalProperties: Map<String, JsonValue>,
) {

private var validated: Boolean = false

/** An error code identifying the error type. */
fun code(): Optional<String> = Optional.ofNullable(code.getNullable("code"))

Expand Down Expand Up @@ -56,6 +54,8 @@ private constructor(
@ExcludeMissing
fun _additionalProperties(): Map<String, JsonValue> = additionalProperties

private var validated: Boolean = false

fun validate(): BatchError = apply {
if (!validated) {
code()
Expand Down Expand Up @@ -83,11 +83,11 @@ private constructor(

@JvmSynthetic
internal fun from(batchError: BatchError) = apply {
this.code = batchError.code
this.message = batchError.message
this.param = batchError.param
this.line = batchError.line
additionalProperties(batchError.additionalProperties)
code = batchError.code
message = batchError.message
param = batchError.param
line = batchError.line
additionalProperties = batchError.additionalProperties.toMutableMap()
}

/** An error code identifying the error type. */
Expand Down Expand Up @@ -124,18 +124,24 @@ private constructor(

fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
this.additionalProperties.clear()
this.additionalProperties.putAll(additionalProperties)
putAllAdditionalProperties(additionalProperties)
}

@JsonAnySetter
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
this.additionalProperties.put(key, value)
additionalProperties.put(key, value)
}

fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
this.additionalProperties.putAll(additionalProperties)
}

fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }

fun removeAllAdditionalProperties(keys: Set<String>) = apply {
keys.forEach(::removeAdditionalProperty)
}

fun build(): BatchError =
BatchError(
code,
Expand Down
Loading