From 1fd8d469a58371e8a79e1689ec07dc0d891bdc0f Mon Sep 17 00:00:00 2001 From: slam Date: Mon, 12 Feb 2024 16:28:11 +0800 Subject: [PATCH] feat(images-api): implement images api - https://platform.openai.com/docs/api-reference/images test for github action add fun create( prompt: String, model: Model? = null, ) --- .../openai-client-core/build.gradle.kts | 2 +- .../com/tddworks/openai/api/chat/api/Chat.kt | 2 ++ .../openai/api/images/api/ImageCreate.kt | 21 +++++++++++++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/openai-client/openai-client-core/build.gradle.kts b/openai-client/openai-client-core/build.gradle.kts index 3003c01..61fb0f9 100644 --- a/openai-client/openai-client-core/build.gradle.kts +++ b/openai-client/openai-client-core/build.gradle.kts @@ -2,7 +2,7 @@ plugins { // alias(libs.plugins.kotlinMultiplatform) alias(libs.plugins.kotlinx.serialization) alias(libs.plugins.kover) - alias(libs.plugins.touchlab.kmmbridge) +// alias(libs.plugins.touchlab.kmmbridge) // id("module.publication") `maven-publish` } diff --git a/openai-client/openai-client-core/src/commonMain/kotlin/com/tddworks/openai/api/chat/api/Chat.kt b/openai-client/openai-client-core/src/commonMain/kotlin/com/tddworks/openai/api/chat/api/Chat.kt index 3257b9f..7ed3b5d 100644 --- a/openai-client/openai-client-core/src/commonMain/kotlin/com/tddworks/openai/api/chat/api/Chat.kt +++ b/openai-client/openai-client-core/src/commonMain/kotlin/com/tddworks/openai/api/chat/api/Chat.kt @@ -1,12 +1,14 @@ package com.tddworks.openai.api.chat.api import kotlinx.coroutines.flow.Flow +import kotlinx.serialization.ExperimentalSerializationApi /** * Chat API - https://platform.openai.com/docs/api-reference/chat * Given a list of messages comprising a conversation, the model will return a response. * Related guide: Chat Completions */ +@OptIn(ExperimentalSerializationApi::class) interface Chat { /** * Create a chat completion. diff --git a/openai-client/openai-client-core/src/commonMain/kotlin/com/tddworks/openai/api/images/api/ImageCreate.kt b/openai-client/openai-client-core/src/commonMain/kotlin/com/tddworks/openai/api/images/api/ImageCreate.kt index db5389f..5b97b53 100644 --- a/openai-client/openai-client-core/src/commonMain/kotlin/com/tddworks/openai/api/images/api/ImageCreate.kt +++ b/openai-client/openai-client-core/src/commonMain/kotlin/com/tddworks/openai/api/images/api/ImageCreate.kt @@ -1,6 +1,7 @@ package com.tddworks.openai.api.images.api import com.tddworks.openai.api.chat.api.Model +import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @@ -9,7 +10,8 @@ import kotlinx.serialization.Serializable * @see [ImageCreate](https://platform.openai.com/docs/api-reference/images/create) */ @Serializable -class ImageCreate( +@ExperimentalSerializationApi +data class ImageCreate( /** * A text description of the desired image(s). The maximum length is 1000 characters for dall-e-2 and 4000 characters for dall-e-3. */ @@ -67,4 +69,19 @@ class ImageCreate( */ @SerialName("quality") val quality: Quality? = null, -) \ No newline at end of file +) { + companion object { + /** + * Create an instance of [ImageCreate]. + * @param prompt A text description of the desired image(s). The maximum length is 1000 characters for dall-e-2 and 4000 characters for dall-e-3. + * @param model The model to use for image generation. Defaults to dall-e-2 + */ + fun create( + prompt: String, + model: Model? = null, + ): ImageCreate = ImageCreate( + prompt = prompt, + model = model + ) + } +} \ No newline at end of file