Skip to content

Commit b86772d

Browse files
ilayaperumalgmarkpollack
authored andcommitted
Add reference doc for Usage changes
- Add documentation for the recent changes on Usage handling - Add details on how to refer model specific native usage - Update upgrade notes
1 parent 16a596f commit b86772d

File tree

2 files changed

+130
-19
lines changed

2 files changed

+130
-19
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
= Using Chat/Embedding Response Usage
2+
3+
== Overview
4+
Spring AI has enhanced its Model Usage handling by introducing `getNativeUsage()` method in the Usage interface and providing a `DefaultUsage` implementation.
5+
This change simplifies how different AI models can track and report their usage metrics while maintaining consistency across the framework.
6+
7+
== Key Changes
8+
9+
=== Usage Interface Enhancement
10+
The `Usage` interface now includes a new method:
11+
```java
12+
Object getNativeUsage();
13+
```
14+
This method allows access to the model-specific native usage data, enabling more detailed usage tracking when needed.
15+
16+
=== Using with ChatClient
17+
18+
Here's a complete example showing how to track usage with OpenAI's ChatClient:
19+
20+
```java
21+
@SpringBootConfiguration
22+
public class Configuration {
23+
24+
@Bean
25+
public OpenAiApi chatCompletionApi() {
26+
return new OpenAiApi(System.getenv("OPENAI_API_KEY"));
27+
}
28+
29+
@Bean
30+
public OpenAiChatModel openAiClient(OpenAiApi openAiApi) {
31+
return new OpenAiChatModel(openAiApi);
32+
}
33+
34+
}
35+
36+
@Service
37+
public class ChatService {
38+
39+
private final OpenAiChatModel chatModel;
40+
41+
public ChatService(OpenAiChatModel chatModel) {
42+
this.chatModel = chatModel;
43+
}
44+
45+
public void demonstrateUsage() {
46+
// Create a chat prompt
47+
Prompt prompt = new Prompt("What is the weather like today?");
48+
49+
ChatClient chatClient = ChatClient.builder(this.chatModel).build();
50+
51+
// Get the chat response
52+
ChatResponse response = chatClient.call(prompt);
53+
54+
// Access the usage information
55+
Usage usage = response.getMetadata().getUsage();
56+
57+
// Get standard usage metrics
58+
System.out.println("Prompt Tokens: " + usage.getPromptTokens());
59+
System.out.println("Completion Tokens: " + usage.getCompletionTokens());
60+
System.out.println("Total Tokens: " + usage.getTotalTokens());
61+
62+
// Access native OpenAI usage data with detailed token information
63+
if (usage.getNativeUsage() instanceof org.springframework.ai.openai.api.OpenAiApi.Usage) {
64+
org.springframework.ai.openai.api.OpenAiApi.Usage nativeUsage =
65+
(org.springframework.ai.openai.api.OpenAiApi.Usage) usage.getNativeUsage();
66+
67+
// Detailed prompt token information
68+
System.out.println("Prompt Tokens Details:");
69+
System.out.println("- Audio Tokens: " + nativeUsage.promptTokensDetails().audioTokens());
70+
System.out.println("- Cached Tokens: " + nativeUsage.promptTokensDetails().cachedTokens());
71+
72+
// Detailed completion token information
73+
System.out.println("Completion Tokens Details:");
74+
System.out.println("- Reasoning Tokens: " + nativeUsage.completionTokenDetails().reasoningTokens());
75+
System.out.println("- Accepted Prediction Tokens: " + nativeUsage.completionTokenDetails().acceptedPredictionTokens());
76+
System.out.println("- Audio Tokens: " + nativeUsage.completionTokenDetails().audioTokens());
77+
System.out.println("- Rejected Prediction Tokens: " + nativeUsage.completionTokenDetails().rejectedPredictionTokens());
78+
}
79+
}
80+
}
81+
```
82+
83+
== Benefits
84+
85+
**Standardization**: Provides a consistent way to handle usage across different AI models
86+
**Flexibility**: Supports model-specific usage data through the native usage feature
87+
**Simplification**: Reduces boilerplate code with the default implementation
88+
**Extensibility**: Easy to extend for specific model requirements while maintaining compatibility
89+
90+
=== Type Safety Considerations
91+
92+
When working with native usage data, consider type casting carefully:
93+
```java
94+
// Safe way to access native usage
95+
if (usage.getNativeUsage() instanceof org.springframework.ai.openai.api.OpenAiApi.Usage) {
96+
org.springframework.ai.openai.api.OpenAiApi.Usage nativeUsage =
97+
(org.springframework.ai.openai.api.OpenAiApi.Usage) usage.getNativeUsage();
98+
// Work with native usage data
99+
}
100+
```

spring-ai-docs/src/main/antora/modules/ROOT/pages/upgrade-notes.adoc

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,41 @@
33

44
== Upgrading to 1.0.0.M6
55

6+
67
=== Changes to Usage Interface and DefaultUsage Implementation
78

8-
* In the link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/metadata/Usage.java[Usage] interface, changed the method name from `getGenerationTokens` to `getCompletionTokens` as that is more common terminology in the industry.
9-
* In link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/metadata/DefaultUsage.java[DefaultUsage], changed parameter types from `Long` to `Integer` for all token-related fields:
9+
The `Usage` interface and its default implementation `DefaultUsage` have undergone the following changes:
10+
11+
1. Method Rename:
12+
* `getGenerationTokens()` is now `getCompletionTokens()`
13+
14+
2. Type Changes:
15+
* All token count fields in `DefaultUsage` changed from `Long` to `Integer`:
1016
** `promptTokens`
1117
** `completionTokens` (formerly `generationTokens`)
1218
** `totalTokens`
13-
* Constructor changes in link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/metadata/DefaultUsage.java[DefaultUsage]:
14-
** Changed parameter types from `Long` to `Integer`
15-
** Renamed `generationTokens` parameter to `completionTokens`
16-
** Removed constructors that used `Long` parameters
1719

18-
=== JSON Serialization Changes
20+
==== Required Actions
21+
22+
* Replace all calls to `getGenerationTokens()` with `getCompletionTokens()`
23+
24+
* Update `DefaultUsage` constructor calls:
25+
[source,java]
26+
----
27+
// Old (M5)
28+
new DefaultUsage(Long promptTokens, Long generationTokens, Long totalTokens)
29+
30+
// New (M6)
31+
new DefaultUsage(Integer promptTokens, Integer completionTokens, Integer totalTokens)
32+
----
33+
34+
35+
NOTE: For more information on handling Usage, refer xref:api/usage-handling.adoc[here]
36+
37+
==== JSON Ser/Deser changes
38+
While M6 maintains backward compatibility for JSON deserialization of the `generationTokens` field, this field will be removed in M7. Any persisted JSON documents using the old field name should be updated to use `completionTokens`.
1939

20-
* The JSON format now uses `completionTokens` instead of `generationTokens`
21-
* For backward compatibility, JSON deserialization still supports the old format with `generationTokens`
22-
* Example of new JSON format:
40+
Example of the new JSON format:
2341
[source,json]
2442
----
2543
{
@@ -29,22 +47,15 @@
2947
}
3048
----
3149

32-
=== Migration Guide
33-
34-
1. Update your code to use `getCompletionTokens()` instead of `getGenerationTokens()`
35-
2. When creating new instances of link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/metadata/DefaultUsage.java[DefaultUsage], use `Integer` instead of `Long` for token counts. This is a breaking change and requires updating all constructor calls.
36-
3. When constructing link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/metadata/DefaultUsage.java[DefaultUsage], use the parameter name `completionTokens` instead of `generationTokens`
37-
4. If you're serializing `DefaultUsage` objects to JSON, update your code to use the new field names
38-
3950

4051
=== Removal of deprecated Amazon Bedrock chat models
4152

4253
Starting 1.0.0-M6, Spring AI transitioned to using Amazon Bedrock's Converse API for all Chat conversation implementations in Spring AI.
4354
All the Amazon Bedrock Chat models are removed except the Embedding models for Cohere and Titan.
4455

45-
=== Migration Guide
56+
NOTE: Refer to xref:api/chat/bedrock-converse.adoc[Bedrock Converse] documentation for using the chat models.
57+
4658

47-
Refer to xref:api/chat/bedrock-converse.adoc[Bedrock Converse] documentation for using the chat models.
4859

4960
== Upgrading to 1.0.0.M5
5061

0 commit comments

Comments
 (0)