openai-java icon indicating copy to clipboard operation
openai-java copied to clipboard

when i use version 0.11 , i get Unrecognized token 'data': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')

Open hbrucex opened this issue 2 years ago • 9 comments

I got an error like "Unrecognized token 'data': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')",When I use the stream property of the CompletionRequest object. I My guess is that the openAi service was developed in python. Service support is 'true' instead of true.

CompletionRequest completionRequest = CompletionRequest.builder()
                    .prompt(question)
                    .model(openAiChattingBody.getModel())
                    .maxTokens(DEFAULT_MAX_TOKEN)
                    .temperature(DEFAULT_TEMPERATURE)
                    .topP(1.0)
                    .frequencyPenalty(0.0)
                    .presencePenalty(0.0)
                    .stream(Boolean.TRUE)
                    .echo(false)
                    .stop(STOPS)
                    .build();

I want you to take care of it

hbrucex avatar Mar 12 '23 14:03 hbrucex

Confirmed.

CompletionRequest(model=text-davinci-003, prompt=woodchucks?, maxTokens=500, temperature=0.7, topP=1.0, n=null, stream=true, logprobs=null, echo=false, stop=[
,  Human:,  AI:], presencePenalty=0.0, frequencyPenalty=0.0, bestOf=null, logitBias=null, user=null)
Exception in thread "main" java.lang.RuntimeException: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'data': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (okhttp3.ResponseBody$BomAwareReader); line: 1, column: 5]
	at io.reactivex.internal.util.ExceptionHelper.wrapOrThrow(ExceptionHelper.java:46)
	at io.reactivex.internal.observers.BlockingMultiObserver.blockingGet(BlockingMultiObserver.java:93)
	at io.reactivex.Single.blockingGet(Single.java:2870)
	at com.theokanning.openai.OpenAiService.createCompletion(OpenAiService.java:116)
	at CryptoApeBot4.main(CryptoApeBot4.java:181)
Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'data': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (okhttp3.ResponseBody$BomAwareReader); line: 1, column: 5]
	at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:2418)

cryptoapebot avatar Mar 12 '23 16:03 cryptoapebot

I met the same problem and asked for help

zengpeng5281314 avatar Mar 13 '23 10:03 zengpeng5281314

The above problem is also encountered. When stream=true is not used, there is no such problem

dragonorant avatar Mar 13 '23 10:03 dragonorant

re.Json

yes

hbrucex avatar Mar 13 '23 13:03 hbrucex

It looks like the stream includes data and data: DONE, so the jackson parser is trying to parse a partial result.

cryptoapebot avatar Mar 13 '23 13:03 cryptoapebot

Because stream is set to true, I haven't found a way to handle the sse message returned by stream. I don't know if it is not supported

Ruanandxian avatar Mar 13 '23 18:03 Ruanandxian

just for fun, I set it to false and the call succeeded, so I don't think it's a problem w/ the actual value, just how it's handled in the response. I wonder if the way to fix it is just to add a BufferedReader in CompletionResult and depending on the stream boolean, CompletionRequest either return the List of choices of the BodyHandlers.ofInputStream() in the HttpResponse.

			BufferedReader data = new BufferedReader(new InputStreamReader(...));
   // Receives the response body as an InputStream
   HttpResponse<InputStream> response = client
     .send(request, BodyHandlers.ofInputStream());

Then if you were expecting a stream, you just read from the stream.

cryptoapebot avatar Mar 13 '23 19:03 cryptoapebot

Any progress on this issue? I am facing thew exact same problem.

Thx. Franz

fschon avatar Mar 15 '23 14:03 fschon

I thought #183 or #186 had a checkin w/ this feature. Take a look at those issues to see if it solves your problem.

cryptoapebot avatar Mar 15 '23 16:03 cryptoapebot

0.12.0 is live with streaming support :+1:

TheoKanning avatar Apr 01 '23 19:04 TheoKanning