Skip to content

Commit

Permalink
Bugfix 1218 - MistralAi streaming blank (langchain4j#1243)
Browse files Browse the repository at this point in the history
## Issue
langchain4j#1218


## Change
```java
MistralAiChatCompletionChoice choice = chatCompletionResponse.getChoices().get(0);

String chunk = choice.getDelta().getContent();
if (isNotNullOrEmpty(chunk)) { // changed form `isNotNullOrBlank` to `isNotNullOrEmpty`
    contentBuilder.append(chunk);
    handler.onNext(chunk);
}
```


## General checklist
<!-- Please double-check the following points and mark them like this:
[X] -->
- [X] There are no breaking changes
- [X] I have added unit and integration tests for my change
- [X] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [ ] I have manually run all the 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, and they are all green
<!-- Before adding documentation and example(s) (below), please wait
until the PR is reviewed and approved. -->
- [ ] 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)
  • Loading branch information
czelabueno authored Jun 7, 2024
1 parent 2c8ff58 commit 8dcfa0d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void onEvent(EventSource eventSource, String id, String type, String data
MistralAiChatCompletionChoice choice = chatCompletionResponse.getChoices().get(0);

String chunk = choice.getDelta().getContent();
if (isNotNullOrBlank(chunk)) {
if (isNotNullOrEmpty(chunk)) {
contentBuilder.append(chunk);
handler.onNext(chunk);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,23 @@ void should_return_valid_json_object_using_model_large() {
// then
assertThat(json).isEqualToIgnoringWhitespace(expectedJson);
}

@Test
void bugfix_1218_allow_blank() {
// given
StreamingChatLanguageModel model = MistralAiStreamingChatModel.builder()
.apiKey(System.getenv("MISTRAL_AI_API_KEY"))
.modelName(MistralAiChatModelName.MISTRAL_SMALL_LATEST)
.temperature(0d)
.build();

String userMessage = "What was inflation rate in germany in 2020? Answer in 1 short sentence. Begin your answer with 'In 2020, ...'";

// when
TestStreamingResponseHandler<AiMessage> responseHandler = new TestStreamingResponseHandler<>();
model.generate(userMessage, responseHandler);

// results in: "In2020, Germany's inflation rate was0.5%."
assertThat(responseHandler.get().content().text()).containsIgnoringCase("In 2020");
}
}

0 comments on commit 8dcfa0d

Please sign in to comment.