11package com.chatgptlite.wanted.data.remote
22
3+ import com.chatgptlite.wanted.constants.matchResultString
34import com.chatgptlite.wanted.constants.matchResultTurboString
45import com.chatgptlite.wanted.data.api.OpenAIApi
56import com.chatgptlite.wanted.models.TextCompletionsParam
@@ -20,58 +21,94 @@ import javax.inject.Inject
2021@Suppress(" UNREACHABLE_CODE" )
2122class OpenAIRepositoryImpl @Inject constructor(
2223 private val openAIApi : OpenAIApi ,
23- ): OpenAIRepository {
24- override fun textCompletionsWithStream (params : TextCompletionsParam ): Flow <String > = callbackFlow {
25- withContext(Dispatchers .IO ) {
26- val response = openAIApi.textCompletionsWithStream(params.toJson()).execute()
27-
28- if (response.isSuccessful) {
29- val input = response.body()?.byteStream()?.bufferedReader() ? : throw Exception ()
30- try {
31- while (true ) {
32- val line =
33- withContext(Dispatchers .IO ) {
24+ ) : OpenAIRepository {
25+ override fun textCompletionsWithStream (params : TextCompletionsParam ): Flow <String > =
26+ callbackFlow {
27+ withContext(Dispatchers .IO ) {
28+ val response = (if (params.isTurbo) openAIApi.textCompletionsTurboWithStream(
29+ params.toJson()
30+ ) else openAIApi.textCompletionsWithStream(params.toJson())).execute()
31+
32+ if (response.isSuccessful) {
33+ val input = response.body()?.byteStream()?.bufferedReader() ? : throw Exception ()
34+ try {
35+ while (true ) {
36+ val line = withContext(Dispatchers .IO ) {
3437 input.readLine()
3538 } ? : continue
36- if (line == " data: [DONE]" ) {
37- close()
38- } else if (line.startsWith(" data:" )) {
39- try {
40- // Handle & convert data -> emit to client
41- val value = lookupDataFromResponseTurbo(line)
42- trySend(value)
43- } catch (e: Exception ) {
44- e.printStackTrace()
39+ if (line == " data: [DONE]" ) {
40+ close()
41+ } else if (line.startsWith(" data:" )) {
42+ try {
43+ // Handle & convert data -> emit to client
44+ val value =
45+ if (params.isTurbo) lookupDataFromResponseTurbo(line) else lookupDataFromResponse(
46+ line
47+ )
48+
49+ if (value.isNotEmpty()) {
50+ trySend(value)
51+ }
52+ } catch (e: Exception ) {
53+
54+ e.printStackTrace()
55+ }
4556 }
4657 }
47- }
48- } catch (e: IOException ) {
49- throw Exception (e)
50- } finally {
51- withContext(Dispatchers .IO ) {
52- input.close()
53- }
58+ } catch (e: IOException ) {
59+ throw Exception (e)
60+ } finally {
61+ withContext(Dispatchers .IO ) {
62+ input.close()
63+ }
5464
55- awaitClose {
5665 close()
5766 }
58- }
59- } else {
60- if (! response.isSuccessful) {
61- var jsonObject: JSONObject ? = null
62- try {
63- jsonObject = JSONObject (response.errorBody()!! .string())
64- } catch (e: JSONException ) {
65- e.printStackTrace()
67+ } else {
68+ if (! response.isSuccessful) {
69+ var jsonObject: JSONObject ? = null
70+ try {
71+ jsonObject = JSONObject (response.errorBody()!! .string())
72+ println (jsonObject)
73+ } catch (e: JSONException ) {
74+ e.printStackTrace()
75+ }
6676 }
67- }
68- trySend(" Failure! Try again." )
69- awaitClose {
77+ trySend(" Failure! Try again." )
7078 close()
7179 }
7280 }
81+
82+ close()
83+ }
84+
85+ private fun lookupDataFromResponse (jsonString : String ): String {
86+ val splitsJsonString = jsonString.split(" [{" )
87+
88+ val indexOfResult: Int = splitsJsonString.indexOfLast {
89+ it.contains(matchResultString)
90+ }
91+
92+ val textSplits =
93+ if (indexOfResult == - 1 ) listOf () else splitsJsonString[indexOfResult].split(" ," )
94+
95+ val indexOfText: Int = textSplits.indexOfLast {
96+ it.contains(matchResultString)
97+ }
98+
99+ if (indexOfText != - 1 ) {
100+ try {
101+ val gson = Gson ()
102+ val jsonObject =
103+ gson.fromJson(" {${textSplits[indexOfText]} }" , JsonObject ::class .java)
104+
105+ return jsonObject.get(" text" ).asString
106+ } catch (e: java.lang.Exception ) {
107+ println (e.localizedMessage)
108+ }
73109 }
74- close()
110+
111+ return " "
75112 }
76113
77114 private fun lookupDataFromResponseTurbo (jsonString : String ): String {
@@ -81,7 +118,8 @@ class OpenAIRepositoryImpl @Inject constructor(
81118 it.contains(matchResultTurboString)
82119 }
83120
84- val textSplits = if (indexOfResult == - 1 ) listOf () else splitsJsonString[indexOfResult].split(" ," )
121+ val textSplits =
122+ if (indexOfResult == - 1 ) listOf () else splitsJsonString[indexOfResult].split(" ," )
85123
86124 val indexOfText: Int = textSplits.indexOfLast {
87125 it.contains(matchResultTurboString)
@@ -90,7 +128,8 @@ class OpenAIRepositoryImpl @Inject constructor(
90128 if (indexOfText != - 1 ) {
91129 try {
92130 val gson = Gson ()
93- val jsonObject = gson.fromJson(" {${textSplits[indexOfText]} }" , JsonObject ::class .java)
131+ val jsonObject =
132+ gson.fromJson(" {${textSplits[indexOfText]} }" , JsonObject ::class .java)
94133
95134 return jsonObject.getAsJsonObject(" delta" ).get(" content" ).asString
96135 } catch (e: java.lang.Exception ) {
0 commit comments