11package com.cjcrafter.openai.chat
22
3+ import com.cjcrafter.openai.exception.OpenAIError
34import com.google.gson.*
45import okhttp3.*
56import okhttp3.MediaType.Companion.toMediaType
6- import okhttp3.OkHttpClient.Builder
77import okhttp3.RequestBody.Companion.toRequestBody
88import java.io.IOException
9- import java.lang.IllegalArgumentException
10- import java.util.concurrent.TimeUnit
119import java.util.function.Consumer
1210
1311/* *
@@ -62,8 +60,6 @@ class ChatBot @JvmOverloads constructor(
6260 *
6361 * @param request The input information for ChatGPT.
6462 * @return The returned response.
65- * @throws IOException If an IO Exception occurs.
66- * @throws IllegalArgumentException If the input arguments are invalid.
6763 */
6864 @Throws(IOException ::class )
6965 fun generateResponse (request : ChatRequest ): ChatResponse {
@@ -75,13 +71,14 @@ class ChatBot @JvmOverloads constructor(
7571 try {
7672 client.newCall(httpRequest).execute().use { response ->
7773
78- // Servers respond to API calls with json blocks. Since raw JSON isn't
79- // very developer friendly, we wrap for easy data access.
8074 rootObject = JsonParser .parseString(response.body!! .string()).asJsonObject
81- require(! rootObject!! .has(" error" )) { rootObject!! .get(" error" ).asJsonObject[" message" ].asString }
75+ if (rootObject!! .has(" error" ))
76+ throw OpenAIError .fromJson(rootObject!! [" error" ].asJsonObject)
77+
8278 return ChatResponse (rootObject!! )
8379 }
8480 } catch (ex: Throwable ) {
81+ println (rootObject)
8582 throw ex
8683 }
8784 }
0 commit comments