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

Supporting SSE #7

Merged
merged 2 commits into from
Apr 9, 2023
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
28 changes: 16 additions & 12 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ plugins {
id("org.jetbrains.kotlin.jvm") version Versions.kotlinVersion
}

group = "mobi.appcent"
version = "1.0.0"
group = AppConfig.groupId
version = AppConfig.version

repositories {
mavenCentral()
Expand All @@ -26,16 +26,20 @@ repositories {
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${Versions.kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-reflect:${Versions.kotlinVersion}")
implementation(Deps.kotlinJdk8)
implementation(Deps.kotlinReflect)

//OkHtpp
implementation(platform("com.squareup.okhttp3:okhttp-bom:4.10.0"))
implementation("com.squareup.okhttp3:okhttp")
implementation("com.squareup.okhttp3:logging-interceptor")
implementation(platform(Deps.okhttpBom))
implementation(Deps.okhttp)
implementation(Deps.okhttpLogging)
implementation(Deps.okhttpSSE)

//Gson
implementation("com.google.code.gson:gson:2.10.1")
implementation(Deps.gson)

//Coroutines
implementation(Deps.coroutines)
}

java {
Expand All @@ -54,15 +58,15 @@ tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach
publishing {
publications {
create<MavenPublication>("maven") {
groupId = "mobi.appcent"
artifactId = "acmopenai-android"
version = "1.0.0"
groupId = AppConfig.groupId
artifactId = AppConfig.artifactId
version = AppConfig.version

from(components["java"])

pom {
packaging = "jar"
name.set("ACMOpenAI-Android")
name.set(AppConfig.name)
scm {
url.set("https://github.com/AppcentMobile/ACMOpenAI-Android")
}
Expand Down
9 changes: 9 additions & 0 deletions buildSrc/src/main/java/AppConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Created by erenalpaslan on 8.04.2023
*/
object AppConfig {
const val groupId: String = "mobi.appcent"
const val artifactId: String = "acmopenai-android"
const val name: String = "ACMOpenAI-Android"
const val version: String = "1.0.0"
}
26 changes: 26 additions & 0 deletions buildSrc/src/main/java/Deps.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Created by erenalpaslan on 8.04.2023
*/
object Versions {
const val kotlinVersion = "1.5.31"
const val okhttpBomVersion = "4.10.0"
const val gsonVersion = "2.10.1"
const val coroutinesVersion = "1.5.2"
}

object Deps {
//Kotlin
const val kotlinJdk8 = "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${Versions.kotlinVersion}"
const val kotlinReflect = "org.jetbrains.kotlin:kotlin-reflect:${Versions.kotlinVersion}"
const val coroutines = "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.coroutinesVersion}"

//OkHttp
const val okhttpBom = "com.squareup.okhttp3:okhttp-bom:${Versions.okhttpBomVersion}"
const val okhttp = "com.squareup.okhttp3:okhttp"
const val okhttpLogging = "com.squareup.okhttp3:logging-interceptor"
const val okhttpSSE = "com.squareup.okhttp3:okhttp-sse"

//Serialization
const val gson = "com.google.code.gson:gson:${Versions.gsonVersion}"

}
3 changes: 0 additions & 3 deletions buildSrc/src/main/java/Versions.kt

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/kotlin/mobi/appcent/openai/apis/AudioApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class AudioApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as CreateTranscriptionResponse
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand Down Expand Up @@ -79,8 +77,6 @@ class AudioApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as CreateTranslationResponse
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand Down
43 changes: 32 additions & 11 deletions src/main/kotlin/mobi/appcent/openai/apis/ChatApi.kt
Original file line number Diff line number Diff line change
@@ -1,38 +1,59 @@
package mobi.appcent.openai.apis

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import mobi.appcent.openai.common.UrlConstant
import mobi.appcent.openai.infrastructure.*
import mobi.appcent.openai.models.CreateChatCompletionRequest
import mobi.appcent.openai.models.CreateChatCompletionResponse
import mobi.appcent.openai.models.CreateChatCompletionStreamResponse
import mobi.appcent.openai.models.CreateCompletionResponse
import mobi.appcent.openai.models.mapper.CreateChatCompletionStreamResponseMapper

/**
* Created by erenalpaslan on 5.04.2023
*/
class ChatApi: BaseApi() {
class ChatApi : BaseApi() {

/**
* Creates a completion for the chat message
*
* @param body
* @return CreateChatCompletionResponse
*/
fun createChatCompletion(body: CreateChatCompletionRequest): CreateChatCompletionResponse {
fun createChatCompletion(body: CreateChatCompletionRequest): Flow<CreateChatCompletionResponse?> {
val localVariableBody: CreateChatCompletionRequest = body

val localVariableConfig = RequestConfig(
RequestMethod.POST,
UrlConstant.CHAT_COMPLETION_URL
)
val response = apiClient.request<CreateChatCompletionResponse>(
localVariableConfig, localVariableBody
)

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as CreateChatCompletionResponse
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
return if (localVariableBody.stream == true) {
apiClient.collectEvent<CreateChatCompletionStreamResponse>(
localVariableConfig, localVariableBody
).map { response ->
return@map CreateChatCompletionStreamResponseMapper.transform(response)
}
}else {
val response = apiClient.request<CreateChatCompletionResponse>(
localVariableConfig,
localVariableBody
)
flow {
when (response.responseType) {
ResponseType.Success -> emit((response as Success<*>).data as CreateChatCompletionResponse)
ResponseType.ClientError -> throw ClientException(
(response as ClientError<*>).body as? String ?: "Client error"
)

ResponseType.ServerError -> throw ServerException(
(response as ServerError<*>).message ?: "Server error"
)
}
}
}
}

Expand Down
29 changes: 19 additions & 10 deletions src/main/kotlin/mobi/appcent/openai/apis/CompletionApi.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mobi.appcent.openai.apis

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import mobi.appcent.openai.common.UrlConstant
import mobi.appcent.openai.infrastructure.*
import mobi.appcent.openai.models.CreateCompletionRequest
Expand All @@ -16,23 +18,30 @@ class CompletionApi: BaseApi() {
* @param body
* @return CreateCompletionResponse
*/
fun createCompletion(body: CreateCompletionRequest): CreateCompletionResponse {
fun createCompletion(body: CreateCompletionRequest): Flow<CreateCompletionResponse?> {
val localVariableBody: CreateCompletionRequest = body

val localVariableConfig = RequestConfig(
RequestMethod.POST,
UrlConstant.COMPLETION_URL
)
val response = apiClient.request<CreateCompletionResponse>(
localVariableConfig, localVariableBody
)

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as CreateCompletionResponse
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
return if (localVariableBody.stream == true) {
apiClient.collectEvent<CreateCompletionResponse>(
localVariableConfig, localVariableBody
)
}else {
flow<CreateCompletionResponse> {
val response = apiClient.request<CreateCompletionResponse>(
localVariableConfig, localVariableBody
)

when (response.responseType) {
ResponseType.Success -> emit((response as Success<*>).data as CreateCompletionResponse)
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/mobi/appcent/openai/apis/EditApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class EditApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as CreateEditResponse
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/mobi/appcent/openai/apis/EmbeddingApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ class EmbeddingApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as CreateEmbeddingResponse
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand Down
4 changes: 0 additions & 4 deletions src/main/kotlin/mobi/appcent/openai/apis/EngineApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class EngineApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as ListEnginesResponse
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand All @@ -52,8 +50,6 @@ class EngineApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as Engine
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand Down
10 changes: 0 additions & 10 deletions src/main/kotlin/mobi/appcent/openai/apis/FilesApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class FilesApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as ListFilesResponse
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand Down Expand Up @@ -60,8 +58,6 @@ class FilesApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as OpenAIFile
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand All @@ -85,8 +81,6 @@ class FilesApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as DeleteFileResponse
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand All @@ -110,8 +104,6 @@ class FilesApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as OpenAIFile
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand All @@ -135,8 +127,6 @@ class FilesApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as kotlin.String
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand Down
12 changes: 0 additions & 12 deletions src/main/kotlin/mobi/appcent/openai/apis/FineTuneApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class FineTuneApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as FineTune
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand All @@ -52,8 +50,6 @@ class FineTuneApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as ListFineTunesResponse
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand All @@ -77,8 +73,6 @@ class FineTuneApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as FineTune
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand All @@ -102,8 +96,6 @@ class FineTuneApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as FineTune
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand All @@ -128,8 +120,6 @@ class FineTuneApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as ListFineTuneEventsResponse
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand All @@ -153,8 +143,6 @@ class FineTuneApi: BaseApi() {

return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as Model
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
}
Expand Down
Loading