Skip to content

Commit 103cfb2

Browse files
committed
chore: Modify conversation examples
Signed-off-by: Javier Aliaga <[email protected]>
1 parent c75ca06 commit 103cfb2

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

examples/src/main/java/io/dapr/examples/conversation/AssistantMessageDemo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ public static void main(String[] args) {
108108
// Process and display the response
109109
if (response != null && response.getOutputs() != null && !response.getOutputs().isEmpty()) {
110110
ConversationResultAlpha2 result = response.getOutputs().get(0);
111+
UsageUtils.printUsage(result);
112+
111113
if (result.getChoices() != null && !result.getChoices().isEmpty()) {
112114
ConversationResultChoices choice = result.getChoices().get(0);
113-
114115
if (choice.getMessage() != null && choice.getMessage().getContent() != null) {
115116
System.out.printf("Assistant Response: %s%n", choice.getMessage().getContent());
116117
}

examples/src/main/java/io/dapr/examples/conversation/ToolsCallDemo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public static void main(String[] args) {
8080
// Process and display the response
8181
if (response != null && response.getOutputs() != null && !response.getOutputs().isEmpty()) {
8282
ConversationResultAlpha2 result = response.getOutputs().get(0);
83+
UsageUtils.printUsage(result);
84+
8385
if (result.getChoices() != null && !result.getChoices().isEmpty()) {
8486
ConversationResultChoices choice = result.getChoices().get(0);
8587

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2026 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.examples.conversation;
15+
16+
import io.dapr.client.domain.ConversationResultAlpha2;
17+
import io.dapr.client.domain.ConversationResultCompletionUsage;
18+
import org.springframework.util.StringUtils;
19+
20+
public class UsageUtils {
21+
static void printUsage(ConversationResultAlpha2 result) {
22+
if (!StringUtils.hasText(result.getModel())){
23+
return;
24+
}
25+
26+
System.out.printf("Conversation model : %s", result.getModel());
27+
System.out.println();
28+
var usage = result.getUsage();
29+
printUsage(usage);
30+
31+
printCompletionDetails(usage);
32+
printPromptDetails(usage);
33+
34+
}
35+
36+
private static void printUsage(ConversationResultCompletionUsage usage) {
37+
System.out.println("Token Usage Details:");
38+
System.out.printf(" Completion tokens: %d", usage.getCompletionTokens());
39+
System.out.println();
40+
System.out.printf(" Prompt tokens: %d", usage.getPromptTokens());
41+
System.out.println();
42+
System.out.printf(" Total tokens: %d", usage.getTotalTokens());
43+
System.out.println();
44+
}
45+
46+
private static void printPromptDetails(ConversationResultCompletionUsage usage) {
47+
var completionDetails = usage.getCompletionTokenDetails();
48+
49+
// Display completion token breakdown if available
50+
System.out.println("Prompt Token Details:");
51+
if (completionDetails.getReasoningTokens() > 0) {
52+
System.out.printf(" Reasoning tokens: %d", completionDetails.getReasoningTokens());
53+
System.out.println();
54+
}
55+
if (completionDetails.getAudioTokens() > 0) {
56+
System.out.printf(" Audio tokens: %d", completionDetails.getAudioTokens());
57+
System.out.println();
58+
}
59+
System.out.println();
60+
}
61+
62+
private static void printCompletionDetails(ConversationResultCompletionUsage usage) {
63+
// Print detailed token usage information
64+
var completionDetails = usage.getCompletionTokenDetails();
65+
66+
System.out.println("Completion Token Details:");
67+
// If audio tokens are available, display them
68+
if ( completionDetails.getAudioTokens() > 0) {
69+
System.out.printf(" Audio tokens: %d", completionDetails.getAudioTokens());
70+
System.out.println();
71+
}
72+
73+
// Display completion token breakdown if available
74+
if (completionDetails.getReasoningTokens() > 0) {
75+
System.out.printf(" Reasoning tokens: %d", completionDetails.getReasoningTokens());
76+
System.out.println();
77+
}
78+
79+
// Display completion token breakdown if available
80+
if (completionDetails.getAcceptedPredictionTokens() > 0) {
81+
System.out.printf(" Accepted prediction tokens: %d", completionDetails.getAcceptedPredictionTokens());
82+
System.out.println();
83+
}
84+
85+
// Display completion token breakdown if available
86+
if (completionDetails.getRejectedPredictionTokens() > 0) {
87+
System.out.printf(" Rejected prediction tokens: %d", completionDetails.getRejectedPredictionTokens());
88+
System.out.println();
89+
}
90+
System.out.println();
91+
}
92+
}

examples/src/main/java/io/dapr/examples/conversation/UserMessageDemo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public static void main(String[] args) {
5858
// Extract and print the conversation result
5959
if (response != null && response.getOutputs() != null && !response.getOutputs().isEmpty()) {
6060
ConversationResultAlpha2 result = response.getOutputs().get(0);
61+
UsageUtils.printUsage(result);
62+
6163
if (result.getChoices() != null && !result.getChoices().isEmpty()) {
6264
ConversationResultChoices choice = result.getChoices().get(0);
6365
if (choice.getMessage() != null && choice.getMessage().getContent() != null) {

0 commit comments

Comments
 (0)