Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

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

Closed
hbrucex opened this issue Mar 12, 2023 · 10 comments

Comments

@hbrucex
Copy link

hbrucex commented Mar 12, 2023

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

@cryptoapebot
Copy link

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)

@zengpeng5281314
Copy link

I met the same problem and asked for help

@dragonorant
Copy link

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

@hbrucex
Copy link
Author

hbrucex commented Mar 13, 2023

re.Json

yes

@cryptoapebot
Copy link

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

@Ruanandxian
Copy link

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

@cryptoapebot
Copy link

cryptoapebot commented Mar 13, 2023

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.

@fschon
Copy link

fschon commented Mar 15, 2023

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

Thx. Franz

@cryptoapebot
Copy link

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

n3bul4 added a commit to n3bul4/openai-java that referenced this issue Mar 17, 2023
Utilize retrofit2.http.Streaming and retrofit2.Call<ResponseBody>
in additional OpenAIApi methods to enable a streamable ResponseBody.

Utilize retrofit2.Callback to get the streamable ResponseBody,
parse Server Sent Events (SSE) and emit them using
io.reactivex.FlowableEmitter.

Enable:

- Streaming of raw bytes
- Streaming of Java objects
- Shutdown of OkHttp ExecutorService

Fixes: TheoKanning#51, TheoKanning#83, TheoKanning#182, TheoKanning#184
TheoKanning pushed a commit that referenced this issue Mar 28, 2023
Utilize retrofit2.http.Streaming and retrofit2.Call<ResponseBody>
in additional OpenAIApi methods to enable a streamable ResponseBody.

Utilize retrofit2.Callback to get the streamable ResponseBody,
parse Server Sent Events (SSE) and emit them using
io.reactivex.FlowableEmitter.

Enable:

- Streaming of raw bytes
- Streaming of Java objects
- Shutdown of OkHttp ExecutorService

Fixes: #51, #83, #182, #184
@TheoKanning
Copy link
Owner

0.12.0 is live with streaming support 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants