-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrated Library to use Kotlin Ktor instead of Retrofit.
- Loading branch information
1 parent
6322374
commit 5e0877a
Showing
8 changed files
with
89 additions
and
121 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
18 changes: 0 additions & 18 deletions
18
ApiLibrary/src/main/java/com/example/apilibrary/library/Interceptor.kt
This file was deleted.
Oops, something went wrong.
21 changes: 7 additions & 14 deletions
21
ApiLibrary/src/main/java/com/example/apilibrary/library/ModelClass.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 |
---|---|---|
@@ -1,21 +1,14 @@ | ||
package com.example.apilibrary.library | ||
|
||
import com.squareup.moshi.Json | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
internal data class PromptChat( | ||
@Json(name = "history") val history : Array<String>, | ||
@Json(name = "user_input") val user_input : String | ||
val history : Array<String>, | ||
val user_input : String | ||
) | ||
|
||
@Serializable | ||
internal data class Prompt( | ||
@Json(name = "user_input") val user_input : String | ||
) | ||
|
||
data class PromptChatCompletion( | ||
@Json(name = "message") val message : String | ||
) | ||
|
||
data class PromptCompletion( | ||
@Json(name = "message") val message : String | ||
) | ||
|
||
val user_input : String | ||
) |
91 changes: 60 additions & 31 deletions
91
ApiLibrary/src/main/java/com/example/apilibrary/library/OpenAiClient.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 |
---|---|---|
@@ -1,33 +1,62 @@ | ||
package com.example.apilibrary.library | ||
|
||
import com.squareup.moshi.Moshi | ||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.moshi.MoshiConverterFactory | ||
import java.util.concurrent.TimeUnit | ||
|
||
internal object OpenAiClient { | ||
|
||
private val BASE_URL = "https://spitfire-interractions.onrender.com/" | ||
|
||
private val okhttpClient = OkHttpClient.Builder() | ||
.readTimeout(3000, TimeUnit.MILLISECONDS) | ||
.connectTimeout(2000, TimeUnit.MILLISECONDS) | ||
.addInterceptor(MyInterceptor()) | ||
.addInterceptor(HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY }) | ||
.build() | ||
|
||
private val moshi = Moshi.Builder() | ||
.add(KotlinJsonAdapterFactory()) | ||
.build() | ||
|
||
private val retrofit = Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.addConverterFactory(MoshiConverterFactory.create(moshi)) | ||
.client(okhttpClient) | ||
.build() | ||
|
||
val service: OpenAiService = retrofit.create(OpenAiService::class.java) | ||
} | ||
import android.util.Log | ||
import io.ktor.client.* | ||
import io.ktor.client.call.* | ||
import io.ktor.client.engine.cio.* | ||
import io.ktor.client.plugins.* | ||
import io.ktor.client.plugins.contentnegotiation.* | ||
import io.ktor.client.plugins.logging.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.serialization.kotlinx.json.* | ||
import kotlinx.serialization.json.Json | ||
|
||
private const val TAG = "OpenAiClient" | ||
|
||
internal class OpenAiClient { | ||
|
||
val client = HttpClient(CIO) { | ||
install(ContentNegotiation) { | ||
json(Json{ | ||
prettyPrint = true | ||
isLenient = true | ||
}) | ||
} | ||
|
||
install(Logging) { | ||
logger = object : Logger { | ||
override fun log(message: String) { | ||
Log.d(TAG, "Network Message, log: $message") | ||
} | ||
} | ||
level=LogLevel.ALL | ||
} | ||
|
||
install(HttpTimeout) { | ||
socketTimeoutMillis = 10_000 | ||
requestTimeoutMillis = 10_000 | ||
connectTimeoutMillis = 10_000 | ||
} | ||
} | ||
|
||
suspend fun getUserChatResponse(userPrompt: String, userPromptChat : Array<String>, userId : String) : String { | ||
val response : HttpResponse = client.post("https://spitfire-interractions.onrender.com/api/chat/completions/") { | ||
header("Accept", "application/json") | ||
header("Content-Type", "application/json") | ||
header("Cookie", userId) | ||
setBody(PromptChat(history = userPromptChat, user_input = userPrompt)) | ||
} | ||
return response.body() | ||
} | ||
|
||
suspend fun getChatResponse(userPrompt: String, userId : String) : String { | ||
val response : HttpResponse = client.post("https://spitfire-interractions.onrender.com/api/chat/") { | ||
header("Accept", "application/json") | ||
header("Content-Type", "application/json") | ||
header("Cookie", userId) | ||
setBody(Prompt(user_input = userPrompt)) | ||
} | ||
return response.body() | ||
} | ||
} |
15 changes: 0 additions & 15 deletions
15
ApiLibrary/src/main/java/com/example/apilibrary/library/OpenAiService.kt
This file was deleted.
Oops, something went wrong.
24 changes: 10 additions & 14 deletions
24
ApiLibrary/src/main/java/com/example/apilibrary/wrapperclass/OpenAiCaller.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
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