Skip to content

Commit 863cf38

Browse files
committed
Add null check and javadoc to UsageUtils
- Add null check to UsageUtils#getCumulativeUsage() arguments - Add javadoc the methods
1 parent f7e8e7f commit 863cf38

File tree

1 file changed

+31
-6
lines changed
  • spring-ai-core/src/main/java/org/springframework/ai/chat/metadata

1 file changed

+31
-6
lines changed

spring-ai-core/src/main/java/org/springframework/ai/chat/metadata/UsageUtils.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,46 @@
2525
*/
2626
public class UsageUtils {
2727

28+
/**
29+
* Accumulate usage tokens from the previous chat response to the current usage
30+
* tokens.
31+
* @param currentUsage the current usage.
32+
* @param previousChatResponse the previous chat response.
33+
* @return accumulated usage.
34+
*/
2835
public static Usage getCumulativeUsage(final Usage currentUsage, final ChatResponse previousChatResponse) {
29-
Long promptTokens = currentUsage.getPromptTokens().longValue();
30-
Long generationTokens = currentUsage.getGenerationTokens().longValue();
31-
Long totalTokens = currentUsage.getTotalTokens().longValue();
32-
// Make sure to accumulate the usage from the previous chat response.
36+
Usage usageFromPreviousChatResponse = null;
3337
if (previousChatResponse != null && previousChatResponse.getMetadata() != null
3438
&& previousChatResponse.getMetadata().getUsage() != null) {
35-
Usage usageFromPreviousChatResponse = previousChatResponse.getMetadata().getUsage();
39+
usageFromPreviousChatResponse = previousChatResponse.getMetadata().getUsage();
40+
}
41+
else {
42+
// Return the curent usage when the previous chat response usage is empty or
43+
// null.
44+
return currentUsage;
45+
}
46+
// For a valid usage from previous chat response, accumulate it to the current
47+
// usage.
48+
if (!isEmpty(currentUsage)) {
49+
Long promptTokens = currentUsage.getPromptTokens().longValue();
50+
Long generationTokens = currentUsage.getGenerationTokens().longValue();
51+
Long totalTokens = currentUsage.getTotalTokens().longValue();
52+
// Make sure to accumulate the usage from the previous chat response.
3653
promptTokens += usageFromPreviousChatResponse.getPromptTokens();
3754
generationTokens += usageFromPreviousChatResponse.getGenerationTokens();
3855
totalTokens += usageFromPreviousChatResponse.getTotalTokens();
56+
return new DefaultUsage(promptTokens, generationTokens, totalTokens);
3957
}
40-
return new DefaultUsage(promptTokens, generationTokens, totalTokens);
58+
// When current usage is empty, return the usage from the previous chat response.
59+
return usageFromPreviousChatResponse;
4160
}
4261

62+
/**
63+
* Check if the {@link Usage} is empty. Returns true when the {@link Usage} is null.
64+
* Returns true when the {@link Usage} has zero tokens.
65+
* @param usage the usage to check against.
66+
* @return the boolean value to represent if it is empty.
67+
*/
4368
public static boolean isEmpty(Usage usage) {
4469
if (usage == null) {
4570
return true;

0 commit comments

Comments
 (0)