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] Streaming AI Service: a NullPointerException when using non-OpenAI APIs #921

Closed
hehr opened this issue Apr 11, 2024 · 2 comments · Fixed by #939
Closed

[BUG] Streaming AI Service: a NullPointerException when using non-OpenAI APIs #921

hehr opened this issue Apr 11, 2024 · 2 comments · Fixed by #939
Labels
bug Something isn't working P1 Highest priority

Comments

@hehr
Copy link

hehr commented Apr 11, 2024

Describe the bug

java.lang.NullPointerException

Log and Stack trace

at dev.langchain4j.model.output.TokenUsage.add(TokenUsage.java:88) ~[langchain4j-core-0.29.1.jar:na]
at dev.langchain4j.service.AiServiceStreamingResponseHandler.onComplete(AiServiceStreamingResponseHandler.java:92) ~[langchain4j-0.29.1.jar:na]
at dev.langchain4j.model.openai.OpenAiStreamingChatModel.lambda$generate$1(OpenAiStreamingChatModel.java:166) ~[langchain4j-open-ai-0.29.1.jar:na]
at dev.ai4j.openai4j.StreamingRequestExecutor$2.onEvent(StreamingRequestExecutor.java:170) ~[openai4j-0.17.0.jar:na]
at okhttp3.internal.sse.RealEventSource.onEvent(RealEventSource.kt:101) [okhttp-sse-4.9.3.jar:na]
at okhttp3.internal.sse.ServerSentEventReader.completeEvent(ServerSentEventReader.kt:108) ~[okhttp-sse-4.9.3.jar:na]
at okhttp3.internal.sse.ServerSentEventReader.processNextEvent(ServerSentEventReader.kt:52) ~[okhttp-sse-4.9.3.jar:na]
at okhttp3.internal.sse.RealEventSource.processResponse(RealEventSource.kt:75) [okhttp-sse-4.9.3.jar:na]
at okhttp3.internal.sse.RealEventSource.onResponse(RealEventSource.kt:46) [okhttp-sse-4.9.3.jar:na]
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519) [okhttp-4.11.0.jar:na]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_332]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_332]
at java.lang.Thread.run(Thread.java:750) [na:1.8.0_332]

To Reproduce

  1. create your model ,like this:
public static StreamingChatLanguageModel createStreamingChatLanguageMode(String mode, int maxToken) {
        return OpenAiStreamingChatModel.builder()
                .baseUrl(moonshot_url)
                .apiKey(moonshot_api_key)
                .modelName(mode)
                .logRequests(true)//open log request 
                .temperature(temperature)
                .maxTokens(maxToken)
                .build();
    }
  1. declare your services, like :
public interface IChatAgent  {

    // use TokenStream 
    TokenStream chat( String question);

}
  1. create AiService :
 IChatAgent chatAgent = AiServices.builder(IChatAgent.class)
                .streamingChatLanguageModel(chatModel)
                .chatMemory(MessageWindowChatMemory.withMaxMessages(5))
                .build();
  1. start token steam ,like this:
 chatAgent.chat( result.getInput())
                        .onNext(text -> {
                            try {
                                emitter.send(
                                        collector.append(text).toString(), MediaType.TEXT_PLAIN);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }).onComplete(messageResponse -> emitter.complete())
                        .onError(emitter::completeWithError)
                        .start();
  1. than you will see this bug in your log .
  2. if close logRequests , NullPointerException disappear ;
    Expected behavior

fix this bug , or allow custom TokenStream ;

Please complete the following information:

  • LangChain4j version: 0.29.1
  • LLM(s) used: moonshot_v1_8k
  • Java version: JDK 1.8
  • Spring Boot version (if applicable):
    implementation 'org.springframework.boot:spring-boot-starter-web'

Additional context

@hehr hehr added the bug Something isn't working label Apr 11, 2024
@langchain4j langchain4j added the P2 High priority label Apr 11, 2024
@langchain4j langchain4j changed the title [BUG] a NullPointerException [BUG] Streaming AI Service: a NullPointerException when using non-OpenAI APIs Apr 11, 2024
@langchain4j
Copy link
Owner

This happends with other models as well:

java.lang.NullPointerException: Cannot read field "inputTokenCount" because "that" is null
	at dev.langchain4j.model.output.TokenUsage.add(TokenUsage.java:88) ~[langchain4j-core-0.29.1.jar:na]
	at dev.langchain4j.service.AiServiceStreamingResponseHandler.onComplete(AiServiceStreamingResponseHandler.java:92) ~[langchain4j-0.29.1.jar:na]
	at dev.langchain4j.model.azure.AzureOpenAiStreamingChatModel.generate(AzureOpenAiStreamingChatModel.java:278) ~[langchain4j-azure-open-ai-0.29.1.jar:na]
	at dev.langchain4j.model.azure.AzureOpenAiStreamingChatModel.generate(AzureOpenAiStreamingChatModel.java:223) ~[langchain4j-azure-open-ai-0.29.1.jar:na]
	at dev.langchain4j.service.AiServiceTokenStream$AiServiceOnStart.start(AiServiceTokenStream.java:88) ~[langchain4j-0.29.1.jar:na]

@langchain4j langchain4j added P1 Highest priority and removed P2 High priority labels Apr 11, 2024
langchain4j added a commit that referenced this issue Apr 16, 2024
…939)

## Context
Fixed #921

## Change
When `TokenUsage` is null, do not fail with NPE

## Checklist
Before submitting this PR, please check the following points:
- [X] I have added unit and integration tests for my change
- [X] All unit and integration tests in the module I have added/changed
are green
- [X] All unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules are green
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added my new module in the
[BOM](https://github.com/langchain4j/langchain4j/blob/main/langchain4j-bom/pom.xml)
(only when a new module is added)
@hehr
Copy link
Author

hehr commented Apr 24, 2024

OK,I have updated v0.30 ;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P1 Highest priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants