Skip to content

Commit 58efee6

Browse files
ilayaperumalgmarkpollack
authored andcommitted
Refactor OpenAI chat options
- Deprecate existing OpenAI chat options - Add the options' methods by removing the prefix "with" - Update the following options - OpenAI chat options - OpenAI image options - OpenAI moderation options - OpenAI embedding options - OpenAI audiospeech options - OpenAI audio transcription options - Update referecens and docs - Deprecate 'with' methods in builders
1 parent c14618f commit 58efee6

File tree

53 files changed

+736
-303
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+736
-303
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ public class OpenAiAudioSpeechModel implements SpeechModel, StreamingSpeechModel
7979
public OpenAiAudioSpeechModel(OpenAiAudioApi audioApi) {
8080
this(audioApi,
8181
OpenAiAudioSpeechOptions.builder()
82-
.withModel(OpenAiAudioApi.TtsModel.TTS_1.getValue())
83-
.withResponseFormat(AudioResponseFormat.MP3)
84-
.withVoice(OpenAiAudioApi.SpeechRequest.Voice.ALLOY)
85-
.withSpeed(SPEED)
82+
.model(OpenAiAudioApi.TtsModel.TTS_1.getValue())
83+
.responseFormat(AudioResponseFormat.MP3)
84+
.voice(OpenAiAudioApi.SpeechRequest.Voice.ALLOY)
85+
.speed(SPEED)
8686
.build());
8787
}
8888

@@ -189,12 +189,12 @@ private OpenAiAudioApi.SpeechRequest createRequest(SpeechPrompt request) {
189189
private OpenAiAudioSpeechOptions merge(OpenAiAudioSpeechOptions source, OpenAiAudioSpeechOptions target) {
190190
OpenAiAudioSpeechOptions.Builder mergedBuilder = OpenAiAudioSpeechOptions.builder();
191191

192-
mergedBuilder.withModel(source.getModel() != null ? source.getModel() : target.getModel());
193-
mergedBuilder.withInput(source.getInput() != null ? source.getInput() : target.getInput());
194-
mergedBuilder.withVoice(source.getVoice() != null ? source.getVoice() : target.getVoice());
195-
mergedBuilder.withResponseFormat(
192+
mergedBuilder.model(source.getModel() != null ? source.getModel() : target.getModel());
193+
mergedBuilder.input(source.getInput() != null ? source.getInput() : target.getInput());
194+
mergedBuilder.voice(source.getVoice() != null ? source.getVoice() : target.getVoice());
195+
mergedBuilder.responseFormat(
196196
source.getResponseFormat() != null ? source.getResponseFormat() : target.getResponseFormat());
197-
mergedBuilder.withSpeed(source.getSpeed() != null ? source.getSpeed() : target.getSpeed());
197+
mergedBuilder.speed(source.getSpeed() != null ? source.getSpeed() : target.getSpeed());
198198

199199
return mergedBuilder.build();
200200
}

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*
2929
* @author Ahmed Yousri
3030
* @author Hyunjoon Choi
31+
* @author Ilayaperumal Gopinathan
3132
* @since 1.0.0-M1
3233
*/
3334
@JsonInclude(JsonInclude.Include.NON_NULL)
@@ -186,26 +187,71 @@ public static class Builder {
186187

187188
private final OpenAiAudioSpeechOptions options = new OpenAiAudioSpeechOptions();
188189

190+
public Builder model(String model) {
191+
this.options.model = model;
192+
return this;
193+
}
194+
195+
public Builder input(String input) {
196+
this.options.input = input;
197+
return this;
198+
}
199+
200+
public Builder voice(Voice voice) {
201+
this.options.voice = voice;
202+
return this;
203+
}
204+
205+
public Builder responseFormat(AudioResponseFormat responseFormat) {
206+
this.options.responseFormat = responseFormat;
207+
return this;
208+
}
209+
210+
public Builder speed(Float speed) {
211+
this.options.speed = speed;
212+
return this;
213+
}
214+
215+
/**
216+
* @deprecated use {@link #model(String)} instead.
217+
*/
218+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
189219
public Builder withModel(String model) {
190220
this.options.model = model;
191221
return this;
192222
}
193223

224+
/**
225+
* @deprecated use {@link #input(String)} instead.
226+
*/
227+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
194228
public Builder withInput(String input) {
195229
this.options.input = input;
196230
return this;
197231
}
198232

233+
/**
234+
* @deprecated use {@link #voice(Voice)} instead.
235+
*/
236+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
199237
public Builder withVoice(Voice voice) {
200238
this.options.voice = voice;
201239
return this;
202240
}
203241

242+
/**
243+
* @deprecated use {@link #responseFormat(AudioResponseFormat)} instead.
244+
*/
245+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
204246
public Builder withResponseFormat(AudioResponseFormat responseFormat) {
205247
this.options.responseFormat = responseFormat;
206248
return this;
207249
}
208250

251+
/**
252+
* @deprecated use {@link #speed(Float)} instead.
253+
*/
254+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
209255
public Builder withSpeed(Float speed) {
210256
this.options.speed = speed;
211257
return this;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public class OpenAiAudioTranscriptionModel implements Model<AudioTranscriptionPr
6363
public OpenAiAudioTranscriptionModel(OpenAiAudioApi audioApi) {
6464
this(audioApi,
6565
OpenAiAudioTranscriptionOptions.builder()
66-
.withModel(OpenAiAudioApi.WhisperModel.WHISPER_1.getValue())
67-
.withResponseFormat(OpenAiAudioApi.TranscriptResponseFormat.JSON)
68-
.withTemperature(0.7f)
66+
.model(OpenAiAudioApi.WhisperModel.WHISPER_1.getValue())
67+
.responseFormat(OpenAiAudioApi.TranscriptResponseFormat.JSON)
68+
.temperature(0.7f)
6969
.build());
7070
}
7171

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* @author Michael Lavelle
3131
* @author Christian Tzolov
3232
* @author Piotr Olaszewski
33+
* @author Ilayaperumal Gopinathan
3334
* @since 0.8.1
3435
*/
3536
@JsonInclude(Include.NON_NULL)
@@ -181,31 +182,85 @@ public Builder(OpenAiAudioTranscriptionOptions options) {
181182
this.options = options;
182183
}
183184

185+
public Builder model(String model) {
186+
this.options.model = model;
187+
return this;
188+
}
189+
190+
public Builder language(String language) {
191+
this.options.language = language;
192+
return this;
193+
}
194+
195+
public Builder prompt(String prompt) {
196+
this.options.prompt = prompt;
197+
return this;
198+
}
199+
200+
public Builder responseFormat(TranscriptResponseFormat responseFormat) {
201+
this.options.responseFormat = responseFormat;
202+
return this;
203+
}
204+
205+
public Builder temperature(Float temperature) {
206+
this.options.temperature = temperature;
207+
return this;
208+
}
209+
210+
public Builder granularityType(GranularityType granularityType) {
211+
this.options.granularityType = granularityType;
212+
return this;
213+
}
214+
215+
/**
216+
* @deprecated use {@link #model( String)} instead.
217+
*/
218+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
184219
public Builder withModel(String model) {
185220
this.options.model = model;
186221
return this;
187222
}
188223

224+
/**
225+
* @deprecated use {@link #language( String)} instead.
226+
*/
227+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
189228
public Builder withLanguage(String language) {
190229
this.options.language = language;
191230
return this;
192231
}
193232

233+
/**
234+
* @deprecated use {@link #prompt( String)} instead.
235+
*/
236+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
194237
public Builder withPrompt(String prompt) {
195238
this.options.prompt = prompt;
196239
return this;
197240
}
198241

242+
/**
243+
* @deprecated use {@link #responseFormat( TranscriptResponseFormat)} instead.
244+
*/
245+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
199246
public Builder withResponseFormat(TranscriptResponseFormat responseFormat) {
200247
this.options.responseFormat = responseFormat;
201248
return this;
202249
}
203250

251+
/**
252+
* @deprecated use {@link #temperature( Float)} instead.
253+
*/
254+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
204255
public Builder withTemperature(Float temperature) {
205256
this.options.temperature = temperature;
206257
return this;
207258
}
208259

260+
/**
261+
* @deprecated use {@link #granularityType( GranularityType)} instead.
262+
*/
263+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
209264
public Builder withGranularityType(GranularityType granularityType) {
210265
this.options.granularityType = granularityType;
211266
return this;

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ public class OpenAiChatModel extends AbstractToolCallSupport implements ChatMode
143143
* @throws IllegalArgumentException if openAiApi is null
144144
*/
145145
public OpenAiChatModel(OpenAiApi openAiApi) {
146-
this(openAiApi,
147-
OpenAiChatOptions.builder().withModel(OpenAiApi.DEFAULT_CHAT_MODEL).withTemperature(0.7).build());
146+
this(openAiApi, OpenAiChatOptions.builder().model(OpenAiApi.DEFAULT_CHAT_MODEL).temperature(0.7).build());
148147
}
149148

150149
/**
@@ -588,13 +587,13 @@ else if (message.getMessageType() == MessageType.TOOL) {
588587
if (!CollectionUtils.isEmpty(enabledToolsToUse)) {
589588

590589
request = ModelOptionsUtils.merge(
591-
OpenAiChatOptions.builder().withTools(this.getFunctionTools(enabledToolsToUse)).build(), request,
590+
OpenAiChatOptions.builder().tools(this.getFunctionTools(enabledToolsToUse)).build(), request,
592591
ChatCompletionRequest.class);
593592
}
594593
// Remove `streamOptions` from the request if it is not a streaming request
595594
if (request.streamOptions() != null && !stream) {
596595
logger.warn("Removing streamOptions from the request as it is not a streaming request!");
597-
request = request.withStreamOptions(null);
596+
request = request.streamOptions(null);
598597
}
599598

600599
return request;

0 commit comments

Comments
 (0)