-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update openai client generation (#545)
- Loading branch information
1 parent
f502e25
commit 91b282c
Showing
44 changed files
with
959 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package ai.xef.openai | ||
|
||
import kotlin.jvm.JvmInline | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.SerializationException | ||
import kotlinx.serialization.builtins.serializer | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
|
||
@Serializable(with = OpenAIModelSerializer::class) | ||
sealed interface OpenAIModel<T> { | ||
|
||
fun value(): String = | ||
when (this) { | ||
is CustomModel -> model | ||
is StandardModel -> model.toString() | ||
} | ||
} | ||
|
||
@Serializable @JvmInline value class CustomModel<T>(val model: String) : OpenAIModel<T> | ||
|
||
@Serializable @JvmInline value class StandardModel<T>(val model: T) : OpenAIModel<T> | ||
|
||
class OpenAIModelSerializer<T>(private val dataSerializer: KSerializer<T>) : | ||
KSerializer<OpenAIModel<T>> { | ||
override val descriptor: SerialDescriptor = dataSerializer.descriptor | ||
|
||
override fun serialize(encoder: Encoder, value: OpenAIModel<T>) = | ||
when (value) { | ||
is CustomModel<T> -> String.serializer().serialize(encoder, value.model) | ||
is StandardModel<T> -> dataSerializer.serialize(encoder, value.model) | ||
} | ||
|
||
override fun deserialize(decoder: Decoder) = | ||
try { | ||
StandardModel(dataSerializer.deserialize(decoder)) | ||
} catch (e: SerializationException) { | ||
CustomModel(String.serializer().deserialize(decoder)) | ||
} catch (e: IllegalArgumentException) { | ||
CustomModel(String.serializer().deserialize(decoder)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
.../commonMain/kotlin/com/xebia/functional/openai/models/CreateChatCompletionRequestModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* Please note: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
* Do not edit this file manually. | ||
*/ | ||
@file:Suppress("ArrayInDataClass", "EnumEntryName", "RemoveRedundantQualifierName", "UnusedImport") | ||
|
||
package com.xebia.functional.openai.models | ||
|
||
import kotlinx.serialization.* | ||
|
||
/** | ||
* ID of the model to use. See the | ||
* [model endpoint compatibility](/docs/models/model-endpoint-compatibility) table for details on | ||
* which models work with the Chat API. | ||
* | ||
* Values: | ||
* gptMinus4Minus1106MinusPreview,gptMinus4MinusVisionMinusPreview,gptMinus4,gptMinus4Minus0314,gptMinus4Minus0613,gptMinus4Minus32k,gptMinus4Minus32kMinus0314,gptMinus4Minus32kMinus0613,gptMinus3Period5MinusTurbo,gptMinus3Period5MinusTurboMinus16k,gptMinus3Period5MinusTurboMinus0301,gptMinus3Period5MinusTurboMinus0613,gptMinus3Period5MinusTurboMinus16kMinus0613 | ||
*/ | ||
@Serializable | ||
enum class CreateChatCompletionRequestModel(val value: kotlin.String) { | ||
|
||
@SerialName(value = "gpt-4-1106-preview") gptMinus4Minus1106MinusPreview("gpt-4-1106-preview"), | ||
@SerialName(value = "gpt-4-vision-preview") | ||
gptMinus4MinusVisionMinusPreview("gpt-4-vision-preview"), | ||
@SerialName(value = "gpt-4") gptMinus4("gpt-4"), | ||
@SerialName(value = "gpt-4-0314") gptMinus4Minus0314("gpt-4-0314"), | ||
@SerialName(value = "gpt-4-0613") gptMinus4Minus0613("gpt-4-0613"), | ||
@SerialName(value = "gpt-4-32k") gptMinus4Minus32k("gpt-4-32k"), | ||
@SerialName(value = "gpt-4-32k-0314") gptMinus4Minus32kMinus0314("gpt-4-32k-0314"), | ||
@SerialName(value = "gpt-4-32k-0613") gptMinus4Minus32kMinus0613("gpt-4-32k-0613"), | ||
@SerialName(value = "gpt-3.5-turbo") gptMinus3Period5MinusTurbo("gpt-3.5-turbo"), | ||
@SerialName(value = "gpt-3.5-turbo-16k") gptMinus3Period5MinusTurboMinus16k("gpt-3.5-turbo-16k"), | ||
@SerialName(value = "gpt-3.5-turbo-0301") | ||
gptMinus3Period5MinusTurboMinus0301("gpt-3.5-turbo-0301"), | ||
@SerialName(value = "gpt-3.5-turbo-0613") | ||
gptMinus3Period5MinusTurboMinus0613("gpt-3.5-turbo-0613"), | ||
@SerialName(value = "gpt-3.5-turbo-16k-0613") | ||
gptMinus3Period5MinusTurboMinus16kMinus0613("gpt-3.5-turbo-16k-0613"); | ||
|
||
/** | ||
* Override [toString()] to avoid using the enum variable name as the value, and instead use the | ||
* actual value defined in the API spec file. | ||
* | ||
* This solves a problem when the variable name and its value are different, and ensures that the | ||
* client sends the correct enum values to the server always. | ||
*/ | ||
override fun toString(): kotlin.String = value | ||
|
||
companion object { | ||
/** Converts the provided [data] to a [String] on success, null otherwise. */ | ||
fun encode(data: kotlin.Any?): kotlin.String? = | ||
if (data is CreateChatCompletionRequestModel) "$data" else null | ||
|
||
/** Returns a valid [CreateChatCompletionRequestModel] for [data], null otherwise. */ | ||
fun decode(data: kotlin.Any?): CreateChatCompletionRequestModel? = | ||
data?.let { | ||
val normalizedData = "$it".lowercase() | ||
values().firstOrNull { value -> it == value || normalizedData == "$value".lowercase() } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
.../src/commonMain/kotlin/com/xebia/functional/openai/models/CreateCompletionRequestModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Please note: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
* Do not edit this file manually. | ||
*/ | ||
@file:Suppress("ArrayInDataClass", "EnumEntryName", "RemoveRedundantQualifierName", "UnusedImport") | ||
|
||
package com.xebia.functional.openai.models | ||
|
||
import kotlinx.serialization.* | ||
|
||
/** | ||
* ID of the model to use. You can use the [List models](/docs/api-reference/models/list) API to see | ||
* all of your available models, or see our [Model overview](/docs/models/overview) for descriptions | ||
* of them. | ||
* | ||
* Values: | ||
* babbageMinus002,davinciMinus002,gptMinus3Period5MinusTurboMinusInstruct,textMinusDavinciMinus003,textMinusDavinciMinus002,textMinusDavinciMinus001,codeMinusDavinciMinus002,textMinusCurieMinus001,textMinusBabbageMinus001,textMinusAdaMinus001 | ||
*/ | ||
@Serializable | ||
enum class CreateCompletionRequestModel(val value: kotlin.String) { | ||
|
||
@SerialName(value = "babbage-002") babbageMinus002("babbage-002"), | ||
@SerialName(value = "davinci-002") davinciMinus002("davinci-002"), | ||
@SerialName(value = "gpt-3.5-turbo-instruct") | ||
gptMinus3Period5MinusTurboMinusInstruct("gpt-3.5-turbo-instruct"), | ||
@SerialName(value = "text-davinci-003") textMinusDavinciMinus003("text-davinci-003"), | ||
@SerialName(value = "text-davinci-002") textMinusDavinciMinus002("text-davinci-002"), | ||
@SerialName(value = "text-davinci-001") textMinusDavinciMinus001("text-davinci-001"), | ||
@SerialName(value = "code-davinci-002") codeMinusDavinciMinus002("code-davinci-002"), | ||
@SerialName(value = "text-curie-001") textMinusCurieMinus001("text-curie-001"), | ||
@SerialName(value = "text-babbage-001") textMinusBabbageMinus001("text-babbage-001"), | ||
@SerialName(value = "text-ada-001") textMinusAdaMinus001("text-ada-001"); | ||
|
||
/** | ||
* Override [toString()] to avoid using the enum variable name as the value, and instead use the | ||
* actual value defined in the API spec file. | ||
* | ||
* This solves a problem when the variable name and its value are different, and ensures that the | ||
* client sends the correct enum values to the server always. | ||
*/ | ||
override fun toString(): kotlin.String = value | ||
|
||
companion object { | ||
/** Converts the provided [data] to a [String] on success, null otherwise. */ | ||
fun encode(data: kotlin.Any?): kotlin.String? = | ||
if (data is CreateCompletionRequestModel) "$data" else null | ||
|
||
/** Returns a valid [CreateCompletionRequestModel] for [data], null otherwise. */ | ||
fun decode(data: kotlin.Any?): CreateCompletionRequestModel? = | ||
data?.let { | ||
val normalizedData = "$it".lowercase() | ||
values().firstOrNull { value -> it == value || normalizedData == "$value".lowercase() } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...client/src/commonMain/kotlin/com/xebia/functional/openai/models/CreateEditRequestModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Please note: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
* Do not edit this file manually. | ||
*/ | ||
@file:Suppress("ArrayInDataClass", "EnumEntryName", "RemoveRedundantQualifierName", "UnusedImport") | ||
|
||
package com.xebia.functional.openai.models | ||
|
||
import kotlinx.serialization.* | ||
|
||
/** | ||
* ID of the model to use. You can use the `text-davinci-edit-001` or `code-davinci-edit-001` model | ||
* with this endpoint. | ||
* | ||
* Values: textMinusDavinciMinusEditMinus001,codeMinusDavinciMinusEditMinus001 | ||
*/ | ||
@Serializable | ||
enum class CreateEditRequestModel(val value: kotlin.String) { | ||
|
||
@SerialName(value = "text-davinci-edit-001") | ||
textMinusDavinciMinusEditMinus001("text-davinci-edit-001"), | ||
@SerialName(value = "code-davinci-edit-001") | ||
codeMinusDavinciMinusEditMinus001("code-davinci-edit-001"); | ||
|
||
/** | ||
* Override [toString()] to avoid using the enum variable name as the value, and instead use the | ||
* actual value defined in the API spec file. | ||
* | ||
* This solves a problem when the variable name and its value are different, and ensures that the | ||
* client sends the correct enum values to the server always. | ||
*/ | ||
override fun toString(): kotlin.String = value | ||
|
||
companion object { | ||
/** Converts the provided [data] to a [String] on success, null otherwise. */ | ||
fun encode(data: kotlin.Any?): kotlin.String? = | ||
if (data is CreateEditRequestModel) "$data" else null | ||
|
||
/** Returns a valid [CreateEditRequestModel] for [data], null otherwise. */ | ||
fun decode(data: kotlin.Any?): CreateEditRequestModel? = | ||
data?.let { | ||
val normalizedData = "$it".lowercase() | ||
values().firstOrNull { value -> it == value || normalizedData == "$value".lowercase() } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.