Skip to content

Commit 94bc79d

Browse files
committed
refactor(spring-ai-openai): fix import style
Signed-off-by: SenreySong <25841017+SenreySong@users.noreply.github.com>
1 parent 7ba3908 commit 94bc79d

File tree

9 files changed

+397
-600
lines changed

9 files changed

+397
-600
lines changed

models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatModel.java

Lines changed: 92 additions & 148 deletions
Large diffs are not rendered by default.

models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java

Lines changed: 215 additions & 228 deletions
Large diffs are not rendered by default.

models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiApi.java

Lines changed: 68 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public ResponseEntity<ChatCompletion> chatCompletionEntity(ChatCompletionRequest
198198
headers.addAll(additionalHttpHeader);
199199
addDefaultHeadersIfMissing(headers);
200200
})
201-
.body(dynamicRequestBody)
201+
.body(dynamicRequestBody)
202202
.retrieve()
203203
.toEntity(ChatCompletion.class);
204204
// @formatter:on
@@ -263,14 +263,12 @@ public Flux<ChatCompletionChunk> chatCompletionStream(ChatCompletionRequest chat
263263
}
264264
Object dynamicBody = createDynamicRequestBody(chatRequest);
265265
// @formatter:off
266-
return this.webClient
267-
.post()
268-
.uri(this.completionsPath)
269-
.headers(
270-
headers -> {
271-
headers.addAll(additionalHttpHeader);
272-
addDefaultHeadersIfMissing(headers);
273-
}) // @formatter:on
266+
return this.webClient.post()
267+
.uri(this.completionsPath)
268+
.headers(headers -> {
269+
headers.addAll(additionalHttpHeader);
270+
addDefaultHeadersIfMissing(headers);
271+
}) // @formatter:on
274272
.bodyValue(dynamicBody)
275273
.retrieve()
276274
.bodyToFlux(String.class)
@@ -312,13 +310,13 @@ public Flux<ChatCompletionChunk> chatCompletionStream(ChatCompletionRequest chat
312310
/**
313311
* Creates an embedding vector representing the input text or token array.
314312
* @param embeddingRequest The embedding request.
313+
* @return Returns list of {@link Embedding} wrapped in {@link EmbeddingList}.
315314
* @param <T> Type of the entity in the data list. Can be a {@link String} or
316315
* {@link List} of tokens (e.g. Integers). For embedding multiple inputs in a single
317316
* request, You can pass a {@link List} of {@link String} or {@link List} of
318317
* {@link List} of tokens. For example:
319318
*
320319
* <pre>{@code List.of("text1", "text2", "text3") or List.of(List.of(1, 2, 3), List.of(3, 4, 5))} </pre>
321-
* @return Returns list of {@link Embedding} wrapped in {@link EmbeddingList}.
322320
*/
323321
public <T> ResponseEntity<EmbeddingList<Embedding>> embeddings(EmbeddingRequest<T> embeddingRequest) {
324322

@@ -1126,37 +1124,36 @@ public enum OutputModality {
11261124
*/
11271125
@JsonInclude(Include.NON_NULL)
11281126
public record ChatCompletionRequest( // @formatter:off
1129-
@JsonProperty("messages") List<ChatCompletionMessage> messages,
1130-
@JsonProperty("model") String model,
1131-
@JsonProperty("store") Boolean store,
1132-
@JsonProperty("metadata") Map<String, String> metadata,
1133-
@JsonProperty("frequency_penalty") Double frequencyPenalty,
1134-
@JsonProperty("logit_bias") Map<String, Integer> logitBias,
1135-
@JsonProperty("logprobs") Boolean logprobs,
1136-
@JsonProperty("top_logprobs") Integer topLogprobs,
1137-
@JsonProperty("max_tokens") Integer maxTokens, // original field for specifying token usage.
1138-
@JsonProperty("max_completion_tokens")
1139-
Integer maxCompletionTokens, // new field for gpt-o1 and other reasoning models
1140-
@JsonProperty("n") Integer n,
1141-
@JsonProperty("modalities") List<OutputModality> outputModalities,
1142-
@JsonProperty("audio") AudioParameters audioParameters,
1143-
@JsonProperty("presence_penalty") Double presencePenalty,
1144-
@JsonProperty("response_format") ResponseFormat responseFormat,
1145-
@JsonProperty("seed") Integer seed,
1146-
@JsonProperty("service_tier") String serviceTier,
1147-
@JsonProperty("stop") List<String> stop,
1148-
@JsonProperty("stream") Boolean stream,
1149-
@JsonProperty("stream_options") StreamOptions streamOptions,
1150-
@JsonProperty("temperature") Double temperature,
1151-
@JsonProperty("top_p") Double topP,
1152-
@JsonProperty("tools") List<FunctionTool> tools,
1153-
@JsonProperty("tool_choice") Object toolChoice,
1154-
@JsonProperty("parallel_tool_calls") Boolean parallelToolCalls,
1155-
@JsonProperty("user") String user,
1156-
@JsonProperty("reasoning_effort") String reasoningEffort,
1157-
@JsonProperty("web_search_options") WebSearchOptions webSearchOptions,
1158-
@JsonProperty("verbosity") String verbosity,
1159-
@JsonProperty("extra_body") Map<String, Object> extraBody) {
1127+
@JsonProperty("messages") List<ChatCompletionMessage> messages,
1128+
@JsonProperty("model") String model,
1129+
@JsonProperty("store") Boolean store,
1130+
@JsonProperty("metadata") Map<String, String> metadata,
1131+
@JsonProperty("frequency_penalty") Double frequencyPenalty,
1132+
@JsonProperty("logit_bias") Map<String, Integer> logitBias,
1133+
@JsonProperty("logprobs") Boolean logprobs,
1134+
@JsonProperty("top_logprobs") Integer topLogprobs,
1135+
@JsonProperty("max_tokens") Integer maxTokens, // original field for specifying token usage.
1136+
@JsonProperty("max_completion_tokens") Integer maxCompletionTokens, // new field for gpt-o1 and other reasoning models
1137+
@JsonProperty("n") Integer n,
1138+
@JsonProperty("modalities") List<OutputModality> outputModalities,
1139+
@JsonProperty("audio") AudioParameters audioParameters,
1140+
@JsonProperty("presence_penalty") Double presencePenalty,
1141+
@JsonProperty("response_format") ResponseFormat responseFormat,
1142+
@JsonProperty("seed") Integer seed,
1143+
@JsonProperty("service_tier") String serviceTier,
1144+
@JsonProperty("stop") List<String> stop,
1145+
@JsonProperty("stream") Boolean stream,
1146+
@JsonProperty("stream_options") StreamOptions streamOptions,
1147+
@JsonProperty("temperature") Double temperature,
1148+
@JsonProperty("top_p") Double topP,
1149+
@JsonProperty("tools") List<FunctionTool> tools,
1150+
@JsonProperty("tool_choice") Object toolChoice,
1151+
@JsonProperty("parallel_tool_calls") Boolean parallelToolCalls,
1152+
@JsonProperty("user") String user,
1153+
@JsonProperty("reasoning_effort") String reasoningEffort,
1154+
@JsonProperty("web_search_options") WebSearchOptions webSearchOptions,
1155+
@JsonProperty("verbosity") String verbosity,
1156+
@JsonProperty("extra_body") Map<String, Object> extraBody) {
11601157

11611158
/**
11621159
* Shortcut constructor for a chat completion request with the given messages, model and temperature.
@@ -1166,37 +1163,9 @@ public record ChatCompletionRequest( // @formatter:off
11661163
* @param temperature What sampling temperature to use, between 0 and 1.
11671164
*/
11681165
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model, Double temperature) {
1169-
this(
1170-
messages,
1171-
model,
1172-
null,
1173-
null,
1174-
null,
1175-
null,
1176-
null,
1177-
null,
1178-
null,
1179-
null,
1180-
null,
1181-
null,
1182-
null,
1183-
null,
1184-
null,
1185-
null,
1186-
null,
1187-
null,
1188-
false,
1189-
null,
1190-
temperature,
1191-
null,
1192-
null,
1193-
null,
1194-
null,
1195-
null,
1196-
null,
1197-
null,
1198-
null,
1199-
null);
1166+
this(messages, model, null, null, null, null, null, null, null, null, null, null, null, null, null,
1167+
null, null, null, false, null, temperature, null,
1168+
null, null, null, null, null, null, null,null);
12001169
}
12011170

12021171
/**
@@ -1207,37 +1176,10 @@ public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model,
12071176
* @param audio Parameters for audio output. Required when audio output is requested with outputModalities: ["audio"].
12081177
*/
12091178
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model, AudioParameters audio, boolean stream) {
1210-
this(
1211-
messages,
1212-
model,
1213-
null,
1214-
null,
1215-
null,
1216-
null,
1217-
null,
1218-
null,
1219-
null,
1220-
null,
1221-
null,
1222-
List.of(OutputModality.AUDIO, OutputModality.TEXT),
1223-
audio,
1224-
null,
1225-
null,
1226-
null,
1227-
null,
1228-
null,
1229-
stream,
1230-
null,
1231-
null,
1232-
null,
1233-
null,
1234-
null,
1235-
null,
1236-
null,
1237-
null,
1238-
null,
1239-
null,
1240-
null);
1179+
this(messages, model, null, null, null, null, null, null,
1180+
null, null, null, List.of(OutputModality.AUDIO, OutputModality.TEXT), audio, null, null,
1181+
null, null, null, stream, null, null, null,
1182+
null, null, null, null, null, null, null,null);
12411183
}
12421184

12431185
/**
@@ -1250,37 +1192,9 @@ public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model,
12501192
* as they become available, with the stream terminated by a data: [DONE] message.
12511193
*/
12521194
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model, Double temperature, boolean stream) {
1253-
this(
1254-
messages,
1255-
model,
1256-
null,
1257-
null,
1258-
null,
1259-
null,
1260-
null,
1261-
null,
1262-
null,
1263-
null,
1264-
null,
1265-
null,
1266-
null,
1267-
null,
1268-
null,
1269-
null,
1270-
null,
1271-
null,
1272-
stream,
1273-
null,
1274-
temperature,
1275-
null,
1276-
null,
1277-
null,
1278-
null,
1279-
null,
1280-
null,
1281-
null,
1282-
null,
1283-
null);
1195+
this(messages, model, null, null, null, null, null, null, null, null, null,
1196+
null, null, null, null, null, null, null, stream, null, temperature, null,
1197+
null, null, null, null, null, null, null,null);
12841198
}
12851199

12861200
/**
@@ -1294,37 +1208,9 @@ public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model,
12941208
*/
12951209
public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model,
12961210
List<FunctionTool> tools, Object toolChoice) {
1297-
this(
1298-
messages,
1299-
model,
1300-
null,
1301-
null,
1302-
null,
1303-
null,
1304-
null,
1305-
null,
1306-
null,
1307-
null,
1308-
null,
1309-
null,
1310-
null,
1311-
null,
1312-
null,
1313-
null,
1314-
null,
1315-
null,
1316-
false,
1317-
null,
1318-
0.8,
1319-
null,
1320-
tools,
1321-
toolChoice,
1322-
null,
1323-
null,
1324-
null,
1325-
null,
1326-
null,
1327-
null);
1211+
this(messages, model, null, null, null, null, null, null, null, null, null,
1212+
null, null, null, null, null, null, null, false, null, 0.8, null,
1213+
tools, toolChoice, null, null, null, null, null,null);
13281214
}
13291215

13301216
/**
@@ -1335,10 +1221,9 @@ public ChatCompletionRequest(List<ChatCompletionMessage> messages, String model,
13351221
* as they become available, with the stream terminated by a data: [DONE] message.
13361222
*/
13371223
public ChatCompletionRequest(List<ChatCompletionMessage> messages, Boolean stream) {
1338-
this(
1339-
messages, null, null, null, null, null, null, null, null, null, null, null, null, null,
1340-
null, null, null, null, stream, null, null, null, null, null, null, null, null, null,
1341-
null, null);
1224+
this(messages, null, null, null, null, null, null, null, null, null, null, null, null, null,
1225+
null, null, null, null, stream, null, null, null, null, null, null, null, null, null,
1226+
null, null);
13421227
}
13431228

13441229
/**
@@ -1348,37 +1233,10 @@ public ChatCompletionRequest(List<ChatCompletionMessage> messages, Boolean strea
13481233
* @return A new {@link ChatCompletionRequest} with the specified stream options.
13491234
*/
13501235
public ChatCompletionRequest streamOptions(StreamOptions streamOptions) {
1351-
return new ChatCompletionRequest(
1352-
this.messages,
1353-
this.model,
1354-
this.store,
1355-
this.metadata,
1356-
this.frequencyPenalty,
1357-
this.logitBias,
1358-
this.logprobs,
1359-
this.topLogprobs,
1360-
this.maxTokens,
1361-
this.maxCompletionTokens,
1362-
this.n,
1363-
this.outputModalities,
1364-
this.audioParameters,
1365-
this.presencePenalty,
1366-
this.responseFormat,
1367-
this.seed,
1368-
this.serviceTier,
1369-
this.stop,
1370-
this.stream,
1371-
streamOptions,
1372-
this.temperature,
1373-
this.topP,
1374-
this.tools,
1375-
this.toolChoice,
1376-
this.parallelToolCalls,
1377-
this.user,
1378-
this.reasoningEffort,
1379-
this.webSearchOptions,
1380-
this.verbosity,
1381-
this.extraBody);
1236+
return new ChatCompletionRequest(this.messages, this.model, this.store, this.metadata, this.frequencyPenalty, this.logitBias, this.logprobs,
1237+
this.topLogprobs, this.maxTokens, this.maxCompletionTokens, this.n, this.outputModalities, this.audioParameters, this.presencePenalty,
1238+
this.responseFormat, this.seed, this.serviceTier, this.stop, this.stream, streamOptions, this.temperature, this.topP,
1239+
this.tools, this.toolChoice, this.parallelToolCalls, this.user, this.reasoningEffort, this.webSearchOptions, this.verbosity,this.extraBody);
13821240
}
13831241

13841242
/**
@@ -1583,17 +1441,16 @@ public String getValue() {
15831441
@JsonInclude(Include.NON_NULL)
15841442
@JsonIgnoreProperties(ignoreUnknown = true)
15851443
public record ChatCompletionMessage( // @formatter:off
1586-
@JsonProperty("content") Object rawContent,
1587-
@JsonProperty("role") Role role,
1588-
@JsonProperty("name") String name,
1589-
@JsonProperty("tool_call_id") String toolCallId,
1590-
@JsonProperty("tool_calls")
1591-
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
1592-
List<ToolCall> toolCalls,
1593-
@JsonProperty("refusal") String refusal,
1594-
@JsonProperty("audio") AudioOutput audioOutput,
1595-
@JsonProperty("annotations") List<Annotation> annotations,
1596-
@JsonProperty("reasoning_content") String reasoningContent) { // @formatter:on
1444+
@JsonProperty("content") Object rawContent,
1445+
@JsonProperty("role") Role role,
1446+
@JsonProperty("name") String name,
1447+
@JsonProperty("tool_call_id") String toolCallId,
1448+
@JsonProperty("tool_calls")
1449+
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY) List<ToolCall> toolCalls,
1450+
@JsonProperty("refusal") String refusal,
1451+
@JsonProperty("audio") AudioOutput audioOutput,
1452+
@JsonProperty("annotations") List<Annotation> annotations,
1453+
@JsonProperty("reasoning_content") String reasoningContent) { // @formatter:on
15971454

15981455
/**
15991456
* Create a chat completion message with the given content and role. All other

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/api/OpenAiApiBuilderTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@
4242
import org.springframework.web.client.ResponseErrorHandler;
4343
import org.springframework.web.client.RestClient;
4444
import org.springframework.web.reactive.function.client.WebClient;
45-
import static org.assertj.core.api.Assertions.*;
46-
import static org.mockito.Mockito.*;
45+
46+
import static org.assertj.core.api.Assertions.assertThat;
47+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
48+
import static org.mockito.Mockito.mock;
4749

4850
public class OpenAiApiBuilderTests {
4951

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/api/OpenAiApiIT.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
import org.springframework.ai.openai.api.OpenAiApi.EmbeddingList;
3737
import org.springframework.core.io.ClassPathResource;
3838
import org.springframework.http.ResponseEntity;
39-
import static org.assertj.core.api.Assertions.*;
39+
40+
import static org.assertj.core.api.Assertions.assertThat;
41+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4042

4143
/**
4244
* @author Christian Tzolov

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/api/OpenAiStreamFunctionCallingHelperTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323

2424
import org.junit.jupiter.api.Test;
2525
import org.mockito.Mockito;
26-
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
2726

28-
import static org.assertj.core.api.Assertions.*;
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
2929

3030
/**
3131
* Unit tests for {@link OpenAiStreamFunctionCallingHelper}

0 commit comments

Comments
 (0)