Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
## Issue
Fixes langchain4j#1853

## Change
`AiMessage` created by `OpenAiStreamingResponseBuilder` should contain
both text and tool calls if LLM responded with both.

## General checklist
- [X] There are no breaking changes
- [ ] 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
- [x] 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
- [ ] 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/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring) (if
applicable)
  • Loading branch information
langchain4j authored Oct 7, 2024
1 parent 658ca4e commit 7c82a14
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public static Message toOpenAiMessage(ChatMessage message) {
.collect(toList());

return AssistantMessage.builder()
.content(aiMessage.text())
.toolCalls(toolCalls)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static dev.langchain4j.internal.Utils.isNullOrBlank;
import static dev.langchain4j.model.openai.InternalOpenAiHelper.finishReasonFrom;
import static dev.langchain4j.model.openai.InternalOpenAiHelper.tokenUsageFrom;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;

/**
Expand Down Expand Up @@ -142,23 +144,21 @@ public void append(CompletionResponse partialResponse) {

public Response<AiMessage> build() {

String content = contentBuilder.toString();
if (!content.isEmpty()) {
return Response.from(
AiMessage.from(content),
tokenUsage,
finishReason
);
}
String text = contentBuilder.toString();

String toolName = toolNameBuilder.toString();
if (!toolName.isEmpty()) {
ToolExecutionRequest toolExecutionRequest = ToolExecutionRequest.builder()
.name(toolName)
.arguments(toolArgumentsBuilder.toString())
.build();

AiMessage aiMessage = isNullOrBlank(text) ?
AiMessage.from(toolExecutionRequest) :
AiMessage.from(text, singletonList(toolExecutionRequest));

return Response.from(
AiMessage.from(toolExecutionRequest),
aiMessage,
tokenUsage,
finishReason
);
Expand All @@ -172,8 +172,21 @@ public Response<AiMessage> build() {
.arguments(it.argumentsBuilder.toString())
.build())
.collect(toList());

AiMessage aiMessage = isNullOrBlank(text) ?
AiMessage.from(toolExecutionRequests) :
AiMessage.from(text, toolExecutionRequests);

return Response.from(
aiMessage,
tokenUsage,
finishReason
);
}

if (!isNullOrBlank(text)) {
return Response.from(
AiMessage.from(toolExecutionRequests),
AiMessage.from(text),
tokenUsage,
finishReason
);
Expand Down

0 comments on commit 7c82a14

Please sign in to comment.