Skip to content

Commit 7d4cbf0

Browse files
committed
refactor: replace static OpenAiInitializer with OpenAiConfig and migrate API calls to coroutine suspend functions.
1 parent 81aedd6 commit 7d4cbf0

File tree

9 files changed

+201
-588
lines changed

9 files changed

+201
-588
lines changed

README.md

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,20 @@ dependencies {
2828
~~~
2929

3030
## Getting started
31-
1. Initialize the client by calling the following function. You only need to do this once, ideally, in your Application class using environment variables for the API_KEY.
32-
~~~kotlin
33-
OpenAiInitializer.initialize("API_KEY")
34-
~~~
35-
2. Create an instance of `OpenAiClient`
31+
1. Create an instance of `OpenAiConfig` and `OpenApiClient`:
3632
```kotlin
37-
val client = OpenApiClient()
33+
val config = OpenAiConfig(apiKey = "YOUR_API_KEY")
34+
val client = OpenApiClient(config)
3835
```
39-
3. Make a request
36+
2. Make a request using Coroutines:
4037
```kotlin
41-
client.getTextCompletion("Hello chat gpt! what is the meaning of life?") { result, error ->
42-
if (error != null) {
43-
// Handle error
44-
} else if (result != null) {
38+
lifecycleScope.launch {
39+
try {
40+
val result = client.getTextCompletion("Hello chat gpt! what is the meaning of life?")
4541
Log.d("TAG", result.choices[0].text)
42+
} catch (e: Exception) {
43+
// Handle error
44+
e.printStackTrace()
4645
}
4746
}
4847
```
@@ -68,8 +67,6 @@ client.getTextCompletion("Hello chat gpt! what is the meaning of life?") { resul
6867
- [x] Fine-tuning
6968
- [x] Completions (Legacy)
7069
- [x] Edits (Legacy)
71-
72-
## Upcoming APIs
7370
- [x] Streaming for Assistants
7471

7572
## License

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@ dependencies {
5252
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
5353
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
5454

55+
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
5556
implementation project(path: ':openai')
5657
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.mardillu.openai.test
22

33
import android.app.Application
4-
import com.mardillu.openai.OpenAiInitializer
54

65
/**
76
* Created on 26/03/2023 at 5:49 PM
@@ -11,6 +10,5 @@ import com.mardillu.openai.OpenAiInitializer
1110
class App : Application() {
1211
override fun onCreate() {
1312
super.onCreate()
14-
OpenAiInitializer.initialize(getString(R.string.OPEN_AI_API_KEY))
1513
}
1614
}

app/src/main/java/com/mardillu/openai/test/MainActivity.kt

Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package com.mardillu.openai.test
33
import android.os.Bundle
44
import android.util.Log
55
import androidx.appcompat.app.AppCompatActivity
6+
import androidx.lifecycle.lifecycleScope
7+
import com.mardillu.openai.OpenAiConfig
68
import com.mardillu.openai.model.Message
7-
import com.mardillu.openai.network.LoggingApiService
8-
import com.mardillu.openai.network.LoggingClient
99
import com.mardillu.openai.network.OpenApiClient
1010
import com.mardillu.openai.test.databinding.ActivityMainBinding
11+
import kotlinx.coroutines.launch
1112
import java.io.File
1213

1314
class MainActivity : AppCompatActivity() {
@@ -19,109 +20,103 @@ class MainActivity : AppCompatActivity() {
1920
binding = ActivityMainBinding.inflate(layoutInflater)
2021
setContentView(binding.root)
2122

22-
val chatGptService = OpenApiClient()
23-
val loggingApiService = LoggingClient()
24-
25-
// loggingApiService.logRequestTime(
26-
// "test/test",
27-
// 200,
28-
// 500,
29-
// 300,
30-
// 200,
31-
// ){ result, error ->
32-
// if (error != null){
33-
// Log.d("TAG", "onCreate: =======> FAILED <============")
34-
// error.printStackTrace()
35-
// } else {
36-
// Log.d("TAG", "onCreate: =======> SUCCESS <============")
37-
// }
38-
// }
39-
40-
chatGptService.getTextCompletion("Hello chat gpt! what is the meaning of life?") { result, error ->
41-
if (error != null) {
42-
// Handle error
43-
} else if (result != null) {
23+
// Initialize config (Replace with actual key or load from secure storage)
24+
val config = OpenAiConfig(apiKey = "YOUR_API_KEY_HERE")
25+
val chatGptService = OpenApiClient(config)
26+
27+
lifecycleScope.launch {
28+
try {
29+
val result = chatGptService.getTextCompletion("Hello chat gpt! what is the meaning of life?")
4430
binding.result1.text = result.choices[0].text
31+
} catch (e: Exception) {
32+
Log.e("MainActivity", "Error in getTextCompletion", e)
4533
}
4634
}
4735

48-
chatGptService.getChatCompletion(messages = listOf(Message("user", "What is the update with your weekly PR review"))) { result, error ->
49-
if (error != null) {
50-
// Handle error
51-
} else if (result != null) {
36+
lifecycleScope.launch {
37+
try {
38+
val result = chatGptService.getChatCompletion(messages = listOf(Message("user", "What is the update with your weekly PR review")))
5239
binding.result2.text = result.choices[0].message.content
40+
} catch (e: Exception) {
41+
Log.e("MainActivity", "Error in getChatCompletion", e)
5342
}
5443
}
5544

56-
chatGptService.getEditCompletionAlt(input = "What day of the wek is it?", instruction = "Fix the spelling mistakes") { result, error ->
57-
if (error != null) {
58-
// Handle error
59-
} else if (result != null) {
45+
lifecycleScope.launch {
46+
try {
47+
val result = chatGptService.getEditCompletion(input = "What day of the wek is it?", instruction = "Fix the spelling mistakes")
6048
binding.result3.text = result.choices[0].text
49+
} catch (e: Exception) {
50+
Log.e("MainActivity", "Error in getEditCompletion", e)
6151
}
6252
}
6353

64-
chatGptService.getEmbeddings("Hello chat gpt! what is the meaning of life?") { result, error ->
65-
if (error != null) {
66-
// Handle error
67-
} else if (result != null) {
54+
lifecycleScope.launch {
55+
try {
56+
val result = chatGptService.getEmbeddings("Hello chat gpt! what is the meaning of life?")
6857
binding.result4.text = result.data[0].embedding.size.toString()
58+
} catch (e: Exception) {
59+
Log.e("MainActivity", "Error in getEmbeddings", e)
6960
}
7061
}
7162

72-
chatGptService.createImage("A cute baby sea otter") { result, error ->
73-
if (error != null) {
74-
// Handle error
75-
} else if (result != null) {
63+
lifecycleScope.launch {
64+
try {
65+
val result = chatGptService.createImage("A cute baby sea otter")
7666
binding.result5.text = result.data[0].url
67+
} catch (e: Exception) {
68+
Log.e("MainActivity", "Error in createImage", e)
7769
}
7870
}
7971

80-
chatGptService.getModeration("I want to kill them.") { result, error ->
81-
if (error != null) {
82-
// Handle error
83-
} else if (result != null) {
72+
lifecycleScope.launch {
73+
try {
74+
val result = chatGptService.getModeration("I want to kill them.")
8475
binding.result6.text = result.results[0].categories.hate.toString()
76+
} catch (e: Exception) {
77+
Log.e("MainActivity", "Error in getModeration", e)
8578
}
8679
}
8780

88-
chatGptService.createImageEdit(
89-
imageFromAssets("img.png"),
90-
"A cute cat sitting on a white table",
91-
imageFromAssets("img.png")
92-
) { result, error ->
93-
if (error != null) {
94-
// Handle error
95-
} else if (result != null) {
81+
lifecycleScope.launch {
82+
try {
83+
val result = chatGptService.createImageEdit(
84+
imageFromAssets("img.png"),
85+
"A cute cat sitting on a white table",
86+
imageFromAssets("img.png")
87+
)
9688
binding.result7.text = result.data[0].url
89+
} catch (e: Exception) {
90+
Log.e("MainActivity", "Error in createImageEdit", e)
9791
}
9892
}
9993

100-
chatGptService.createImageVariation(imageFromAssets("img.png")) { result, error ->
101-
if (error != null) {
102-
// Handle error
103-
} else if (result != null) {
94+
lifecycleScope.launch {
95+
try {
96+
val result = chatGptService.createImageVariation(imageFromAssets("img.png"))
10497
binding.result8.text = result.data[0].url
98+
} catch (e: Exception) {
99+
Log.e("MainActivity", "Error in createImageVariation", e)
105100
}
106101
}
107102

108-
chatGptService.createTranscription(imageFromAssets("audio.m4a")) { result, error ->
109-
if (error != null) {
110-
// Handle error
111-
} else if (result != null) {
103+
lifecycleScope.launch {
104+
try {
105+
val result = chatGptService.createTranscription(imageFromAssets("audio.m4a"))
112106
binding.result9.text = result.text
107+
} catch (e: Exception) {
108+
Log.e("MainActivity", "Error in createTranscription", e)
113109
}
114110
}
115111

116-
chatGptService.createTranslation(imageFromAssets("audio.m4a")) { result, error ->
117-
if (error != null) {
118-
// Handle error
119-
} else if (result != null) {
112+
lifecycleScope.launch {
113+
try {
114+
val result = chatGptService.createTranslation(imageFromAssets("audio.m4a"))
120115
binding.result10.text = result.text
116+
} catch (e: Exception) {
117+
Log.e("MainActivity", "Error in createTranslation", e)
121118
}
122119
}
123-
124-
125120
}
126121

127122
private fun imageFromAssets(name: String): File {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.mardillu.openai
2+
3+
import java.util.concurrent.TimeUnit
4+
5+
data class OpenAiConfig(
6+
val apiKey: String,
7+
val baseUrl: String = "https://api.openai.com/v1/",
8+
val timeout: Long = 60L,
9+
val timeUnit: TimeUnit = TimeUnit.SECONDS
10+
)

openai/src/main/java/com/mardillu/openai/OpenAiInitializer.kt

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)