|
| 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 | +``` |
0 commit comments