You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spring-ai-docs/src/main/antora/modules/ROOT/pages/api/clients/openai-chat.adoc
+94-29Lines changed: 94 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
= OpenAI
1
+
= OpenAI Chat
2
2
3
3
Spring AI supports ChatGPT, the AI language model by OpenAI. ChatGPT has been instrumental in sparking interest in AI-driven text generation, thanks to its creation of industry-leading text generation models and embeddings.
4
4
@@ -17,11 +17,80 @@ Exporting an environment variable is one way to set that configuration property:
17
17
export SPRING_AI_OPENAI_API_KEY=<INSERT KEY HERE>
18
18
----
19
19
20
-
== Project Dependencies
20
+
=== Configure the OpenAI Chat Client Manually
21
21
22
-
Refer to the xref:getting-started.adoc#_dependency_management[Dependency Management] section to add Milestone and/or Snapshot Repositories to your build file.
22
+
Add the `spring-ai-openai` dependency to your project's Maven `pom.xml` file:
NOTE: The `spring-ai-openai` dependency provides access also to the `OpenAiEmbeddingClient`. For more information about the `OpenAiEmbeddingClient` refer to the link:../embeddings/openai-embeddings.html[OpenAI Embeddings Client] section.
42
+
43
+
Next, create an `OpenAiChatClient` instance and use it to compute the similarity between two input texts:
44
+
45
+
[source,java]
46
+
----
47
+
var openAiApi = new OpenAiApi(System.getenv("OPENAI_API_KEY"));
48
+
49
+
var chatClient = new OpenAiChatClient(openAiApi)
50
+
.withDefaultOptions(OpenAiChatOptions.builder()
51
+
.withModel("gpt-35-turbo")
52
+
.withTemperature(0.4)
53
+
.withMaxTokens(200)
54
+
.build());
55
+
56
+
ChatResponse response = chatClient.call(
57
+
new Prompt("Generate the names of 5 famous pirates."));
58
+
59
+
// Or with streaming responses
60
+
Flux<ChatResponse> response = chatClient.stream(
61
+
new Prompt("Generate the names of 5 famous pirates."));
62
+
----
63
+
64
+
The `OpenAiChatOptions` provides the configuration information for the chat requests.
65
+
The `OpenAiChatOptions.Builder` is fluent options builder.
66
+
67
+
==== OpenAiChatOptions
68
+
69
+
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java[OpenAiChatOptions.java] provides provides the configuration information for the chat requests, such as the model to use, the temperature, the frequency penalty, etc.
70
+
71
+
The default options can be configured using the `spring.ai.openai.chat.options` properties as well.
23
72
24
-
Then add the Spring Boot Starter dependency to your project's Maven `pom.xml` build file:
73
+
On start-time use the `OpenAiChatClient#withDefaultOptions()` to set the default options applicable for all chat completion requests.
74
+
At run-time you can override the default options with `OpenAiChatOptions` instance in the request `Prompt`.
75
+
76
+
For example to override the default model name and temperature for a specific request:
77
+
78
+
[source,java]
79
+
----
80
+
ChatResponse response = chatClient.call(
81
+
new Prompt(
82
+
"Generate the names of 5 famous pirates.",
83
+
AzureOpenAiChatOptions.builder()
84
+
.withModel("gpt-4-32k")
85
+
.withTemperature(0.4)
86
+
.build()
87
+
));
88
+
----
89
+
90
+
=== OpenAiChatClient Auto-configuration
91
+
92
+
Spring AI provides Spring Boot auto-configuration for the OpenAI Chat Client.
93
+
To enable it add the following dependency to your project's Maven `pom.xml` file:
25
94
26
95
[source, xml]
27
96
----
@@ -41,12 +110,29 @@ dependencies {
41
110
}
42
111
----
43
112
113
+
NOTE: Refer to the xref:getting-started.adoc#_dependency_management[Dependency Management] section to add Milestone and/or Snapshot Repositories to your build file.
114
+
115
+
The Spring AI project defines a configuration property named `spring.ai.openai.api-key` that you should set to the value of the `API Key` obtained from openai.com.
116
+
117
+
Exporting an environment variable is one way to set that configuration property:
118
+
119
+
[source,shell]
120
+
----
121
+
export SPRING_AI_OPENAI_API_KEY=<INSERT KEY HERE>
122
+
----
44
123
45
-
== Sample Code
124
+
==== Sample Code
46
125
47
126
This will create a `ChatClient` implementation that you can inject into your class.
48
127
Here is an example of a simple `@Controller` class that uses the `ChatClient` implementation.
49
128
129
+
[source,application.properties]
130
+
----
131
+
spring.ai.openai.api-key=YOUR_API_KEY
132
+
spring.ai.openai.chat.options.model=gpt-35-turbo
133
+
spring.ai.openai.chat.options.temperature=0.7
134
+
----
135
+
50
136
[source,java]
51
137
----
52
138
@RestController
@@ -74,18 +160,11 @@ public class ChatController {
74
160
}
75
161
----
76
162
77
-
== OpenAiChatOptions
78
-
79
-
The http://OpenAiChatOptions.java[OpenAiChatOptions.java] allows you to configure OpenAI options, such as the model to use, the temperature, the frequency penalty, etc.
80
-
You can assign the default options, at startup, to the `OpenAiChatClient` using the `withDefaultOptions()` method. You can also override the default options at runtime by passing in the `OpenAiChatOptions` object to the `Prompt` constructor.
81
-
82
-
The default options can be configured using the `spring.ai.openai.chat.options` properties as well.
83
-
84
-
== OpenAI Properties
163
+
== OpenAI Chat Properties
85
164
86
165
The prefix `spring.ai.openai` is used as the property prefix that lets you connect to OpenAI.
87
166
88
-
[cols="3,5,3"]
167
+
[cols="3,5,1"]
89
168
|====
90
169
| Property | Description | Default
91
170
@@ -95,7 +174,7 @@ The prefix `spring.ai.openai` is used as the property prefix that lets you conne
95
174
96
175
The prefix `spring.ai.openai.chat` is the property prefix that lets you configure the `ChatClient` implementation for OpenAI.
97
176
98
-
[cols="3,5,3"]
177
+
[cols="3,5,1"]
99
178
|====
100
179
| Property | Description | Default
101
180
@@ -117,21 +196,7 @@ The prefix `spring.ai.openai.chat` is the property prefix that lets you configur
117
196
| spring.ai.openai.chat.options.user | A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. | -
118
197
|====
119
198
120
-
The prefix `spring.ai.openai.embedding` is property prefix that configures the `EmbeddingClient` implementation for OpenAI.
121
-
122
-
[cols="3,5,3"]
123
-
|====
124
-
| Property | Description | Default
125
-
| spring.ai.openai.embedding.base-url | Optional overrides the spring.ai.openai.base-url to provide chat specific url | -
126
-
| spring.ai.openai.embedding.api-key | Optional overrides the spring.ai.openai.api-key to provide chat specific api-key | -
127
-
| spring.ai.openai.embedding.options.model | The model to use | text-embedding-ada-002
128
-
| spring.ai.openai.embedding.options.encodingFormat | The format to return the embeddings in. Can be either float or base64. | -
129
-
| spring.ai.openai.embedding.options.user | A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. | -
130
-
|====
131
199
132
200
NOTE: You can override the common `spring.ai.openai.base-url` and `spring.ai.openai.api-key` for the `ChatClient` and `EmbeddingClient` implementations.
133
201
The `spring.ai.openai.chat.base-url` and `spring.ai.openai.chat.api-key` properties if set take precedence over the common properties.
134
-
Similarly, the `spring.ai.openai.embedding.base-url` and `spring.ai.openai.embedding.api-key` properties if set take precedence over the common properties.
135
202
This is useful if you want to use different OpenAI accounts for different models and different model endpoints.
136
-
137
-
Also by default, the `spring.ai.openai.chat.options.model` is set to `gpt-35-turbo` and the `spring.ai.openai.embedding.options.model` is set to `text-embedding-ada-002`.
Copy file name to clipboardExpand all lines: spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/openai-embeddings.adoc
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ Exporting an environment variable is one way to set that configuration property:
19
19
export SPRING_AI_OPENAI_API_KEY=<INSERT KEY HERE>
20
20
----
21
21
22
-
== Configure the OpenAI Embedding Client Manually
22
+
=== Configure the OpenAI Embedding Client Manually
23
23
24
24
Add the `spring-ai-openai` dependency to your project's Maven `pom.xml` file:
25
25
[source, xml]
@@ -40,7 +40,7 @@ dependencies {
40
40
}
41
41
----
42
42
43
-
NOTE: The `spring-ai-openai` dependency provides access also to the `OpenAiChatClient`. For more information about the `AzureOpenAiChatClient` refer to the link:../Clients/openai.html[OpenAI Chat Client] section.
43
+
NOTE: The `spring-ai-openai` dependency provides access also to the `OpenAiChatClient`. For more information about the `OpenAiChatClient` refer to the link:../clients/openai-chat.html[OpenAI Chat Client] section.
44
44
45
45
Next, create an `OpenAiEmbeddingClient` instance and use it to compute the similarity between two input texts:
The `OpenAiEmbeddingOptions` provides the configuration information for the embedding requests.
62
62
The options class offers a `builder()` for easy options creation.
63
63
64
-
== OpenAiEmbeddingOptions
64
+
==== OpenAiEmbeddingOptions
65
65
66
-
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiEmbeddingOptions.java[OpenAiEmbeddingOptions.java] provide the OpenAI configures, such as the model to use, the temperature, the frequency penalty, etc.
66
+
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiEmbeddingOptions.java[OpenAiEmbeddingOptions.java] provides the OpenAI configures, such as the model to use, the temperature, the frequency penalty, etc.
67
67
68
68
The default options can be configured using the `spring.ai.openai.embedding.options` properties as well.
0 commit comments