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
This commit is contained in:
committed by
Mark Pollack
parent
c14618fe9a
commit
58efee6d90
@@ -89,10 +89,10 @@ For example:
|
||||
[source,java]
|
||||
----
|
||||
OpenAiAudioSpeechOptions speechOptions = OpenAiAudioSpeechOptions.builder()
|
||||
.withModel("tts-1")
|
||||
.withVoice(OpenAiAudioApi.SpeechRequest.Voice.ALLOY)
|
||||
.withResponseFormat(OpenAiAudioApi.SpeechRequest.AudioResponseFormat.MP3)
|
||||
.withSpeed(1.0f)
|
||||
.model("tts-1")
|
||||
.voice(OpenAiAudioApi.SpeechRequest.Voice.ALLOY)
|
||||
.responseFormat(OpenAiAudioApi.SpeechRequest.AudioResponseFormat.MP3)
|
||||
.speed(1.0f)
|
||||
.build();
|
||||
|
||||
SpeechPrompt speechPrompt = new SpeechPrompt("Hello, this is a text-to-speech example.", this.speechOptions);
|
||||
@@ -131,9 +131,9 @@ var openAiAudioApi = new OpenAiAudioApi(System.getenv("OPENAI_API_KEY"));
|
||||
var openAiAudioSpeechModel = new OpenAiAudioSpeechModel(this.openAiAudioApi);
|
||||
|
||||
var speechOptions = OpenAiAudioSpeechOptions.builder()
|
||||
.withResponseFormat(OpenAiAudioApi.SpeechRequest.AudioResponseFormat.MP3)
|
||||
.withSpeed(1.0f)
|
||||
.withModel(OpenAiAudioApi.TtsModel.TTS_1.value)
|
||||
.responseFormat(OpenAiAudioApi.SpeechRequest.AudioResponseFormat.MP3)
|
||||
.speed(1.0f)
|
||||
.model(OpenAiAudioApi.TtsModel.TTS_1.value)
|
||||
.build();
|
||||
|
||||
var speechPrompt = new SpeechPrompt("Hello, this is a text-to-speech example.", this.speechOptions);
|
||||
|
||||
@@ -150,7 +150,7 @@ OpenAiChatModel chatModel = ...
|
||||
UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
|
||||
|
||||
ChatResponse response = this.chatModel.call(new Prompt(this.userMessage,
|
||||
OpenAiChatOptions.builder().withFunction("CurrentWeather").build())); // Enable the function
|
||||
OpenAiChatOptions.builder().function("CurrentWeather").build())); // Enable the function
|
||||
|
||||
logger.info("Response: {}", response);
|
||||
----
|
||||
@@ -179,7 +179,7 @@ OpenAiChatModel chatModel = ...
|
||||
UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
|
||||
|
||||
var promptOptions = OpenAiChatOptions.builder()
|
||||
.withFunctionCallbacks(List.of(FunctionCallback.builder()
|
||||
.functionCallbacks(List.of(FunctionCallback.builder()
|
||||
.function("CurrentWeather", new MockWeatherService()) // (1) function name and instance
|
||||
.description("Get the weather in location") // (2) function description
|
||||
.inputType(MockWeatherService.Request.class) // (3) function input type
|
||||
@@ -229,13 +229,13 @@ BiFunction<MockWeatherService.Request, ToolContext, MockWeatherService.Response>
|
||||
};
|
||||
|
||||
OpenAiChatOptions options = OpenAiChatOptions.builder()
|
||||
.withModel(OpenAiApi.ChatModel.GPT_4_O.getValue())
|
||||
.withFunctionCallbacks(List.of(FunctionCallback.builder()
|
||||
.model(OpenAiApi.ChatModel.GPT_4_O.getValue())
|
||||
.functionCallbacks(List.of(FunctionCallback.builder()
|
||||
.function("getCurrentWeather", this.weatherFunction)
|
||||
.description("Get the weather in location")
|
||||
.inputType(MockWeatherService.Request.class)
|
||||
.build()))
|
||||
.withToolContext(Map.of("sessionId", "123", "userId", "user456"))
|
||||
.toolContext(Map.of("sessionId", "123", "userId", "user456"))
|
||||
.build();
|
||||
----
|
||||
|
||||
|
||||
@@ -152,8 +152,8 @@ ChatResponse response = chatModel.call(
|
||||
new Prompt(
|
||||
"Generate the names of 5 famous pirates.",
|
||||
OpenAiChatOptions.builder()
|
||||
.withModel("gpt-4-o")
|
||||
.withTemperature(0.4)
|
||||
.model("gpt-4-o")
|
||||
.temperature(0.4)
|
||||
.build()
|
||||
));
|
||||
----
|
||||
@@ -190,7 +190,7 @@ var userMessage = new UserMessage("Explain what do you see on this picture?",
|
||||
new Media(MimeTypeUtils.IMAGE_PNG, this.imageResource));
|
||||
|
||||
ChatResponse response = chatModel.call(new Prompt(this.userMessage,
|
||||
OpenAiChatOptions.builder().withModel(OpenAiApi.ChatModel.GPT_4_O.getValue()).build()));
|
||||
OpenAiChatOptions.builder().model(OpenAiApi.ChatModel.GPT_4_O.getValue()).build()));
|
||||
----
|
||||
|
||||
TIP: GPT_4_VISION_PREVIEW will continue to be available only to existing users of this model starting June 17, 2024. If you are not an existing user, please use the GPT_4_O or GPT_4_TURBO models. More details https://platform.openai.com/docs/deprecations/2024-06-06-gpt-4-32k-and-vision-preview-models[here]
|
||||
@@ -204,7 +204,7 @@ var userMessage = new UserMessage("Explain what do you see on this picture?",
|
||||
"https://docs.spring.io/spring-ai/reference/_images/multimodal.test.png"));
|
||||
|
||||
ChatResponse response = chatModel.call(new Prompt(this.userMessage,
|
||||
OpenAiChatOptions.builder().withModel(OpenAiApi.ChatModel.GPT_4_O.getValue()).build()));
|
||||
OpenAiChatOptions.builder().model(OpenAiApi.ChatModel.GPT_4_O.getValue()).build()));
|
||||
----
|
||||
|
||||
TIP: You can pass multiple images as well.
|
||||
@@ -244,7 +244,7 @@ var userMessage = new UserMessage("What is this recording about?",
|
||||
List.of(new Media(MimeTypeUtils.parseMimeType("audio/mp3"), audioResource)));
|
||||
|
||||
ChatResponse response = chatModel.call(new Prompt(List.of(userMessage),
|
||||
OpenAiChatOptions.builder().withModel(OpenAiApi.ChatModel.GPT_4_O_AUDIO_PREVIEW).build()));
|
||||
OpenAiChatOptions.builder().model(OpenAiApi.ChatModel.GPT_4_O_AUDIO_PREVIEW).build()));
|
||||
----
|
||||
|
||||
TIP: You can pass multiple audio files as well.
|
||||
@@ -267,9 +267,9 @@ var userMessage = new UserMessage("Tell me joke about Spring Framework");
|
||||
|
||||
ChatResponse response = chatModel.call(new Prompt(List.of(userMessage),
|
||||
OpenAiChatOptions.builder()
|
||||
.withModel(OpenAiApi.ChatModel.GPT_4_O_AUDIO_PREVIEW)
|
||||
.withOutputModalities(List.of("text", "audio"))
|
||||
.withOutputAudio(new AudioParameters(Voice.ALLOY, AudioResponseFormat.WAV))
|
||||
.model(OpenAiApi.ChatModel.GPT_4_O_AUDIO_PREVIEW)
|
||||
.outputModalities(List.of("text", "audio"))
|
||||
.outputAudio(new AudioParameters(Voice.ALLOY, AudioResponseFormat.WAV))
|
||||
.build()));
|
||||
|
||||
String text = response.getResult().getOutput().getContent(); // audio transcript
|
||||
@@ -322,8 +322,8 @@ String jsonSchema = """
|
||||
|
||||
Prompt prompt = new Prompt("how can I solve 8x + 7 = -23",
|
||||
OpenAiChatOptions.builder()
|
||||
.withModel(ChatModel.GPT_4_O_MINI)
|
||||
.withResponseFormat(new ResponseFormat(ResponseFormat.Type.JSON_SCHEMA, this.jsonSchema))
|
||||
.model(ChatModel.GPT_4_O_MINI)
|
||||
.responseFormat(new ResponseFormat(ResponseFormat.Type.JSON_SCHEMA, this.jsonSchema))
|
||||
.build());
|
||||
|
||||
ChatResponse response = this.openAiChatModel.call(this.prompt);
|
||||
@@ -362,8 +362,8 @@ var jsonSchema = this.outputConverter.getJsonSchema();
|
||||
|
||||
Prompt prompt = new Prompt("how can I solve 8x + 7 = -23",
|
||||
OpenAiChatOptions.builder()
|
||||
.withModel(ChatModel.GPT_4_O_MINI)
|
||||
.withResponseFormat(new ResponseFormat(ResponseFormat.Type.JSON_SCHEMA, this.jsonSchema))
|
||||
.model(ChatModel.GPT_4_O_MINI)
|
||||
.responseFormat(new ResponseFormat(ResponseFormat.Type.JSON_SCHEMA, this.jsonSchema))
|
||||
.build());
|
||||
|
||||
ChatResponse response = this.openAiChatModel.call(this.prompt);
|
||||
@@ -393,8 +393,8 @@ val jsonSchema = outputConverter.jsonSchema;
|
||||
|
||||
val prompt = Prompt("how can I solve 8x + 7 = -23",
|
||||
OpenAiChatOptions.builder()
|
||||
.withModel(ChatModel.GPT_4_O_MINI)
|
||||
.withResponseFormat(ResponseFormat(ResponseFormat.Type.JSON_SCHEMA, jsonSchema))
|
||||
.model(ChatModel.GPT_4_O_MINI)
|
||||
.responseFormat(ResponseFormat(ResponseFormat.Type.JSON_SCHEMA, jsonSchema))
|
||||
.build())
|
||||
|
||||
val response = openAiChatModel.call(prompt)
|
||||
@@ -498,9 +498,9 @@ Next, create an `OpenAiChatModel` and use it for text generations:
|
||||
----
|
||||
var openAiApi = new OpenAiApi(System.getenv("OPENAI_API_KEY"));
|
||||
var openAiChatOptions = OpenAiChatOptions.builder()
|
||||
.withModel("gpt-3.5-turbo")
|
||||
.withTemperature(0.4)
|
||||
.withMaxTokens(200)
|
||||
.model("gpt-3.5-turbo")
|
||||
.temperature(0.4)
|
||||
.maxTokens(200)
|
||||
.build();
|
||||
var chatModel = new OpenAiChatModel(this.openAiApi, this.openAiChatOptions);
|
||||
|
||||
|
||||
@@ -128,7 +128,7 @@ For example to override the default model name for a specific request:
|
||||
EmbeddingResponse embeddingResponse = embeddingModel.call(
|
||||
new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"),
|
||||
OpenAiEmbeddingOptions.builder()
|
||||
.withModel("Different-Embedding-Model-Deployment-Name")
|
||||
.model("Different-Embedding-Model-Deployment-Name")
|
||||
.build()));
|
||||
----
|
||||
|
||||
@@ -199,8 +199,8 @@ var embeddingModel = new OpenAiEmbeddingModel(
|
||||
this.openAiApi,
|
||||
MetadataMode.EMBED,
|
||||
OpenAiEmbeddingOptions.builder()
|
||||
.withModel("text-embedding-ada-002")
|
||||
.withUser("user-6")
|
||||
.model("text-embedding-ada-002")
|
||||
.user("user-6")
|
||||
.build(),
|
||||
RetryUtils.DEFAULT_RETRY_TEMPLATE);
|
||||
|
||||
|
||||
@@ -118,10 +118,10 @@ For example to override the OpenAI specific options such as quality and the numb
|
||||
ImageResponse response = openaiImageModel.call(
|
||||
new ImagePrompt("A light cream colored mini golden doodle",
|
||||
OpenAiImageOptions.builder()
|
||||
.withQuality("hd")
|
||||
.withN(4)
|
||||
.withHeight(1024)
|
||||
.withWidth(1024).build())
|
||||
.quality("hd")
|
||||
.N(4)
|
||||
.height(1024)
|
||||
.width(1024).build())
|
||||
|
||||
);
|
||||
----
|
||||
|
||||
@@ -78,7 +78,7 @@ For example:
|
||||
[source,java]
|
||||
----
|
||||
OpenAiModerationOptions moderationOptions = OpenAiModerationOptions.builder()
|
||||
.withModel("text-moderation-latest")
|
||||
.model("text-moderation-latest")
|
||||
.build();
|
||||
|
||||
ModerationPrompt moderationPrompt = new ModerationPrompt("Text to be moderated", this.moderationOptions);
|
||||
@@ -161,7 +161,7 @@ OpenAiModerationApi openAiModerationApi = new OpenAiModerationApi(System.getenv(
|
||||
OpenAiModerationModel openAiModerationModel = new OpenAiModerationModel(this.openAiModerationApi);
|
||||
|
||||
OpenAiModerationOptions moderationOptions = OpenAiModerationOptions.builder()
|
||||
.withModel("text-moderation-latest")
|
||||
.model("text-moderation-latest")
|
||||
.build();
|
||||
|
||||
ModerationPrompt moderationPrompt = new ModerationPrompt("Text to be moderated", this.moderationOptions);
|
||||
|
||||
Reference in New Issue
Block a user