Skip to content

Commit

Permalink
feat: Added image create method for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
mucahit-tekin committed Nov 2, 2023
1 parent 802f6f1 commit a6e3a46
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.aallam.openai.api.chat.ChatCompletionRequest
import com.aallam.openai.api.chat.ChatMessage
import com.aallam.openai.api.chat.ChatRole
import com.aallam.openai.api.http.Timeout
import com.aallam.openai.api.image.ImageCreation
import com.aallam.openai.api.image.ImageSize
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.client.OpenAI
import com.aallam.openai.client.OpenAIConfig
Expand All @@ -26,6 +28,7 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.json.JSONObject
import java.util.Date
import java.util.HashMap
import kotlin.time.Duration.Companion.seconds

Expand Down Expand Up @@ -209,6 +212,31 @@ class ReactNativeOpenaiModule(reactContext: ReactApplicationContext) :
}
}

@ReactMethod
public fun imageCreate(input: ReadableMap,promise: Promise){
val prompt = input.getString("prompt") as String;
val n = if (input.hasKey("n")) input.getInt("n") else null
val size = if (input.hasKey("n")) input.getString("size") else null

runBlocking {
job = scope.launch {
var imageResult = openAIClient?.imageURL(creation = ImageCreation(prompt,n, ImageSize(size ?: "512x512")))
val map = mapOf(
"created" to Date().time,
"data" to (imageResult?.map {
mapOf(
"url" to it.url
)
} ?: emptyList())
)
val toReadableMap = Arguments.makeNativeMap(map)
promise.resolve(toReadableMap)
}

}

}

private fun toList(array: ReadableArray?): List<String> {
val list = mutableListOf<String>()
if (array != null) {
Expand Down

0 comments on commit a6e3a46

Please sign in to comment.