@@ -13,22 +13,26 @@ Object getNativeUsage();
13
13
```
14
14
This method allows access to the model-specific native usage data, enabling more detailed usage tracking when needed.
15
15
16
- === Using with ChatClient
16
+ === Using with ChatModel
17
17
18
- Here's a complete example showing how to track usage with OpenAI's ChatClient :
18
+ Here's a complete example showing how to track usage with OpenAI's ChatModel :
19
19
20
20
```java
21
21
@SpringBootConfiguration
22
22
public class Configuration {
23
23
24
24
@Bean
25
25
public OpenAiApi chatCompletionApi() {
26
- return new OpenAiApi(System.getenv("OPENAI_API_KEY"));
26
+ return OpenAiApi.builder()
27
+ .apiKey(System.getenv("OPENAI_API_KEY"))
28
+ .build();
27
29
}
28
30
29
31
@Bean
30
32
public OpenAiChatModel openAiClient(OpenAiApi openAiApi) {
31
- return new OpenAiChatModel(openAiApi);
33
+ return OpenAiChatModel.builder()
34
+ .openAiApi(openAiApi)
35
+ .build();
32
36
}
33
37
34
38
}
@@ -46,10 +50,8 @@ public class ChatService {
46
50
// Create a chat prompt
47
51
Prompt prompt = new Prompt("What is the weather like today?");
48
52
49
- ChatClient chatClient = ChatClient.builder(this.chatModel).build();
50
-
51
53
// Get the chat response
52
- ChatResponse response = chatClient .call(prompt);
54
+ ChatResponse response = this.chatModel .call(prompt);
53
55
54
56
// Access the usage information
55
57
Usage usage = response.getMetadata().getUsage();
@@ -80,6 +82,26 @@ public class ChatService {
80
82
}
81
83
```
82
84
85
+ === Using with ChatClient
86
+
87
+ If you are using the `ChatClient`, you can access the usage information using the `ChatResponse` object:
88
+
89
+ ```java
90
+ // Create a chat prompt
91
+ Prompt prompt = new Prompt("What is the weather like today?");
92
+
93
+ // Create a chat client
94
+ ChatClient chatClient = ChatClient.create(chatModel);
95
+
96
+ // Get the chat response
97
+ ChatResponse response = chatClient.prompt(prompt)
98
+ .call()
99
+ .chatResponse();
100
+
101
+ // Access the usage information
102
+ Usage usage = response.getMetadata().getUsage();
103
+ ```
104
+
83
105
== Benefits
84
106
85
107
**Standardization**: Provides a consistent way to handle usage across different AI models
0 commit comments