Skip to content

Commit a1b8729

Browse files
committed
fix(decoder): using regex for lookup response stream from open ai api
1 parent 13777fb commit a1b8729

File tree

6 files changed

+31
-37
lines changed

6 files changed

+31
-37
lines changed

.idea/compiler.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/kotlinc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/chatgptlite/wanted/data/remote/OpenAIRepositoryImpl.kt

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.chatgptlite.wanted.data.remote
22

3+
import android.util.Log
34
import com.chatgptlite.wanted.constants.matchResultString
4-
import com.chatgptlite.wanted.constants.matchResultTurboString
55
import com.chatgptlite.wanted.data.api.OpenAIApi
66
import com.chatgptlite.wanted.models.TextCompletionsParam
77
import com.chatgptlite.wanted.models.toJson
@@ -24,7 +24,7 @@ class OpenAIRepositoryImpl @Inject constructor(
2424
override fun textCompletionsWithStream(params: TextCompletionsParam): Flow<String> =
2525
callbackFlow {
2626
withContext(Dispatchers.IO) {
27-
val response = (if (params.isTurbo) openAIApi.textCompletionsTurboWithStream(
27+
val response = (if (params.isChatCompletions) openAIApi.textCompletionsTurboWithStream(
2828
params.toJson()
2929
) else openAIApi.textCompletionsWithStream(params.toJson())).execute()
3030

@@ -41,20 +41,21 @@ class OpenAIRepositoryImpl @Inject constructor(
4141
try {
4242
// Handle & convert data -> emit to client
4343
val value =
44-
if (params.isTurbo) lookupDataFromResponseTurbo(line) else lookupDataFromResponse(
44+
if (params.isChatCompletions) lookupDataFromResponseTurbo(line) else lookupDataFromResponse(
4545
line
4646
)
4747

4848
if (value.isNotEmpty()) {
4949
trySend(value)
5050
}
5151
} catch (e: Exception) {
52-
5352
e.printStackTrace()
53+
Log.e("ChatGPT Lite BUG", e.toString())
5454
}
5555
}
5656
}
5757
} catch (e: IOException) {
58+
Log.e("ChatGPT Lite BUG", e.toString())
5859
throw Exception(e)
5960
} finally {
6061
withContext(Dispatchers.IO) {
@@ -69,11 +70,12 @@ class OpenAIRepositoryImpl @Inject constructor(
6970
try {
7071
jsonObject = JSONObject(response.errorBody()!!.string())
7172
println(jsonObject)
73+
trySend("Failure! Try again. $jsonObject")
7274
} catch (e: JSONException) {
7375
e.printStackTrace()
7476
}
7577
}
76-
trySend("Failure! Try again.")
78+
trySend("Failure! Try again")
7779
close()
7880
}
7981
}
@@ -111,31 +113,13 @@ class OpenAIRepositoryImpl @Inject constructor(
111113
}
112114

113115
private fun lookupDataFromResponseTurbo(jsonString: String): String {
114-
val splitsJsonString = jsonString.split("[{")
116+
val regex = """"content"\s*:\s*"([^"]+)"""".toRegex()
117+
val matchResult = regex.find(jsonString)
115118

116-
val indexOfResult: Int = splitsJsonString.indexOfLast {
117-
it.contains(matchResultTurboString)
119+
if (matchResult != null && matchResult.groupValues.size > 1) {
120+
return matchResult.groupValues[1]
118121
}
119122

120-
val textSplits =
121-
if (indexOfResult == -1) listOf() else splitsJsonString[indexOfResult].split(",")
122-
123-
val indexOfText: Int = textSplits.indexOfLast {
124-
it.contains(matchResultTurboString)
125-
}
126-
127-
if (indexOfText != -1) {
128-
try {
129-
val gson = Gson()
130-
val jsonObject =
131-
gson.fromJson("{${textSplits[indexOfText]}}", JsonObject::class.java)
132-
133-
return jsonObject.getAsJsonObject("delta").get("content").asString
134-
} catch (e: java.lang.Exception) {
135-
println(e.localizedMessage)
136-
}
137-
}
138-
139-
return ""
123+
return " "
140124
}
141125
}

app/src/main/java/com/chatgptlite/wanted/models/GPTModel.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.chatgptlite.wanted.models
22

3-
enum class GPTModel(val model: String, val maxTokens: Int) {
4-
gpt35Turbo("gpt-3.5-turbo", 4000),
5-
davinci("text-davinci-003", 4000),
3+
enum class GPTModel(val model: String, val maxTokens: Int, val isChatCompletion: Boolean = false) {
4+
gpt4("gpt-4", 8192, isChatCompletion = true),
5+
gpt4v0613("gpt-4-0613", 8192, isChatCompletion = true),
6+
gpt4p32k("gpt-4-32k", 32768, isChatCompletion = true),
7+
gpt4p32kv0613("gpt-4-32k-0613", 32768, isChatCompletion = true),
8+
gpt35Turbo("gpt-3.5-turbo", 4096),
9+
davinci("text-davinci-003", 4096),
610
curie("text-curie-001", 2048),
711
babbage("text-babbage-001", 2048),
812
ada("text-ada-001", 2048)

app/src/main/java/com/chatgptlite/wanted/models/TextCompletionsParam.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ data class TextCompletionsParam(
1818
@SerializedName("maxTokens")
1919
val maxTokens: Int = 2048,
2020
@SerializedName("model")
21-
val model: GPTModel = GPTModel.gpt35Turbo,
21+
val model: GPTModel = GPTModel.gpt4,
2222
@SerializedName("messages")
2323
val messagesTurbo: List<MessageTurbo> = emptyList(),
2424
) {
@@ -52,8 +52,8 @@ data class TextCompletionsParam(
5252
return result
5353
}
5454

55-
val isTurbo: Boolean
56-
get() = model == GPTModel.gpt35Turbo
55+
val isChatCompletions: Boolean
56+
get() = model.isChatCompletion
5757
}
5858

5959
fun TextCompletionsParam.toJson(): JsonObject {
@@ -62,7 +62,7 @@ fun TextCompletionsParam.toJson(): JsonObject {
6262
json.addProperty("stream", stream)
6363
json.addProperty("model", model.model)
6464

65-
if (model == GPTModel.gpt35Turbo) {
65+
if (isChatCompletions) {
6666
val jsonArray = JsonArray()
6767
for (message in messagesTurbo) jsonArray.add(message.toJson())
6868

0 commit comments

Comments
 (0)