@@ -5,9 +5,7 @@ import com.cjcrafter.openai.completions.CompletionRequest
55import com.cjcrafter.openai.completions.CompletionResponse
66import com.cjcrafter.openai.completions.CompletionResponseChunk
77import com.cjcrafter.openai.completions.CompletionUsage
8- import com.cjcrafter.openai.exception.WrappedIOError
9- import com.google.gson.JsonObject
10- import com.google.gson.JsonParser
8+ import com.fasterxml.jackson.databind.node.ObjectNode
119import okhttp3.*
1210import okhttp3.MediaType.Companion.toMediaType
1311import okhttp3.RequestBody.Companion.toRequestBody
@@ -55,10 +53,10 @@ open class OpenAIImpl @JvmOverloads constructor(
5553 private val client : OkHttpClient = OkHttpClient ()
5654): OpenAI {
5755 protected val mediaType = " application/json; charset=utf-8" .toMediaType()
58- protected val gson = OpenAI .createGson ()
56+ protected val objectMapper = OpenAI .createObjectMapper ()
5957
6058 protected open fun buildRequest (request : Any , endpoint : String ): Request {
61- val json = gson.toJson (request)
59+ val json = objectMapper.writeValueAsString (request)
6260 val body: RequestBody = json.toRequestBody(mediaType)
6361 return Request .Builder ()
6462 .url(" https://api.openai.com/$endpoint " )
@@ -73,12 +71,8 @@ open class OpenAIImpl @JvmOverloads constructor(
7371 request.stream = false // use streamCompletion for stream=true
7472 val httpRequest = buildRequest(request, COMPLETIONS_ENDPOINT )
7573
76- try {
77- val httpResponse = client.newCall(httpRequest).execute()
78- println (httpResponse)
79- } catch (ex: IOException ) {
80- throw WrappedIOError (ex)
81- }
74+ val httpResponse = client.newCall(httpRequest).execute()
75+ println (httpResponse)
8276
8377 return CompletionResponse (" 1" , 1 , " 1" , listOf (), CompletionUsage (1 , 1 , 1 ))
8478 }
@@ -104,7 +98,7 @@ open class OpenAIImpl @JvmOverloads constructor(
10498 }
10599
106100 val json = httpResponse.body?.byteStream()?.bufferedReader() ? : throw IOException (" Response body is null" )
107- return gson.fromJson (json, ChatResponse ::class .java)
101+ return objectMapper.readValue (json, ChatResponse ::class .java)
108102 }
109103
110104 override fun streamChatCompletion (request : ChatRequest ): Iterable <ChatResponseChunk > {
@@ -150,7 +144,7 @@ open class OpenAIImpl @JvmOverloads constructor(
150144 override fun next (): ChatResponseChunk {
151145 val currentLine = nextLine ? : throw NoSuchElementException (" No more lines" )
152146 // println(" $currentLine")
153- chunk = chunk?.apply { update(JsonParser .parseString (currentLine).asJsonObject ) } ? : gson.fromJson (currentLine, ChatResponseChunk ::class .java)
147+ chunk = chunk?.apply { update(objectMapper.readTree (currentLine) as ObjectNode ) } ? : objectMapper.readValue (currentLine, ChatResponseChunk ::class .java)
154148 nextLine = readNextLine(reader) // Prepare the next line
155149 return chunk!!
156150 }
@@ -159,8 +153,6 @@ open class OpenAIImpl @JvmOverloads constructor(
159153 }
160154 }
161155
162-
163-
164156 companion object {
165157 const val COMPLETIONS_ENDPOINT = " v1/completions"
166158 const val CHAT_ENDPOINT = " v1/chat/completions"
0 commit comments