Skip to content

Commit 04a5b2a

Browse files
committed
Refactor OpenAIAudio builder methods
- Refactor the builder methods to remove `with` as the prefix. - Introduce new methods with updated naming conventions. - Deprecate the existing `with*` methods to maintain backward compatibility. Signed-off-by: Alexandros Pappas <[email protected]>
1 parent bfbc64b commit 04a5b2a

File tree

6 files changed

+184
-30
lines changed

6 files changed

+184
-30
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,11 @@ private OpenAiAudioApi.SpeechRequest createRequest(SpeechPrompt request) {
177177
: request.getInstructions().getText();
178178

179179
OpenAiAudioApi.SpeechRequest.Builder requestBuilder = OpenAiAudioApi.SpeechRequest.builder()
180-
.withModel(options.getModel())
181-
.withInput(input)
182-
.withVoice(options.getVoice())
183-
.withResponseFormat(options.getResponseFormat())
184-
.withSpeed(options.getSpeed());
180+
.model(options.getModel())
181+
.input(input)
182+
.voice(options.getVoice())
183+
.responseFormat(options.getResponseFormat())
184+
.speed(options.getSpeed());
185185

186186
return requestBuilder.build();
187187
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,13 @@ OpenAiAudioApi.TranscriptionRequest createRequest(AudioTranscriptionPrompt trans
168168
}
169169

170170
return OpenAiAudioApi.TranscriptionRequest.builder()
171-
.withFile(toBytes(transcriptionPrompt.getInstructions()))
172-
.withResponseFormat(options.getResponseFormat())
173-
.withPrompt(options.getPrompt())
174-
.withTemperature(options.getTemperature())
175-
.withLanguage(options.getLanguage())
176-
.withModel(options.getModel())
177-
.withGranularityType(options.getGranularityType())
171+
.file(toBytes(transcriptionPrompt.getInstructions()))
172+
.responseFormat(options.getResponseFormat())
173+
.prompt(options.getPrompt())
174+
.temperature(options.getTemperature())
175+
.language(options.getLanguage())
176+
.model(options.getModel())
177+
.granularityType(options.getGranularityType())
178178
.build();
179179
}
180180

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

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,31 +457,76 @@ public static class Builder {
457457

458458
private Float speed;
459459

460+
/**
461+
* @deprecated use {@link #model(String)} instead.
462+
*/
463+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
460464
public Builder withModel(String model) {
461465
this.model = model;
462466
return this;
463467
}
464468

469+
public Builder model(String model) {
470+
this.model = model;
471+
return this;
472+
}
473+
474+
/**
475+
* @deprecated use {@link #input(String)} instead.
476+
*/
477+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
465478
public Builder withInput(String input) {
466479
this.input = input;
467480
return this;
468481
}
469482

483+
public Builder input(String input) {
484+
this.input = input;
485+
return this;
486+
}
487+
488+
/**
489+
* @deprecated use {@link #voice(Voice)} instead.
490+
*/
491+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
470492
public Builder withVoice(Voice voice) {
471493
this.voice = voice;
472494
return this;
473495
}
474496

497+
public Builder voice(Voice voice) {
498+
this.voice = voice;
499+
return this;
500+
}
501+
502+
/**
503+
* @deprecated use {@link #responseFormat(AudioResponseFormat)} instead.
504+
*/
505+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
475506
public Builder withResponseFormat(AudioResponseFormat responseFormat) {
476507
this.responseFormat = responseFormat;
477508
return this;
478509
}
479510

511+
public Builder responseFormat(AudioResponseFormat responseFormat) {
512+
this.responseFormat = responseFormat;
513+
return this;
514+
}
515+
516+
/**
517+
* @deprecated use {@link #speed(Float)} instead.
518+
*/
519+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
480520
public Builder withSpeed(Float speed) {
481521
this.speed = speed;
482522
return this;
483523
}
484524

525+
public Builder speed(Float speed) {
526+
this.speed = speed;
527+
return this;
528+
}
529+
485530
public SpeechRequest build() {
486531
Assert.hasText(this.model, "model must not be empty");
487532
Assert.hasText(this.input, "input must not be empty");
@@ -569,41 +614,104 @@ public static class Builder {
569614

570615
private GranularityType granularityType;
571616

617+
/**
618+
* @deprecated use {@link #file(byte[])} instead.
619+
*/
620+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
572621
public Builder withFile(byte[] file) {
573622
this.file = file;
574623
return this;
575624
}
576625

626+
public Builder file(byte[] file) {
627+
this.file = file;
628+
return this;
629+
}
630+
631+
/**
632+
* @deprecated use {@link #model(String)} instead.
633+
*/
634+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
577635
public Builder withModel(String model) {
578636
this.model = model;
579637
return this;
580638
}
581639

640+
public Builder model(String model) {
641+
this.model = model;
642+
return this;
643+
}
644+
645+
/**
646+
* @deprecated use {@link #language(String)} instead.
647+
*/
648+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
582649
public Builder withLanguage(String language) {
583650
this.language = language;
584651
return this;
585652
}
586653

654+
public Builder language(String language) {
655+
this.language = language;
656+
return this;
657+
}
658+
659+
/**
660+
* @deprecated use {@link #prompt(String)} instead.
661+
*/
662+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
587663
public Builder withPrompt(String prompt) {
588664
this.prompt = prompt;
589665
return this;
590666
}
591667

668+
public Builder prompt(String prompt) {
669+
this.prompt = prompt;
670+
return this;
671+
}
672+
673+
/**
674+
* @deprecated use {@link #responseFormat(TranscriptResponseFormat)} instead.
675+
*/
676+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
592677
public Builder withResponseFormat(TranscriptResponseFormat response_format) {
593678
this.responseFormat = response_format;
594679
return this;
595680
}
596681

682+
public Builder responseFormat(TranscriptResponseFormat responseFormat) {
683+
this.responseFormat = responseFormat;
684+
return this;
685+
}
686+
687+
/**
688+
* @deprecated use {@link #temperature(Float)} instead.
689+
*/
690+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
597691
public Builder withTemperature(Float temperature) {
598692
this.temperature = temperature;
599693
return this;
600694
}
601695

696+
public Builder temperature(Float temperature) {
697+
this.temperature = temperature;
698+
return this;
699+
}
700+
701+
/**
702+
* @deprecated use {@link #granularityType(GranularityType)} instead.
703+
*/
704+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
602705
public Builder withGranularityType(GranularityType granularityType) {
603706
this.granularityType = granularityType;
604707
return this;
605708
}
606709

710+
public Builder granularityType(GranularityType granularityType) {
711+
this.granularityType = granularityType;
712+
return this;
713+
}
714+
607715
public TranscriptionRequest build() {
608716
Assert.notNull(this.file, "file must not be null");
609717
Assert.hasText(this.model, "model must not be empty");
@@ -658,31 +766,76 @@ public static class Builder {
658766

659767
private Float temperature;
660768

769+
/**
770+
* @deprecated use {@link #file(byte[])} instead.
771+
*/
772+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
661773
public Builder withFile(byte[] file) {
662774
this.file = file;
663775
return this;
664776
}
665777

778+
public Builder file(byte[] file) {
779+
this.file = file;
780+
return this;
781+
}
782+
783+
/**
784+
* @deprecated use {@link #model(String)} instead.
785+
*/
786+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
666787
public Builder withModel(String model) {
667788
this.model = model;
668789
return this;
669790
}
670791

792+
public Builder model(String model) {
793+
this.model = model;
794+
return this;
795+
}
796+
797+
/**
798+
* @deprecated use {@link #prompt(String)} instead.
799+
*/
800+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
671801
public Builder withPrompt(String prompt) {
672802
this.prompt = prompt;
673803
return this;
674804
}
675805

806+
public Builder prompt(String prompt) {
807+
this.prompt = prompt;
808+
return this;
809+
}
810+
811+
/**
812+
* @deprecated use {@link #responseFormat(TranscriptResponseFormat)} instead.
813+
*/
814+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
676815
public Builder withResponseFormat(TranscriptResponseFormat responseFormat) {
677816
this.responseFormat = responseFormat;
678817
return this;
679818
}
680819

820+
public Builder responseFormat(TranscriptResponseFormat responseFormat) {
821+
this.responseFormat = responseFormat;
822+
return this;
823+
}
824+
825+
/**
826+
* @deprecated use {@link #temperature(Float)} instead.
827+
*/
828+
@Deprecated(forRemoval = true, since = "1.0.0-M6")
681829
public Builder withTemperature(Float temperature) {
682830
this.temperature = temperature;
683831
return this;
684832
}
685833

834+
public Builder temperature(Float temperature) {
835+
this.temperature = temperature;
836+
return this;
837+
}
838+
686839
public TranslationRequest build() {
687840
Assert.notNull(this.file, "file must not be null");
688841
Assert.hasText(this.model, "model must not be empty");

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ void speechTranscriptionAndTranslation() throws IOException {
4848

4949
byte[] speech = this.audioApi
5050
.createSpeech(SpeechRequest.builder()
51-
.withModel(TtsModel.TTS_1_HD.getValue())
52-
.withInput("Hello, my name is Chris and I love Spring A.I.")
53-
.withVoice(Voice.ONYX)
51+
.model(TtsModel.TTS_1_HD.getValue())
52+
.input("Hello, my name is Chris and I love Spring A.I.")
53+
.voice(Voice.ONYX)
5454
.build())
5555
.getBody();
5656

@@ -60,22 +60,23 @@ void speechTranscriptionAndTranslation() throws IOException {
6060

6161
StructuredResponse translation = this.audioApi
6262
.createTranslation(
63-
TranslationRequest.builder().withModel(WhisperModel.WHISPER_1.getValue()).withFile(speech).build(),
63+
TranslationRequest.builder().model(WhisperModel.WHISPER_1.getValue()).file(speech).build(),
6464
StructuredResponse.class)
6565
.getBody();
6666

6767
assertThat(translation.text().replaceAll(",", "")).isEqualTo("Hello my name is Chris and I love Spring AI.");
6868

69-
StructuredResponse transcriptionEnglish = this.audioApi.createTranscription(
70-
TranscriptionRequest.builder().withModel(WhisperModel.WHISPER_1.getValue()).withFile(speech).build(),
71-
StructuredResponse.class)
69+
StructuredResponse transcriptionEnglish = this.audioApi
70+
.createTranscription(
71+
TranscriptionRequest.builder().model(WhisperModel.WHISPER_1.getValue()).file(speech).build(),
72+
StructuredResponse.class)
7273
.getBody();
7374

7475
assertThat(transcriptionEnglish.text().replaceAll(",", ""))
7576
.isEqualTo("Hello my name is Chris and I love Spring AI.");
7677

7778
StructuredResponse transcriptionDutch = this.audioApi
78-
.createTranscription(TranscriptionRequest.builder().withFile(speech).withLanguage("nl").build(),
79+
.createTranscription(TranscriptionRequest.builder().file(speech).language("nl").build(),
7980
StructuredResponse.class)
8081
.getBody();
8182

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ var openAiAudioApi = new OpenAiAudioApi(System.getenv("OPENAI_API_KEY"));
156156
var openAiAudioSpeechModel = new OpenAiAudioSpeechModel(this.openAiAudioApi);
157157
158158
OpenAiAudioSpeechOptions speechOptions = OpenAiAudioSpeechOptions.builder()
159-
.withVoice(OpenAiAudioApi.SpeechRequest.Voice.ALLOY)
160-
.withSpeed(1.0f)
161-
.withResponseFormat(OpenAiAudioApi.SpeechRequest.AudioResponseFormat.MP3)
162-
.withModel(OpenAiAudioApi.TtsModel.TTS_1.value)
159+
.voice(OpenAiAudioApi.SpeechRequest.Voice.ALLOY)
160+
.speed(1.0f)
161+
.responseFormat(OpenAiAudioApi.SpeechRequest.AudioResponseFormat.MP3)
162+
.model(OpenAiAudioApi.TtsModel.TTS_1.value)
163163
.build();
164164
165165
SpeechPrompt speechPrompt = new SpeechPrompt("Today is a wonderful day to build something people love!", this.speechOptions);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ For example:
9191
OpenAiAudioApi.TranscriptResponseFormat responseFormat = OpenAiAudioApi.TranscriptResponseFormat.VTT;
9292
9393
OpenAiAudioTranscriptionOptions transcriptionOptions = OpenAiAudioTranscriptionOptions.builder()
94-
.withLanguage("en")
95-
.withPrompt("Ask not this, but ask that")
96-
.withTemperature(0f)
97-
.withResponseFormat(this.responseFormat)
94+
.language("en")
95+
.prompt("Ask not this, but ask that")
96+
.temperature(0f)
97+
.responseFormat(this.responseFormat)
9898
.build();
9999
AudioTranscriptionPrompt transcriptionRequest = new AudioTranscriptionPrompt(audioFile, this.transcriptionOptions);
100100
AudioTranscriptionResponse response = openAiTranscriptionModel.call(this.transcriptionRequest);
@@ -132,8 +132,8 @@ var openAiAudioApi = new OpenAiAudioApi(System.getenv("OPENAI_API_KEY"));
132132
var openAiAudioTranscriptionModel = new OpenAiAudioTranscriptionModel(this.openAiAudioApi);
133133
134134
var transcriptionOptions = OpenAiAudioTranscriptionOptions.builder()
135-
.withResponseFormat(TranscriptResponseFormat.TEXT)
136-
.withTemperature(0f)
135+
.responseFormat(TranscriptResponseFormat.TEXT)
136+
.temperature(0f)
137137
.build();
138138
139139
var audioFile = new FileSystemResource("/path/to/your/resource/speech/jfk.flac");

0 commit comments

Comments
 (0)