Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [AiServices] OllamaStreamingChatModel partial response deserialization throws MalformedJsonException #1013

Closed
kchaber opened this issue Apr 24, 2024 · 5 comments
Labels
bug Something isn't working question Further information is requested

Comments

@kchaber
Copy link

kchaber commented Apr 24, 2024

Describe the bug
The com.google.gson.stream.MalformedJsonException exception is thrown during TokenStream consumption when using the OllamaStreamingChatModel through the AiServices.

Log and Stack trace

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 2 path $
	at com.google.gson.Gson.assertFullConsumption(Gson.java:908)
	at com.google.gson.Gson.fromJson(Gson.java:898)
	at com.google.gson.Gson.fromJson(Gson.java:846)
	at com.google.gson.Gson.fromJson(Gson.java:817)
	at dev.langchain4j.model.ollama.OllamaClient$2.onResponse(OllamaClient.java:132)
	at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:161)
	at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 2 column 2 path $
	at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1564)
	at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1405)
	at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:543)
	at com.google.gson.stream.JsonReader.peek(JsonReader.java:426)
	at com.google.gson.Gson.assertFullConsumption(Gson.java:904)
	... 9 more

To Reproduce

public interface StreamingAssistant {

    TokenStream chat(String userMessage);

}

OllamaStreamingChatModel ollamaModel = OllamaStreamingChatModel.builder()
    .baseUrl("<url>")
    .modelName("mistral")
    .build();

StreamingAssistant assistant = AiServices.builder(StreamingAssistant.class)
    .streamingChatLanguageModel(ollamaModel)
    .build();

assistant.chat("Tell me a joke")
    .onNext(System.out::println)
    .onComplete(System.out::println)
    .onError(Throwable::printStackTrace)
    .start();

Expected behavior
No exception is thrown when processing streaming response.

Please complete the following information:

  • LangChain4j version: 0.30.0
  • LLM(s) used: ollama mistral
  • Java version: 17
  • Spring Boot version (if applicable):

Additional context
The exception is thrown in the OllamaClient when the partial response does not contain valid json as presented in the below debug:
image

@kchaber kchaber added the bug Something isn't working label Apr 24, 2024
@langchain4j
Copy link
Owner

langchain4j commented Apr 25, 2024

Hi, it should be already fixed by this.
Could you please check snapshot version?

<dependency>
  <groupId>dev.langchain4j</groupId>
  <artifactId>langchain4j-ollama</artifactId>
  <version>0.31.0-SNAPSHOT</version>
</dependency>

@langchain4j langchain4j added the question Further information is requested label Apr 25, 2024
@kchaber
Copy link
Author

kchaber commented Apr 26, 2024

Hi @langchain4j, I've checked the snapshot version but it still does not work.
However it seems that mentioned change is not included in the snapshot build. I can see that last build was on 16/04 where #867 was merged on 19th and is not included in the artifact.
image

@langchain4j
Copy link
Owner

Hi @kchaber, thanks for the heads up!
I have fixed snapshot build, 0.31.0-SNAPSHOT can be used now.

@langchain4j
Copy link
Owner

@kchaber please close this issue if it worked for you 🙏

@kchaber
Copy link
Author

kchaber commented Apr 29, 2024

@langchain4j yes, that works correctly now ☺️ Thank you!

@kchaber kchaber closed this as completed Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants