Skip to content

Commit d66549e

Browse files
committed
GH-2743: Correct erroneous content in documents
Fixes GH-2743 (#2743) Revised the following files to ensure accuracy: - `openai-chat.adoc` - `openai-embeddings.adoc` - `usage-handling.adoc` Fixes #2743 Signed-off-by: YunKui Lu <[email protected]>
1 parent 15a5069 commit d66549e

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-chat.adoc

+7-4
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,9 @@ Next, create an `OpenAiChatModel` and use it for text generations:
514514

515515
[source,java]
516516
----
517-
var openAiApi = new OpenAiApi(System.getenv("OPENAI_API_KEY"));
517+
var openAiApi = OpenAiApi.builder()
518+
.apiKey(System.getenv("OPENAI_API_KEY"))
519+
.build();
518520
var openAiChatOptions = OpenAiChatOptions.builder()
519521
.model("gpt-3.5-turbo")
520522
.temperature(0.4)
@@ -531,7 +533,7 @@ Flux<ChatResponse> response = this.chatModel.stream(
531533
----
532534

533535
The `OpenAiChatOptions` provides the configuration information for the chat requests.
534-
The `OpenAiChatOptions.Builder` is a fluent options-builder.
536+
The `OpenAiApi.Builder` and `OpenAiChatOptions.Builder` are fluent options-builders for API client and chat config respectively.
535537

536538
== Low-level OpenAiApi Client [[low-level-api]]
537539

@@ -545,8 +547,9 @@ Here is a simple snippet showing how to use the API programmatically:
545547

546548
[source,java]
547549
----
548-
OpenAiApi openAiApi =
549-
new OpenAiApi(System.getenv("OPENAI_API_KEY"));
550+
OpenAiApi openAiApi = OpenAiApi.builder()
551+
.apiKey(System.getenv("OPENAI_API_KEY"))
552+
.build();
550553
551554
ChatCompletionMessage chatCompletionMessage =
552555
new ChatCompletionMessage("Hello world", Role.USER);

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/openai-embeddings.adoc

+4-2
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ Next, create an `OpenAiEmbeddingModel` instance and use it to compute the simila
212212

213213
[source,java]
214214
----
215-
var openAiApi = new OpenAiApi(System.getenv("OPENAI_API_KEY"));
215+
var openAiApi = OpenAiApi.builder()
216+
.apiKey(System.getenv("OPENAI_API_KEY"))
217+
.build();
216218
217219
var embeddingModel = new OpenAiEmbeddingModel(
218220
this.openAiApi,
@@ -228,6 +230,6 @@ EmbeddingResponse embeddingResponse = this.embeddingModel
228230
----
229231

230232
The `OpenAiEmbeddingOptions` provides the configuration information for the embedding requests.
231-
The options class offers a `builder()` for easy options creation.
233+
The api and options class offers a `builder()` for easy options creation.
232234

233235

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/usage-handling.adoc

+29-7
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,26 @@ Object getNativeUsage();
1313
```
1414
This method allows access to the model-specific native usage data, enabling more detailed usage tracking when needed.
1515

16-
=== Using with ChatClient
16+
=== Using with ChatModel
1717

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:
1919

2020
```java
2121
@SpringBootConfiguration
2222
public class Configuration {
2323

2424
@Bean
2525
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();
2729
}
2830

2931
@Bean
3032
public OpenAiChatModel openAiClient(OpenAiApi openAiApi) {
31-
return new OpenAiChatModel(openAiApi);
33+
return OpenAiChatModel.builder()
34+
.openAiApi(openAiApi)
35+
.build();
3236
}
3337

3438
}
@@ -46,10 +50,8 @@ public class ChatService {
4650
// Create a chat prompt
4751
Prompt prompt = new Prompt("What is the weather like today?");
4852

49-
ChatClient chatClient = ChatClient.builder(this.chatModel).build();
50-
5153
// Get the chat response
52-
ChatResponse response = chatClient.call(prompt);
54+
ChatResponse response = this.chatModel.call(prompt);
5355

5456
// Access the usage information
5557
Usage usage = response.getMetadata().getUsage();
@@ -80,6 +82,26 @@ public class ChatService {
8082
}
8183
```
8284

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+
83105
== Benefits
84106

85107
**Standardization**: Provides a consistent way to handle usage across different AI models

0 commit comments

Comments
 (0)