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

Fix #935: DefaultAiService compatibility for TokenUsage result is null #942

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -85,13 +85,13 @@ public Integer totalTokenCount() {
* <p>Fields which are null in both responses will be null in the result.
*
* @param that The token usage to add to this one.
* @return a new {@link TokenUsage} instance with the token usage of both responses added together.
* @return a new {@link TokenUsage} instance with the token usage of both responses added together,
* or a new instance of the original fields if that token usage is null.
*/
public TokenUsage add(TokenUsage that) {
if (that == null) {
return new TokenUsage(inputTokenCount, outputTokenCount, totalTokenCount);
}

return new TokenUsage(
sum(this.inputTokenCount, that.inputTokenCount),
sum(this.outputTokenCount, that.outputTokenCount),
Expand Down
Expand Up @@ -89,7 +89,10 @@ public void test_add() {
new TokenUsage(null, null, null)
.add(new TokenUsage(null, null, null)))
.isEqualTo(new TokenUsage(null, null, null));
}

@Test
public void test_add_null() {
assertThat(new TokenUsage(1, 2, 3).add(null))
.isEqualTo(new TokenUsage(1, 2, 3));
}
Expand Down
Expand Up @@ -104,7 +104,7 @@ private Response<AiMessage> generate(List<ChatMessage> messages,

return Response.from(
aiMessageFrom(response),
null,
tokenUsageFrom(response.usage()),
finishReasonFrom(response.choices().get(0).finishReason())
);
}
Expand Down
Expand Up @@ -237,7 +237,7 @@ private static ToolExecutionRequest toToolExecutionRequest(ToolCall toolCall) {

public static TokenUsage tokenUsageFrom(Usage openAiUsage) {
if (openAiUsage == null) {
return null;
return new TokenUsage();
}
return new TokenUsage(
openAiUsage.promptTokens(),
Expand Down