lateinit initialization error when using createChatCompletion()
Closed this issue · 4 comments
When I attempt to use createChatCompletion()
, I always receive this error:
lateinit property response has not been initialized
Here the function I am using
fun chat(text: String): String {
messages.add(text.toUserMessage())
val response = openai.createChatCompletion(request)
messages.add(response[0].message)
return response[0].message.content
}
I checked out OpenAI.createChatCompletion()
and I can see that response
is the lateinit variable that apparently isn't being initialized.
val httpResponse = client.newCall(httpRequest).execute()
lateinit var response: ChatResponse
OpenAICallback(true, { throw it }) {
response = gson.fromJson(it, ChatResponse::class.java)
}.onResponse(httpResponse)
return response
val httpResponse = client.newCall(httpRequest).execute()
lateinit var response: ChatResponse
OpenAICallback(true, { throw it }) {
response = gson.fromJson(it, ChatResponse::class.java) // initialized here
}.onResponse(httpResponse)
return response
Can you triple-check that you are on the correct version (Currently 1.3.0
)?
I just ran this to confirm, it works for me:
https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/src/test/kotlin/KotlinTest.kt#L141-L143
So I checked and I am on 1.3.0
and it wasn't working. Out of curiosity, I downgraded to 1.2.7
and it worked. I then went back to 1.3.0
and it still works. I'm really not sure what happened here.
Glad you got it fixed. If you build something cool, feel free to share it in discussions.
I get this error:
com.google.gson.JsonSyntaxException: java.io.EOFException: End of input at line 1 column 2 path $.
at com.google.gson.internal.Streams.parse(Streams.java:59)
at com.google.gson.JsonParser.parseReader(JsonParser.java:102)
at com.google.gson.JsonParser.parseReader(JsonParser.java:70)
at com.google.gson.JsonParser.parseString(JsonParser.java:51)
at com.cjcrafter.openai.OpenAICallback.handleStream(OpenAICallback.kt:66)
at com.cjcrafter.openai.OpenAICallback.onResponse(OpenAICallback.kt:30)
at com.cjcrafter.openai.OpenAI.createChatCompletion(OpenAI.kt:252)
...
Caused by: java.io.EOFException: End of input at line 1 column 2 path $.
at com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1457)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:503)
at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:422)
at com.google.gson.internal.bind.TypeAdapters$28.read(TypeAdapters.java:779)
at com.google.gson.internal.bind.TypeAdapters$28.read(TypeAdapters.java:725)
at com.google.gson.internal.Streams.parse(Streams.java:49)
and then a lot of this:
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 8 path $
at com.google.gson.JsonParser.parseReader(JsonParser.java:76)
at com.google.gson.JsonParser.parseString(JsonParser.java:51)
at com.cjcrafter.openai.OpenAICallback.handleStream(OpenAICallback.kt:66)
at com.cjcrafter.openai.OpenAICallback.onResponse(OpenAICallback.kt:30)
at com.cjcrafter.openai.OpenAI.createChatCompletion(OpenAI.kt:252)
and finally this:
lateinit property response has not been initialized kotlin.UninitializedPropertyAccessException: lateinit property response has not been initialized