From 6e2e98829dbad9f94fbda1bd5345e5fa68d461de Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Tue, 21 Oct 2025 11:17:26 +0200 Subject: [PATCH 01/49] First implementation of the OpenAI Java SDK, supporting only Embeddings Fix #3368 Signed-off-by: Julien Dubois --- models/spring-ai-openai-official/README.md | 5 + models/spring-ai-openai-official/pom.xml | 88 + .../OpenAiOfficialEmbeddingModel.java | 167 + .../OpenAiOfficialEmbeddingOptions.java | 179 + .../OpenAiOfficialTestConfiguration.java | 49 + .../openaiofficial/embedding/EmbeddingIT.java | 123 + .../src/test/resources/text_source.txt | 4124 +++++++++++++++++ .../test/script/deploy-azure-openai-models.sh | 93 + pom.xml | 3 + 9 files changed, 4831 insertions(+) create mode 100644 models/spring-ai-openai-official/README.md create mode 100644 models/spring-ai-openai-official/pom.xml create mode 100644 models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java create mode 100644 models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingOptions.java create mode 100644 models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java create mode 100644 models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/EmbeddingIT.java create mode 100644 models/spring-ai-openai-official/src/test/resources/text_source.txt create mode 100755 models/spring-ai-openai-official/src/test/script/deploy-azure-openai-models.sh diff --git a/models/spring-ai-openai-official/README.md b/models/spring-ai-openai-official/README.md new file mode 100644 index 00000000000..a51fe3c2cf6 --- /dev/null +++ b/models/spring-ai-openai-official/README.md @@ -0,0 +1,5 @@ +# OpenAI Java API Library + +This is the official OpenAI Java SDK from OpenAI, which provides integration with OpenAI's services, including Azure OpenAI. + +[OpenAI Java API Library GitHub repository](https://github.com/openai/openai-java) diff --git a/models/spring-ai-openai-official/pom.xml b/models/spring-ai-openai-official/pom.xml new file mode 100644 index 00000000000..4f041f58b53 --- /dev/null +++ b/models/spring-ai-openai-official/pom.xml @@ -0,0 +1,88 @@ + + + + + 4.0.0 + + org.springframework.ai + spring-ai-parent + 1.1.0-SNAPSHOT + ../../pom.xml + + spring-ai-openai-official + jar + Spring AI Model - OpenAI Official + OpenAI Java API Library support + https://github.com/spring-projects/spring-ai + + + https://github.com/spring-projects/spring-ai + git://github.com/spring-projects/spring-ai.git + git@github.com:spring-projects/spring-ai.git + + + + + + + + + + org.springframework.ai + spring-ai-model + ${project.parent.version} + + + + com.openai + openai-java + ${openai-official.version} + + + + + org.springframework + spring-context-support + + + + org.slf4j + slf4j-api + + + + + org.springframework.ai + spring-ai-test + ${project.version} + test + + + + org.springframework.boot + spring-boot-starter-test + test + + + + io.micrometer + micrometer-observation-test + test + + + + diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java new file mode 100644 index 00000000000..7a7ee96751e --- /dev/null +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java @@ -0,0 +1,167 @@ +package org.springframework.ai.openaiofficial; + +import com.openai.client.OpenAIClient; +import com.openai.models.embeddings.CreateEmbeddingResponse; +import com.openai.models.embeddings.EmbeddingCreateParams; +import io.micrometer.observation.ObservationRegistry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.ai.chat.metadata.DefaultUsage; +import org.springframework.ai.document.Document; +import org.springframework.ai.document.MetadataMode; +import org.springframework.ai.embedding.AbstractEmbeddingModel; +import org.springframework.ai.embedding.Embedding; +import org.springframework.ai.embedding.EmbeddingRequest; +import org.springframework.ai.embedding.EmbeddingResponse; +import org.springframework.ai.embedding.EmbeddingResponseMetadata; +import org.springframework.ai.embedding.observation.DefaultEmbeddingModelObservationConvention; +import org.springframework.ai.embedding.observation.EmbeddingModelObservationContext; +import org.springframework.ai.embedding.observation.EmbeddingModelObservationConvention; +import org.springframework.ai.embedding.observation.EmbeddingModelObservationDocumentation; +import org.springframework.ai.model.EmbeddingUtils; +import org.springframework.ai.observation.conventions.AiProvider; +import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +import static com.openai.models.embeddings.EmbeddingModel.TEXT_EMBEDDING_ADA_002; + +public class OpenAiOfficialEmbeddingModel extends AbstractEmbeddingModel { + + private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialEmbeddingModel.class); + + private static final EmbeddingModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultEmbeddingModelObservationConvention(); + + private final OpenAIClient openAiClient; + + private final OpenAiOfficialEmbeddingOptions defaultOptions; + + private final MetadataMode metadataMode; + + /** + * Observation registry used for instrumentation. + */ + private final ObservationRegistry observationRegistry; + + /** + * Conventions to use for generating observations. + */ + private EmbeddingModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; + + public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient) { + this(openAiClient, MetadataMode.EMBED); + } + + public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode) { + this(openAiClient, metadataMode, + OpenAiOfficialEmbeddingOptions.builder().deploymentName(TEXT_EMBEDDING_ADA_002.toString()).build()); + } + + public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, + OpenAiOfficialEmbeddingOptions options) { + this(openAiClient, metadataMode, options, ObservationRegistry.NOOP); + } + + public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, + OpenAiOfficialEmbeddingOptions options, ObservationRegistry observationRegistry) { + + Assert.notNull(openAiClient, "com.openai.client.OpenAIClient must not be null"); + Assert.notNull(metadataMode, "Metadata mode must not be null"); + Assert.notNull(options, "Options must not be null"); + Assert.notNull(observationRegistry, "Observation registry must not be null"); + this.openAiClient = openAiClient; + this.metadataMode = metadataMode; + this.defaultOptions = options; + this.observationRegistry = observationRegistry; + } + + @Override + public float[] embed(Document document) { + logger.debug("Retrieving embeddings"); + + EmbeddingResponse response = this + .call(new EmbeddingRequest(List.of(document.getFormattedContent(this.metadataMode)), null)); + logger.debug("Embeddings retrieved"); + + if (CollectionUtils.isEmpty(response.getResults())) { + return new float[0]; + } + return response.getResults().get(0).getOutput(); + } + + @Override + public EmbeddingResponse call(EmbeddingRequest embeddingRequest) { + logger.debug("Retrieving embeddings"); + + OpenAiOfficialEmbeddingOptions options = OpenAiOfficialEmbeddingOptions.builder() + .from(this.defaultOptions) + .merge(embeddingRequest.getOptions()) + .build(); + + EmbeddingRequest embeddingRequestWithMergedOptions = new EmbeddingRequest(embeddingRequest.getInstructions(), + options); + + EmbeddingCreateParams embeddingCreateParams = options + .toOpenAiCreateParams(embeddingRequestWithMergedOptions.getInstructions()); + + var observationContext = EmbeddingModelObservationContext.builder() + .embeddingRequest(embeddingRequestWithMergedOptions) + .provider(AiProvider.AZURE_OPENAI.value()) + .build(); + + return Objects.requireNonNull( + EmbeddingModelObservationDocumentation.EMBEDDING_MODEL_OPERATION + .observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, + this.observationRegistry) + .observe(() -> { + CreateEmbeddingResponse response = this.openAiClient.embeddings().create(embeddingCreateParams); + + logger.debug("Embeddings retrieved"); + var embeddingResponse = generateEmbeddingResponse(response); + observationContext.setResponse(embeddingResponse); + return embeddingResponse; + })); + } + + private EmbeddingResponse generateEmbeddingResponse(CreateEmbeddingResponse response) { + + List data = generateEmbeddingList(response.data()); + EmbeddingResponseMetadata metadata = new EmbeddingResponseMetadata(); + metadata.setUsage(getDefaultUsage(response.usage())); + return new EmbeddingResponse(data, metadata); + } + + private DefaultUsage getDefaultUsage(CreateEmbeddingResponse.Usage nativeUsage) { + return new DefaultUsage(Math.toIntExact(nativeUsage.promptTokens()), 0, + Math.toIntExact(nativeUsage.totalTokens()), nativeUsage); + } + + private List generateEmbeddingList(List nativeData) { + List data = new ArrayList<>(); + for (com.openai.models.embeddings.Embedding nativeDatum : nativeData) { + List nativeDatumEmbedding = nativeDatum.embedding(); + Long nativeIndex = nativeDatum.index(); + Embedding embedding = new Embedding(EmbeddingUtils.toPrimitive(nativeDatumEmbedding), + Math.toIntExact(nativeIndex)); + data.add(embedding); + } + return data; + } + + public OpenAiOfficialEmbeddingOptions getDefaultOptions() { + return this.defaultOptions; + } + + /** + * Use the provided convention for reporting observation data + * @param observationConvention The provided convention + */ + public void setObservationConvention(EmbeddingModelObservationConvention observationConvention) { + Assert.notNull(observationConvention, "observationConvention cannot be null"); + this.observationConvention = observationConvention; + } + +} diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingOptions.java new file mode 100644 index 00000000000..d36e4d24c33 --- /dev/null +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingOptions.java @@ -0,0 +1,179 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaiofficial; + +import com.openai.models.embeddings.EmbeddingCreateParams; +import org.springframework.ai.embedding.EmbeddingOptions; + +import java.util.List; + +/** + * The configuration information for the embedding requests. + */ +public class OpenAiOfficialEmbeddingOptions implements EmbeddingOptions { + + /** + * An identifier for the caller or end user of the operation. This may be used for + * tracking or rate-limiting purposes. + */ + private String user; + + /** + * The model name used. When using Azure AI Foundry, this is also used as the default + * deployment name. + */ + private String model; + + /** + * The deployment name as defined in Azure AI Foundry. On Azure AI Foundry, the + * default deployment name is the same as the model name. When using OpenAI directly, + * this value isn't used. + */ + private String deploymentName; + + /* + * The number of dimensions the resulting output embeddings should have. Only + * supported in `text-embedding-3` and later models. + */ + private Integer dimensions; + + public static Builder builder() { + return new Builder(); + } + + @Override + public String getModel() { + return this.model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getUser() { + return this.user; + } + + public void setUser(String user) { + this.user = user; + } + + public String getDeploymentName() { + return this.deploymentName; + } + + public void setDeploymentName(String deploymentName) { + this.deploymentName = deploymentName; + } + + @Override + public Integer getDimensions() { + return this.dimensions; + } + + public void setDimensions(Integer dimensions) { + this.dimensions = dimensions; + } + + public EmbeddingCreateParams toOpenAiCreateParams(List instructions) { + + EmbeddingCreateParams.Builder builder = EmbeddingCreateParams.builder(); + + if (instructions != null && instructions.size() > 0) { + builder.input(EmbeddingCreateParams.Input.ofArrayOfStrings(instructions)); + } + if (this.getDeploymentName() != null) { + builder.model(this.getDeploymentName()); + } + if (this.getUser() != null) { + builder.user(this.getUser()); + } + if (this.getDimensions() != null) { + builder.dimensions(this.getDimensions()); + } + return builder.build(); + } + + public static final class Builder { + + private final OpenAiOfficialEmbeddingOptions options = new OpenAiOfficialEmbeddingOptions(); + + public Builder from(OpenAiOfficialEmbeddingOptions fromOptions) { + this.options.setUser(fromOptions.getUser()); + this.options.setDeploymentName(fromOptions.getDeploymentName()); + this.options.setDimensions(fromOptions.getDimensions()); + return this; + } + + public Builder merge(EmbeddingOptions from) { + if (from instanceof OpenAiOfficialEmbeddingOptions castFrom) { + + if (castFrom.getUser() != null) { + this.options.setUser(castFrom.getUser()); + } + if (castFrom.getDeploymentName() != null) { + this.options.setDeploymentName(castFrom.getDeploymentName()); + } + if (castFrom.getDimensions() != null) { + this.options.setDimensions(castFrom.getDimensions()); + } + } + return this; + } + + public Builder from(EmbeddingCreateParams openAiCreateParams) { + + if (openAiCreateParams.user().isPresent()) { + this.options.setUser(openAiCreateParams.user().get()); + } + this.options.setDeploymentName(openAiCreateParams.model().toString()); + if (openAiCreateParams.dimensions().isPresent()) { + this.options.setDimensions(Math.toIntExact(openAiCreateParams.dimensions().get())); + } + return this; + } + + public Builder user(String user) { + this.options.setUser(user); + return this; + } + + public Builder deploymentName(String model) { + this.options.setDeploymentName(model); + return this; + } + + public Builder model(String model) { + this.options.setModel(model); + return this; + } + + public Builder dimensions(Integer dimensions) { + this.options.dimensions = dimensions; + return this; + } + + public OpenAiOfficialEmbeddingOptions build() { + if (this.options.deploymentName == null) { + this.options.deploymentName = this.options.model; + } + return this.options; + } + + } + +} diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java new file mode 100644 index 00000000000..c545b138768 --- /dev/null +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java @@ -0,0 +1,49 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaiofficial; + +import com.openai.client.OpenAIClient; +import com.openai.client.okhttp.OpenAIOkHttpClient; +import org.springframework.ai.model.ApiKey; +import org.springframework.ai.model.SimpleApiKey; +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.util.StringUtils; + +@SpringBootConfiguration +public class OpenAiOfficialTestConfiguration { + + private ApiKey getApiKey() { + String apiKey = System.getenv("OPENAI_API_KEY"); + if (!StringUtils.hasText(apiKey)) { + throw new IllegalArgumentException( + "You must provide an API key. Put it in an environment variable under the name OPENAI_API_KEY"); + } + return new SimpleApiKey(apiKey); + } + + @Bean + public OpenAIClient openAIClient() { + return OpenAIOkHttpClient.fromEnv(); + } + + @Bean + public OpenAiOfficialEmbeddingModel openAiEmbeddingModel(OpenAIClient client) { + return new OpenAiOfficialEmbeddingModel(client); + } + +} diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/EmbeddingIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/EmbeddingIT.java new file mode 100644 index 00000000000..5b1cad488a3 --- /dev/null +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/EmbeddingIT.java @@ -0,0 +1,123 @@ +/* + * Copyright 2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaiofficial.embedding; + +import java.nio.charset.StandardCharsets; +import java.util.List; + +import com.openai.models.embeddings.EmbeddingModel; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; + +import org.springframework.ai.document.Document; +import org.springframework.ai.embedding.EmbeddingRequest; +import org.springframework.ai.embedding.EmbeddingResponse; +import org.springframework.ai.embedding.TokenCountBatchingStrategy; +import org.springframework.ai.openaiofficial.OpenAiOfficialEmbeddingModel; +import org.springframework.ai.openaiofficial.OpenAiOfficialEmbeddingOptions; +import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.Resource; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +@SpringBootTest(classes = OpenAiOfficialTestConfiguration.class) +@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") +class EmbeddingIT { + + private Resource resource = new DefaultResourceLoader().getResource("classpath:text_source.txt"); + + @Autowired + private OpenAiOfficialEmbeddingModel openAiOfficialEmbeddingModel; + + @Test + void defaultEmbedding() { + assertThat(this.openAiOfficialEmbeddingModel).isNotNull(); + + EmbeddingResponse embeddingResponse = this.openAiOfficialEmbeddingModel + .embedForResponse(List.of("Hello World")); + assertThat(embeddingResponse.getResults()).hasSize(1); + assertThat(embeddingResponse.getResults().get(0)).isNotNull(); + assertThat(embeddingResponse.getResults().get(0).getOutput()).hasSize(1536); + assertThat(embeddingResponse.getMetadata().getUsage().getTotalTokens()).isEqualTo(2); + assertThat(embeddingResponse.getMetadata().getUsage().getPromptTokens()).isEqualTo(2); + + assertThat(this.openAiOfficialEmbeddingModel.dimensions()).isEqualTo(1536); + } + + @Test + void embeddingBatchDocuments() throws Exception { + assertThat(this.openAiOfficialEmbeddingModel).isNotNull(); + List embeddings = this.openAiOfficialEmbeddingModel.embed( + List.of(new Document("Hello world"), new Document("Hello Spring"), new Document("Hello Spring AI!")), + OpenAiOfficialEmbeddingOptions.builder() + .model(EmbeddingModel.TEXT_EMBEDDING_ADA_002.toString()) + .build(), + new TokenCountBatchingStrategy()); + assertThat(embeddings.size()).isEqualTo(3); + embeddings.forEach( + embedding -> assertThat(embedding.length).isEqualTo(this.openAiOfficialEmbeddingModel.dimensions())); + } + + @Test + void embeddingBatchDocumentsThatExceedTheLimit() throws Exception { + assertThat(this.openAiOfficialEmbeddingModel).isNotNull(); + String contentAsString = this.resource.getContentAsString(StandardCharsets.UTF_8); + assertThatThrownBy(() -> this.openAiOfficialEmbeddingModel.embed( + List.of(new Document("Hello World"), new Document(contentAsString)), + OpenAiOfficialEmbeddingOptions.builder() + .model(EmbeddingModel.TEXT_EMBEDDING_ADA_002.toString()) + .build(), + new TokenCountBatchingStrategy())) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + void embedding3Large() { + + EmbeddingResponse embeddingResponse = this.openAiOfficialEmbeddingModel + .call(new EmbeddingRequest(List.of("Hello World"), + OpenAiOfficialEmbeddingOptions.builder() + .model(EmbeddingModel.TEXT_EMBEDDING_3_LARGE.toString()) + .build())); + assertThat(embeddingResponse.getResults()).hasSize(1); + assertThat(embeddingResponse.getResults().get(0)).isNotNull(); + assertThat(embeddingResponse.getResults().get(0).getOutput()).hasSize(3072); + assertThat(embeddingResponse.getMetadata().getUsage().getTotalTokens()).isEqualTo(2); + assertThat(embeddingResponse.getMetadata().getUsage().getPromptTokens()).isEqualTo(2); + } + + @Test + void textEmbeddingAda002() { + + EmbeddingResponse embeddingResponse = this.openAiOfficialEmbeddingModel + .call(new EmbeddingRequest(List.of("Hello World"), + OpenAiOfficialEmbeddingOptions.builder() + .model(EmbeddingModel.TEXT_EMBEDDING_3_SMALL.toString()) + .build())); + assertThat(embeddingResponse.getResults()).hasSize(1); + assertThat(embeddingResponse.getResults().get(0)).isNotNull(); + assertThat(embeddingResponse.getResults().get(0).getOutput()).hasSize(1536); + + assertThat(embeddingResponse.getMetadata().getUsage().getTotalTokens()).isEqualTo(2); + assertThat(embeddingResponse.getMetadata().getUsage().getPromptTokens()).isEqualTo(2); + } + +} diff --git a/models/spring-ai-openai-official/src/test/resources/text_source.txt b/models/spring-ai-openai-official/src/test/resources/text_source.txt new file mode 100644 index 00000000000..5f777418da0 --- /dev/null +++ b/models/spring-ai-openai-official/src/test/resources/text_source.txt @@ -0,0 +1,4124 @@ + + Spring Framework Documentation + + + Version 6.0.0 + + Chapter 1. Spring Framework Overview + + + Spring makes it easy to create Java enterprise applications. It provides everything you need to + embrace the Java language in an enterprise environment, with support for Groovy and Kotlin as + alternative languages on the JVM, and with the flexibility to create many kinds of architectures + depending on an application’s needs. As of Spring Framework 5.1, Spring requires JDK 8+ (Java SE + 8+) and provides out-of-the-box support for JDK 11 LTS. Java SE 8 update 60 is suggested as the + minimum patch release for Java 8, but it is generally recommended to use a recent patch release. + + Spring supports a wide range of application scenarios. In a large enterprise, applications often exist + for a long time and have to run on a JDK and application server whose upgrade cycle is beyond + developer control. Others may run as a single jar with the server embedded, possibly in a cloud + environment. Yet others may be standalone applications (such as batch or integration workloads) + that do not need a server. + + + Spring is open source. It has a large and active community that provides continuous feedback based + on a diverse range of real-world use cases. This has helped Spring to successfully evolve over a very + long time. + + 1.1. What We Mean by "Spring" + + + The term "Spring" means different things in different contexts. It can be used to refer to the Spring + Framework project itself, which is where it all started. Over time, other Spring projects have been + built on top of the Spring Framework. Most often, when people say "Spring", they mean the entire + family of projects. This reference documentation focuses on the foundation: the Spring Framework + itself. + + + The Spring Framework is divided into modules. Applications can choose which modules they need. + At the heart are the modules of the core container, including a configuration model and a + dependency injection mechanism. Beyond that, the Spring Framework provides foundational + support for different application architectures, including messaging, transactional data and + persistence, and web. It also includes the Servlet-based Spring MVC web framework and, in + parallel, the Spring WebFlux reactive web framework. + + + A note about modules: Spring’s framework jars allow for deployment to JDK 9’s module path + ("Jigsaw"). For use in Jigsaw-enabled applications, the Spring Framework 5 jars come with + "Automatic-Module-Name" manifest entries which define stable language-level module names + ("spring.core", "spring.context", etc.) independent from jar artifact names (the jars follow the same + naming pattern with "-" instead of ".", e.g. "spring-core" and "spring-context"). Of course, Spring’s + framework jars keep working fine on the classpath on both JDK 8 and 9+. + + 1.2. History of Spring and the Spring Framework + + + Spring came into being in 2003 as a response to the complexity of the early J2EE specifications. + While some consider Java EE and its modern-day successor Jakarta EE to be in competition with + Spring, they are in fact complementary. The Spring programming model does not embrace the + Jakarta EE platform specification; rather, it integrates with carefully selected individual + + specifications from the traditional EE umbrella: + + + • Servlet API (JSR 340) + + • WebSocket API (JSR 356) + + • Concurrency Utilities (JSR 236) + + • JSON Binding API (JSR 367) + + • Bean Validation (JSR 303) + + • JPA (JSR 338) + + • JMS (JSR 914) + + • as well as JTA/JCA setups for transaction coordination, if necessary. + + + The Spring Framework also supports the Dependency Injection (JSR 330) and Common Annotations + (JSR 250) specifications, which application developers may choose to use instead of the Spring- + specific mechanisms provided by the Spring Framework. Originally, those were based on common + javax packages. + + As of Spring Framework 6.0, Spring has been upgraded to the Jakarta EE 9 level (e.g. Servlet 5.0+, + JPA 3.0+), based on the jakarta namespace instead of the traditional javax packages. With EE 9 as + the minimum and EE 10 supported already, Spring is prepared to provide out-of-the-box support + for the further evolution of the Jakarta EE APIs. Spring Framework 6.0 is fully compatible with + Tomcat 10.1, Jetty 11 and Undertow 2.3 as web servers, and also with Hibernate ORM 6.1. + + + Over time, the role of Java/Jakarta EE in application development has evolved. In the early days of + J2EE and Spring, applications were created to be deployed to an application server. Today, with the + help of Spring Boot, applications are created in a devops- and cloud-friendly way, with the Servlet + container embedded and trivial to change. As of Spring Framework 5, a WebFlux application does + not even use the Servlet API directly and can run on servers (such as Netty) that are not Servlet + containers. + + + Spring continues to innovate and to evolve. Beyond the Spring Framework, there are other projects, + such as Spring Boot, Spring Security, Spring Data, Spring Cloud, Spring Batch, among others. It’s + important to remember that each project has its own source code repository, issue tracker, and + release cadence. See spring.io/projects for the complete list of Spring projects. + + 1.3. Design Philosophy + + + When you learn about a framework, it’s important to know not only what it does but what + principles it follows. Here are the guiding principles of the Spring Framework: + + + • Provide choice at every level. Spring lets you defer design decisions as late as possible. For + example, you can switch persistence providers through configuration without changing your + code. The same is true for many other infrastructure concerns and integration with third-party + APIs. + + • Accommodate diverse perspectives. Spring embraces flexibility and is not opinionated about + how things should be done. It supports a wide range of application needs with different + perspectives. + + • Maintain strong backward compatibility. Spring’s evolution has been carefully managed to + force few breaking changes between versions. Spring supports a carefully chosen range of JDK + versions and third-party libraries to facilitate maintenance of applications and libraries that + depend on Spring. + + • Care about API design. The Spring team puts a lot of thought and time into making APIs that are + intuitive and that hold up across many versions and many years. + + • Set high standards for code quality. The Spring Framework puts a strong emphasis on + meaningful, current, and accurate javadoc. It is one of very few projects that can claim clean + code structure with no circular dependencies between packages. + + 1.4. Feedback and Contributions + + + For how-to questions or diagnosing or debugging issues, we suggest using Stack Overflow. Click + here for a list of the suggested tags to use on Stack Overflow. If you’re fairly certain that there is a + problem in the Spring Framework or would like to suggest a feature, please use the GitHub Issues. + + If you have a solution in mind or a suggested fix, you can submit a pull request on Github. + However, please keep in mind that, for all but the most trivial issues, we expect a ticket to be filed + in the issue tracker, where discussions take place and leave a record for future reference. + + + For more details see the guidelines at the CONTRIBUTING, top-level project page. + + 1.5. Getting Started + + + If you are just getting started with Spring, you may want to begin using the Spring Framework by + creating a Spring Boot-based application. Spring Boot provides a quick (and opinionated) way to + create a production-ready Spring-based application. It is based on the Spring Framework, favors + convention over configuration, and is designed to get you up and running as quickly as possible. + + + You can use start.spring.io to generate a basic project or follow one of the "Getting Started" guides, + such as Getting Started Building a RESTful Web Service. As well as being easier to digest, these + guides are very task focused, and most of them are based on Spring Boot. They also cover other + projects from the Spring portfolio that you might want to consider when solving a particular + problem. + + Chapter 2. Core Technologies + + + This part of the reference documentation covers all the technologies that are absolutely integral to + the Spring Framework. + + + Foremost amongst these is the Spring Framework’s Inversion of Control (IoC) container. A thorough + treatment of the Spring Framework’s IoC container is closely followed by comprehensive coverage + of Spring’s Aspect-Oriented Programming (AOP) technologies. The Spring Framework has its own + AOP framework, which is conceptually easy to understand and which successfully addresses the + 80% sweet spot of AOP requirements in Java enterprise programming. + + + Coverage of Spring’s integration with AspectJ (currently the richest — in terms of features — and + certainly most mature AOP implementation in the Java enterprise space) is also provided. + + + AOT processing can be used to optimize your application ahead-of-time. It is typically used for + native image deployment using GraalVM. + + 2.1. The IoC Container + + + This chapter covers Spring’s Inversion of Control (IoC) container. + + + 2.1.1. Introduction to the Spring IoC Container and Beans + + This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) + principle. IoC is also known as dependency injection (DI). It is a process whereby objects define + their dependencies (that is, the other objects they work with) only through constructor arguments, + arguments to a factory method, or properties that are set on the object instance after it is + constructed or returned from a factory method. The container then injects those dependencies + when it creates the bean. This process is fundamentally the inverse (hence the name, Inversion of + Control) of the bean itself controlling the instantiation or location of its dependencies by using + direct construction of classes or a mechanism such as the Service Locator pattern. + + + The org.springframework.beans and org.springframework.context packages are the basis for Spring + Framework’s IoC container. The BeanFactory interface provides an advanced configuration + mechanism capable of managing any type of object. ApplicationContext is a sub-interface of + BeanFactory. It adds: + + + • Easier integration with Spring’s AOP features + + • Message resource handling (for use in internationalization) + + • Event publication + + • Application-layer specific contexts such as the WebApplicationContext for use in web + applications. + + + In short, the BeanFactory provides the configuration framework and basic functionality, and the + ApplicationContext adds more enterprise-specific functionality. The ApplicationContext is a + complete superset of the BeanFactory and is used exclusively in this chapter in descriptions of + Spring’s IoC container. For more information on using the BeanFactory instead of the + + ApplicationContext, see the section covering the BeanFactory API. + + + In Spring, the objects that form the backbone of your application and that are managed by the + Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and + managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your + application. Beans, and the dependencies among them, are reflected in the configuration metadata + used by a container. + + + 2.1.2. Container Overview + + The org.springframework.context.ApplicationContext interface represents the Spring IoC container + and is responsible for instantiating, configuring, and assembling the beans. The container gets its + instructions on what objects to instantiate, configure, and assemble by reading configuration + metadata. The configuration metadata is represented in XML, Java annotations, or Java code. It lets + you express the objects that compose your application and the rich interdependencies between + those objects. + + + Several implementations of the ApplicationContext interface are supplied with Spring. In stand- + alone applications, it is common to create an instance of ClassPathXmlApplicationContext or + FileSystemXmlApplicationContext. While XML has been the traditional format for defining + configuration metadata, you can instruct the container to use Java annotations or code as the + metadata format by providing a small amount of XML configuration to declaratively enable support + for these additional metadata formats. + + + In most application scenarios, explicit user code is not required to instantiate one or more + instances of a Spring IoC container. For example, in a web application scenario, a simple eight (or + so) lines of boilerplate web descriptor XML in the web.xml file of the application typically suffices + (see Convenient ApplicationContext Instantiation for Web Applications). If you use the Spring Tools + for Eclipse (an Eclipse-powered development environment), you can easily create this boilerplate + configuration with a few mouse clicks or keystrokes. + + + The following diagram shows a high-level view of how Spring works. Your application classes are + combined with configuration metadata so that, after the ApplicationContext is created and + initialized, you have a fully configured and executable system or application. + + Figure 1. The Spring IoC container + + + Configuration Metadata + + As the preceding diagram shows, the Spring IoC container consumes a form of configuration + metadata. This configuration metadata represents how you, as an application developer, tell the + Spring container to instantiate, configure, and assemble the objects in your application. + + + Configuration metadata is traditionally supplied in a simple and intuitive XML format, which is + what most of this chapter uses to convey key concepts and features of the Spring IoC container. + + + XML-based metadata is not the only allowed form of configuration metadata. The + Spring IoC container itself is totally decoupled from the format in which this +  configuration metadata is actually written. These days, many developers choose + Java-based configuration for their Spring applications. + + + For information about using other forms of metadata with the Spring container, see: + + + • Annotation-based configuration: Spring 2.5 introduced support for annotation-based + configuration metadata. + + • Java-based configuration: Starting with Spring 3.0, many features provided by the Spring + JavaConfig project became part of the core Spring Framework. Thus, you can define beans + external to your application classes by using Java rather than XML files. To use these new + features, see the @Configuration, @Bean, @Import, and @DependsOn annotations. + + Spring configuration consists of at least one and typically more than one bean definition that the + container must manage. XML-based configuration metadata configures these beans as + elements inside a top-level element. Java configuration typically uses @Bean-annotated + methods within a @Configuration class. + + These bean definitions correspond to the actual objects that make up your application. Typically, + you define service layer objects, data access objects (DAOs), presentation objects such as Struts + Action instances, infrastructure objects such as Hibernate SessionFactories, JMS Queues, and so + forth. Typically, one does not configure fine-grained domain objects in the container, because it is + + usually the responsibility of DAOs and business logic to create and load domain objects. However, + you can use Spring’s integration with AspectJ to configure objects that have been created outside + the control of an IoC container. See Using AspectJ to dependency-inject domain objects with Spring. + + + The following example shows the basic structure of XML-based configuration metadata: + + + + + + + +   ① ② +   +   + + +   +   +   + + +   + + + + + + ① The id attribute is a string that identifies the individual bean definition. + + ② The class attribute defines the type of the bean and uses the fully qualified classname. + + The value of the id attribute refers to collaborating objects. The XML for referring to collaborating + objects is not shown in this example. See Dependencies for more information. + + + Instantiating a Container + + The location path or paths supplied to an ApplicationContext constructor are resource strings that + let the container load configuration metadata from a variety of external resources, such as the local + file system, the Java CLASSPATH, and so on. + + + Java + + + ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", + "daos.xml"); + + + + Kotlin + + + val context = ClassPathXmlApplicationContext("services.xml", "daos.xml") + + After you learn about Spring’s IoC container, you may want to know more about + Spring’s Resource abstraction (as described in Resources), which provides a +  convenient mechanism for reading an InputStream from locations defined in a + URI syntax. In particular, Resource paths are used to construct applications + contexts, as described in Application Contexts and Resource Paths. + + + The following example shows the service layer objects (services.xml) configuration file: + + + + + + + +   + + +   +   +   +   +   + + +   + + + + + + + The following example shows the data access objects daos.xml file: + + + + + + + +   +   +   + + +   +   +   + + +   + + + + + In the preceding example, the service layer consists of the PetStoreServiceImpl class and two data + access objects of the types JpaAccountDao and JpaItemDao (based on the JPA Object-Relational + Mapping standard). The property name element refers to the name of the JavaBean property, and the + ref element refers to the name of another bean definition. This linkage between id and ref + elements expresses the dependency between collaborating objects. For details of configuring an + object’s dependencies, see Dependencies. + + + + Composing XML-based Configuration Metadata + + It can be useful to have bean definitions span multiple XML files. Often, each individual XML + configuration file represents a logical layer or module in your architecture. + + + You can use the application context constructor to load bean definitions from all these XML + fragments. This constructor takes multiple Resource locations, as was shown in the previous section. + Alternatively, use one or more occurrences of the element to load bean definitions from + another file or files. The following example shows how to do so: + + + + +   +   +   + + +   +   + + + + + In the preceding example, external bean definitions are loaded from three files: services.xml, + messageSource.xml, and themeSource.xml. All location paths are relative to the definition file doing + the importing, so services.xml must be in the same directory or classpath location as the file doing + the importing, while messageSource.xml and themeSource.xml must be in a resources location below + the location of the importing file. As you can see, a leading slash is ignored. However, given that + these paths are relative, it is better form not to use the slash at all. The contents of the files being + imported, including the top level element, must be valid XML bean definitions, according + to the Spring Schema. + + It is possible, but not recommended, to reference files in parent directories using a + relative "../" path. Doing so creates a dependency on a file that is outside the + current application. In particular, this reference is not recommended for + classpath: URLs (for example, classpath:../services.xml), where the runtime + resolution process chooses the “nearest” classpath root and then looks into its + parent directory. Classpath configuration changes may lead to the choice of a + different, incorrect directory. +  + You can always use fully qualified resource locations instead of relative paths: for + example, file:C:/config/services.xml or classpath:/config/services.xml. + However, be aware that you are coupling your application’s configuration to + specific absolute locations. It is generally preferable to keep an indirection for such + absolute locations — for example, through "${…}" placeholders that are resolved + against JVM system properties at runtime. + + + The namespace itself provides the import directive feature. Further configuration features beyond + plain bean definitions are available in a selection of XML namespaces provided by Spring — for + example, the context and util namespaces. + + + + The Groovy Bean Definition DSL + + As a further example for externalized configuration metadata, bean definitions can also be + expressed in Spring’s Groovy Bean Definition DSL, as known from the Grails framework. Typically, + such configuration live in a ".groovy" file with the structure shown in the following example: + + + + beans { +   dataSource(BasicDataSource) { +   driverClassName = "org.hsqldb.jdbcDriver" +   url = "jdbc:hsqldb:mem:grailsDB" +   username = "sa" +   password = "" +   settings = [mynew:"setting"] +   } +   sessionFactory(SessionFactory) { +   dataSource = dataSource +   } +   myService(MyService) { +   nestedBean = { AnotherBean bean -> +   dataSource = dataSource +   } +   } + } + + + + This configuration style is largely equivalent to XML bean definitions and even supports Spring’s + XML configuration namespaces. It also allows for importing XML bean definition files through an + importBeans directive. + + Using the Container + + The ApplicationContext is the interface for an advanced factory capable of maintaining a registry of + different beans and their dependencies. By using the method T getBean(String name, Class + requiredType), you can retrieve instances of your beans. + + The ApplicationContext lets you read bean definitions and access them, as the following example + shows: + + + Java + + + // create and configure beans + ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", + "daos.xml"); + + + // retrieve configured instance + PetStoreService service = context.getBean("petStore", PetStoreService.class); + + + // use configured instance + List userList = service.getUsernameList(); + + + + Kotlin + + + import org.springframework.beans.factory.getBean + + + // create and configure beans + val context = ClassPathXmlApplicationContext("services.xml", "daos.xml") + + + // retrieve configured instance + val service = context.getBean("petStore") + + + // use configured instance + var userList = service.getUsernameList() + + + + With Groovy configuration, bootstrapping looks very similar. It has a different context + implementation class which is Groovy-aware (but also understands XML bean definitions). The + following example shows Groovy configuration: + + + Java + + + ApplicationContext context = new GenericGroovyApplicationContext("services.groovy", + "daos.groovy"); + + + + Kotlin + + + val context = GenericGroovyApplicationContext("services.groovy", "daos.groovy") + + + + The most flexible variant is GenericApplicationContext in combination with reader delegates — for + example, with XmlBeanDefinitionReader for XML files, as the following example shows: + + Java + + + GenericApplicationContext context = new GenericApplicationContext(); + new XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml"); + context.refresh(); + + + + Kotlin + + + val context = GenericApplicationContext() + XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml") + context.refresh() + + + + You can also use the GroovyBeanDefinitionReader for Groovy files, as the following example shows: + + + Java + + + GenericApplicationContext context = new GenericApplicationContext(); + new GroovyBeanDefinitionReader(context).loadBeanDefinitions("services.groovy", + "daos.groovy"); + context.refresh(); + + + + Kotlin + + + val context = GenericApplicationContext() + GroovyBeanDefinitionReader(context).loadBeanDefinitions("services.groovy", + "daos.groovy") + context.refresh() + + + + You can mix and match such reader delegates on the same ApplicationContext, reading bean + definitions from diverse configuration sources. + + + You can then use getBean to retrieve instances of your beans. The ApplicationContext interface has a + few other methods for retrieving beans, but, ideally, your application code should never use them. + Indeed, your application code should have no calls to the getBean() method at all and thus have no + dependency on Spring APIs at all. For example, Spring’s integration with web frameworks provides + dependency injection for various web framework components such as controllers and JSF-managed + beans, letting you declare a dependency on a specific bean through metadata (such as an + autowiring annotation). + + + 2.1.3. Bean Overview + + A Spring IoC container manages one or more beans. These beans are created with the configuration + metadata that you supply to the container (for example, in the form of XML definitions). + + + Within the container itself, these bean definitions are represented as BeanDefinition objects, which + contain (among other information) the following metadata: + + • A package-qualified class name: typically, the actual implementation class of the bean being + + defined. + + • Bean behavioral configuration elements, which state how the bean should behave in the + container (scope, lifecycle callbacks, and so forth). + + • References to other beans that are needed for the bean to do its work. These references are also + called collaborators or dependencies. + + • Other configuration settings to set in the newly created object — for example, the size limit of + the pool or the number of connections to use in a bean that manages a connection pool. + + + This metadata translates to a set of properties that make up each bean definition. The following + table describes these properties: + + + Table 1. The bean definition + + Property Explained in… + + Class Instantiating Beans + + Name Naming Beans + + Scope Bean Scopes + + Constructor arguments Dependency Injection + + Properties Dependency Injection + + Autowiring mode Autowiring Collaborators + + Lazy initialization mode Lazy-initialized Beans + + Initialization method Initialization Callbacks + + Destruction method Destruction Callbacks + + + In addition to bean definitions that contain information on how to create a specific bean, the + ApplicationContext implementations also permit the registration of existing objects that are created + outside the container (by users). This is done by accessing the ApplicationContext’s BeanFactory + through the getBeanFactory() method, which returns the DefaultListableBeanFactory + implementation. DefaultListableBeanFactory supports this registration through the + registerSingleton(..) and registerBeanDefinition(..) methods. However, typical applications + work solely with beans defined through regular bean definition metadata. + + + Bean metadata and manually supplied singleton instances need to be registered as + early as possible, in order for the container to properly reason about them during + autowiring and other introspection steps. While overriding existing metadata and +  existing singleton instances is supported to some degree, the registration of new + beans at runtime (concurrently with live access to the factory) is not officially + supported and may lead to concurrent access exceptions, inconsistent state in the + bean container, or both. + + + + Naming Beans + + Every bean has one or more identifiers. These identifiers must be unique within the container that + hosts the bean. A bean usually has only one identifier. However, if it requires more than one, the + + extra ones can be considered aliases. + + + In XML-based configuration metadata, you use the id attribute, the name attribute, or both to specify + the bean identifiers. The id attribute lets you specify exactly one id. Conventionally, these names + are alphanumeric ('myBean', 'someService', etc.), but they can contain special characters as well. If + you want to introduce other aliases for the bean, you can also specify them in the name attribute, + separated by a comma (,), semicolon (;), or white space. As a historical note, in versions prior to + Spring 3.1, the id attribute was defined as an xsd:ID type, which constrained possible characters. As + of 3.1, it is defined as an xsd:string type. Note that bean id uniqueness is still enforced by the + container, though no longer by XML parsers. + + + You are not required to supply a name or an id for a bean. If you do not supply a name or id explicitly, + the container generates a unique name for that bean. However, if you want to refer to that bean by + name, through the use of the ref element or a Service Locator style lookup, you must provide a + name. Motivations for not supplying a name are related to using inner beans and autowiring + collaborators. + + + Bean Naming Conventions + + The convention is to use the standard Java convention for instance field names when naming + beans. That is, bean names start with a lowercase letter and are camel-cased from there. + Examples of such names include accountManager, accountService, userDao, loginController, and + so forth. + + + Naming beans consistently makes your configuration easier to read and understand. Also, if + you use Spring AOP, it helps a lot when applying advice to a set of beans related by name. + + + + + With component scanning in the classpath, Spring generates bean names for + unnamed components, following the rules described earlier: essentially, taking the + simple class name and turning its initial character to lower-case. However, in the +  (unusual) special case when there is more than one character and both the first + and second characters are upper case, the original casing gets preserved. These are + the same rules as defined by java.beans.Introspector.decapitalize (which Spring + uses here). + + + + Aliasing a Bean outside the Bean Definition + + In a bean definition itself, you can supply more than one name for the bean, by using a + combination of up to one name specified by the id attribute and any number of other names in the + name attribute. These names can be equivalent aliases to the same bean and are useful for some + situations, such as letting each component in an application refer to a common dependency by + using a bean name that is specific to that component itself. + + Specifying all aliases where the bean is actually defined is not always adequate, however. It is + sometimes desirable to introduce an alias for a bean that is defined elsewhere. This is commonly + the case in large systems where configuration is split amongst each subsystem, with each + subsystem having its own set of object definitions. In XML-based configuration metadata, you can + use the element to accomplish this. The following example shows how to do so: + + + + + + In this case, a bean (in the same container) named fromName may also, after the use of this alias + definition, be referred to as toName. + + + For example, the configuration metadata for subsystem A may refer to a DataSource by the name of + subsystemA-dataSource. The configuration metadata for subsystem B may refer to a DataSource by + the name of subsystemB-dataSource. When composing the main application that uses both these + subsystems, the main application refers to the DataSource by the name of myApp-dataSource. To have + all three names refer to the same object, you can add the following alias definitions to the + configuration metadata: + + + + + + + + + Now each component and the main application can refer to the dataSource through a name that is + unique and guaranteed not to clash with any other definition (effectively creating a namespace), + yet they refer to the same bean. + + + Java-configuration + + If you use Javaconfiguration, the @Bean annotation can be used to provide aliases. See Using + the @Bean Annotation for details. + + + + + Instantiating Beans + + A bean definition is essentially a recipe for creating one or more objects. The container looks at the + recipe for a named bean when asked and uses the configuration metadata encapsulated by that + bean definition to create (or acquire) an actual object. + + + If you use XML-based configuration metadata, you specify the type (or class) of object that is to be + instantiated in the class attribute of the element. This class attribute (which, internally, is a + Class property on a BeanDefinition instance) is usually mandatory. (For exceptions, see + Instantiation by Using an Instance Factory Method and Bean Definition Inheritance.) You can use + the Class property in one of two ways: + + + • Typically, to specify the bean class to be constructed in the case where the container itself + directly creates the bean by calling its constructor reflectively, somewhat equivalent to Java + code with the new operator. + + • To specify the actual class containing the static factory method that is invoked to create the + object, in the less common case where the container invokes a static factory method on a class + to create the bean. The object type returned from the invocation of the static factory method + may be the same class or another class entirely. + + Nested class names + + If you want to configure a bean definition for a nested class, you may use either the binary + name or the source name of the nested class. + + + For example, if you have a class called SomeThing in the com.example package, and this + SomeThing class has a static nested class called OtherThing, they can be separated by a dollar + sign ($) or a dot (.). So the value of the class attribute in a bean definition would be + com.example.SomeThing$OtherThing or com.example.SomeThing.OtherThing. + + + + + + Instantiation with a Constructor + + When you create a bean by the constructor approach, all normal classes are usable by and + compatible with Spring. That is, the class being developed does not need to implement any specific + interfaces or to be coded in a specific fashion. Simply specifying the bean class should suffice. + However, depending on what type of IoC you use for that specific bean, you may need a default + (empty) constructor. + + + The Spring IoC container can manage virtually any class you want it to manage. It is not limited to + managing true JavaBeans. Most Spring users prefer actual JavaBeans with only a default (no- + argument) constructor and appropriate setters and getters modeled after the properties in the + container. You can also have more exotic non-bean-style classes in your container. If, for example, + you need to use a legacy connection pool that absolutely does not adhere to the JavaBean + specification, Spring can manage it as well. + + + With XML-based configuration metadata you can specify your bean class as follows: + + + + + + + + + + + For details about the mechanism for supplying arguments to the constructor (if required) and + setting object instance properties after the object is constructed, see Injecting Dependencies. + + + + Instantiation with a Static Factory Method + + When defining a bean that you create with a static factory method, use the class attribute to specify + the class that contains the static factory method and an attribute named factory-method to specify + the name of the factory method itself. You should be able to call this method (with optional + arguments, as described later) and return a live object, which subsequently is treated as if it had + been created through a constructor. One use for such a bean definition is to call static factories in + legacy code. + + + The following bean definition specifies that the bean will be created by calling a factory method. + The definition does not specify the type (class) of the returned object, but rather the class + containing the factory method. In this example, the createInstance() method must be a static + method. The following example shows how to specify a factory method: + + + + + + The following example shows a class that would work with the preceding bean definition: + + + Java + + + public class ClientService { +   private static ClientService clientService = new ClientService(); +   private ClientService() {} + + +   public static ClientService createInstance() { +   return clientService; +   } + } + + + + Kotlin + + + class ClientService private constructor() { +   companion object { +   private val clientService = ClientService() +   @JvmStatic +   fun createInstance() = clientService +   } + } + + + + For details about the mechanism for supplying (optional) arguments to the factory method and + setting object instance properties after the object is returned from the factory, see Dependencies + and Configuration in Detail. + + + + Instantiation by Using an Instance Factory Method + + Similar to instantiation through a static factory method, instantiation with an instance factory + method invokes a non-static method of an existing bean from the container to create a new bean. + To use this mechanism, leave the class attribute empty and, in the factory-bean attribute, specify + the name of a bean in the current (or parent or ancestor) container that contains the instance + method that is to be invoked to create the object. Set the name of the factory method itself with the + factory-method attribute. The following example shows how to configure such a bean: + + + +   + + + + + + + + + The following example shows the corresponding class: + + + Java + + + public class DefaultServiceLocator { + + +   private static ClientService clientService = new ClientServiceImpl(); + + +   public ClientService createClientServiceInstance() { +   return clientService; +   } + } + + + + Kotlin + + + class DefaultServiceLocator { +   companion object { +   private val clientService = ClientServiceImpl() +   } +   fun createClientServiceInstance(): ClientService { +   return clientService +   } + } + + + + One factory class can also hold more than one factory method, as the following example shows: + + + + +   + + + + + + + + + The following example shows the corresponding class: + + + Java + + + public class DefaultServiceLocator { + + +   private static ClientService clientService = new ClientServiceImpl(); + + +   private static AccountService accountService = new AccountServiceImpl(); + + +   public ClientService createClientServiceInstance() { +   return clientService; +   } + + +   public AccountService createAccountServiceInstance() { +   return accountService; +   } + } + + + + Kotlin + + + class DefaultServiceLocator { +   companion object { +   private val clientService = ClientServiceImpl() +   private val accountService = AccountServiceImpl() +   } + + +   fun createClientServiceInstance(): ClientService { +   return clientService +   } + + +   fun createAccountServiceInstance(): AccountService { +   return accountService +   } + } + + + + This approach shows that the factory bean itself can be managed and configured through + dependency injection (DI). See Dependencies and Configuration in Detail. + + + In Spring documentation, "factory bean" refers to a bean that is configured in the + Spring container and that creates objects through an instance or static factory +  method. By contrast, FactoryBean (notice the capitalization) refers to a Spring- + specific FactoryBean implementation class. + + + + Determining a Bean’s Runtime Type + + The runtime type of a specific bean is non-trivial to determine. A specified class in the bean + metadata definition is just an initial class reference, potentially combined with a declared factory + method or being a FactoryBean class which may lead to a different runtime type of the bean, or not + + being set at all in case of an instance-level factory method (which is resolved via the specified + factory-bean name instead). Additionally, AOP proxying may wrap a bean instance with an + interface-based proxy with limited exposure of the target bean’s actual type (just its implemented + interfaces). + + The recommended way to find out about the actual runtime type of a particular bean is a + BeanFactory.getType call for the specified bean name. This takes all of the above cases into account + and returns the type of object that a BeanFactory.getBean call is going to return for the same bean + name. + + 2.1.4. Dependencies + + A typical enterprise application does not consist of a single object (or bean in the Spring parlance). + Even the simplest application has a few objects that work together to present what the end-user + sees as a coherent application. This next section explains how you go from defining a number of + bean definitions that stand alone to a fully realized application where objects collaborate to achieve + a goal. + + + Dependency Injection + + Dependency injection (DI) is a process whereby objects define their dependencies (that is, the other + objects with which they work) only through constructor arguments, arguments to a factory method, + or properties that are set on the object instance after it is constructed or returned from a factory + method. The container then injects those dependencies when it creates the bean. This process is + fundamentally the inverse (hence the name, Inversion of Control) of the bean itself controlling the + instantiation or location of its dependencies on its own by using direct construction of classes or the + Service Locator pattern. + + + Code is cleaner with the DI principle, and decoupling is more effective when objects are provided + with their dependencies. The object does not look up its dependencies and does not know the + location or class of the dependencies. As a result, your classes become easier to test, particularly + when the dependencies are on interfaces or abstract base classes, which allow for stub or mock + implementations to be used in unit tests. + + + DI exists in two major variants: Constructor-based dependency injection and Setter-based + dependency injection. + + + + Constructor-based Dependency Injection + + Constructor-based DI is accomplished by the container invoking a constructor with a number of + arguments, each representing a dependency. Calling a static factory method with specific + arguments to construct the bean is nearly equivalent, and this discussion treats arguments to a + constructor and to a static factory method similarly. The following example shows a class that can + only be dependency-injected with constructor injection: + + Java + + + public class SimpleMovieLister { + + +   // the SimpleMovieLister has a dependency on a MovieFinder +   private final MovieFinder movieFinder; + + +   // a constructor so that the Spring container can inject a MovieFinder +   public SimpleMovieLister(MovieFinder movieFinder) { +   this.movieFinder = movieFinder; +   } + + +   // business logic that actually uses the injected MovieFinder is omitted... + } + + + + Kotlin + + + // a constructor so that the Spring container can inject a MovieFinder + class SimpleMovieLister(private val movieFinder: MovieFinder) { +   // business logic that actually uses the injected MovieFinder is omitted... + } + + + + Notice that there is nothing special about this class. It is a POJO that has no dependencies on + container specific interfaces, base classes, or annotations. + + + Constructor Argument Resolution + + Constructor argument resolution matching occurs by using the argument’s type. If no potential + ambiguity exists in the constructor arguments of a bean definition, the order in which the + constructor arguments are defined in a bean definition is the order in which those arguments are + supplied to the appropriate constructor when the bean is being instantiated. Consider the following + class: + + + Java + + + package x.y; + + + public class ThingOne { + + +   public ThingOne(ThingTwo thingTwo, ThingThree thingThree) { +   // ... +   } + } + + Kotlin + + + package x.y + + + class ThingOne(thingTwo: ThingTwo, thingThree: ThingThree) + + + + Assuming that the ThingTwo and ThingThree classes are not related by inheritance, no potential + ambiguity exists. Thus, the following configuration works fine, and you do not need to specify the + constructor argument indexes or types explicitly in the element. + + + + +   +   +   +   + + +   + + +   + + + + + When another bean is referenced, the type is known, and matching can occur (as was the case with + the preceding example). When a simple type is used, such as true, Spring cannot + determine the type of the value, and so cannot match by type without help. Consider the following + class: + + + Java + + + package examples; + + + public class ExampleBean { + + +   // Number of years to calculate the Ultimate Answer +   private final int years; + + +   // The Answer to Life, the Universe, and Everything +   private final String ultimateAnswer; + + +   public ExampleBean(int years, String ultimateAnswer) { +   this.years = years; +   this.ultimateAnswer = ultimateAnswer; +   } + } + + Kotlin + + + package examples + + + class ExampleBean( +   private val years: Int, // Number of years to calculate the Ultimate Answer +   private val ultimateAnswer: String // The Answer to Life, the Universe, and + Everything + ) + + + + Constructor argument type matching + In the preceding scenario, the container can use type matching with simple types if you explicitly + specify the type of the constructor argument by using the type attribute, as the following example + shows: + + + + +   +   + + + + + Constructor argument index + You can use the index attribute to specify explicitly the index of constructor arguments, as the + following example shows: + + + + +   +   + + + + + In addition to resolving the ambiguity of multiple simple values, specifying an index resolves + ambiguity where a constructor has two arguments of the same type. + +  The index is 0-based. + + + Constructor argument name + You can also use the constructor parameter name for value disambiguation, as the following + example shows: + + + + +   +   + + + + + Keep in mind that, to make this work out of the box, your code must be compiled with the debug + flag enabled so that Spring can look up the parameter name from the constructor. If you cannot or + + do not want to compile your code with the debug flag, you can use the @ConstructorProperties JDK + annotation to explicitly name your constructor arguments. The sample class would then have to + look as follows: + + + Java + + + package examples; + + + public class ExampleBean { + + +   // Fields omitted + + +   @ConstructorProperties({"years", "ultimateAnswer"}) +   public ExampleBean(int years, String ultimateAnswer) { +   this.years = years; +   this.ultimateAnswer = ultimateAnswer; +   } + } + + + + Kotlin + + + package examples + + + class ExampleBean + @ConstructorProperties("years", "ultimateAnswer") + constructor(val years: Int, val ultimateAnswer: String) + + + + + Setter-based Dependency Injection + + Setter-based DI is accomplished by the container calling setter methods on your beans after + invoking a no-argument constructor or a no-argument static factory method to instantiate your + bean. + + The following example shows a class that can only be dependency-injected by using pure setter + injection. This class is conventional Java. It is a POJO that has no dependencies on container specific + interfaces, base classes, or annotations. + + Java + + + public class SimpleMovieLister { + + +   // the SimpleMovieLister has a dependency on the MovieFinder +   private MovieFinder movieFinder; + + +   // a setter method so that the Spring container can inject a MovieFinder +   public void setMovieFinder(MovieFinder movieFinder) { +   this.movieFinder = movieFinder; +   } + + +   // business logic that actually uses the injected MovieFinder is omitted... + } + + + + Kotlin + + + class SimpleMovieLister { + + +   // a late-initialized property so that the Spring container can inject a + MovieFinder +   lateinit var movieFinder: MovieFinder + + +   // business logic that actually uses the injected MovieFinder is omitted... + } + + + + The ApplicationContext supports constructor-based and setter-based DI for the beans it manages. It + also supports setter-based DI after some dependencies have already been injected through the + constructor approach. You configure the dependencies in the form of a BeanDefinition, which you + use in conjunction with PropertyEditor instances to convert properties from one format to another. + However, most Spring users do not work with these classes directly (that is, programmatically) but + rather with XML bean definitions, annotated components (that is, classes annotated with @Component, + @Controller, and so forth), or @Bean methods in Java-based @Configuration classes. These sources are + then converted internally into instances of BeanDefinition and used to load an entire Spring IoC + container instance. + + Constructor-based or setter-based DI? + + Since you can mix constructor-based and setter-based DI, it is a good rule of thumb to use + constructors for mandatory dependencies and setter methods or configuration methods for + optional dependencies. Note that use of the @Autowired annotation on a setter method can + be used to make the property be a required dependency; however, constructor injection with + programmatic validation of arguments is preferable. + + The Spring team generally advocates constructor injection, as it lets you implement + application components as immutable objects and ensures that required dependencies are + not null. Furthermore, constructor-injected components are always returned to the client + (calling) code in a fully initialized state. As a side note, a large number of constructor + arguments is a bad code smell, implying that the class likely has too many responsibilities and + should be refactored to better address proper separation of concerns. + + + Setter injection should primarily only be used for optional dependencies that can be assigned + reasonable default values within the class. Otherwise, not-null checks must be performed + everywhere the code uses the dependency. One benefit of setter injection is that setter + methods make objects of that class amenable to reconfiguration or re-injection later. + Management through JMX MBeans is therefore a compelling use case for setter injection. + + + Use the DI style that makes the most sense for a particular class. Sometimes, when dealing + with third-party classes for which you do not have the source, the choice is made for you. For + example, if a third-party class does not expose any setter methods, then constructor injection + may be the only available form of DI. + + + + + + Dependency Resolution Process + + The container performs bean dependency resolution as follows: + + + • The ApplicationContext is created and initialized with configuration metadata that describes all + the beans. Configuration metadata can be specified by XML, Java code, or annotations. + + • For each bean, its dependencies are expressed in the form of properties, constructor arguments, + or arguments to the static-factory method (if you use that instead of a normal constructor). + These dependencies are provided to the bean, when the bean is actually created. + + • Each property or constructor argument is an actual definition of the value to set, or a reference + to another bean in the container. + + • Each property or constructor argument that is a value is converted from its specified format to + the actual type of that property or constructor argument. By default, Spring can convert a value + supplied in string format to all built-in types, such as int, long, String, boolean, and so forth. + + The Spring container validates the configuration of each bean as the container is created. However, + the bean properties themselves are not set until the bean is actually created. Beans that are + singleton-scoped and set to be pre-instantiated (the default) are created when the container is + created. Scopes are defined in Bean Scopes. Otherwise, the bean is created only when it is + requested. Creation of a bean potentially causes a graph of beans to be created, as the bean’s + dependencies and its dependencies' dependencies (and so on) are created and assigned. Note that + + resolution mismatches among those dependencies may show up late — that is, on first creation of + the affected bean. + + + Circular dependencies + + If you use predominantly constructor injection, it is possible to create an unresolvable + circular dependency scenario. + + + For example: Class A requires an instance of class B through constructor injection, and class B + requires an instance of class A through constructor injection. If you configure beans for + classes A and B to be injected into each other, the Spring IoC container detects this circular + reference at runtime, and throws a BeanCurrentlyInCreationException. + + + One possible solution is to edit the source code of some classes to be configured by setters + rather than constructors. Alternatively, avoid constructor injection and use setter injection + only. In other words, although it is not recommended, you can configure circular + dependencies with setter injection. + + + Unlike the typical case (with no circular dependencies), a circular dependency between bean + A and bean B forces one of the beans to be injected into the other prior to being fully + initialized itself (a classic chicken-and-egg scenario). + + + + You can generally trust Spring to do the right thing. It detects configuration problems, such as + references to non-existent beans and circular dependencies, at container load-time. Spring sets + properties and resolves dependencies as late as possible, when the bean is actually created. This + means that a Spring container that has loaded correctly can later generate an exception when you + request an object if there is a problem creating that object or one of its dependencies — for + example, the bean throws an exception as a result of a missing or invalid property. This potentially + delayed visibility of some configuration issues is why ApplicationContext implementations by + default pre-instantiate singleton beans. At the cost of some upfront time and memory to create + these beans before they are actually needed, you discover configuration issues when the + ApplicationContext is created, not later. You can still override this default behavior so that singleton + beans initialize lazily, rather than being eagerly pre-instantiated. + + + If no circular dependencies exist, when one or more collaborating beans are being injected into a + dependent bean, each collaborating bean is totally configured prior to being injected into the + dependent bean. This means that, if bean A has a dependency on bean B, the Spring IoC container + completely configures bean B prior to invoking the setter method on bean A. In other words, the + bean is instantiated (if it is not a pre-instantiated singleton), its dependencies are set, and the + relevant lifecycle methods (such as a configured init method or the InitializingBean callback + method) are invoked. + + + + Examples of Dependency Injection + + The following example uses XML-based configuration metadata for setter-based DI. A small part of + a Spring XML configuration file specifies some bean definitions as follows: + + +   +   +   +   + + +   +   +   + + + + + + + + + The following example shows the corresponding ExampleBean class: + + + Java + + + public class ExampleBean { + + +   private AnotherBean beanOne; + + +   private YetAnotherBean beanTwo; + + +   private int i; + + +   public void setBeanOne(AnotherBean beanOne) { +   this.beanOne = beanOne; +   } + + +   public void setBeanTwo(YetAnotherBean beanTwo) { +   this.beanTwo = beanTwo; +   } + + +   public void setIntegerProperty(int i) { +   this.i = i; +   } + } + + + + Kotlin + + + class ExampleBean { +   lateinit var beanOne: AnotherBean +   lateinit var beanTwo: YetAnotherBean +   var i: Int = 0 + } + + + + In the preceding example, setters are declared to match against the properties specified in the XML + + file. The following example uses constructor-based DI: + + + + +   +   +   +   + + +   +   + + +   + + + + + + + + + The following example shows the corresponding ExampleBean class: + + + Java + + + public class ExampleBean { + + +   private AnotherBean beanOne; + + +   private YetAnotherBean beanTwo; + + +   private int i; + + +   public ExampleBean( +   AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) { +   this.beanOne = anotherBean; +   this.beanTwo = yetAnotherBean; +   this.i = i; +   } + } + + + + Kotlin + + + class ExampleBean( +   private val beanOne: AnotherBean, +   private val beanTwo: YetAnotherBean, +   private val i: Int) + + + + The constructor arguments specified in the bean definition are used as arguments to the + constructor of the ExampleBean. + + + Now consider a variant of this example, where, instead of using a constructor, Spring is told to call + a static factory method to return an instance of the object: + + +   +   +   + + + + + + + + + The following example shows the corresponding ExampleBean class: + + + Java + + + public class ExampleBean { + + +   // a private constructor +   private ExampleBean(...) { +   ... +   } + + +   // a static factory method; the arguments to this method can be +   // considered the dependencies of the bean that is returned, +   // regardless of how those arguments are actually used. +   public static ExampleBean createInstance ( +   AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) { + + +   ExampleBean eb = new ExampleBean (...); +   // some other operations... +   return eb; +   } + } + + + + Kotlin + + + class ExampleBean private constructor() { +   companion object { +   // a static factory method; the arguments to this method can be +   // considered the dependencies of the bean that is returned, +   // regardless of how those arguments are actually used. +   @JvmStatic +   fun createInstance(anotherBean: AnotherBean, yetAnotherBean: YetAnotherBean, + i: Int): ExampleBean { +   val eb = ExampleBean (...) +   // some other operations... +   return eb +   } +   } + } + + Arguments to the static factory method are supplied by elements, exactly the + same as if a constructor had actually been used. The type of the class being returned by the factory + method does not have to be of the same type as the class that contains the static factory method + (although, in this example, it is). An instance (non-static) factory method can be used in an + essentially identical fashion (aside from the use of the factory-bean attribute instead of the class + attribute), so we do not discuss those details here. + + + Dependencies and Configuration in Detail + + As mentioned in the previous section, you can define bean properties and constructor arguments as + references to other managed beans (collaborators) or as values defined inline. Spring’s XML-based + configuration metadata supports sub-element types within its and + elements for this purpose. + + + + Straight Values (Primitives, Strings, and so on) + + The value attribute of the element specifies a property or constructor argument as a + human-readable string representation. Spring’s conversion service is used to convert these values + from a String to the actual type of the property or argument. The following example shows various + values being set: + + + + +   +   +   +   +   + + + + + The following example uses the p-namespace for even more succinct XML configuration: + + + + + + +   + + + + + + + The preceding XML is more succinct. However, typos are discovered at runtime rather than design + + time, unless you use an IDE (such as IntelliJ IDEA or the Spring Tools for Eclipse) that supports + automatic property completion when you create bean definitions. Such IDE assistance is highly + recommended. + + + You can also configure a java.util.Properties instance, as follows: + + + + + + +   +   +   +   jdbc.driver.className=com.mysql.jdbc.Driver +   jdbc.url=jdbc:mysql://localhost:3306/mydb +   +   + + + + + The Spring container converts the text inside the element into a java.util.Properties + instance by using the JavaBeans PropertyEditor mechanism. This is a nice shortcut, and is one of a + few places where the Spring team do favor the use of the nested element over the value + attribute style. + + + The idref element + + The idref element is simply an error-proof way to pass the id (a string value - not a reference) of + another bean in the container to a or element. The following + example shows how to use it: + + + + + + + +   +   +   + + + + + The preceding bean definition snippet is exactly equivalent (at runtime) to the following snippet: + + + + + + + +   + + + + + The first form is preferable to the second, because using the idref tag lets the container validate at + deployment time that the referenced, named bean actually exists. In the second variation, no + + validation is performed on the value that is passed to the targetName property of the client bean. + Typos are only discovered (with most likely fatal results) when the client bean is actually + instantiated. If the client bean is a prototype bean, this typo and the resulting exception may only + be discovered long after the container is deployed. + + + The local attribute on the idref element is no longer supported in the 4.0 beans + XSD, since it does not provide value over a regular bean reference any more. +  Change your existing idref local references to idref bean when upgrading to the + 4.0 schema. + + + A common place (at least in versions earlier than Spring 2.0) where the element brings + value is in the configuration of AOP interceptors in a ProxyFactoryBean bean definition. Using + elements when you specify the interceptor names prevents you from misspelling an + interceptor ID. + + + + References to Other Beans (Collaborators) + + The ref element is the final element inside a or definition element. + Here, you set the value of the specified property of a bean to be a reference to another bean (a + collaborator) managed by the container. The referenced bean is a dependency of the bean whose + property is to be set, and it is initialized on demand as needed before the property is set. (If the + collaborator is a singleton bean, it may already be initialized by the container.) All references are + ultimately a reference to another object. Scoping and validation depend on whether you specify the + ID or name of the other object through the bean or parent attribute. + + + Specifying the target bean through the bean attribute of the tag is the most general form and + allows creation of a reference to any bean in the same container or parent container, regardless of + whether it is in the same XML file. The value of the bean attribute may be the same as the id + attribute of the target bean or be the same as one of the values in the name attribute of the target + bean. The following example shows how to use a ref element: + + + + + + + + Specifying the target bean through the parent attribute creates a reference to a bean that is in a + parent container of the current container. The value of the parent attribute may be the same as + either the id attribute of the target bean or one of the values in the name attribute of the target bean. + The target bean must be in a parent container of the current one. You should use this bean + reference variant mainly when you have a hierarchy of containers and you want to wrap an + existing bean in a parent container with a proxy that has the same name as the parent bean. The + following pair of listings shows how to use the parent attribute: + + + + + +   + + + + +   class="org.springframework.aop.framework.ProxyFactoryBean"> +   +   +   +   + + + + + + The local attribute on the ref element is no longer supported in the 4.0 beans XSD, +  since it does not provide value over a regular bean reference any more. Change + your existing ref local references to ref bean when upgrading to the 4.0 schema. + + + + Inner Beans + + A element inside the or elements defines an inner bean, as + the following example shows: + + + + +   +   +   +   +   +   +   + + + + + An inner bean definition does not require a defined ID or name. If specified, the container does not + use such a value as an identifier. The container also ignores the scope flag on creation, because + inner beans are always anonymous and are always created with the outer bean. It is not possible to + access inner beans independently or to inject them into collaborating beans other than into the + enclosing bean. + + + As a corner case, it is possible to receive destruction callbacks from a custom scope — for example, + for a request-scoped inner bean contained within a singleton bean. The creation of the inner bean + instance is tied to its containing bean, but destruction callbacks let it participate in the request + scope’s lifecycle. This is not a common scenario. Inner beans typically simply share their containing + bean’s scope. + + + + Collections + + The , , , and elements set the properties and arguments of the Java + Collection types List, Set, Map, and Properties, respectively. The following example shows how to + use them: + + +   +   +   +   administrator@example.org +   support@example.org +   development@example.org +   +   +   +   +   +   a list element followed by a reference +   +   +   +   +   +   +   +   +   +   +   +   +   +   just some string +   +   +   + + + + + The value of a map key or value, or a set value, can also be any of the following elements: + + + + bean | ref | idref | list | set | map | props | value | null + + + + Collection Merging + + The Spring container also supports merging collections. An application developer can define a + parent , , or element and have child , , or + elements inherit and override values from the parent collection. That is, the child collection’s + values are the result of merging the elements of the parent and child collections, with the child’s + collection elements overriding values specified in the parent collection. + + + This section on merging discusses the parent-child bean mechanism. Readers unfamiliar with + parent and child bean definitions may wish to read the relevant section before continuing. + + + The following example demonstrates collection merging: + + +   +   +   +   administrator@example.com +   support@example.com +   +   +   +   +   +   +   +   sales@example.com +   support@example.co.uk +   +   +   + + + + + Notice the use of the merge=true attribute on the element of the adminEmails property of the + child bean definition. When the child bean is resolved and instantiated by the container, the + resulting instance has an adminEmails Properties collection that contains the result of merging the + child’s adminEmails collection with the parent’s adminEmails collection. The following listing shows + the result: + + + + administrator=administrator@example.com + sales=sales@example.com + support=support@example.co.uk + + + + The child Properties collection’s value set inherits all property elements from the parent , + and the child’s value for the support value overrides the value in the parent collection. + + + This merging behavior applies similarly to the , , and collection types. In the + specific case of the element, the semantics associated with the List collection type (that is, + the notion of an ordered collection of values) is maintained. The parent’s values precede all of the + child list’s values. In the case of the Map, Set, and Properties collection types, no ordering exists. + Hence, no ordering semantics are in effect for the collection types that underlie the associated Map, + Set, and Properties implementation types that the container uses internally. + + + Limitations of Collection Merging + + You cannot merge different collection types (such as a Map and a List). If you do attempt to do so, an + appropriate Exception is thrown. The merge attribute must be specified on the lower, inherited, child + definition. Specifying the merge attribute on a parent collection definition is redundant and does not + result in the desired merging. + + Strongly-typed collection + + Thanks to Java’s support for generic types, you can use strongly typed collections. That is, it is + possible to declare a Collection type such that it can only contain (for example) String elements. If + you use Spring to dependency-inject a strongly-typed Collection into a bean, you can take + advantage of Spring’s type-conversion support such that the elements of your strongly-typed + Collection instances are converted to the appropriate type prior to being added to the Collection. + The following Java class and bean definition show how to do so: + + + Java + + + public class SomeClass { + + +   private Map accounts; + + +   public void setAccounts(Map accounts) { +   this.accounts = accounts; +   } + } + + + + Kotlin + + + class SomeClass { +   lateinit var accounts: Map + } + + + + + +   +   +   +   +   +   +   +   +   + + + + + When the accounts property of the something bean is prepared for injection, the generics + information about the element type of the strongly-typed Map is available by + reflection. Thus, Spring’s type conversion infrastructure recognizes the various value elements as + being of type Float, and the string values (9.99, 2.75, and 3.99) are converted into an actual Float + type. + + + + Null and Empty String Values + + Spring treats empty arguments for properties and the like as empty Strings. The following XML- + based configuration metadata snippet sets the email property to the empty String value (""). + + +   + + + + + The preceding example is equivalent to the following Java code: + + + Java + + + exampleBean.setEmail(""); + + + + Kotlin + + + exampleBean.email = "" + + + + The element handles null values. The following listing shows an example: + + + + +   +   +   + + + + + The preceding configuration is equivalent to the following Java code: + + + Java + + + exampleBean.setEmail(null); + + + + Kotlin + + + exampleBean.email = null + + + + + XML Shortcut with the p-namespace + + The p-namespace lets you use the bean element’s attributes (instead of nested elements) + to describe your property values collaborating beans, or both. + + + Spring supports extensible configuration formats with namespaces, which are based on an XML + Schema definition. The beans configuration format discussed in this chapter is defined in an XML + Schema document. However, the p-namespace is not defined in an XSD file and exists only in the + core of Spring. + + + The following example shows two XML snippets (the first uses standard XML format and the + second uses the p-namespace) that resolve to the same result: + + + + +   +   +   + + +   + + + + + The example shows an attribute in the p-namespace called email in the bean definition. This tells + Spring to include a property declaration. As previously mentioned, the p-namespace does not have + a schema definition, so you can set the name of the attribute to the property name. + + + This next example includes two more bean definitions that both have a reference to another bean: + + + + + + +   +   +   +   + + +   + + +   +   +   + + + + + This example includes not only a property value using the p-namespace but also uses a special + format to declare property references. Whereas the first bean definition uses to create a reference from bean john to bean jane, the second bean + definition uses p:spouse-ref="jane" as an attribute to do the exact same thing. In this case, spouse is + the property name, whereas the -ref part indicates that this is not a straight value but rather a + reference to another bean. + + The p-namespace is not as flexible as the standard XML format. For example, the + format for declaring property references clashes with properties that end in Ref, +  whereas the standard XML format does not. We recommend that you choose your + approach carefully and communicate this to your team members to avoid + producing XML documents that use all three approaches at the same time. + + + + XML Shortcut with the c-namespace + + Similar to the XML Shortcut with the p-namespace, the c-namespace, introduced in Spring 3.1, + allows inlined attributes for configuring the constructor arguments rather then nested constructor- + arg elements. + + + The following example uses the c: namespace to do the same thing as the from Constructor-based + Dependency Injection: + + + + + + +   +   + + +   +   +   +   +   +   + + +   +   + + + + + + + The c: namespace uses the same conventions as the p: one (a trailing -ref for bean references) for + setting the constructor arguments by their names. Similarly, it needs to be declared in the XML file + even though it is not defined in an XSD schema (it exists inside the Spring core). + + For the rare cases where the constructor argument names are not available (usually if the bytecode + was compiled without debugging information), you can use fallback to the argument indexes, as + follows: + + + + + + + + Due to the XML grammar, the index notation requires the presence of the leading + _, as XML attribute names cannot start with a number (even though some IDEs +  allow it). A corresponding index notation is also available for + elements but not commonly used since the plain order of declaration is usually + sufficient there. + + + In practice, the constructor resolution mechanism is quite efficient in matching arguments, so + unless you really need to, we recommend using the name notation throughout your configuration. + + + + Compound Property Names + + You can use compound or nested property names when you set bean properties, as long as all + components of the path except the final property name are not null. Consider the following bean + definition: + + + + +   + + + + + The something bean has a fred property, which has a bob property, which has a sammy property, and + that final sammy property is being set to a value of 123. In order for this to work, the fred property of + something and the bob property of fred must not be null after the bean is constructed. Otherwise, a + NullPointerException is thrown. + + + Using depends-on + + If a bean is a dependency of another bean, that usually means that one bean is set as a property of + another. Typically you accomplish this with the element in XML-based configuration + metadata. However, sometimes dependencies between beans are less direct. An example is when a + static initializer in a class needs to be triggered, such as for database driver registration. The + depends-on attribute can explicitly force one or more beans to be initialized before the bean using + this element is initialized. The following example uses the depends-on attribute to express a + dependency on a single bean: + + + + + + + + + To express a dependency on multiple beans, supply a list of bean names as the value of the depends- + on attribute (commas, whitespace, and semicolons are valid delimiters): + + +   + + + + + + + + + + The depends-on attribute can specify both an initialization-time dependency and, in + the case of singleton beans only, a corresponding destruction-time dependency. +  Dependent beans that define a depends-on relationship with a given bean are + destroyed first, prior to the given bean itself being destroyed. Thus, depends-on can + also control shutdown order. + + + + Lazy-initialized Beans + + By default, ApplicationContext implementations eagerly create and configure all singleton beans as + part of the initialization process. Generally, this pre-instantiation is desirable, because errors in the + configuration or surrounding environment are discovered immediately, as opposed to hours or + even days later. When this behavior is not desirable, you can prevent pre-instantiation of a + singleton bean by marking the bean definition as being lazy-initialized. A lazy-initialized bean tells + the IoC container to create a bean instance when it is first requested, rather than at startup. + + + In XML, this behavior is controlled by the lazy-init attribute on the element, as the + following example shows: + + + + + + + + + When the preceding configuration is consumed by an ApplicationContext, the lazy bean is not + eagerly pre-instantiated when the ApplicationContext starts, whereas the not.lazy bean is eagerly + pre-instantiated. + + + However, when a lazy-initialized bean is a dependency of a singleton bean that is not lazy- + initialized, the ApplicationContext creates the lazy-initialized bean at startup, because it must + satisfy the singleton’s dependencies. The lazy-initialized bean is injected into a singleton bean + elsewhere that is not lazy-initialized. + + You can also control lazy-initialization at the container level by using the default-lazy-init + attribute on the element, as the following example shows: + + + + +   + + + Autowiring Collaborators + + The Spring container can autowire relationships between collaborating beans. You can let Spring + resolve collaborators (other beans) automatically for your bean by inspecting the contents of the + ApplicationContext. Autowiring has the following advantages: + + • Autowiring can significantly reduce the need to specify properties or constructor arguments. + (Other mechanisms such as a bean template discussed elsewhere in this chapter are also + valuable in this regard.) + + • Autowiring can update a configuration as your objects evolve. For example, if you need to add a + dependency to a class, that dependency can be satisfied automatically without you needing to + modify the configuration. Thus autowiring can be especially useful during development, + without negating the option of switching to explicit wiring when the code base becomes more + stable. + + + When using XML-based configuration metadata (see Dependency Injection), you can specify the + autowire mode for a bean definition with the autowire attribute of the element. The + autowiring functionality has four modes. You specify autowiring per bean and can thus choose + which ones to autowire. The following table describes the four autowiring modes: + + + Table 2. Autowiring modes + + Mode Explanation + no (Default) No autowiring. Bean references must be defined by ref elements. + Changing the default setting is not recommended for larger deployments, + because specifying collaborators explicitly gives greater control and clarity. To + some extent, it documents the structure of a system. + byName Autowiring by property name. Spring looks for a bean with the same name as + the property that needs to be autowired. For example, if a bean definition is + set to autowire by name and it contains a master property (that is, it has a + setMaster(..) method), Spring looks for a bean definition named master and + uses it to set the property. + byType Lets a property be autowired if exactly one bean of the property type exists in + the container. If more than one exists, a fatal exception is thrown, which + indicates that you may not use byType autowiring for that bean. If there are no + matching beans, nothing happens (the property is not set). + constructor Analogous to byType but applies to constructor arguments. If there is not + exactly one bean of the constructor argument type in the container, a fatal + error is raised. + + + With byType or constructor autowiring mode, you can wire arrays and typed collections. In such + cases, all autowire candidates within the container that match the expected type are provided to + satisfy the dependency. You can autowire strongly-typed Map instances if the expected key type is + String. An autowired Map instance’s values consist of all bean instances that match the expected + type, and the Map instance’s keys contain the corresponding bean names. + + Limitations and Disadvantages of Autowiring + + Autowiring works best when it is used consistently across a project. If autowiring is not used in + general, it might be confusing to developers to use it to wire only one or two bean definitions. + + + Consider the limitations and disadvantages of autowiring: + + • Explicit dependencies in property and constructor-arg settings always override autowiring. You + cannot autowire simple properties such as primitives, Strings, and Classes (and arrays of such + simple properties). This limitation is by-design. + + • Autowiring is less exact than explicit wiring. Although, as noted in the earlier table, Spring is + careful to avoid guessing in case of ambiguity that might have unexpected results. The + relationships between your Spring-managed objects are no longer documented explicitly. + + • Wiring information may not be available to tools that may generate documentation from a + Spring container. + + • Multiple bean definitions within the container may match the type specified by the setter + method or constructor argument to be autowired. For arrays, collections, or Map instances, this is + not necessarily a problem. However, for dependencies that expect a single value, this ambiguity + is not arbitrarily resolved. If no unique bean definition is available, an exception is thrown. + + + In the latter scenario, you have several options: + + • Abandon autowiring in favor of explicit wiring. + + • Avoid autowiring for a bean definition by setting its autowire-candidate attributes to false, as + described in the next section. + + • Designate a single bean definition as the primary candidate by setting the primary attribute of its + element to true. + + • Implement the more fine-grained control available with annotation-based configuration, as + described in Annotation-based Container Configuration. + + + + Excluding a Bean from Autowiring + + On a per-bean basis, you can exclude a bean from autowiring. In Spring’s XML format, set the + autowire-candidate attribute of the element to false. The container makes that specific bean + definition unavailable to the autowiring infrastructure (including annotation style configurations + such as @Autowired). + + + The autowire-candidate attribute is designed to only affect type-based autowiring. + It does not affect explicit references by name, which get resolved even if the +  specified bean is not marked as an autowire candidate. As a consequence, + autowiring by name nevertheless injects a bean if the name matches. + + + You can also limit autowire candidates based on pattern-matching against bean names. The top- + level element accepts one or more patterns within its default-autowire-candidates + attribute. For example, to limit autowire candidate status to any bean whose name ends with + Repository, provide a value of *Repository. To provide multiple patterns, define them in a comma- + separated list. An explicit value of true or false for a bean definition’s autowire-candidate attribute + + always takes precedence. For such beans, the pattern matching rules do not apply. + + + These techniques are useful for beans that you never want to be injected into other beans by + autowiring. It does not mean that an excluded bean cannot itself be configured by using + autowiring. Rather, the bean itself is not a candidate for autowiring other beans. + + + Method Injection + + In most application scenarios, most beans in the container are singletons. When a singleton bean + needs to collaborate with another singleton bean or a non-singleton bean needs to collaborate with + another non-singleton bean, you typically handle the dependency by defining one bean as a + property of the other. A problem arises when the bean lifecycles are different. Suppose singleton + bean A needs to use non-singleton (prototype) bean B, perhaps on each method invocation on A. + The container creates the singleton bean A only once, and thus only gets one opportunity to set the + properties. The container cannot provide bean A with a new instance of bean B every time one is + needed. + + A solution is to forego some inversion of control. You can make bean A aware of the container by + implementing the ApplicationContextAware interface, and by making a getBean("B") call to the + container ask for (a typically new) bean B instance every time bean A needs it. The following + example shows this approach: + + Java + + + // a class that uses a stateful Command-style class to perform some processing + package fiona.apple; + + + // Spring-API imports + import org.springframework.beans.BeansException; + import org.springframework.context.ApplicationContext; + import org.springframework.context.ApplicationContextAware; + + + public class CommandManager implements ApplicationContextAware { + + +   private ApplicationContext applicationContext; + + +   public Object process(Map commandState) { +   // grab a new instance of the appropriate Command +   Command command = createCommand(); +   // set the state on the (hopefully brand new) Command instance +   command.setState(commandState); +   return command.execute(); +   } + + +   protected Command createCommand() { +   // notice the Spring API dependency! +   return this.applicationContext.getBean("command", Command.class); +   } + + +   public void setApplicationContext( +   ApplicationContext applicationContext) throws BeansException { +   this.applicationContext = applicationContext; +   } + } + + Kotlin + + + // a class that uses a stateful Command-style class to perform some processing + package fiona.apple + + + // Spring-API imports + import org.springframework.context.ApplicationContext + import org.springframework.context.ApplicationContextAware + + + class CommandManager : ApplicationContextAware { + + +   private lateinit var applicationContext: ApplicationContext + + +   fun process(commandState: Map<*, *>): Any { +   // grab a new instance of the appropriate Command +   val command = createCommand() +   // set the state on the (hopefully brand new) Command instance +   command.state = commandState +   return command.execute() +   } + + +   // notice the Spring API dependency! +   protected fun createCommand() = +   applicationContext.getBean("command", Command::class.java) + + +   override fun setApplicationContext(applicationContext: ApplicationContext) { +   this.applicationContext = applicationContext +   } + } + + + + The preceding is not desirable, because the business code is aware of and coupled to the Spring + Framework. Method Injection, a somewhat advanced feature of the Spring IoC container, lets you + handle this use case cleanly. + + + + You can read more about the motivation for Method Injection in this blog entry. + + + + + + Lookup Method Injection + + Lookup method injection is the ability of the container to override methods on container-managed + beans and return the lookup result for another named bean in the container. The lookup typically + involves a prototype bean, as in the scenario described in the preceding section. The Spring + Framework implements this method injection by using bytecode generation from the CGLIB library + to dynamically generate a subclass that overrides the method. + + • For this dynamic subclassing to work, the class that the Spring bean container + subclasses cannot be final, and the method to be overridden cannot be final, + either. + + • Unit-testing a class that has an abstract method requires you to subclass the + class yourself and to supply a stub implementation of the abstract method. +  • Concrete methods are also necessary for component scanning, which requires + concrete classes to pick up. + + • A further key limitation is that lookup methods do not work with factory + methods and in particular not with @Bean methods in configuration classes, + since, in that case, the container is not in charge of creating the instance and + therefore cannot create a runtime-generated subclass on the fly. + + + In the case of the CommandManager class in the previous code snippet, the Spring container + dynamically overrides the implementation of the createCommand() method. The CommandManager class + does not have any Spring dependencies, as the reworked example shows: + + + Java + + + package fiona.apple; + + + // no more Spring imports! + + + public abstract class CommandManager { + + +   public Object process(Object commandState) { +   // grab a new instance of the appropriate Command interface +   Command command = createCommand(); +   // set the state on the (hopefully brand new) Command instance +   command.setState(commandState); +   return command.execute(); +   } + + +   // okay... but where is the implementation of this method? +   protected abstract Command createCommand(); + } + + Kotlin + + + package fiona.apple + + + // no more Spring imports! + + + abstract class CommandManager { + + +   fun process(commandState: Any): Any { +   // grab a new instance of the appropriate Command interface +   val command = createCommand() +   // set the state on the (hopefully brand new) Command instance +   command.state = commandState +   return command.execute() +   } + + +   // okay... but where is the implementation of this method? +   protected abstract fun createCommand(): Command + } + + + + In the client class that contains the method to be injected (the CommandManager in this case), the + method to be injected requires a signature of the following form: + + + + [abstract] theMethodName(no-arguments); + + + + If the method is abstract, the dynamically-generated subclass implements the method. Otherwise, + the dynamically-generated subclass overrides the concrete method defined in the original class. + Consider the following example: + + + + + +   + + + + + +   + + + + + The bean identified as commandManager calls its own createCommand() method whenever it needs a + new instance of the myCommand bean. You must be careful to deploy the myCommand bean as a prototype + if that is actually what is needed. If it is a singleton, the same instance of the myCommand bean is + returned each time. + + + Alternatively, within the annotation-based component model, you can declare a lookup method + through the @Lookup annotation, as the following example shows: + + Java + + + public abstract class CommandManager { + + +   public Object process(Object commandState) { +   Command command = createCommand(); +   command.setState(commandState); +   return command.execute(); +   } + + +   @Lookup("myCommand") +   protected abstract Command createCommand(); + } + + + + Kotlin + + + abstract class CommandManager { + + +   fun process(commandState: Any): Any { +   val command = createCommand() +   command.state = commandState +   return command.execute() +   } + + +   @Lookup("myCommand") +   protected abstract fun createCommand(): Command + } + + + + Or, more idiomatically, you can rely on the target bean getting resolved against the declared return + type of the lookup method: + + + Java + + + public abstract class CommandManager { + + +   public Object process(Object commandState) { +   Command command = createCommand(); +   command.setState(commandState); +   return command.execute(); +   } + + +   @Lookup +   protected abstract Command createCommand(); + } + + Kotlin + + + abstract class CommandManager { + + +   fun process(commandState: Any): Any { +   val command = createCommand() +   command.state = commandState +   return command.execute() +   } + + +   @Lookup +   protected abstract fun createCommand(): Command + } + + + + Note that you should typically declare such annotated lookup methods with a concrete stub + implementation, in order for them to be compatible with Spring’s component scanning rules where + abstract classes get ignored by default. This limitation does not apply to explicitly registered or + explicitly imported bean classes. + + + Another way of accessing differently scoped target beans is an ObjectFactory/ + Provider injection point. See Scoped Beans as Dependencies. +  + You may also find the ServiceLocatorFactoryBean (in the + org.springframework.beans.factory.config package) to be useful. + + + + Arbitrary Method Replacement + + A less useful form of method injection than lookup method injection is the ability to replace + arbitrary methods in a managed bean with another method implementation. You can safely skip + the rest of this section until you actually need this functionality. + + + With XML-based configuration metadata, you can use the replaced-method element to replace an + existing method implementation with another, for a deployed bean. Consider the following class, + which has a method called computeValue that we want to override: + + + Java + + + public class MyValueCalculator { + + +   public String computeValue(String input) { +   // some real code... +   } + + +   // some other methods... + } + + Kotlin + + + class MyValueCalculator { + + +   fun computeValue(input: String): String { +   // some real code... +   } + + +   // some other methods... + } + + + + A class that implements the org.springframework.beans.factory.support.MethodReplacer interface + provides the new method definition, as the following example shows: + + + Java + + + /** +  * meant to be used to override the existing computeValue(String) +  * implementation in MyValueCalculator +  */ + public class ReplacementComputeValue implements MethodReplacer { + + +   public Object reimplement(Object o, Method m, Object[] args) throws Throwable { +   // get the input value, work with it, and return a computed result +   String input = (String) args[0]; +   ... +   return ...; +   } + } + + + + Kotlin + + + /** +  * meant to be used to override the existing computeValue(String) +  * implementation in MyValueCalculator +  */ + class ReplacementComputeValue : MethodReplacer { + + +   override fun reimplement(obj: Any, method: Method, args: Array): Any { +   // get the input value, work with it, and return a computed result +   val input = args[0] as String; +   ... +   return ...; +   } + } + + + + The bean definition to deploy the original class and specify the method override would resemble + the following example: + + +   +   +   String +   + + + + + + + + You can use one or more elements within the element to indicate + the method signature of the method being overridden. The signature for the arguments is + necessary only if the method is overloaded and multiple variants exist within the class. For + convenience, the type string for an argument may be a substring of the fully qualified type name. + For example, the following all match java.lang.String: + + + + java.lang.String + String + Str + + + + Because the number of arguments is often enough to distinguish between each possible choice, this + shortcut can save a lot of typing, by letting you type only the shortest string that matches an + argument type. + + 2.1.5. Bean Scopes + + When you create a bean definition, you create a recipe for creating actual instances of the class + defined by that bean definition. The idea that a bean definition is a recipe is important, because it + means that, as with a class, you can create many object instances from a single recipe. + + + You can control not only the various dependencies and configuration values that are to be plugged + into an object that is created from a particular bean definition but also control the scope of the + objects created from a particular bean definition. This approach is powerful and flexible, because + you can choose the scope of the objects you create through configuration instead of having to bake + in the scope of an object at the Java class level. Beans can be defined to be deployed in one of a + number of scopes. The Spring Framework supports six scopes, four of which are available only if + you use a web-aware ApplicationContext. You can also create a custom scope. + + The following table describes the supported scopes: + + + Table 3. Bean scopes + + Scope Description + + singleton (Default) Scopes a single bean definition to a single object instance for each + Spring IoC container. + + prototype Scopes a single bean definition to any number of object instances. + + Scope Description + + request Scopes a single bean definition to the lifecycle of a single HTTP request. That + is, each HTTP request has its own instance of a bean created off the back of a + single bean definition. Only valid in the context of a web-aware Spring + ApplicationContext. + + session Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid + in the context of a web-aware Spring ApplicationContext. + + application Scopes a single bean definition to the lifecycle of a ServletContext. Only valid + in the context of a web-aware Spring ApplicationContext. + + websocket Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the + context of a web-aware Spring ApplicationContext. + + + + As of Spring 3.0, a thread scope is available but is not registered by default. For +  more information, see the documentation for SimpleThreadScope. For instructions + on how to register this or any other custom scope, see Using a Custom Scope. + + + + The Singleton Scope + + Only one shared instance of a singleton bean is managed, and all requests for beans with an ID or + IDs that match that bean definition result in that one specific bean instance being returned by the + Spring container. + + + To put it another way, when you define a bean definition and it is scoped as a singleton, the Spring + IoC container creates exactly one instance of the object defined by that bean definition. This single + instance is stored in a cache of such singleton beans, and all subsequent requests and references + for that named bean return the cached object. The following image shows how the singleton scope + works: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Spring’s concept of a singleton bean differs from the singleton pattern as defined in the Gang of + Four (GoF) patterns book. The GoF singleton hard-codes the scope of an object such that one and + + only one instance of a particular class is created per ClassLoader. The scope of the Spring singleton + is best described as being per-container and per-bean. This means that, if you define one bean for a + particular class in a single Spring container, the Spring container creates one and only one instance + of the class defined by that bean definition. The singleton scope is the default scope in Spring. To + define a bean as a singleton in XML, you can define a bean as shown in the following example: + + + + + + + + + + + + + The Prototype Scope + + The non-singleton prototype scope of bean deployment results in the creation of a new bean + instance every time a request for that specific bean is made. That is, the bean is injected into + another bean or you request it through a getBean() method call on the container. As a rule, you + should use the prototype scope for all stateful beans and the singleton scope for stateless beans. + + + The following diagram illustrates the Spring prototype scope: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + (A data access object (DAO) is not typically configured as a prototype, because a typical DAO does + not hold any conversational state. It was easier for us to reuse the core of the singleton diagram.) + + The following example defines a bean as a prototype in XML: + + + + + + + + In contrast to the other scopes, Spring does not manage the complete lifecycle of a prototype bean. + + The container instantiates, configures, and otherwise assembles a prototype object and hands it to + the client, with no further record of that prototype instance. Thus, although initialization lifecycle + callback methods are called on all objects regardless of scope, in the case of prototypes, configured + destruction lifecycle callbacks are not called. The client code must clean up prototype-scoped + objects and release expensive resources that the prototype beans hold. To get the Spring container + to release resources held by prototype-scoped beans, try using a custom bean post-processor, which + holds a reference to beans that need to be cleaned up. + + + In some respects, the Spring container’s role in regard to a prototype-scoped bean is a replacement + for the Java new operator. All lifecycle management past that point must be handled by the client. + (For details on the lifecycle of a bean in the Spring container, see Lifecycle Callbacks.) + + + Singleton Beans with Prototype-bean Dependencies + + When you use singleton-scoped beans with dependencies on prototype beans, be aware that + dependencies are resolved at instantiation time. Thus, if you dependency-inject a prototype-scoped + bean into a singleton-scoped bean, a new prototype bean is instantiated and then dependency- + injected into the singleton bean. The prototype instance is the sole instance that is ever supplied to + the singleton-scoped bean. + + + However, suppose you want the singleton-scoped bean to acquire a new instance of the prototype- + scoped bean repeatedly at runtime. You cannot dependency-inject a prototype-scoped bean into + your singleton bean, because that injection occurs only once, when the Spring container + instantiates the singleton bean and resolves and injects its dependencies. If you need a new + instance of a prototype bean at runtime more than once, see Method Injection. + + + Request, Session, Application, and WebSocket Scopes + + The request, session, application, and websocket scopes are available only if you use a web-aware + Spring ApplicationContext implementation (such as XmlWebApplicationContext). If you use these + scopes with regular Spring IoC containers, such as the ClassPathXmlApplicationContext, an + IllegalStateException that complains about an unknown bean scope is thrown. + + + + Initial Web Configuration + + To support the scoping of beans at the request, session, application, and websocket levels (web- + scoped beans), some minor initial configuration is required before you define your beans. (This + initial setup is not required for the standard scopes: singleton and prototype.) + + + How you accomplish this initial setup depends on your particular Servlet environment. + + + If you access scoped beans within Spring Web MVC, in effect, within a request that is processed by + the Spring DispatcherServlet, no special setup is necessary. DispatcherServlet already exposes all + relevant state. + + + If you use a Servlet web container, with requests processed outside of Spring’s DispatcherServlet + (for example, when using JSF or Struts), you need to register the + org.springframework.web.context.request.RequestContextListener ServletRequestListener. This can + be done programmatically by using the WebApplicationInitializer interface. Alternatively, add the + following declaration to your web application’s web.xml file: + + +   ... +   +   +   org.springframework.web.context.request.RequestContextListener +   +   +   ... + + + + + Alternatively, if there are issues with your listener setup, consider using Spring’s + RequestContextFilter. The filter mapping depends on the surrounding web application + configuration, so you have to change it as appropriate. The following listing shows the filter part of + a web application: + + + + +   ... +   +   requestContextFilter +   org.springframework.web.filter.RequestContextFilter +   +   +   requestContextFilter +   /* +   +   ... + + + + + DispatcherServlet, RequestContextListener, and RequestContextFilter all do exactly the same thing, + namely bind the HTTP request object to the Thread that is servicing that request. This makes beans + that are request- and session-scoped available further down the call chain. + + + + Request scope + + Consider the following XML configuration for a bean definition: + + + + + + + + The Spring container creates a new instance of the LoginAction bean by using the loginAction bean + definition for each and every HTTP request. That is, the loginAction bean is scoped at the HTTP + request level. You can change the internal state of the instance that is created as much as you want, + because other instances created from the same loginAction bean definition do not see these + changes in state. They are particular to an individual request. When the request completes + processing, the bean that is scoped to the request is discarded. + + + When using annotation-driven components or Java configuration, the @RequestScope annotation can + + be used to assign a component to the request scope. The following example shows how to do so: + + + Java + + + @RequestScope + @Component + public class LoginAction { +   // ... + } + + + + Kotlin + + + @RequestScope + @Component + class LoginAction { +   // ... + } + + + + + Session Scope + + Consider the following XML configuration for a bean definition: + + + + + + + + The Spring container creates a new instance of the UserPreferences bean by using the + userPreferences bean definition for the lifetime of a single HTTP Session. In other words, the + userPreferences bean is effectively scoped at the HTTP Session level. As with request-scoped beans, + you can change the internal state of the instance that is created as much as you want, knowing that + other HTTP Session instances that are also using instances created from the same userPreferences + bean definition do not see these changes in state, because they are particular to an individual HTTP + Session. When the HTTP Session is eventually discarded, the bean that is scoped to that particular + HTTP Session is also discarded. + + + When using annotation-driven components or Java configuration, you can use the @SessionScope + annotation to assign a component to the session scope. + + + Java + + + @SessionScope + @Component + public class UserPreferences { +   // ... + } + + Kotlin + + + @SessionScope + @Component + class UserPreferences { +   // ... + } + + + + + Application Scope + + Consider the following XML configuration for a bean definition: + + + + + + + + The Spring container creates a new instance of the AppPreferences bean by using the appPreferences + bean definition once for the entire web application. That is, the appPreferences bean is scoped at the + ServletContext level and stored as a regular ServletContext attribute. This is somewhat similar to a + Spring singleton bean but differs in two important ways: It is a singleton per ServletContext, not per + Spring ApplicationContext (for which there may be several in any given web application), and it is + actually exposed and therefore visible as a ServletContext attribute. + + + When using annotation-driven components or Java configuration, you can use the + @ApplicationScope annotation to assign a component to the application scope. The following + example shows how to do so: + + + Java + + + @ApplicationScope + @Component + public class AppPreferences { +   // ... + } + + + + Kotlin + + + @ApplicationScope + @Component + class AppPreferences { +   // ... + } + + + + + WebSocket Scope + + WebSocket scope is associated with the lifecycle of a WebSocket session and applies to STOMP over + WebSocket applications, see WebSocket scope for more details. + + Scoped Beans as Dependencies + + The Spring IoC container manages not only the instantiation of your objects (beans), but also the + wiring up of collaborators (or dependencies). If you want to inject (for example) an HTTP request- + scoped bean into another bean of a longer-lived scope, you may choose to inject an AOP proxy in + place of the scoped bean. That is, you need to inject a proxy object that exposes the same public + interface as the scoped object but that can also retrieve the real target object from the relevant + scope (such as an HTTP request) and delegate method calls onto the real object. + + + You may also use between beans that are scoped as singleton, + with the reference then going through an intermediate proxy that is serializable + and therefore able to re-obtain the target singleton bean on deserialization. + + + When declaring against a bean of scope prototype, every + method call on the shared proxy leads to the creation of a new target instance to + which the call is then being forwarded. + + Also, scoped proxies are not the only way to access beans from shorter scopes in a + lifecycle-safe fashion. You may also declare your injection point (that is, the +  constructor or setter argument or autowired field) as ObjectFactory, + allowing for a getObject() call to retrieve the current instance on demand every + time it is needed — without holding on to the instance or storing it separately. + + + As an extended variant, you may declare ObjectProvider which + delivers several additional access variants, including getIfAvailable and + getIfUnique. + + + The JSR-330 variant of this is called Provider and is used with a + Provider declaration and a corresponding get() call for every + retrieval attempt. See here for more details on JSR-330 overall. + + + The configuration in the following example is only one line, but it is important to understand the + “why” as well as the “how” behind it: + + + + + +   +   +   +   ① +   + + +   +   +   +   +   + + + + ① The line that defines the proxy. + + To create such a proxy, you insert a child element into a scoped bean definition + (see Choosing the Type of Proxy to Create and XML Schema-based configuration). Why do + definitions of beans scoped at the request, session and custom-scope levels require the element? Consider the following singleton bean definition and contrast it with what you + need to define for the aforementioned scopes (note that the following userPreferences bean + definition as it stands is incomplete): + + + + + + + +   + + + + + In the preceding example, the singleton bean (userManager) is injected with a reference to the HTTP + Session-scoped bean (userPreferences). The salient point here is that the userManager bean is a + singleton: it is instantiated exactly once per container, and its dependencies (in this case only one, + the userPreferences bean) are also injected only once. This means that the userManager bean + operates only on the exact same userPreferences object (that is, the one with which it was originally + injected). + + This is not the behavior you want when injecting a shorter-lived scoped bean into a longer-lived + scoped bean (for example, injecting an HTTP Session-scoped collaborating bean as a dependency + into singleton bean). Rather, you need a single userManager object, and, for the lifetime of an HTTP + Session, you need a userPreferences object that is specific to the HTTP Session. Thus, the container + + creates an object that exposes the exact same public interface as the UserPreferences class (ideally + an object that is a UserPreferences instance), which can fetch the real UserPreferences object from + the scoping mechanism (HTTP request, Session, and so forth). The container injects this proxy + object into the userManager bean, which is unaware that this UserPreferences reference is a proxy. In + this example, when a UserManager instance invokes a method on the dependency-injected + UserPreferences object, it is actually invoking a method on the proxy. The proxy then fetches the + real UserPreferences object from (in this case) the HTTP Session and delegates the method + invocation onto the retrieved real UserPreferences object. + + Thus, you need the following (correct and complete) configuration when injecting request- and + session-scoped beans into collaborating objects, as the following example shows: + + + + +   + + + + +   + + + + + + Choosing the Type of Proxy to Create + + By default, when the Spring container creates a proxy for a bean that is marked up with the + element, a CGLIB-based class proxy is created. + + + CGLIB proxies intercept only public method calls! Do not call non-public methods +  on such a proxy. They are not delegated to the actual scoped target object. + + + Alternatively, you can configure the Spring container to create standard JDK interface-based + proxies for such scoped beans, by specifying false for the value of the proxy-target-class attribute + of the element. Using JDK interface-based proxies means that you do not need + additional libraries in your application classpath to affect such proxying. However, it also means + that the class of the scoped bean must implement at least one interface and that all collaborators + into which the scoped bean is injected must reference the bean through one of its interfaces. The + following example shows a proxy based on an interface: + + + + + +   + + + + +   + + + + + For more detailed information about choosing class-based or interface-based proxying, see + Proxying Mechanisms. + + Custom Scopes + + The bean scoping mechanism is extensible. You can define your own scopes or even redefine + existing scopes, although the latter is considered bad practice and you cannot override the built-in + singleton and prototype scopes. + + + + Creating a Custom Scope + + To integrate your custom scopes into the Spring container, you need to implement the + org.springframework.beans.factory.config.Scope interface, which is described in this section. For an + idea of how to implement your own scopes, see the Scope implementations that are supplied with + the Spring Framework itself and the Scope javadoc, which explains the methods you need to + implement in more detail. + + + The Scope interface has four methods to get objects from the scope, remove them from the scope, + and let them be destroyed. + + + The session scope implementation, for example, returns the session-scoped bean (if it does not + exist, the method returns a new instance of the bean, after having bound it to the session for future + reference). The following method returns the object from the underlying scope: + + + Java + + + Object get(String name, ObjectFactory objectFactory) + + + + Kotlin + + + fun get(name: String, objectFactory: ObjectFactory<*>): Any + + + + The session scope implementation, for example, removes the session-scoped bean from the + underlying session. The object should be returned, but you can return null if the object with the + specified name is not found. The following method removes the object from the underlying scope: + + + Java + + + Object remove(String name) + + + + Kotlin + + + fun remove(name: String): Any + + + + The following method registers a callback that the scope should invoke when it is destroyed or + when the specified object in the scope is destroyed: + + + Java + + + void registerDestructionCallback(String name, Runnable destructionCallback) + + Kotlin + + + fun registerDestructionCallback(name: String, destructionCallback: Runnable) + + + + See the javadoc or a Spring scope implementation for more information on destruction callbacks. + + The following method obtains the conversation identifier for the underlying scope: + + + Java + + + String getConversationId() + + + + Kotlin + + + fun getConversationId(): String + + + + This identifier is different for each scope. For a session scoped implementation, this identifier can + be the session identifier. + + + + Using a Custom Scope + + After you write and test one or more custom Scope implementations, you need to make the Spring + container aware of your new scopes. The following method is the central method to register a new + Scope with the Spring container: + + + Java + + + void registerScope(String scopeName, Scope scope); + + + + Kotlin + + + fun registerScope(scopeName: String, scope: Scope) + + + + This method is declared on the ConfigurableBeanFactory interface, which is available through the + BeanFactory property on most of the concrete ApplicationContext implementations that ship with + Spring. + + + The first argument to the registerScope(..) method is the unique name associated with a scope. + Examples of such names in the Spring container itself are singleton and prototype. The second + argument to the registerScope(..) method is an actual instance of the custom Scope + implementation that you wish to register and use. + + Suppose that you write your custom Scope implementation, and then register it as shown in the + next example. + + The next example uses SimpleThreadScope, which is included with Spring but is not +  registered by default. The instructions would be the same for your own custom + Scope implementations. + + + Java + + + Scope threadScope = new SimpleThreadScope(); + beanFactory.registerScope("thread", threadScope); + + + + Kotlin + + + val threadScope = SimpleThreadScope() + beanFactory.registerScope("thread", threadScope) + + + + You can then create bean definitions that adhere to the scoping rules of your custom Scope, as + follows: + + + + + + + + With a custom Scope implementation, you are not limited to programmatic registration of the scope. + You can also do the Scope registration declaratively, by using the CustomScopeConfigurer class, as the + following example shows: + + + + + +   +   +   +   +   +   +   +   +   + + +   +   +   +   + + +   +   +   + + + + + + + + When you place within a declaration for a FactoryBean +  implementation, it is the factory bean itself that is scoped, not the object returned + from getObject(). + + + 2.1.6. Customizing the Nature of a Bean + + The Spring Framework provides a number of interfaces you can use to customize the nature of a + bean. This section groups them as follows: + + • Lifecycle Callbacks + + • ApplicationContextAware and BeanNameAware + + • Other Aware Interfaces + + + Lifecycle Callbacks + + To interact with the container’s management of the bean lifecycle, you can implement the Spring + InitializingBean and DisposableBean interfaces. The container calls afterPropertiesSet() for the + + former and destroy() for the latter to let the bean perform certain actions upon initialization and + destruction of your beans. + + + The JSR-250 @PostConstruct and @PreDestroy annotations are generally considered + best practice for receiving lifecycle callbacks in a modern Spring application. Using + these annotations means that your beans are not coupled to Spring-specific +  interfaces. For details, see Using @PostConstruct and @PreDestroy. + + + If you do not want to use the JSR-250 annotations but you still want to remove + coupling, consider init-method and destroy-method bean definition metadata. + + + Internally, the Spring Framework uses BeanPostProcessor implementations to process any callback + interfaces it can find and call the appropriate methods. If you need custom features or other + lifecycle behavior Spring does not by default offer, you can implement a BeanPostProcessor yourself. + For more information, see Container Extension Points. + + + In addition to the initialization and destruction callbacks, Spring-managed objects may also + implement the Lifecycle interface so that those objects can participate in the startup and shutdown + process, as driven by the container’s own lifecycle. + + + The lifecycle callback interfaces are described in this section. + + + + Initialization Callbacks + + The org.springframework.beans.factory.InitializingBean interface lets a bean perform + initialization work after the container has set all necessary properties on the bean. The + InitializingBean interface specifies a single method: + + + + void afterPropertiesSet() throws Exception; + + + + We recommend that you do not use the InitializingBean interface, because it unnecessarily couples + the code to Spring. Alternatively, we suggest using the @PostConstruct annotation or specifying a + POJO initialization method. In the case of XML-based configuration metadata, you can use the init- + method attribute to specify the name of the method that has a void no-argument signature. With + Java configuration, you can use the initMethod attribute of @Bean. See Receiving Lifecycle Callbacks. + Consider the following example: + + + + + + + + Java + + + public class ExampleBean { + + +   public void init() { +   // do some initialization work +   } + } + + Kotlin + + + class ExampleBean { + + +   fun init() { +   // do some initialization work +   } + } + + + + The preceding example has almost exactly the same effect as the following example (which consists + of two listings): + + + + + + + + Java + + + public class AnotherExampleBean implements InitializingBean { + + +   @Override +   public void afterPropertiesSet() { +   // do some initialization work +   } + } + + + + Kotlin + + + class AnotherExampleBean : InitializingBean { + + +   override fun afterPropertiesSet() { +   // do some initialization work +   } + } + + + + However, the first of the two preceding examples does not couple the code to Spring. + + + + Destruction Callbacks + + Implementing the org.springframework.beans.factory.DisposableBean interface lets a bean get a + callback when the container that contains it is destroyed. The DisposableBean interface specifies a + single method: + + + + void destroy() throws Exception; + + + + We recommend that you do not use the DisposableBean callback interface, because it unnecessarily + couples the code to Spring. Alternatively, we suggest using the @PreDestroy annotation or specifying + a generic method that is supported by bean definitions. With XML-based configuration metadata, + you can use the destroy-method attribute on the . With Java configuration, you can use the + + destroyMethod attribute of @Bean. See Receiving Lifecycle Callbacks. Consider the following + definition: + + + + + + + + Java + + + public class ExampleBean { + + +   public void cleanup() { +   // do some destruction work (like releasing pooled connections) +   } + } + + + + Kotlin + + + class ExampleBean { + + +   fun cleanup() { +   // do some destruction work (like releasing pooled connections) +   } + } + + + + The preceding definition has almost exactly the same effect as the following definition: + + + + + + + + Java + + + public class AnotherExampleBean implements DisposableBean { + + +   @Override +   public void destroy() { +   // do some destruction work (like releasing pooled connections) +   } + } + + + + Kotlin + + + class AnotherExampleBean : DisposableBean { + + +   override fun destroy() { +   // do some destruction work (like releasing pooled connections) +   } + } + + However, the first of the two preceding definitions does not couple the code to Spring. + + + You can assign the destroy-method attribute of a element a special (inferred) + value, which instructs Spring to automatically detect a public close or shutdown + method on the specific bean class. (Any class that implements + java.lang.AutoCloseable or java.io.Closeable would therefore match.) You can +  also set this special (inferred) value on the default-destroy-method attribute of a + element to apply this behavior to an entire set of beans (see Default + Initialization and Destroy Methods). Note that this is the default behavior with Java + configuration. + + + + Default Initialization and Destroy Methods + + When you write initialization and destroy method callbacks that do not use the Spring-specific + InitializingBean and DisposableBean callback interfaces, you typically write methods with names + such as init(), initialize(), dispose(), and so on. Ideally, the names of such lifecycle callback + methods are standardized across a project so that all developers use the same method names and + ensure consistency. + + + You can configure the Spring container to “look” for named initialization and destroy callback + method names on every bean. This means that you, as an application developer, can write your + application classes and use an initialization callback called init(), without having to configure an + init-method="init" attribute with each bean definition. The Spring IoC container calls that method + when the bean is created (and in accordance with the standard lifecycle callback contract described + previously). This feature also enforces a consistent naming convention for initialization and destroy + method callbacks. + + Suppose that your initialization callback methods are named init() and your destroy callback + methods are named destroy(). Your class then resembles the class in the following example: + + + Java + + + public class DefaultBlogService implements BlogService { + + +   private BlogDao blogDao; + + +   public void setBlogDao(BlogDao blogDao) { +   this.blogDao = blogDao; +   } + + +   // this is (unsurprisingly) the initialization callback method +   public void init() { +   if (this.blogDao == null) { +   throw new IllegalStateException("The [blogDao] property must be set."); +   } +   } + } + + Kotlin + + + class DefaultBlogService : BlogService { + + +   private var blogDao: BlogDao? = null + + +   // this is (unsurprisingly) the initialization callback method +   fun init() { +   if (blogDao == null) { +   throw IllegalStateException("The [blogDao] property must be set.") +   } +   } + } + + + + You could then use that class in a bean resembling the following: + + + + + + +   +   +   + + + + + + + The presence of the default-init-method attribute on the top-level element attribute causes + the Spring IoC container to recognize a method called init on the bean class as the initialization + method callback. When a bean is created and assembled, if the bean class has such a method, it is + invoked at the appropriate time. + + + You can configure destroy method callbacks similarly (in XML, that is) by using the default- + destroy-method attribute on the top-level element. + + + Where existing bean classes already have callback methods that are named at variance with the + convention, you can override the default by specifying (in XML, that is) the method name by using + the init-method and destroy-method attributes of the itself. + + + The Spring container guarantees that a configured initialization callback is called immediately after + a bean is supplied with all dependencies. Thus, the initialization callback is called on the raw bean + reference, which means that AOP interceptors and so forth are not yet applied to the bean. A target + bean is fully created first and then an AOP proxy (for example) with its interceptor chain is applied. + If the target bean and the proxy are defined separately, your code can even interact with the raw + target bean, bypassing the proxy. Hence, it would be inconsistent to apply the interceptors to the + init method, because doing so would couple the lifecycle of the target bean to its proxy or + interceptors and leave strange semantics when your code interacts directly with the raw target + bean. \ No newline at end of file diff --git a/models/spring-ai-openai-official/src/test/script/deploy-azure-openai-models.sh b/models/spring-ai-openai-official/src/test/script/deploy-azure-openai-models.sh new file mode 100755 index 00000000000..1735f50fe95 --- /dev/null +++ b/models/spring-ai-openai-official/src/test/script/deploy-azure-openai-models.sh @@ -0,0 +1,93 @@ +#!/usr/bin/env bash + +# Execute this script to deploy the needed Azure OpenAI models to execute the integration tests. +# +# For this, you need to have Azure CLI installed: https://learn.microsoft.com/cli/azure/install-azure-cli +# +# Azure CLI runs on: +# - Windows (using Windows Command Prompt (CMD), PowerShell, or Windows Subsystem for Linux (WSL)): https://learn.microsoft.com/cli/azure/install-azure-cli-windows +# - macOS: https://learn.microsoft.com/cli/azure/install-azure-cli-macos +# - Linux: https://learn.microsoft.com/cli/azure/install-azure-cli-linux +# - Docker: https://learn.microsoft.com/cli/azure/run-azure-cli-docker +# +# Once installed, you can run the following commands to check your installation is correct: +# az --version +# az --help + +echo "Setting up environment variables..." +echo "----------------------------------" +PROJECT="spring-ai-open-ai-official-$RANDOM-$RANDOM-$RANDOM" +RESOURCE_GROUP="rg-$PROJECT" +LOCATION="swedencentral" +AI_SERVICE="ai-$PROJECT" +TAG="$PROJECT" + +echo "Creating the resource group..." +echo "------------------------------" +az group create \ + --name "$RESOURCE_GROUP" \ + --location "$LOCATION" \ + --tags system="$TAG" + +# If you want to know the available SKUs, run the following Azure CLI command: +# az cognitiveservices account list-skus --location "$LOCATION" -o table + +echo "Creating the Cognitive Service..." +echo "---------------------------------" +az cognitiveservices account create \ + --name "$AI_SERVICE" \ + --resource-group "$RESOURCE_GROUP" \ + --location "$LOCATION" \ + --custom-domain "$AI_SERVICE" \ + --tags system="$TAG" \ + --kind "OpenAI" \ + --sku "S0" + +# If you want to know the available models, run the following Azure CLI command: +# az cognitiveservices account list-models --resource-group "$RESOURCE_GROUP" --name "$AI_SERVICE" -o table + +echo "Deploying Embedding Models" +echo "==========================" + +models=("text-embedding-ada-002" "text-embedding-3-small" "text-embedding-3-large") +versions=("2" "1" "1") +skus=("Standard" "Standard" "Standard") + +for i in "${!models[@]}"; do + model="${models[$i]}" + sku="${skus[$i]}" + version="${versions[$i]}" + echo "Deploying $model..." + az cognitiveservices account deployment create \ + --name "$AI_SERVICE" \ + --resource-group "$RESOURCE_GROUP" \ + --deployment-name "$model" \ + --model-name "$model" \ + --model-version "$version"\ + --model-format "OpenAI" \ + --sku-capacity 1 \ + --sku-name "$sku" || echo "Failed to deploy $model. Check SKU and region compatibility." +done + +echo "Storing the key and endpoint in environment variables..." +echo "--------------------------------------------------------" +AZURE_OPENAI_KEY=$( + az cognitiveservices account keys list \ + --name "$AI_SERVICE" \ + --resource-group "$RESOURCE_GROUP" \ + | jq -r .key1 + ) +AZURE_OPENAI_ENDPOINT=$( + az cognitiveservices account show \ + --name "$AI_SERVICE" \ + --resource-group "$RESOURCE_GROUP" \ + | jq -r .properties.endpoint + ) + +echo "AZURE_OPENAI_KEY=$AZURE_OPENAI_KEY" +echo "AZURE_OPENAI_ENDPOINT=$AZURE_OPENAI_ENDPOINT" + +# Once you finish the tests, you can delete the resource group with the following command: +#echo "Deleting the resource group..." +#echo "------------------------------" +#az group delete --name "$RESOURCE_GROUP" --yes diff --git a/pom.xml b/pom.xml index 9695f90f231..9b05fdc5a55 100644 --- a/pom.xml +++ b/pom.xml @@ -180,6 +180,7 @@ models/spring-ai-oci-genai models/spring-ai-ollama models/spring-ai-openai + models/spring-ai-openai-official models/spring-ai-postgresml models/spring-ai-stability-ai models/spring-ai-transformers @@ -277,6 +278,7 @@ 4.0.0 4.3.4 1.0.0-beta.16 + 4.5.0 1.1.0 2.2.21 @@ -836,6 +838,7 @@ org.springframework.ai.mistralai/**/*IT.java org.springframework.ai.oci/**/*IT.java org.springframework.ai.ollama/**/*IT.java + org.springframework.ai.openaiofficial/**/*IT.java org.springframework.ai.postgresml/**/*IT.java org.springframework.ai.stabilityai/**/*IT.java org.springframework.ai.transformers/**/*IT.java From f0278fafbbe45ae8fa25db4e2e2527efaa308791 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Wed, 22 Oct 2025 18:17:30 +0200 Subject: [PATCH 02/49] Implementation of the OpenAI Java SDK - Add support for ImageModel - Refactor the existing code now that we have support for 2 models Signed-off-by: Julien Dubois --- .gitignore | 2 +- models/spring-ai-openai-official/pom.xml | 2 +- .../OpenAiOfficialEmbeddingModel.java | 37 +- .../OpenAiOfficialEmbeddingOptions.java | 38 +- .../OpenAiOfficialImageModel.java | 145 +++++++ .../OpenAiOfficialImageOptions.java | 366 ++++++++++++++++++ ...OpenAiOfficialImageGenerationMetadata.java | 67 ++++ .../OpenAiOfficialImageResponseMetadata.java | 69 ++++ .../OpenAiOfficialTestConfiguration.java | 26 +- ...ialTestConfigurationWithObservability.java | 60 +++ ...IT.java => OpenAiOfficialEmbeddingIT.java} | 24 +- ...AiOfficialEmbeddingModelObservationIT.java | 101 +++++ .../image/OpenAiOfficialImageModelIT.java | 85 ++++ ...OpenAiOfficialImageModelObservationIT.java | 96 +++++ .../test/script/deploy-azure-openai-models.sh | 33 +- .../observation/conventions/AiProvider.java | 5 + 16 files changed, 1103 insertions(+), 53 deletions(-) create mode 100644 models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageModel.java create mode 100644 models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageOptions.java create mode 100644 models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageGenerationMetadata.java create mode 100644 models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageResponseMetadata.java create mode 100644 models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java rename models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/{EmbeddingIT.java => OpenAiOfficialEmbeddingIT.java} (88%) create mode 100644 models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/OpenAiOfficialEmbeddingModelObservationIT.java create mode 100644 models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelIT.java create mode 100644 models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java diff --git a/.gitignore b/.gitignore index 4e9567af1aa..05954172d63 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ target .classpath .project .settings +.env bin build.log integration-repo @@ -55,4 +56,3 @@ tmp plans - diff --git a/models/spring-ai-openai-official/pom.xml b/models/spring-ai-openai-official/pom.xml index 4f041f58b53..8cb93891c14 100644 --- a/models/spring-ai-openai-official/pom.xml +++ b/models/spring-ai-openai-official/pom.xml @@ -1,6 +1,6 @@ org.springframework diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java new file mode 100644 index 00000000000..249197cfba7 --- /dev/null +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java @@ -0,0 +1,198 @@ +package org.springframework.ai.openaiofficial; + +import com.openai.azure.AzureOpenAIServiceVersion; +import com.openai.credential.Credential; + +import java.net.Proxy; +import java.time.Duration; +import java.util.Map; + +public class AbstractOpenAiOfficialOptions { + + /** + * The deployment URL to connect to OpenAI. + */ + private String baseUrl; + + /** + * The API key to connect to OpenAI. + */ + private String apiKey; + + /** + * Credentials used to connect to Azure OpenAI. + */ + private Credential credential; + + /** + * The model name used. When using Azure AI Foundry, this is also used as the default + * deployment name. + */ + private String model; + + /** + * The deployment name as defined in Azure AI Foundry. On Azure AI Foundry, the + * default deployment name is the same as the model name. When using OpenAI directly, + * this value isn't used. + */ + private String azureDeploymentName; + + /** + * The Azure OpenAI Service version to use when connecting to Azure AI Foundry. + */ + private AzureOpenAIServiceVersion azureOpenAIServiceVersion; + + /** + * The organization ID to use when connecting to Azure OpenAI. + */ + private String organizationId; + + /** + * Whether Azure OpenAI is detected. + */ + private boolean isAzure; + + /** + * Whether GitHub Models is detected. + */ + private boolean isGitHubModels; + + /** + * Request timeout for OpenAI client. + */ + private Duration timeout; + + /** + * Maximum number of retries for OpenAI client. + */ + private Integer maxRetries; + + /** + * Proxy settings for OpenAI client. + */ + private Proxy proxy; + + /** + * Custom headers to add to OpenAI client requests. + */ + private Map customHeaders; + + public String getBaseUrl() { + return baseUrl; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + + public String getApiKey() { + return apiKey; + } + + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + } + + public Credential getCredential() { + return credential; + } + + public void setCredential(Credential credential) { + this.credential = credential; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getAzureDeploymentName() { + return azureDeploymentName; + } + + public void setAzureDeploymentName(String azureDeploymentName) { + this.azureDeploymentName = azureDeploymentName; + } + + /** + * Alias for getAzureDeploymentName() + */ + public String getDeploymentName() { + return azureDeploymentName; + } + + /** + * Alias for setAzureDeploymentName() + */ + public void setDeploymentName(String azureDeploymentName) { + this.azureDeploymentName = azureDeploymentName; + } + + public AzureOpenAIServiceVersion getAzureOpenAIServiceVersion() { + return azureOpenAIServiceVersion; + } + + public void setAzureOpenAIServiceVersion(AzureOpenAIServiceVersion azureOpenAIServiceVersion) { + this.azureOpenAIServiceVersion = azureOpenAIServiceVersion; + } + + public String getOrganizationId() { + return organizationId; + } + + public void setOrganizationId(String organizationId) { + this.organizationId = organizationId; + } + + public boolean isAzure() { + return isAzure; + } + + public void setAzure(boolean azure) { + isAzure = azure; + } + + public boolean isGitHubModels() { + return isGitHubModels; + } + + public void setGitHubModels(boolean gitHubModels) { + isGitHubModels = gitHubModels; + } + + public Duration getTimeout() { + return timeout; + } + + public void setTimeout(Duration timeout) { + this.timeout = timeout; + } + + public Integer getMaxRetries() { + return maxRetries; + } + + public void setMaxRetries(Integer maxRetries) { + this.maxRetries = maxRetries; + } + + public Proxy getProxy() { + return proxy; + } + + public void setProxy(Proxy proxy) { + this.proxy = proxy; + } + + public Map getCustomHeaders() { + return customHeaders; + } + + public void setCustomHeaders(Map customHeaders) { + this.customHeaders = customHeaders; + } + +} diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java index 64238f9eeed..cb5f54b3592 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java @@ -1,3 +1,19 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.ai.openaiofficial; import com.openai.client.OpenAIClient; @@ -27,6 +43,8 @@ import java.util.List; import java.util.Objects; +import static org.springframework.ai.openaiofficial.setup.OpenAiOfficialSetup.setupSyncClient; + /** * Embedding Model implementation using the OpenAI Java SDK. * @@ -42,7 +60,7 @@ public class OpenAiOfficialEmbeddingModel extends AbstractEmbeddingModel { private final OpenAIClient openAiClient; - private final OpenAiOfficialEmbeddingOptions defaultOptions; + private final OpenAiOfficialEmbeddingOptions options; private final MetadataMode metadataMode; @@ -50,31 +68,58 @@ public class OpenAiOfficialEmbeddingModel extends AbstractEmbeddingModel { private EmbeddingModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; + public OpenAiOfficialEmbeddingModel() { + this(null, null, null, null); + } + + public OpenAiOfficialEmbeddingModel(OpenAiOfficialEmbeddingOptions options) { + this(null, null, options, null); + } + + public OpenAiOfficialEmbeddingModel(MetadataMode metadataMode, OpenAiOfficialEmbeddingOptions options) { + this(null, metadataMode, options, null); + } + + public OpenAiOfficialEmbeddingModel(OpenAiOfficialEmbeddingOptions options, + ObservationRegistry observationRegistry) { + this(null, null, options, observationRegistry); + } + + public OpenAiOfficialEmbeddingModel(MetadataMode metadataMode, OpenAiOfficialEmbeddingOptions options, + ObservationRegistry observationRegistry) { + this(null, metadataMode, options, observationRegistry); + } + public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient) { - this(openAiClient, MetadataMode.EMBED); + this(openAiClient, null, null, null); } public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode) { - this(openAiClient, metadataMode, OpenAiOfficialEmbeddingOptions.builder().model(DEFAULT_MODEL_NAME).build()); + this(openAiClient, metadataMode, null, null); } public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, OpenAiOfficialEmbeddingOptions options) { - this(openAiClient, metadataMode, options, ObservationRegistry.NOOP); + this(openAiClient, metadataMode, options, null); } public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, OpenAiOfficialEmbeddingOptions options, ObservationRegistry observationRegistry) { - Assert.notNull(openAiClient, "com.openai.client.OpenAIClient must not be null"); - Assert.notNull(metadataMode, "Metadata mode must not be null"); - Assert.notNull(options, "Options must not be null"); - Assert.notNull(options.getModel(), "Model name must not be null"); - Assert.notNull(observationRegistry, "Observation registry must not be null"); - this.openAiClient = openAiClient; - this.metadataMode = metadataMode; - this.defaultOptions = options; - this.observationRegistry = observationRegistry; + if (options == null) { + this.options = OpenAiOfficialEmbeddingOptions.builder().model(DEFAULT_MODEL_NAME).build(); + } + else { + this.options = options; + } + this.openAiClient = Objects.requireNonNullElseGet(openAiClient, + () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), + this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), + this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), + this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), + this.options.getProxy(), this.options.getCustomHeaders())); + this.metadataMode = Objects.requireNonNullElse(metadataMode, MetadataMode.EMBED); + this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); } @Override @@ -91,7 +136,7 @@ public float[] embed(Document document) { @Override public EmbeddingResponse call(EmbeddingRequest embeddingRequest) { OpenAiOfficialEmbeddingOptions options = OpenAiOfficialEmbeddingOptions.builder() - .from(this.defaultOptions) + .from(this.options) .merge(embeddingRequest.getOptions()) .build(); @@ -150,8 +195,8 @@ private List generateEmbeddingList(List setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), + this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), + this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), + this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), + this.options.getProxy(), this.options.getCustomHeaders())); + this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); } - public OpenAiOfficialImageOptions getDefaultOptions() { - return this.defaultOptions; + public OpenAiOfficialImageOptions getOptions() { + return this.options; } @Override public ImageResponse call(ImagePrompt imagePrompt) { OpenAiOfficialImageOptions options = OpenAiOfficialImageOptions.builder() - .from(this.defaultOptions) + .from(this.options) .merge(imagePrompt.getOptions()) .build(); diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageOptions.java index da65ae2ca0e..651de12cca0 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageOptions.java @@ -27,7 +27,7 @@ * * @author Julien Dubois */ -public class OpenAiOfficialImageOptions implements ImageOptions { +public class OpenAiOfficialImageOptions extends AbstractOpenAiOfficialOptions implements ImageOptions { public static final String DEFAULT_IMAGE_MODEL = ImageModel.DALL_E_3.toString(); @@ -37,19 +37,6 @@ public class OpenAiOfficialImageOptions implements ImageOptions { */ private Integer n; - /** - * The model name used. When using Azure AI Foundry, this is also used as the default - * deployment name. By default, dall-e-3. - */ - private String model = ImageModel.DALL_E_3.toString(); - - /** - * The deployment name as defined in Azure AI Foundry. On Azure AI Foundry, the - * default deployment name is the same as the model name. When using OpenAI directly, - * this value isn't used. - */ - private String deploymentName; - /** * The width of the generated images. Must be one of 256, 512, or 1024 for dall-e-2. */ @@ -106,15 +93,6 @@ public void setN(Integer n) { this.n = n; } - @Override - public String getModel() { - return this.model; - } - - public void setModel(String model) { - this.model = model; - } - @Override public Integer getWidth() { return this.width; @@ -180,41 +158,27 @@ public void setStyle(String style) { this.style = style; } - public String getDeploymentName() { - return this.deploymentName; - } - - public void setDeploymentName(String deploymentName) { - this.deploymentName = deploymentName; - } - @Override public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof OpenAiOfficialImageOptions that)) { + if (o == null || getClass() != o.getClass()) return false; - } - return Objects.equals(this.n, that.n) && Objects.equals(this.model, that.model) - && Objects.equals(this.deploymentName, that.deploymentName) && Objects.equals(this.width, that.width) - && Objects.equals(this.height, that.height) && Objects.equals(this.quality, that.quality) - && Objects.equals(this.responseFormat, that.responseFormat) && Objects.equals(this.size, that.size) - && Objects.equals(this.style, that.style) && Objects.equals(this.user, that.user); + OpenAiOfficialImageOptions that = (OpenAiOfficialImageOptions) o; + return Objects.equals(n, that.n) && Objects.equals(width, that.width) && Objects.equals(height, that.height) + && Objects.equals(quality, that.quality) && Objects.equals(responseFormat, that.responseFormat) + && Objects.equals(size, that.size) && Objects.equals(style, that.style) + && Objects.equals(user, that.user); } @Override public int hashCode() { - return Objects.hash(this.n, this.model, this.deploymentName, this.width, this.height, this.quality, - this.responseFormat, this.size, this.style, this.user); + return Objects.hash(n, width, height, quality, responseFormat, size, style, user); } @Override public String toString() { - return "OpenAiOfficialImageOptions{" + "n=" + n + ", model='" + model + '\'' + ", deploymentName='" - + deploymentName + '\'' + ", width=" + width + ", height=" + height + ", quality='" + quality + '\'' - + ", responseFormat='" + responseFormat + '\'' + ", size='" + size + '\'' + ", style='" + style + '\'' - + ", user='" + user + '\'' + '}'; + return "OpenAiOfficialImageOptions{" + "n=" + n + ", width=" + width + ", height=" + height + ", quality='" + + quality + '\'' + ", responseFormat='" + responseFormat + '\'' + ", size='" + size + '\'' + ", style='" + + style + '\'' + ", user='" + user + '\'' + '}'; } public ImageGenerateParams toOpenAiImageGenerateParams(ImagePrompt imagePrompt) { @@ -355,9 +319,6 @@ public Builder style(String style) { } public OpenAiOfficialImageOptions build() { - if (this.options.deploymentName == null) { - this.options.deploymentName = this.options.model; - } return this.options; } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java new file mode 100644 index 00000000000..3215ffbb937 --- /dev/null +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java @@ -0,0 +1,39 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaiofficial.setup; + +import com.azure.identity.AuthenticationUtil; +import com.azure.identity.DefaultAzureCredentialBuilder; +import com.openai.credential.BearerTokenCredential; +import com.openai.credential.Credential; + +/** + * Helps configure the OpenAI Java SDK, depending on the platform used. This code is + * inspired by LangChain4j's + * `dev.langchain4j.model.openaiofficial.AzureInternalOpenAiOfficialHelper` class, which + * is coded by the same author (Julien Dubois, from Microsoft). + * + * @author Julien Dubois + */ +class AzureInternalOpenAiOfficialHelper { + + static Credential getAzureCredential() { + return BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier( + new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default")); + } + +} diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java new file mode 100644 index 00000000000..e0be64381e2 --- /dev/null +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java @@ -0,0 +1,223 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaiofficial.setup; + +import com.openai.azure.AzureOpenAIServiceVersion; +import com.openai.azure.credential.AzureApiKeyCredential; +import com.openai.client.OpenAIClient; +import com.openai.client.okhttp.OpenAIOkHttpClient; +import com.openai.credential.Credential; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.Proxy; +import java.time.Duration; +import java.util.Collections; +import java.util.Map; +import java.util.stream.Collectors; + +import static java.time.Duration.ofSeconds; + +/** + * Helps configure the OpenAI Java SDK, depending on the platform used. This code is + * inspired by LangChain4j's + * `dev.langchain4j.model.openaiofficial.InternalOpenAiOfficialHelper` class, which is + * coded by the same author (Julien Dubois, from Microsoft). + * + * @author Julien Dubois + */ +public class OpenAiOfficialSetup { + + static final String OPENAI_URL = "https://api.openai.com/v1"; + static final String GITHUB_MODELS_URL = "https://models.inference.ai.azure.com"; + static final String GITHUB_TOKEN = "GITHUB_TOKEN"; + static final String DEFAULT_USER_AGENT = "spring-ai-openai-official"; + + private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialSetup.class); + + private static final Duration DEFAULT_DURATION = ofSeconds(60); + + private static final int DEFAULT_MAX_RETRIES = 3; + + public enum ModelHost { + + OPENAI, AZURE_OPENAI, GITHUB_MODELS + + } + + public static OpenAIClient setupSyncClient(String baseUrl, String apiKey, Credential credential, + String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAiServiceVersion, String organizationId, + boolean isAzure, boolean isGitHubModels, String modelName, Duration timeout, Integer maxRetries, + Proxy proxy, Map customHeaders) { + + if (apiKey == null && credential == null) { + var openAiKey = System.getenv("OPENAI_API_KEY"); + if (openAiKey != null) { + apiKey = openAiKey; + logger.debug("OpenAI API Key detected from environment variable OPENAI_API_KEY."); + } + var azureOpenAiKey = System.getenv("AZURE_OPENAI_KEY"); + if (azureOpenAiKey != null) { + apiKey = azureOpenAiKey; + logger.debug("Azure OpenAI Key detected from environment variable AZURE_OPENAI_KEY."); + } + } + if (baseUrl == null) { + var openAiBaseUrl = System.getenv("OPENAI_BASE_URL"); + if (openAiBaseUrl != null) { + baseUrl = openAiBaseUrl; + logger.debug("OpenAI Base URL detected from environment variable OPENAI_BASE_URL."); + } + var azureOpenAiBaseUrl = System.getenv("AZURE_OPENAI_BASE_URL"); + if (azureOpenAiBaseUrl != null) { + baseUrl = azureOpenAiBaseUrl; + logger.debug("Azure OpenAI Base URL detected from environment variable AZURE_OPENAI_BASE_URL."); + } + } + + ModelHost modelHost = detectModelHost(isAzure, isGitHubModels, baseUrl, azureDeploymentName, + azureOpenAiServiceVersion); + if (timeout == null) { + timeout = DEFAULT_DURATION; + } + if (maxRetries == null) { + maxRetries = DEFAULT_MAX_RETRIES; + } + + OpenAIOkHttpClient.Builder builder = OpenAIOkHttpClient.builder(); + builder + .baseUrl(calculateBaseUrl(baseUrl, modelHost, modelName, azureDeploymentName, azureOpenAiServiceVersion)); + + Credential calculatedCredential = calculateCredential(modelHost, apiKey, credential); + String calculatedApiKey = calculateApiKey(modelHost, apiKey); + if (calculatedCredential == null && calculatedApiKey == null) { + throw new IllegalArgumentException("Either apiKey or credential must be set to authenticate"); + } + else if (calculatedCredential != null) { + builder.credential(calculatedCredential); + } + else { + builder.apiKey(calculatedApiKey); + } + builder.organization(organizationId); + + if (azureOpenAiServiceVersion != null) { + builder.azureServiceVersion(azureOpenAiServiceVersion); + } + + if (proxy != null) { + builder.proxy(proxy); + } + + builder.putHeader("User-Agent", DEFAULT_USER_AGENT); + if (customHeaders != null) { + builder.putAllHeaders(customHeaders.entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> Collections.singletonList(entry.getValue())))); + } + + builder.timeout(timeout); + builder.maxRetries(maxRetries); + return builder.build(); + } + + static ModelHost detectModelHost(boolean isAzure, boolean isGitHubModels, String baseUrl, + String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAIServiceVersion) { + if (isAzure) { + return ModelHost.AZURE_OPENAI; // Forced by the user + } + if (isGitHubModels) { + return ModelHost.GITHUB_MODELS; // Forced by the user + } + if (baseUrl != null) { + if (baseUrl.endsWith("openai.azure.com") || baseUrl.endsWith("openai.azure.com/") + || baseUrl.endsWith("cognitiveservices.azure.com") + || baseUrl.endsWith("cognitiveservices.azure.com/")) { + return ModelHost.AZURE_OPENAI; + } + else if (baseUrl.startsWith(GITHUB_MODELS_URL)) { + return ModelHost.GITHUB_MODELS; + } + } + if (azureDeploymentName != null || azureOpenAIServiceVersion != null) { + return ModelHost.AZURE_OPENAI; + } + return ModelHost.OPENAI; + } + + static String calculateBaseUrl(final String baseUrl, ModelHost modelHost, String modelName, + String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAiServiceVersion) { + if (modelHost == ModelHost.OPENAI) { + if (baseUrl == null || baseUrl.isBlank()) { + return OPENAI_URL; + } + return baseUrl; + } + else if (modelHost == ModelHost.GITHUB_MODELS) { + return GITHUB_MODELS_URL; + } + else if (modelHost == ModelHost.AZURE_OPENAI) { + // Using Azure OpenAI + String tmpUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl; + // If the Azure deployment name is not configured, the model name will be used + // by default by the OpenAI Java + // SDK + if (azureDeploymentName != null && !azureDeploymentName.equals(modelName)) { + tmpUrl += "/openai/deployments/" + azureDeploymentName; + } + if (azureOpenAiServiceVersion != null) { + tmpUrl += "?api-version=" + azureOpenAiServiceVersion.value(); + } + return tmpUrl; + } + else { + throw new IllegalArgumentException("Unknown model host: " + modelHost); + } + } + + static Credential calculateCredential(ModelHost modelHost, String apiKey, Credential credential) { + if (apiKey != null) { + if (modelHost == ModelHost.AZURE_OPENAI) { + return AzureApiKeyCredential.create(apiKey); + } + } + else if (credential != null) { + return credential; + } + else if (modelHost == ModelHost.AZURE_OPENAI) { + try { + return AzureInternalOpenAiOfficialHelper.getAzureCredential(); + } + catch (NoClassDefFoundError e) { + throw new IllegalArgumentException("Azure OpenAI was detected, but no credential was provided. " + + "If you want to use passwordless authentication, you need to add the Azure Identity library (groupId=`com.azure`, artifactId=`azure-identity`) to your classpath."); + } + } + return null; + } + + static String calculateApiKey(ModelHost modelHost, String apiKey) { + if (modelHost != ModelHost.AZURE_OPENAI && apiKey != null) { + return apiKey; + } + else if (modelHost == ModelHost.GITHUB_MODELS && System.getenv(GITHUB_TOKEN) != null) { + return System.getenv(GITHUB_TOKEN); + } + return null; + } + +} diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java index fe62fbb8127..7305f4d6903 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java @@ -16,10 +16,6 @@ package org.springframework.ai.openaiofficial; -import com.openai.client.OpenAIClient; -import com.openai.client.okhttp.OpenAIOkHttpClient; -import io.micrometer.observation.tck.TestObservationRegistry; -import org.springframework.ai.document.MetadataMode; import org.springframework.boot.SpringBootConfiguration; import org.springframework.context.annotation.Bean; @@ -32,18 +28,13 @@ public class OpenAiOfficialTestConfiguration { @Bean - public OpenAIClient openAIClient() { - return OpenAIOkHttpClient.fromEnv(); + public OpenAiOfficialEmbeddingModel openAiEmbeddingModel() { + return new OpenAiOfficialEmbeddingModel(); } @Bean - public OpenAiOfficialEmbeddingModel openAiEmbeddingModel(OpenAIClient client) { - return new OpenAiOfficialEmbeddingModel(client); - } - - @Bean - public OpenAiOfficialImageModel openAiImageModel(OpenAIClient client) { - return new OpenAiOfficialImageModel(client); + public OpenAiOfficialImageModel openAiImageModel() { + return new OpenAiOfficialImageModel(); } } diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java index 9670d30e740..53c6e1adf44 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java @@ -16,8 +16,6 @@ package org.springframework.ai.openaiofficial; -import com.openai.client.OpenAIClient; -import com.openai.client.okhttp.OpenAIOkHttpClient; import io.micrometer.observation.tck.TestObservationRegistry; import org.springframework.ai.document.MetadataMode; import org.springframework.boot.SpringBootConfiguration; @@ -40,21 +38,15 @@ public TestObservationRegistry testObservationRegistry() { } @Bean - public OpenAIClient openAIClient() { - return OpenAIOkHttpClient.fromEnv(); - } - - @Bean - public OpenAiOfficialEmbeddingModel openAiEmbeddingModel(OpenAIClient client, - TestObservationRegistry observationRegistry) { - return new OpenAiOfficialEmbeddingModel(client, MetadataMode.EMBED, + public OpenAiOfficialEmbeddingModel openAiEmbeddingModel(TestObservationRegistry observationRegistry) { + return new OpenAiOfficialEmbeddingModel(MetadataMode.EMBED, OpenAiOfficialEmbeddingOptions.builder().model(DEFAULT_EMBEDDING_MODEL).build(), observationRegistry); } @Bean - public OpenAiOfficialImageModel openAiImageModel(OpenAIClient client, TestObservationRegistry observationRegistry) { - return new OpenAiOfficialImageModel(client, - OpenAiOfficialImageOptions.builder().model(DEFAULT_IMAGE_MODEL).build(), observationRegistry); + public OpenAiOfficialImageModel openAiImageModel(TestObservationRegistry observationRegistry) { + return new OpenAiOfficialImageModel(OpenAiOfficialImageOptions.builder().model(DEFAULT_IMAGE_MODEL).build(), + observationRegistry); } } diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/OpenAiOfficialEmbeddingModelObservationIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/OpenAiOfficialEmbeddingModelObservationIT.java index fe946a5a651..a1a7e054b39 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/OpenAiOfficialEmbeddingModelObservationIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/OpenAiOfficialEmbeddingModelObservationIT.java @@ -29,7 +29,6 @@ import org.springframework.ai.observation.conventions.AiProvider; import org.springframework.ai.openaiofficial.OpenAiOfficialEmbeddingModel; import org.springframework.ai.openaiofficial.OpenAiOfficialEmbeddingOptions; -import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfiguration; import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfigurationWithObservability; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelIT.java index 4791e05b447..fe848319c2b 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelIT.java @@ -16,7 +16,6 @@ package org.springframework.ai.openaiofficial.image; -import com.openai.models.embeddings.EmbeddingModel; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java index 539a647f1e5..7da96496375 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java @@ -28,7 +28,6 @@ import org.springframework.ai.observation.conventions.AiProvider; import org.springframework.ai.openaiofficial.OpenAiOfficialImageModel; import org.springframework.ai.openaiofficial.OpenAiOfficialImageOptions; -import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfiguration; import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfigurationWithObservability; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetupTests.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetupTests.java new file mode 100644 index 00000000000..101539a068d --- /dev/null +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetupTests.java @@ -0,0 +1,86 @@ +package org.springframework.ai.openaiofficial.setup; + +import com.openai.client.OpenAIClient; +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.util.Collections; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class OpenAiOfficialSetupTests { + + @Test + void detectModelHost_returnsAzureOpenAI_whenAzureFlagIsTrue() { + OpenAiOfficialSetup.ModelHost result = OpenAiOfficialSetup.detectModelHost(true, false, null, null, null); + + assertEquals(OpenAiOfficialSetup.ModelHost.AZURE_OPENAI, result); + } + + @Test + void detectModelHost_returnsGitHubModels_whenGitHubFlagIsTrue() { + OpenAiOfficialSetup.ModelHost result = OpenAiOfficialSetup.detectModelHost(false, true, null, null, null); + + assertEquals(OpenAiOfficialSetup.ModelHost.GITHUB_MODELS, result); + } + + @Test + void detectModelHost_returnsAzureOpenAI_whenBaseUrlMatchesAzure() { + OpenAiOfficialSetup.ModelHost result = OpenAiOfficialSetup.detectModelHost(false, false, + "https://example.openai.azure.com", null, null); + + assertEquals(OpenAiOfficialSetup.ModelHost.AZURE_OPENAI, result); + } + + @Test + void detectModelHost_returnsGitHubModels_whenBaseUrlMatchesGitHub() { + OpenAiOfficialSetup.ModelHost result = OpenAiOfficialSetup.detectModelHost(false, false, + "https://models.inference.ai.azure.com", null, null); + + assertEquals(OpenAiOfficialSetup.ModelHost.GITHUB_MODELS, result); + } + + @Test + void detectModelHost_returnsOpenAI_whenNoConditionsMatch() { + OpenAiOfficialSetup.ModelHost result = OpenAiOfficialSetup.detectModelHost(false, false, null, null, null); + + assertEquals(OpenAiOfficialSetup.ModelHost.OPENAI, result); + } + + @Test + void setupSyncClient_returnsClient_whenValidApiKeyProvided() { + OpenAIClient client = OpenAiOfficialSetup.setupSyncClient(null, "valid-api-key", null, null, null, null, false, + false, null, Duration.ofSeconds(30), 2, null, null); + + assertNotNull(client); + } + + @Test + void setupSyncClient_appliesCustomHeaders_whenProvided() { + Map customHeaders = Collections.singletonMap("X-Custom-Header", "value"); + + OpenAIClient client = OpenAiOfficialSetup.setupSyncClient(null, "valid-api-key", null, null, null, null, false, + false, null, Duration.ofSeconds(30), 2, null, customHeaders); + + assertNotNull(client); + } + + @Test + void calculateBaseUrl_returnsDefaultOpenAIUrl_whenBaseUrlIsNull() { + String result = OpenAiOfficialSetup.calculateBaseUrl(null, OpenAiOfficialSetup.ModelHost.OPENAI, null, null, + null); + + assertEquals(OpenAiOfficialSetup.OPENAI_URL, result); + } + + @Test + void calculateBaseUrl_returnsGitHubUrl_whenModelHostIsGitHub() { + String result = OpenAiOfficialSetup.calculateBaseUrl(null, OpenAiOfficialSetup.ModelHost.GITHUB_MODELS, null, + null, null); + + assertEquals(OpenAiOfficialSetup.GITHUB_MODELS_URL, result); + } + +} diff --git a/pom.xml b/pom.xml index 9b05fdc5a55..05ef393ca05 100644 --- a/pom.xml +++ b/pom.xml @@ -278,7 +278,8 @@ 4.0.0 4.3.4 1.0.0-beta.16 - 4.5.0 + 4.6.1 + 1.15.4 1.1.0 2.2.21 From 2bc511d2ffb6b429d1464119232760c9d53ab9fa Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Thu, 23 Oct 2025 17:25:59 +0200 Subject: [PATCH 04/49] Implementation of the OpenAI Java SDK Create the OpenAiOfficialChatOptions class to prepare for the implementation of the Chat Model Signed-off-by: Julien Dubois --- .../AbstractOpenAiOfficialOptions.java | 16 + .../OpenAiOfficialChatOptions.java | 767 ++++++++++++++++++ 2 files changed, 783 insertions(+) create mode 100644 models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java index 249197cfba7..0044fd27645 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java @@ -1,3 +1,19 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.ai.openaiofficial; import com.openai.azure.AzureOpenAIServiceVersion; diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java new file mode 100644 index 00000000000..168cd9c9748 --- /dev/null +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java @@ -0,0 +1,767 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaiofficial; + +import com.openai.models.FunctionDefinition; +import com.openai.models.ResponseFormatJsonSchema; +import com.openai.models.chat.completions.ChatCompletionAudioParam; +import com.openai.models.chat.completions.ChatCompletionToolChoiceOption; +import com.openai.models.responses.ResponseCreateParams; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.ai.model.tool.ToolCallingChatOptions; +import org.springframework.ai.tool.ToolCallback; +import org.springframework.lang.Nullable; +import org.springframework.util.Assert; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +/** + * Configuration information for the Chat Model implementation using the OpenAI Java SDK. + * + * @author Julien Dubois + */ +public class OpenAiOfficialChatOptions extends AbstractOpenAiOfficialOptions implements ToolCallingChatOptions { + + private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatOptions.class); + + private Double frequencyPenalty; + + private Map logitBias; + + private Boolean logprobs; + + private Integer topLogprobs; + + private Integer maxTokens; + + private Integer maxCompletionTokens; + + private Integer n; + + private List outputModalities; + + private ChatCompletionAudioParam outputAudio; + + private Double presencePenalty; + + private ResponseFormatJsonSchema responseFormat; + + private ResponseCreateParams.StreamOptions streamOptions; + + private Integer seed; + + private List stop; + + private Double temperature; + + private Double topP; + + private List tools; + + private ChatCompletionToolChoiceOption toolChoice; + + private String user; + + private Boolean parallelToolCalls; + + private Boolean store; + + private Map metadata; + + private String reasoningEffort; + + private String verbosity; + + private String serviceTier; + + private List toolCallbacks = new ArrayList<>(); + + private Set toolNames = new HashSet<>(); + + private Boolean internalToolExecutionEnabled; + + private Map httpHeaders = new HashMap<>(); + + private Map toolContext = new HashMap<>(); + + @Override + public Double getFrequencyPenalty() { + return this.frequencyPenalty; + } + + public void setFrequencyPenalty(Double frequencyPenalty) { + this.frequencyPenalty = frequencyPenalty; + } + + public Map getLogitBias() { + return this.logitBias; + } + + public void setLogitBias(Map logitBias) { + this.logitBias = logitBias; + } + + public Boolean getLogprobs() { + return this.logprobs; + } + + public void setLogprobs(Boolean logprobs) { + this.logprobs = logprobs; + } + + public Integer getTopLogprobs() { + return this.topLogprobs; + } + + public void setTopLogprobs(Integer topLogprobs) { + this.topLogprobs = topLogprobs; + } + + @Override + public Integer getMaxTokens() { + return this.maxTokens; + } + + public void setMaxTokens(Integer maxTokens) { + this.maxTokens = maxTokens; + } + + public Integer getMaxCompletionTokens() { + return this.maxCompletionTokens; + } + + public void setMaxCompletionTokens(Integer maxCompletionTokens) { + this.maxCompletionTokens = maxCompletionTokens; + } + + public Integer getN() { + return this.n; + } + + public void setN(Integer n) { + this.n = n; + } + + public List getOutputModalities() { + return this.outputModalities; + } + + public void setOutputModalities(List outputModalities) { + this.outputModalities = outputModalities; + } + + public ChatCompletionAudioParam getOutputAudio() { + return this.outputAudio; + } + + public void setOutputAudio(ChatCompletionAudioParam outputAudio) { + this.outputAudio = outputAudio; + } + + @Override + public Double getPresencePenalty() { + return this.presencePenalty; + } + + public void setPresencePenalty(Double presencePenalty) { + this.presencePenalty = presencePenalty; + } + + public ResponseFormatJsonSchema getResponseFormat() { + return this.responseFormat; + } + + public void setResponseFormat(ResponseFormatJsonSchema responseFormat) { + this.responseFormat = responseFormat; + } + + public ResponseCreateParams.StreamOptions getStreamOptions() { + return this.streamOptions; + } + + public void setStreamOptions(ResponseCreateParams.StreamOptions streamOptions) { + this.streamOptions = streamOptions; + } + + public Integer getSeed() { + return this.seed; + } + + public void setSeed(Integer seed) { + this.seed = seed; + } + + public List getStop() { + return this.stop; + } + + public void setStop(List stop) { + this.stop = stop; + } + + @Override + public List getStopSequences() { + return getStop(); + } + + public void setStopSequences(List stopSequences) { + setStop(stopSequences); + } + + @Override + public Double getTemperature() { + return this.temperature; + } + + public void setTemperature(Double temperature) { + this.temperature = temperature; + } + + @Override + public Double getTopP() { + return this.topP; + } + + public void setTopP(Double topP) { + this.topP = topP; + } + + public List getTools() { + return this.tools; + } + + public void setTools(List tools) { + this.tools = tools; + } + + public ChatCompletionToolChoiceOption getToolChoice() { + return this.toolChoice; + } + + public void setToolChoice(ChatCompletionToolChoiceOption toolChoice) { + this.toolChoice = toolChoice; + } + + public String getUser() { + return this.user; + } + + public void setUser(String user) { + this.user = user; + } + + public Boolean getParallelToolCalls() { + return this.parallelToolCalls; + } + + public void setParallelToolCalls(Boolean parallelToolCalls) { + this.parallelToolCalls = parallelToolCalls; + } + + public Boolean getStore() { + return this.store; + } + + public void setStore(Boolean store) { + this.store = store; + } + + public Map getMetadata() { + return this.metadata; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } + + public String getReasoningEffort() { + return this.reasoningEffort; + } + + public void setReasoningEffort(String reasoningEffort) { + this.reasoningEffort = reasoningEffort; + } + + public String getVerbosity() { + return this.verbosity; + } + + public void setVerbosity(String verbosity) { + this.verbosity = verbosity; + } + + public String getServiceTier() { + return this.serviceTier; + } + + public void setServiceTier(String serviceTier) { + this.serviceTier = serviceTier; + } + + @Override + public List getToolCallbacks() { + return this.toolCallbacks; + } + + @Override + public void setToolCallbacks(List toolCallbacks) { + Assert.notNull(toolCallbacks, "toolCallbacks cannot be null"); + Assert.noNullElements(toolCallbacks, "toolCallbacks cannot contain null elements"); + this.toolCallbacks = toolCallbacks; + } + + @Override + public Set getToolNames() { + return this.toolNames; + } + + @Override + public void setToolNames(Set toolNames) { + Assert.notNull(toolNames, "toolNames cannot be null"); + Assert.noNullElements(toolNames, "toolNames cannot contain null elements"); + toolNames.forEach(tool -> Assert.hasText(tool, "toolNames cannot contain empty elements")); + this.toolNames = toolNames; + } + + @Override + @Nullable + public Boolean getInternalToolExecutionEnabled() { + return this.internalToolExecutionEnabled; + } + + @Override + public void setInternalToolExecutionEnabled(@Nullable Boolean internalToolExecutionEnabled) { + this.internalToolExecutionEnabled = internalToolExecutionEnabled; + } + + public Map getHttpHeaders() { + return this.httpHeaders; + } + + public void setHttpHeaders(Map httpHeaders) { + this.httpHeaders = httpHeaders; + } + + @Override + public Map getToolContext() { + return this.toolContext; + } + + @Override + public void setToolContext(Map toolContext) { + this.toolContext = toolContext; + } + + @Override + public Integer getTopK() { + return null; + } + + public static Builder builder() { + return new Builder(); + } + + @Override + public OpenAiOfficialChatOptions copy() { + return builder().from(this).build(); + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) + return false; + OpenAiOfficialChatOptions options = (OpenAiOfficialChatOptions) o; + return Objects.equals(frequencyPenalty, options.frequencyPenalty) + && Objects.equals(logitBias, options.logitBias) && Objects.equals(logprobs, options.logprobs) + && Objects.equals(topLogprobs, options.topLogprobs) && Objects.equals(maxTokens, options.maxTokens) + && Objects.equals(n, options.n) && Objects.equals(outputModalities, options.outputModalities) + && Objects.equals(outputAudio, options.outputAudio) + && Objects.equals(presencePenalty, options.presencePenalty) + && Objects.equals(responseFormat, options.responseFormat) + && Objects.equals(streamOptions, options.streamOptions) && Objects.equals(seed, options.seed) + && Objects.equals(stop, options.stop) && Objects.equals(temperature, options.temperature) + && Objects.equals(topP, options.topP) && Objects.equals(tools, options.tools) + && Objects.equals(toolChoice, options.toolChoice) && Objects.equals(user, options.user) + && Objects.equals(parallelToolCalls, options.parallelToolCalls) && Objects.equals(store, options.store) + && Objects.equals(metadata, options.metadata) + && Objects.equals(reasoningEffort, options.reasoningEffort) + && Objects.equals(verbosity, options.verbosity) && Objects.equals(serviceTier, options.serviceTier) + && Objects.equals(toolCallbacks, options.toolCallbacks) && Objects.equals(toolNames, options.toolNames) + && Objects.equals(internalToolExecutionEnabled, options.internalToolExecutionEnabled) + && Objects.equals(httpHeaders, options.httpHeaders) && Objects.equals(toolContext, options.toolContext); + } + + @Override + public int hashCode() { + return Objects.hash(frequencyPenalty, logitBias, logprobs, topLogprobs, maxTokens, n, outputModalities, + outputAudio, presencePenalty, responseFormat, streamOptions, seed, stop, temperature, topP, tools, + toolChoice, user, parallelToolCalls, store, metadata, reasoningEffort, verbosity, serviceTier, + toolCallbacks, toolNames, internalToolExecutionEnabled, httpHeaders, toolContext); + } + + @Override + public String toString() { + return "OpenAiOfficialChatOptions{" + "frequencyPenalty=" + frequencyPenalty + ", logitBias=" + logitBias + + ", logprobs=" + logprobs + ", topLogprobs=" + topLogprobs + ", maxTokens=" + maxTokens + ", n=" + n + + ", outputModalities=" + outputModalities + ", outputAudio=" + outputAudio + ", presencePenalty=" + + presencePenalty + ", responseFormat=" + responseFormat + ", streamOptions=" + streamOptions + + ", seed=" + seed + ", stop=" + stop + ", temperature=" + temperature + ", topP=" + topP + ", tools=" + + tools + ", toolChoice=" + toolChoice + ", user='" + user + '\'' + ", parallelToolCalls=" + + parallelToolCalls + ", store=" + store + ", metadata=" + metadata + ", reasoningEffort='" + + reasoningEffort + '\'' + ", verbosity='" + verbosity + '\'' + ", serviceTier='" + serviceTier + '\'' + + ", toolCallbacks=" + toolCallbacks + ", toolNames=" + toolNames + ", internalToolExecutionEnabled=" + + internalToolExecutionEnabled + ", httpHeaders=" + httpHeaders + ", toolContext=" + toolContext + '}'; + } + + public static final class Builder { + + private final OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + + public Builder from(OpenAiOfficialChatOptions fromOptions) { + this.options.setModel(fromOptions.getModel()); + this.options.setDeploymentName(fromOptions.getDeploymentName()); + this.options.setFrequencyPenalty(fromOptions.getFrequencyPenalty()); + this.options.setLogitBias(fromOptions.getLogitBias()); + this.options.setLogprobs(fromOptions.getLogprobs()); + this.options.setTopLogprobs(fromOptions.getTopLogprobs()); + this.options.setMaxTokens(fromOptions.getMaxTokens()); + this.options.setMaxCompletionTokens(fromOptions.getMaxCompletionTokens()); + this.options.setN(fromOptions.getN()); + this.options.setOutputModalities(fromOptions.getOutputModalities() != null + ? new ArrayList<>(fromOptions.getOutputModalities()) : null); + this.options.setOutputAudio(fromOptions.getOutputAudio()); + this.options.setPresencePenalty(fromOptions.getPresencePenalty()); + this.options.setResponseFormat(fromOptions.getResponseFormat()); + this.options.setStreamOptions(fromOptions.getStreamOptions()); + this.options.setSeed(fromOptions.getSeed()); + this.options.setStop(fromOptions.getStop() != null ? new ArrayList<>(fromOptions.getStop()) : null); + this.options.setTemperature(fromOptions.getTemperature()); + this.options.setTopP(fromOptions.getTopP()); + this.options.setTools(fromOptions.getTools()); + this.options.setToolChoice(fromOptions.getToolChoice()); + this.options.setUser(fromOptions.getUser()); + this.options.setParallelToolCalls(fromOptions.getParallelToolCalls()); + this.options.setToolCallbacks(new ArrayList<>(fromOptions.getToolCallbacks())); + this.options.setToolNames(new HashSet<>(fromOptions.getToolNames())); + this.options.setHttpHeaders( + fromOptions.getHttpHeaders() != null ? new HashMap<>(fromOptions.getHttpHeaders()) : null); + this.options.setInternalToolExecutionEnabled(fromOptions.getInternalToolExecutionEnabled()); + this.options.setToolContext(new HashMap<>(fromOptions.getToolContext())); + this.options.setStore(fromOptions.getStore()); + this.options.setMetadata(fromOptions.getMetadata()); + this.options.setReasoningEffort(fromOptions.getReasoningEffort()); + this.options.setVerbosity(fromOptions.getVerbosity()); + this.options.setServiceTier(fromOptions.getServiceTier()); + return this; + } + + public Builder merge(OpenAiOfficialChatOptions from) { + if (from.getModel() != null) { + this.options.setModel(from.getModel()); + } + if (from.getDeploymentName() != null) { + this.options.setDeploymentName(from.getDeploymentName()); + } + if (from.getFrequencyPenalty() != null) { + this.options.setFrequencyPenalty(from.getFrequencyPenalty()); + } + if (from.getLogitBias() != null) { + this.options.setLogitBias(from.getLogitBias()); + } + if (from.getLogprobs() != null) { + this.options.setLogprobs(from.getLogprobs()); + } + if (from.getTopLogprobs() != null) { + this.options.setTopLogprobs(from.getTopLogprobs()); + } + if (from.getMaxTokens() != null) { + this.options.setMaxTokens(from.getMaxTokens()); + } + if (from.getMaxCompletionTokens() != null) { + this.options.setMaxCompletionTokens(from.getMaxCompletionTokens()); + } + if (from.getN() != null) { + this.options.setN(from.getN()); + } + if (from.getOutputModalities() != null) { + this.options.setOutputModalities(new ArrayList<>(from.getOutputModalities())); + } + if (from.getOutputAudio() != null) { + this.options.setOutputAudio(from.getOutputAudio()); + } + if (from.getPresencePenalty() != null) { + this.options.setPresencePenalty(from.getPresencePenalty()); + } + if (from.getResponseFormat() != null) { + this.options.setResponseFormat(from.getResponseFormat()); + } + if (from.getStreamOptions() != null) { + this.options.setStreamOptions(from.getStreamOptions()); + } + if (from.getSeed() != null) { + this.options.setSeed(from.getSeed()); + } + if (from.getStop() != null) { + this.options.setStop(new ArrayList<>(from.getStop())); + } + if (from.getTemperature() != null) { + this.options.setTemperature(from.getTemperature()); + } + if (from.getTopP() != null) { + this.options.setTopP(from.getTopP()); + } + if (from.getTools() != null) { + this.options.setTools(from.getTools()); + } + if (from.getToolChoice() != null) { + this.options.setToolChoice(from.getToolChoice()); + } + if (from.getUser() != null) { + this.options.setUser(from.getUser()); + } + if (from.getParallelToolCalls() != null) { + this.options.setParallelToolCalls(from.getParallelToolCalls()); + } + if (!from.getToolCallbacks().isEmpty()) { + this.options.setToolCallbacks(new ArrayList<>(from.getToolCallbacks())); + } + if (!from.getToolNames().isEmpty()) { + this.options.setToolNames(new HashSet<>(from.getToolNames())); + } + if (from.getHttpHeaders() != null) { + this.options.setHttpHeaders(new HashMap<>(from.getHttpHeaders())); + } + if (from.getInternalToolExecutionEnabled() != null) { + this.options.setInternalToolExecutionEnabled(from.getInternalToolExecutionEnabled()); + } + if (!from.getToolContext().isEmpty()) { + this.options.setToolContext(new HashMap<>(from.getToolContext())); + } + if (from.getStore() != null) { + this.options.setStore(from.getStore()); + } + if (from.getMetadata() != null) { + this.options.setMetadata(from.getMetadata()); + } + if (from.getReasoningEffort() != null) { + this.options.setReasoningEffort(from.getReasoningEffort()); + } + if (from.getVerbosity() != null) { + this.options.setVerbosity(from.getVerbosity()); + } + if (from.getServiceTier() != null) { + this.options.setServiceTier(from.getServiceTier()); + } + return this; + } + + public Builder model(String model) { + this.options.setModel(model); + return this; + } + + public Builder deploymentName(String deploymentName) { + this.options.setDeploymentName(deploymentName); + return this; + } + + public Builder frequencyPenalty(Double frequencyPenalty) { + this.options.setFrequencyPenalty(frequencyPenalty); + return this; + } + + public Builder logitBias(Map logitBias) { + this.options.setLogitBias(logitBias); + return this; + } + + public Builder logprobs(Boolean logprobs) { + this.options.setLogprobs(logprobs); + return this; + } + + public Builder topLogprobs(Integer topLogprobs) { + this.options.setTopLogprobs(topLogprobs); + return this; + } + + public Builder maxTokens(Integer maxTokens) { + if (maxTokens != null && this.options.getMaxCompletionTokens() != null) { + logger + .warn("Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " + + "The previously set maxCompletionTokens ({}) will be cleared and maxTokens ({}) will be used.", + this.options.getMaxCompletionTokens(), maxTokens); + this.options.setMaxCompletionTokens(null); + } + this.options.setMaxTokens(maxTokens); + return this; + } + + public Builder maxCompletionTokens(Integer maxCompletionTokens) { + if (maxCompletionTokens != null && this.options.getMaxTokens() != null) { + logger + .warn("Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " + + "The previously set maxTokens ({}) will be cleared and maxCompletionTokens ({}) will be used.", + this.options.getMaxTokens(), maxCompletionTokens); + this.options.setMaxTokens(null); + } + this.options.setMaxCompletionTokens(maxCompletionTokens); + return this; + } + + public Builder N(Integer n) { + this.options.setN(n); + return this; + } + + public Builder outputModalities(List modalities) { + this.options.setOutputModalities(modalities); + return this; + } + + public Builder outputAudio(ChatCompletionAudioParam audio) { + this.options.setOutputAudio(audio); + return this; + } + + public Builder presencePenalty(Double presencePenalty) { + this.options.setPresencePenalty(presencePenalty); + return this; + } + + public Builder responseFormat(ResponseFormatJsonSchema responseFormat) { + this.options.setResponseFormat(responseFormat); + return this; + } + + public Builder seed(Integer seed) { + this.options.setSeed(seed); + return this; + } + + public Builder stop(List stop) { + this.options.setStop(stop); + return this; + } + + public Builder temperature(Double temperature) { + this.options.setTemperature(temperature); + return this; + } + + public Builder topP(Double topP) { + this.options.setTopP(topP); + return this; + } + + public Builder tools(List tools) { + this.options.setTools(tools); + return this; + } + + public Builder toolChoice(ChatCompletionToolChoiceOption toolChoice) { + this.options.setToolChoice(toolChoice); + return this; + } + + public Builder user(String user) { + this.options.setUser(user); + return this; + } + + public Builder parallelToolCalls(Boolean parallelToolCalls) { + this.options.setParallelToolCalls(parallelToolCalls); + return this; + } + + public Builder toolCallbacks(List toolCallbacks) { + this.options.setToolCallbacks(toolCallbacks); + return this; + } + + public Builder toolCallbacks(ToolCallback... toolCallbacks) { + this.options.setToolCallbacks(Arrays.asList(toolCallbacks)); + return this; + } + + public Builder toolNames(Set toolNames) { + Assert.notNull(toolNames, "toolNames cannot be null"); + this.options.setToolNames(toolNames); + return this; + } + + public Builder toolNames(String... toolNames) { + Assert.notNull(toolNames, "toolNames cannot be null"); + this.options.setToolNames(new HashSet<>(Arrays.asList(toolNames))); + return this; + } + + public Builder httpHeaders(Map httpHeaders) { + this.options.setHttpHeaders(httpHeaders); + return this; + } + + public Builder internalToolExecutionEnabled(@Nullable Boolean internalToolExecutionEnabled) { + this.options.setInternalToolExecutionEnabled(internalToolExecutionEnabled); + return this; + } + + public Builder toolContext(Map toolContext) { + this.options.setToolContext(toolContext); + return this; + } + + public Builder store(Boolean store) { + this.options.setStore(store); + return this; + } + + public Builder metadata(Map metadata) { + this.options.setMetadata(metadata); + return this; + } + + public Builder reasoningEffort(String reasoningEffort) { + this.options.setReasoningEffort(reasoningEffort); + return this; + } + + public Builder verbosity(String verbosity) { + this.options.setVerbosity(verbosity); + return this; + } + + public Builder serviceTier(String serviceTier) { + this.options.setServiceTier(serviceTier); + return this; + } + + public OpenAiOfficialChatOptions build() { + return this.options; + } + + } + +} From 54fe12b75f6256193e482a9c5f56c45978e01eff Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Fri, 31 Oct 2025 18:01:21 +0100 Subject: [PATCH 05/49] Implementation of the OpenAI Java SDK Create the OpenAiOfficialChatModel class: - This is a first implementation that is not of good quality yet - Tests do not pass yet Signed-off-by: Julien Dubois --- .../OpenAiOfficialChatModel.java | 784 ++++++++++++++++++ .../OpenAiOfficialChatOptions.java | 51 +- .../setup/OpenAiOfficialSetup.java | 116 ++- .../OpenAiOfficialTestConfiguration.java | 5 + .../chat/OpenAiOfficialChatModelIT.java | 127 +++ .../image/OpenAiOfficialImageModelIT.java | 2 +- ...OpenAiOfficialImageModelObservationIT.java | 2 +- .../test/resources/prompts/system-message.st | 4 + 8 files changed, 1029 insertions(+), 62 deletions(-) create mode 100644 models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java create mode 100644 models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java create mode 100644 models/spring-ai-openai-official/src/test/resources/prompts/system-message.st diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java new file mode 100644 index 00000000000..625c9f2f32b --- /dev/null +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -0,0 +1,784 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaiofficial; + +import com.openai.client.OpenAIClient; +import com.openai.client.OpenAIClientAsync; +import com.openai.core.JsonArray; +import com.openai.core.JsonValue; +import com.openai.models.FunctionDefinition; +import com.openai.models.FunctionParameters; +import com.openai.models.chat.completions.*; +import com.openai.models.completions.CompletionUsage; +import io.micrometer.observation.Observation; +import io.micrometer.observation.ObservationRegistry; +import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.ai.chat.messages.AssistantMessage; +import org.springframework.ai.chat.messages.MessageType; +import org.springframework.ai.chat.messages.ToolResponseMessage; +import org.springframework.ai.chat.messages.UserMessage; +import org.springframework.ai.chat.metadata.ChatGenerationMetadata; +import org.springframework.ai.chat.metadata.ChatResponseMetadata; +import org.springframework.ai.chat.metadata.DefaultUsage; +import org.springframework.ai.chat.metadata.EmptyUsage; +import org.springframework.ai.chat.metadata.Usage; +import org.springframework.ai.chat.model.ChatModel; +import org.springframework.ai.chat.model.ChatResponse; +import org.springframework.ai.chat.model.Generation; +import org.springframework.ai.chat.model.MessageAggregator; +import org.springframework.ai.chat.observation.ChatModelObservationContext; +import org.springframework.ai.chat.observation.ChatModelObservationConvention; +import org.springframework.ai.chat.observation.ChatModelObservationDocumentation; +import org.springframework.ai.chat.observation.DefaultChatModelObservationConvention; +import org.springframework.ai.chat.prompt.ChatOptions; +import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.ai.model.ModelOptionsUtils; +import org.springframework.ai.model.tool.DefaultToolExecutionEligibilityPredicate; +import org.springframework.ai.model.tool.ToolCallingChatOptions; +import org.springframework.ai.model.tool.ToolCallingManager; +import org.springframework.ai.model.tool.ToolExecutionEligibilityPredicate; +import org.springframework.ai.model.tool.ToolExecutionResult; +import org.springframework.ai.model.tool.internal.ToolCallReactiveContextHolder; +import org.springframework.ai.observation.conventions.AiProvider; +import org.springframework.ai.support.UsageCalculator; +import org.springframework.ai.tool.definition.ToolDefinition; +import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; +import reactor.core.scheduler.Schedulers; + +import java.util.ArrayList; +import java.util.Base64; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + +import static org.springframework.ai.openaiofficial.setup.OpenAiOfficialSetup.setupAsyncClient; +import static org.springframework.ai.openaiofficial.setup.OpenAiOfficialSetup.setupSyncClient; + +/** + * Chat Model implementation using the OpenAI Java SDK. + * + * @author Julien Dubois + */ +public class OpenAiOfficialChatModel implements ChatModel { + + private static final String DEFAULT_MODEL_NAME = OpenAiOfficialChatOptions.DEFAULT_CHAT_MODEL; + + private static final ChatModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultChatModelObservationConvention(); + + private static final ToolCallingManager DEFAULT_TOOL_CALLING_MANAGER = ToolCallingManager.builder().build(); + + private final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatModel.class); + + private final OpenAIClient openAiClient; + + private final OpenAIClientAsync openAiClientAsync; + + private final OpenAiOfficialChatOptions options; + + private final ObservationRegistry observationRegistry; + + private final ToolCallingManager toolCallingManager; + + private final ToolExecutionEligibilityPredicate toolExecutionEligibilityPredicate; + + private ChatModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; + + public OpenAiOfficialChatModel() { + this(null, null, null, null, null, null); + } + + public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options) { + this(null, null, options, null, null, null); + } + + public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options, ObservationRegistry observationRegistry) { + this(null, null, options, null, observationRegistry, null); + } + + public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options, ToolCallingManager toolCallingManager, + ObservationRegistry observationRegistry) { + this(null, null, options, toolCallingManager, observationRegistry, null); + } + + public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync) { + this(openAIClient, openAiClientAsync, null, null, null, null); + } + + public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync, + OpenAiOfficialChatOptions options) { + this(openAIClient, openAiClientAsync, options, null, null, null); + } + + public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync, + OpenAiOfficialChatOptions options, ObservationRegistry observationRegistry) { + this(openAIClient, openAiClientAsync, options, null, observationRegistry, null); + } + + public OpenAiOfficialChatModel(OpenAIClient openAiClient, OpenAIClientAsync openAiClientAsync, + OpenAiOfficialChatOptions options, ToolCallingManager toolCallingManager, + ObservationRegistry observationRegistry, + ToolExecutionEligibilityPredicate toolExecutionEligibilityPredicate) { + + if (options == null) { + this.options = OpenAiOfficialChatOptions.builder().model(DEFAULT_MODEL_NAME).build(); + } + else { + this.options = options; + } + this.openAiClient = Objects.requireNonNullElseGet(openAiClient, + () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), + this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), + this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), + this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), + this.options.getProxy(), this.options.getCustomHeaders())); + this.openAiClientAsync = Objects.requireNonNullElseGet(openAiClientAsync, + () -> setupAsyncClient(this.options.getBaseUrl(), this.options.getApiKey(), + this.options.getCredential(), this.options.getAzureDeploymentName(), + this.options.getAzureOpenAIServiceVersion(), this.options.getOrganizationId(), + this.options.isAzure(), this.options.isGitHubModels(), this.options.getModel(), + this.options.getTimeout(), this.options.getMaxRetries(), this.options.getProxy(), + this.options.getCustomHeaders())); + this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); + this.toolCallingManager = Objects.requireNonNullElse(toolCallingManager, DEFAULT_TOOL_CALLING_MANAGER); + this.toolExecutionEligibilityPredicate = Objects.requireNonNullElse(toolExecutionEligibilityPredicate, + new DefaultToolExecutionEligibilityPredicate()); + } + + public OpenAiOfficialChatOptions getOptions() { + return this.options; + } + + @Override + public ChatResponse call(Prompt prompt) { + // Before moving any further, build the final request Prompt, + // merging runtime and default options. + Prompt requestPrompt = buildRequestPrompt(prompt); + return this.internalCall(requestPrompt, null); + } + + public ChatResponse internalCall(Prompt prompt, ChatResponse previousChatResponse) { + + ChatCompletionCreateParams request = createRequest(prompt, false); + + ChatModelObservationContext observationContext = ChatModelObservationContext.builder() + .prompt(prompt) + .provider(AiProvider.OPENAI_OFFICIAL.value()) + .build(); + + ChatResponse response = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION + .observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, + this.observationRegistry) + .observe(() -> { + + ChatCompletion chatCompletion = this.openAiClient.chat().completions().create(request); + + List choices = chatCompletion.choices(); + if (choices.isEmpty()) { + logger.warn("No choices returned for prompt: {}", prompt); + return new ChatResponse(List.of()); + } + + List generations = choices.stream().map(choice -> { + chatCompletion.id(); + choice.finishReason(); + Map metadata = Map.of("id", chatCompletion.id(), "role", + choice.message()._role().asString().isPresent() ? choice.message()._role().asStringOrThrow() + : "", + "index", choice.index(), "finishReason", choice.finishReason().value().toString(), + "refusal", choice.message().refusal().isPresent() ? choice.message().refusal() : "", + "annotations", choice.message().annotations().isPresent() ? choice.message().annotations() + : List.of(Map.of())); + return buildGeneration(choice, metadata); + }).toList(); + + // Current usage + CompletionUsage usage = chatCompletion.usage().orElse(null); + Usage currentChatResponseUsage = usage != null ? getDefaultUsage(usage) : new EmptyUsage(); + Usage accumulatedUsage = UsageCalculator.getCumulativeUsage(currentChatResponseUsage, + previousChatResponse); + ChatResponse chatResponse = new ChatResponse(generations, from(chatCompletion, accumulatedUsage)); + + observationContext.setResponse(chatResponse); + + return chatResponse; + + }); + + if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(prompt.getOptions(), response)) { + var toolExecutionResult = this.toolCallingManager.executeToolCalls(prompt, response); + if (toolExecutionResult.returnDirect()) { + // Return tool execution result directly to the client. + return ChatResponse.builder() + .from(response) + .generations(ToolExecutionResult.buildGenerations(toolExecutionResult)) + .build(); + } + else { + // Send the tool execution result back to the model. + return this.internalCall(new Prompt(toolExecutionResult.conversationHistory(), prompt.getOptions()), + response); + } + } + + return response; + } + + @Override + public Flux stream(Prompt prompt) { + // Before moving any further, build the final request Prompt, + // merging runtime and default options. + Prompt requestPrompt = buildRequestPrompt(prompt); + return internalStream(requestPrompt, null); + } + + public Flux internalStream(Prompt prompt, ChatResponse previousChatResponse) { + return Flux.deferContextual(contextView -> { + ChatCompletionCreateParams request = createRequest(prompt, true); + + // For chunked responses, only the first chunk contains the choice role. + // The rest of the chunks with same ID share the same role. + ConcurrentHashMap roleMap = new ConcurrentHashMap<>(); + + final ChatModelObservationContext observationContext = ChatModelObservationContext.builder() + .prompt(prompt) + .provider(AiProvider.OPENAI_OFFICIAL.value()) + .build(); + + Observation observation = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION.observation( + this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, + this.observationRegistry); + + observation.parentObservation(contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null)).start(); + + Flux chatResponse = Flux.empty(); + // Convert the ChatCompletionChunk into a ChatCompletion to be able to reuse + // the function call handling logic. + this.openAiClientAsync.chat().completions().createStreaming(request).subscribe(chunk -> { + ChatCompletion chatCompletion = chunkToChatCompletion(chunk); + Mono.just(chatCompletion).map(chatCompletion2 -> { + try { + // If an id is not provided, set to "NO_ID" (for compatible APIs). + chatCompletion2.id(); + String id = chatCompletion2.id(); + + List generations = chatCompletion2.choices().stream().map(choice -> { // @formatter:off + roleMap.putIfAbsent(id, choice.message()._role().asString().isPresent() ? choice.message()._role().asStringOrThrow() : ""); + Map metadata = Map.of( + "id", id, + "role", roleMap.getOrDefault(id, ""), + "index", choice.index(), + "finishReason", choice.finishReason().asString(), + "refusal", choice.message().refusal().isPresent() ? choice.message().refusal() : "", + "annotations", choice.message().annotations().isPresent() ? choice.message().annotations() : List.of()); + return buildGeneration(choice, metadata); + }).toList(); + + Optional usage = chatCompletion2.usage(); + Usage currentChatResponseUsage = usage.isPresent()? getDefaultUsage(usage.get()) : new EmptyUsage(); + Usage accumulatedUsage = UsageCalculator.getCumulativeUsage(currentChatResponseUsage, + previousChatResponse); + return new ChatResponse(generations, from(chatCompletion2, accumulatedUsage)); + } + catch (Exception e) { + logger.error("Error processing chat completion", e); + return new ChatResponse(List.of()); + } + }) + .flux() + .buffer(2, 1) + .map(bufferList -> { + ChatResponse firstResponse = bufferList.get(0); + if (request.streamOptions().isPresent()) { + if (bufferList.size() == 2) { + ChatResponse secondResponse = bufferList.get(1); + if (secondResponse!=null) { + // This is the usage from the final Chat response for a + // given Chat request. + Usage usage = secondResponse.getMetadata().getUsage(); + if (!UsageCalculator.isEmpty(usage)) { + // Store the usage from the final response to the + // penultimate response for accumulation. + return new ChatResponse(firstResponse.getResults(), + from(firstResponse.getMetadata(), usage)); + } + } + } + } + return firstResponse; + }); + }) + .onCompleteFuture() + .whenComplete((unused, error) -> { + if (error != null) { + logger.error(error.getMessage(), error); + throw new RuntimeException(error); + } + }); + + Flux flux = chatResponse.flatMap(response -> { + assert prompt.getOptions() != null; + if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(prompt.getOptions(), response)) { + // FIXME: bounded elastic needs to be used since tool calling + // is currently only synchronous + return Flux.deferContextual(ctx -> { + ToolExecutionResult toolExecutionResult; + try { + ToolCallReactiveContextHolder.setContext(ctx); + toolExecutionResult = this.toolCallingManager.executeToolCalls(prompt, response); + } + finally { + ToolCallReactiveContextHolder.clearContext(); + } + if (toolExecutionResult.returnDirect()) { + // Return tool execution result directly to the client. + return Flux.just(ChatResponse.builder() + .from(response) + .generations(ToolExecutionResult.buildGenerations(toolExecutionResult)) + .build()); + } + else { + // Send the tool execution result back to the model. + return this.internalStream( + new Prompt(toolExecutionResult.conversationHistory(), prompt.getOptions()), + response); + } + }).subscribeOn(Schedulers.boundedElastic()); + } + else { + return Flux.just(response); + } + }) + .doOnError(observation::error) + .doFinally(s -> observation.stop()) + .contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation)); + + return new MessageAggregator().aggregate(flux, observationContext::setResponse); + + }); + } + + private Generation buildGeneration(ChatCompletion.Choice choice, Map metadata) { + ChatCompletionMessage message = choice.message(); + List toolCalls = message.toolCalls() + .map(toolCallsList -> toolCallsList.stream() + .filter(toolCall -> toolCall.function().isPresent()) + .map(toolCall -> { + return new AssistantMessage.ToolCall(toolCall.function().get().id(), "function", toolCall.function().get().function().name(), + toolCall.function().get().function().arguments()); + }).toList()).get(); + + var generationMetadataBuilder = ChatGenerationMetadata.builder() + .finishReason(choice.finishReason().value().name()); + + String textContent = message.content().isPresent() ? message.content().get() : ""; + + var assistantMessage = AssistantMessage.builder() + .content(textContent) + .properties(metadata) + .toolCalls(toolCalls) + .build(); + return new Generation(assistantMessage, generationMetadataBuilder.build()); + } + + private ChatResponseMetadata from(ChatCompletion result, Usage usage) { + Assert.notNull(result, "OpenAI ChatCompletion must not be null"); + result.model(); + result.id(); + return ChatResponseMetadata.builder() + .id(result.id()) + .usage(usage) + .model(result.model()) + .keyValue("created", result.created()) + .build(); + } + + private ChatResponseMetadata from(ChatResponseMetadata chatResponseMetadata, Usage usage) { + Assert.notNull(chatResponseMetadata, "OpenAI ChatResponseMetadata must not be null"); + return ChatResponseMetadata.builder() + .id(chatResponseMetadata.getId() != null ? chatResponseMetadata.getId() : "") + .usage(usage) + .model(chatResponseMetadata.getModel() != null ? chatResponseMetadata.getModel() : "") + .build(); + } + + /** + * Convert the ChatCompletionChunk into a ChatCompletion. The Usage is set to null. + * @param chunk the ChatCompletionChunk to convert + * @return the ChatCompletion + */ + private ChatCompletion chunkToChatCompletion(ChatCompletionChunk chunk) { + List choices = chunk.choices() + .stream() + .map(chunkChoice -> ChatCompletion.Choice.builder() + .finishReason(ChatCompletion.Choice.FinishReason.of(chunkChoice.finishReason().toString())) + .index(chunkChoice.index()) + .message(ChatCompletionMessage.builder().content(chunkChoice.delta().content()).build()) + .build()) + .toList(); + + return ChatCompletion.builder() + .id(chunk.id()) + .choices(choices) + .created(chunk.created()) + .model(chunk.model()) + .usage(Objects.requireNonNull(chunk.usage().orElse(null))) + .build(); + } + + private DefaultUsage getDefaultUsage(CompletionUsage usage) { + return new DefaultUsage(Math.toIntExact(usage.promptTokens()), Math.toIntExact(usage.completionTokens()), + Math.toIntExact(usage.totalTokens()), usage); + } + + Prompt buildRequestPrompt(Prompt prompt) { + // Process runtime options + OpenAiOfficialChatOptions runtimeOptions = null; + if (prompt.getOptions() != null) { + if (prompt.getOptions() instanceof ToolCallingChatOptions toolCallingChatOptions) { + runtimeOptions = ModelOptionsUtils.copyToTarget(toolCallingChatOptions, ToolCallingChatOptions.class, + OpenAiOfficialChatOptions.class); + } + else { + runtimeOptions = ModelOptionsUtils.copyToTarget(prompt.getOptions(), ChatOptions.class, + OpenAiOfficialChatOptions.class); + } + } + + // Define request options by merging runtime options and default options + OpenAiOfficialChatOptions requestOptions = OpenAiOfficialChatOptions.builder() + .from(this.options) + .merge(runtimeOptions != null ? runtimeOptions : OpenAiOfficialChatOptions.builder().build()) + .build(); + + // Merge @JsonIgnore-annotated options explicitly since they are ignored by + // Jackson, used by ModelOptionsUtils. + if (runtimeOptions != null) { + if (runtimeOptions.getTopK() != null) { + logger.warn("The topK option is not supported by OpenAI chat models. Ignoring."); + } + + Map mergedHttpHeaders = new HashMap<>(this.options.getHttpHeaders()); + mergedHttpHeaders.putAll(runtimeOptions.getHttpHeaders()); + requestOptions.setHttpHeaders(mergedHttpHeaders); + + requestOptions.setInternalToolExecutionEnabled(runtimeOptions.getInternalToolExecutionEnabled() != null + ? runtimeOptions.getInternalToolExecutionEnabled() + : this.options.getInternalToolExecutionEnabled()); + requestOptions.setToolNames( + ToolCallingChatOptions.mergeToolNames(runtimeOptions.getToolNames(), this.options.getToolNames())); + requestOptions.setToolCallbacks(ToolCallingChatOptions.mergeToolCallbacks(runtimeOptions.getToolCallbacks(), + this.options.getToolCallbacks())); + requestOptions.setToolContext(ToolCallingChatOptions.mergeToolContext(runtimeOptions.getToolContext(), + this.options.getToolContext())); + } + else { + requestOptions.setHttpHeaders(this.options.getHttpHeaders()); + requestOptions.setInternalToolExecutionEnabled(this.options.getInternalToolExecutionEnabled()); + requestOptions.setToolNames(this.options.getToolNames()); + requestOptions.setToolCallbacks(this.options.getToolCallbacks()); + requestOptions.setToolContext(this.options.getToolContext()); + } + + ToolCallingChatOptions.validateToolCallbacks(requestOptions.getToolCallbacks()); + + return new Prompt(prompt.getInstructions(), requestOptions); + } + + ChatCompletionCreateParams createRequest(Prompt prompt, boolean stream) { + + List chatCompletionMessage = prompt.getInstructions().stream().map(message -> { + if (message.getMessageType() == MessageType.USER || message.getMessageType() == MessageType.SYSTEM) { + // Handle simple text content for user and system messages + ChatCompletionMessage.Builder builder = ChatCompletionMessage.builder(); + + if (message instanceof UserMessage userMessage && !CollectionUtils.isEmpty(userMessage.getMedia())) { + // Handle media content (images, audio, files) + List contentParts = new ArrayList<>(); + + if (!message.getText().isEmpty()) { + contentParts.add(ChatCompletionContentPartText.builder().text(message.getText()).build().text()); + } + + // Add media content parts + userMessage.getMedia().forEach(media -> { + String mimeType = media.getMimeType().toString(); + if (mimeType.startsWith("image/")) { + if (media.getData() instanceof java.net.URI) { + contentParts.add(ChatCompletionContentPartImage.builder() + .imageUrl(ChatCompletionContentPartImage.ImageUrl.builder() + .url(media.getData().toString()) + .build()) + .build().imageUrl().url()); + } else { + logger.info("Could not process image media with data of type: {}. Only java.net.URI is supported for image URLs.", + media.getData().getClass().getSimpleName()); + } + } + else if (mimeType.startsWith("audio/")) { + contentParts.add(ChatCompletionContentPartInputAudio.builder() + .inputAudio(ChatCompletionContentPartInputAudio.InputAudio + .builder() + .data(fromAudioData(media.getData())) + .format(mimeType.contains("mp3") + ? ChatCompletionContentPartInputAudio.InputAudio.Format.MP3 + : ChatCompletionContentPartInputAudio.InputAudio.Format.WAV) + .build()) + .build().inputAudio().data()); + } + else { + // Assume it's a file or other media type represented as a data URL + contentParts.add(fromMediaData(media.getMimeType(), media.getData())); + } + }); + builder.content(JsonArray.of(contentParts).asString()); + } + else { + // Simple text message + builder.content(ChatCompletionContentPartText.builder().text(message.getText()).build().text()); + } + + if (message.getMessageType() == MessageType.USER) { + builder.role(JsonValue.from(MessageType.USER)); + } + else { + builder.role(JsonValue.from(MessageType.SYSTEM)); + } + Object refusal = message.getMetadata().get("refusal"); + builder.refusal(refusal != null ? JsonValue.from(refusal.toString()) : JsonValue.from("")); + return List.of(builder.build()); + } + else if (message.getMessageType() == MessageType.ASSISTANT) { + var assistantMessage = (AssistantMessage) message; + ChatCompletionMessage.Builder builder = ChatCompletionMessage.builder() + .role(JsonValue.from(MessageType.ASSISTANT)); + + if (assistantMessage.getText() != null) { + builder.content(ChatCompletionMessage.builder().content(assistantMessage.getText()).build().content()); + } + + if (!CollectionUtils.isEmpty(assistantMessage.getToolCalls())) { + List toolCalls = assistantMessage.getToolCalls() + .stream() + .map(toolCall -> ChatCompletionMessageToolCall.ofFunction( + ChatCompletionMessageFunctionToolCall.builder() + .id(toolCall.id()) + .function(ChatCompletionMessageFunctionToolCall.Function.builder() + .name(toolCall.name()) + .arguments(toolCall.arguments()).build()) + .build()) + ) + .toList(); + builder.toolCalls(toolCalls); + } + + return List.of(builder.build()); + } + else if (message.getMessageType() == MessageType.TOOL) { + ToolResponseMessage toolMessage = (ToolResponseMessage) message; + + return toolMessage.getResponses() + .stream() + .map(toolResponse -> ChatCompletionMessage.builder() + .role(JsonValue.from(MessageType.TOOL)) + .content(ChatCompletionMessage.builder().content(toolResponse.responseData()).build().content()) + .build()) + .toList(); + } + else { + throw new IllegalArgumentException("Unsupported message type: " + message.getMessageType()); + } + }).flatMap(List::stream).toList(); + + ChatCompletionCreateParams.Builder builder = ChatCompletionCreateParams.builder(); + + chatCompletionMessage.forEach(message -> { + builder.addMessage(message); + }); + + OpenAiOfficialChatOptions requestOptions = (OpenAiOfficialChatOptions) prompt.getOptions(); + + // Use deployment name if available (for Azure AI Foundry), otherwise use model + // name + if (requestOptions.getDeploymentName() != null) { + builder.model(requestOptions.getDeploymentName()); + } + else if (requestOptions.getModel() != null) { + builder.model(requestOptions.getModel()); + } + + if (requestOptions.getFrequencyPenalty() != null) { + builder.frequencyPenalty(requestOptions.getFrequencyPenalty()); + } + if (requestOptions.getLogitBias() != null) { + builder.logitBias(ChatCompletionCreateParams.LogitBias.builder() + .putAllAdditionalProperties(requestOptions.getLogitBias().entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> JsonValue.from(entry.getValue())))) + .build()); + } + if (requestOptions.getLogprobs() != null) { + builder.logprobs(requestOptions.getLogprobs()); + } + if (requestOptions.getTopLogprobs() != null) { + builder.topLogprobs(requestOptions.getTopLogprobs()); + } + if (requestOptions.getMaxTokens() != null) { + builder.maxTokens(requestOptions.getMaxTokens()); + } + if (requestOptions.getMaxCompletionTokens() != null) { + builder.maxCompletionTokens(requestOptions.getMaxCompletionTokens()); + } + if (requestOptions.getN() != null) { + builder.n(requestOptions.getN()); + } + if (requestOptions.getOutputAudio() != null) { + builder.audio(requestOptions.getOutputAudio()); + } + if (requestOptions.getPresencePenalty() != null) { + builder.presencePenalty(requestOptions.getPresencePenalty()); + } + if (requestOptions.getResponseFormat() != null) { + builder.responseFormat(requestOptions.getResponseFormat()); + } + if (requestOptions.getSeed() != null) { + builder.seed(requestOptions.getSeed()); + } + if (requestOptions.getStop() != null && !requestOptions.getStop().isEmpty()) { + if (requestOptions.getStop().size() == 1) { + builder.stop(ChatCompletionCreateParams.Stop.ofString(requestOptions.getStop().get(0))); + } + else { + builder.stop(ChatCompletionCreateParams.Stop.ofStrings(requestOptions.getStop())); + } + } + if (requestOptions.getTemperature() != null) { + builder.temperature(requestOptions.getTemperature()); + } + if (requestOptions.getTopP() != null) { + builder.topP(requestOptions.getTopP()); + } + if (requestOptions.getUser() != null) { + builder.user(requestOptions.getUser()); + } + if (requestOptions.getParallelToolCalls() != null) { + builder.parallelToolCalls(requestOptions.getParallelToolCalls()); + } + if (requestOptions.getStore() != null) { + builder.store(requestOptions.getStore()); + } + if (requestOptions.getMetadata() != null && !requestOptions.getMetadata().isEmpty()) { + builder.metadata(ChatCompletionCreateParams.Metadata.builder() + .putAllAdditionalProperties(requestOptions.getMetadata() .entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> JsonValue.from(entry.getValue())))) + .build()); + } + if (requestOptions.getServiceTier() != null) { + builder.serviceTier(ChatCompletionCreateParams.ServiceTier.of(requestOptions.getServiceTier())); + } + + if (stream) { + if (requestOptions.getStreamOptions() != null) { + ChatCompletionStreamOptions.Builder streamOptionsBuilder = ChatCompletionStreamOptions.builder(); + + if (requestOptions.getStreamOptions().includeObfuscation().isPresent()) { + streamOptionsBuilder.includeObfuscation(requestOptions.getStreamOptions().includeObfuscation().get()); + } + streamOptionsBuilder.additionalProperties(requestOptions.getStreamOptions()._additionalProperties()); + builder.streamOptions(streamOptionsBuilder.build()); + } + } + + // Add the tool definitions to the request's tools parameter. + List toolDefinitions = this.toolCallingManager.resolveToolDefinitions(requestOptions); + if (!CollectionUtils.isEmpty(toolDefinitions)) { + builder.tools(getChatCompletionTools(toolDefinitions)); + } + + if (requestOptions.getToolChoice() != null) { + builder.toolChoice(requestOptions.getToolChoice()); + } + + return builder.build(); + } + + private String fromAudioData(Object audioData) { + if (audioData instanceof byte[] bytes) { + return Base64.getEncoder().encodeToString(bytes); + } + throw new IllegalArgumentException("Unsupported audio data type: " + audioData.getClass().getSimpleName()); + } + + private String fromMediaData(org.springframework.util.MimeType mimeType, Object mediaContentData) { + if (mediaContentData instanceof byte[] bytes) { + // Assume the bytes are an image. So, convert the bytes to a base64 encoded + // following the prefix pattern. + return String.format("data:%s;base64,%s", mimeType.toString(), Base64.getEncoder().encodeToString(bytes)); + } + else if (mediaContentData instanceof String text) { + // Assume the text is a URLs or a base64 encoded image prefixed by the user. + return text; + } + else { + throw new IllegalArgumentException( + "Unsupported media data type: " + mediaContentData.getClass().getSimpleName()); + } + } + + private List getChatCompletionTools(List toolDefinitions) { + return toolDefinitions.stream() + .map(toolDefinition -> { + FunctionParameters.Builder parametersBuilder = FunctionParameters.builder(); + parametersBuilder.putAdditionalProperty("type", JsonValue.from("object")); + if (!toolDefinition.inputSchema().isEmpty()) { + parametersBuilder.putAdditionalProperty("strict", JsonValue.from(true)); // TODO allow to have non-strict schemas + parametersBuilder.putAdditionalProperty("json_schema", JsonValue.from(toolDefinition.inputSchema())); + } + FunctionDefinition functionDefinition = FunctionDefinition.builder() + .name(toolDefinition.name()) + .description(toolDefinition.description()) + .parameters(parametersBuilder.build()) + .build(); + + return ChatCompletionTool.ofFunction(ChatCompletionFunctionTool.builder().function(functionDefinition).build()); + } ) + .toList(); + } + + @Override + public ChatOptions getDefaultOptions() { + return this.options.copy(); + } + + /** + * Use the provided convention for reporting observation data + * @param observationConvention The provided convention + */ + public void setObservationConvention(ChatModelObservationConvention observationConvention) { + Assert.notNull(observationConvention, "observationConvention cannot be null"); + this.observationConvention = observationConvention; + } + +} diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java index 168cd9c9748..1c8bf7330d9 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java @@ -37,6 +37,8 @@ import java.util.Objects; import java.util.Set; +import static com.openai.models.ChatModel.GPT_5_MINI; + /** * Configuration information for the Chat Model implementation using the OpenAI Java SDK. * @@ -44,6 +46,8 @@ */ public class OpenAiOfficialChatOptions extends AbstractOpenAiOfficialOptions implements ToolCallingChatOptions { + public static final String DEFAULT_CHAT_MODEL = GPT_5_MINI.asString(); + private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatOptions.class); private Double frequencyPenalty; @@ -60,8 +64,6 @@ public class OpenAiOfficialChatOptions extends AbstractOpenAiOfficialOptions imp private Integer n; - private List outputModalities; - private ChatCompletionAudioParam outputAudio; private Double presencePenalty; @@ -164,14 +166,6 @@ public void setN(Integer n) { this.n = n; } - public List getOutputModalities() { - return this.outputModalities; - } - - public void setOutputModalities(List outputModalities) { - this.outputModalities = outputModalities; - } - public ChatCompletionAudioParam getOutputAudio() { return this.outputAudio; } @@ -396,8 +390,7 @@ public boolean equals(Object o) { return Objects.equals(frequencyPenalty, options.frequencyPenalty) && Objects.equals(logitBias, options.logitBias) && Objects.equals(logprobs, options.logprobs) && Objects.equals(topLogprobs, options.topLogprobs) && Objects.equals(maxTokens, options.maxTokens) - && Objects.equals(n, options.n) && Objects.equals(outputModalities, options.outputModalities) - && Objects.equals(outputAudio, options.outputAudio) + && Objects.equals(n, options.n) && Objects.equals(outputAudio, options.outputAudio) && Objects.equals(presencePenalty, options.presencePenalty) && Objects.equals(responseFormat, options.responseFormat) && Objects.equals(streamOptions, options.streamOptions) && Objects.equals(seed, options.seed) @@ -415,24 +408,24 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(frequencyPenalty, logitBias, logprobs, topLogprobs, maxTokens, n, outputModalities, - outputAudio, presencePenalty, responseFormat, streamOptions, seed, stop, temperature, topP, tools, - toolChoice, user, parallelToolCalls, store, metadata, reasoningEffort, verbosity, serviceTier, - toolCallbacks, toolNames, internalToolExecutionEnabled, httpHeaders, toolContext); + return Objects.hash(frequencyPenalty, logitBias, logprobs, topLogprobs, maxTokens, n, outputAudio, + presencePenalty, responseFormat, streamOptions, seed, stop, temperature, topP, tools, toolChoice, user, + parallelToolCalls, store, metadata, reasoningEffort, verbosity, serviceTier, toolCallbacks, toolNames, + internalToolExecutionEnabled, httpHeaders, toolContext); } @Override public String toString() { return "OpenAiOfficialChatOptions{" + "frequencyPenalty=" + frequencyPenalty + ", logitBias=" + logitBias + ", logprobs=" + logprobs + ", topLogprobs=" + topLogprobs + ", maxTokens=" + maxTokens + ", n=" + n - + ", outputModalities=" + outputModalities + ", outputAudio=" + outputAudio + ", presencePenalty=" - + presencePenalty + ", responseFormat=" + responseFormat + ", streamOptions=" + streamOptions - + ", seed=" + seed + ", stop=" + stop + ", temperature=" + temperature + ", topP=" + topP + ", tools=" - + tools + ", toolChoice=" + toolChoice + ", user='" + user + '\'' + ", parallelToolCalls=" - + parallelToolCalls + ", store=" + store + ", metadata=" + metadata + ", reasoningEffort='" - + reasoningEffort + '\'' + ", verbosity='" + verbosity + '\'' + ", serviceTier='" + serviceTier + '\'' - + ", toolCallbacks=" + toolCallbacks + ", toolNames=" + toolNames + ", internalToolExecutionEnabled=" - + internalToolExecutionEnabled + ", httpHeaders=" + httpHeaders + ", toolContext=" + toolContext + '}'; + + ", outputAudio=" + outputAudio + ", presencePenalty=" + presencePenalty + ", responseFormat=" + + responseFormat + ", streamOptions=" + streamOptions + ", seed=" + seed + ", stop=" + stop + + ", temperature=" + temperature + ", topP=" + topP + ", tools=" + tools + ", toolChoice=" + toolChoice + + ", user='" + user + '\'' + ", parallelToolCalls=" + parallelToolCalls + ", store=" + store + + ", metadata=" + metadata + ", reasoningEffort='" + reasoningEffort + '\'' + ", verbosity='" + + verbosity + '\'' + ", serviceTier='" + serviceTier + '\'' + ", toolCallbacks=" + toolCallbacks + + ", toolNames=" + toolNames + ", internalToolExecutionEnabled=" + internalToolExecutionEnabled + + ", httpHeaders=" + httpHeaders + ", toolContext=" + toolContext + '}'; } public static final class Builder { @@ -449,8 +442,6 @@ public Builder from(OpenAiOfficialChatOptions fromOptions) { this.options.setMaxTokens(fromOptions.getMaxTokens()); this.options.setMaxCompletionTokens(fromOptions.getMaxCompletionTokens()); this.options.setN(fromOptions.getN()); - this.options.setOutputModalities(fromOptions.getOutputModalities() != null - ? new ArrayList<>(fromOptions.getOutputModalities()) : null); this.options.setOutputAudio(fromOptions.getOutputAudio()); this.options.setPresencePenalty(fromOptions.getPresencePenalty()); this.options.setResponseFormat(fromOptions.getResponseFormat()); @@ -505,9 +496,6 @@ public Builder merge(OpenAiOfficialChatOptions from) { if (from.getN() != null) { this.options.setN(from.getN()); } - if (from.getOutputModalities() != null) { - this.options.setOutputModalities(new ArrayList<>(from.getOutputModalities())); - } if (from.getOutputAudio() != null) { this.options.setOutputAudio(from.getOutputAudio()); } @@ -636,11 +624,6 @@ public Builder N(Integer n) { return this; } - public Builder outputModalities(List modalities) { - this.options.setOutputModalities(modalities); - return this; - } - public Builder outputAudio(ChatCompletionAudioParam audio) { this.options.setOutputAudio(audio); return this; diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java index e0be64381e2..ae573f58ebf 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java @@ -19,7 +19,9 @@ import com.openai.azure.AzureOpenAIServiceVersion; import com.openai.azure.credential.AzureApiKeyCredential; import com.openai.client.OpenAIClient; +import com.openai.client.OpenAIClientAsync; import com.openai.client.okhttp.OpenAIOkHttpClient; +import com.openai.client.okhttp.OpenAIOkHttpClientAsync; import com.openai.credential.Credential; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -64,32 +66,64 @@ public static OpenAIClient setupSyncClient(String baseUrl, String apiKey, Creden boolean isAzure, boolean isGitHubModels, String modelName, Duration timeout, Integer maxRetries, Proxy proxy, Map customHeaders) { - if (apiKey == null && credential == null) { - var openAiKey = System.getenv("OPENAI_API_KEY"); - if (openAiKey != null) { - apiKey = openAiKey; - logger.debug("OpenAI API Key detected from environment variable OPENAI_API_KEY."); - } - var azureOpenAiKey = System.getenv("AZURE_OPENAI_KEY"); - if (azureOpenAiKey != null) { - apiKey = azureOpenAiKey; - logger.debug("Azure OpenAI Key detected from environment variable AZURE_OPENAI_KEY."); - } + baseUrl = detectBaseUrlFromEnv(baseUrl); + var modelHost = detectModelHost(isAzure, isGitHubModels, baseUrl, azureDeploymentName, + azureOpenAiServiceVersion); + if (timeout == null) { + timeout = DEFAULT_DURATION; } - if (baseUrl == null) { - var openAiBaseUrl = System.getenv("OPENAI_BASE_URL"); - if (openAiBaseUrl != null) { - baseUrl = openAiBaseUrl; - logger.debug("OpenAI Base URL detected from environment variable OPENAI_BASE_URL."); - } - var azureOpenAiBaseUrl = System.getenv("AZURE_OPENAI_BASE_URL"); - if (azureOpenAiBaseUrl != null) { - baseUrl = azureOpenAiBaseUrl; - logger.debug("Azure OpenAI Base URL detected from environment variable AZURE_OPENAI_BASE_URL."); - } + if (maxRetries == null) { + maxRetries = DEFAULT_MAX_RETRIES; + } + + OpenAIOkHttpClient.Builder builder = OpenAIOkHttpClient.builder(); + builder + .baseUrl(calculateBaseUrl(baseUrl, modelHost, modelName, azureDeploymentName, azureOpenAiServiceVersion)); + + Credential calculatedCredential = calculateCredential(modelHost, apiKey, credential); + String calculatedApiKey = calculateApiKey(modelHost, apiKey); + if (calculatedCredential == null && calculatedApiKey == null) { + throw new IllegalArgumentException("Either apiKey or credential must be set to authenticate"); + } + else if (calculatedCredential != null) { + builder.credential(calculatedCredential); + } + else { + builder.apiKey(calculatedApiKey); + } + builder.organization(organizationId); + + if (azureOpenAiServiceVersion != null) { + builder.azureServiceVersion(azureOpenAiServiceVersion); } - ModelHost modelHost = detectModelHost(isAzure, isGitHubModels, baseUrl, azureDeploymentName, + if (proxy != null) { + builder.proxy(proxy); + } + + builder.putHeader("User-Agent", DEFAULT_USER_AGENT); + if (customHeaders != null) { + builder.putAllHeaders(customHeaders.entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> Collections.singletonList(entry.getValue())))); + } + + builder.timeout(timeout); + builder.maxRetries(maxRetries); + return builder.build(); + } + + /** + * The asynchronous client setup is the same as the synchronous one in the OpenAI Java + * SDK, but uses a different client implementation. + */ + public static OpenAIClientAsync setupAsyncClient(String baseUrl, String apiKey, Credential credential, + String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAiServiceVersion, String organizationId, + boolean isAzure, boolean isGitHubModels, String modelName, Duration timeout, Integer maxRetries, + Proxy proxy, Map customHeaders) { + + baseUrl = detectBaseUrlFromEnv(baseUrl); + var modelHost = detectModelHost(isAzure, isGitHubModels, baseUrl, azureDeploymentName, azureOpenAiServiceVersion); if (timeout == null) { timeout = DEFAULT_DURATION; @@ -98,7 +132,7 @@ public static OpenAIClient setupSyncClient(String baseUrl, String apiKey, Creden maxRetries = DEFAULT_MAX_RETRIES; } - OpenAIOkHttpClient.Builder builder = OpenAIOkHttpClient.builder(); + OpenAIOkHttpClientAsync.Builder builder = OpenAIOkHttpClientAsync.builder(); builder .baseUrl(calculateBaseUrl(baseUrl, modelHost, modelName, azureDeploymentName, azureOpenAiServiceVersion)); @@ -135,8 +169,25 @@ else if (calculatedCredential != null) { return builder.build(); } + static String detectBaseUrlFromEnv(String baseUrl) { + if (baseUrl == null) { + var openAiBaseUrl = System.getenv("OPENAI_BASE_URL"); + if (openAiBaseUrl != null) { + baseUrl = openAiBaseUrl; + logger.debug("OpenAI Base URL detected from environment variable OPENAI_BASE_URL."); + } + var azureOpenAiBaseUrl = System.getenv("AZURE_OPENAI_BASE_URL"); + if (azureOpenAiBaseUrl != null) { + baseUrl = azureOpenAiBaseUrl; + logger.debug("Azure OpenAI Base URL detected from environment variable AZURE_OPENAI_BASE_URL."); + } + } + return baseUrl; + } + static ModelHost detectModelHost(boolean isAzure, boolean isGitHubModels, String baseUrl, String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAIServiceVersion) { + if (isAzure) { return ModelHost.AZURE_OPENAI; // Forced by the user } @@ -159,8 +210,9 @@ else if (baseUrl.startsWith(GITHUB_MODELS_URL)) { return ModelHost.OPENAI; } - static String calculateBaseUrl(final String baseUrl, ModelHost modelHost, String modelName, - String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAiServiceVersion) { + static String calculateBaseUrl(String baseUrl, ModelHost modelHost, String modelName, String azureDeploymentName, + AzureOpenAIServiceVersion azureOpenAiServiceVersion) { + if (modelHost == ModelHost.OPENAI) { if (baseUrl == null || baseUrl.isBlank()) { return OPENAI_URL; @@ -211,6 +263,18 @@ else if (modelHost == ModelHost.AZURE_OPENAI) { } static String calculateApiKey(ModelHost modelHost, String apiKey) { + if (apiKey == null) { + var openAiKey = System.getenv("OPENAI_API_KEY"); + if (openAiKey != null) { + apiKey = openAiKey; + logger.debug("OpenAI API Key detected from environment variable OPENAI_API_KEY."); + } + var azureOpenAiKey = System.getenv("AZURE_OPENAI_KEY"); + if (azureOpenAiKey != null) { + apiKey = azureOpenAiKey; + logger.debug("Azure OpenAI Key detected from environment variable AZURE_OPENAI_KEY."); + } + } if (modelHost != ModelHost.AZURE_OPENAI && apiKey != null) { return apiKey; } diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java index 7305f4d6903..203ab59d458 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java @@ -37,4 +37,9 @@ public OpenAiOfficialImageModel openAiImageModel() { return new OpenAiOfficialImageModel(); } + @Bean + public OpenAiOfficialChatModel openAiChatModel() { + return new OpenAiOfficialChatModel(); + } + } diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java new file mode 100644 index 00000000000..6e3af9a63b3 --- /dev/null +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java @@ -0,0 +1,127 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaiofficial.chat; + +import org.assertj.core.data.Percentage; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.ai.chat.client.ChatClient; +import org.springframework.ai.chat.memory.ChatMemory; +import org.springframework.ai.chat.memory.MessageWindowChatMemory; +import org.springframework.ai.chat.messages.AssistantMessage; +import org.springframework.ai.chat.messages.Message; +import org.springframework.ai.chat.messages.SystemMessage; +import org.springframework.ai.chat.messages.UserMessage; +import org.springframework.ai.chat.metadata.DefaultUsage; +import org.springframework.ai.chat.metadata.EmptyUsage; +import org.springframework.ai.chat.metadata.Usage; +import org.springframework.ai.chat.model.ChatResponse; +import org.springframework.ai.chat.model.Generation; +import org.springframework.ai.chat.prompt.ChatOptions; +import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.ai.chat.prompt.PromptTemplate; +import org.springframework.ai.chat.prompt.SystemPromptTemplate; +import org.springframework.ai.content.Media; +import org.springframework.ai.converter.BeanOutputConverter; +import org.springframework.ai.converter.ListOutputConverter; +import org.springframework.ai.converter.MapOutputConverter; +import org.springframework.ai.model.tool.DefaultToolCallingManager; +import org.springframework.ai.model.tool.ToolCallingChatOptions; +import org.springframework.ai.model.tool.ToolCallingManager; +import org.springframework.ai.model.tool.ToolExecutionResult; +import org.springframework.ai.openaiofficial.OpenAiOfficialChatModel; +import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfiguration; +import org.springframework.ai.support.ToolCallbacks; +import org.springframework.ai.tool.annotation.Tool; +import org.springframework.ai.tool.function.FunctionToolCallback; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.util.MimeTypeUtils; +import reactor.core.publisher.Flux; + +import java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** + * Integration tests for {@link OpenAiOfficialChatModel}. + * + * @author Julien Dubois + */ +@SpringBootTest(classes = OpenAiOfficialTestConfiguration.class) +@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") +public class OpenAiOfficialChatModelIT { + + private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatModelIT.class); + + @Value("classpath:/prompts/system-message.st") + private Resource systemResource; + + @Autowired + private OpenAiOfficialChatModel chatModel; + + @Test + void roleTest() { + UserMessage userMessage = new UserMessage( + "Tell me about 3 famous pirates from the Golden Age of Piracy and what they did."); + SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(this.systemResource); + Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", "Bob", "voice", "pirate")); + Prompt prompt = new Prompt(List.of(userMessage, systemMessage)); + ChatResponse response = this.chatModel.call(prompt); + assertThat(response.getResults()).hasSize(1); + assertThat(response.getResults().get(0).getOutput().getText()).contains("Blackbeard"); + // needs fine tuning... evaluateQuestionAndAnswer(request, response, false); + } + + @Test + void testMessageHistory() { + UserMessage userMessage = new UserMessage( + "Tell me about 3 famous pirates from the Golden Age of Piracy and why they did."); + SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(this.systemResource); + Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", "Bob", "voice", "pirate")); + Prompt prompt = new Prompt(List.of(userMessage, systemMessage)); + + ChatResponse response = this.chatModel.call(prompt); + assertThat(response.getResult().getOutput().getText()).containsAnyOf("Blackbeard", "Bartholomew"); + + var promptWithMessageHistory = new Prompt(List.of(new UserMessage("Dummy"), response.getResult().getOutput(), + new UserMessage("Repeat the last assistant message."))); + response = this.chatModel.call(promptWithMessageHistory); + + assertThat(response.getResult().getOutput().getText()).containsAnyOf("Blackbeard", "Bartholomew"); + } + +} diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelIT.java index fe848319c2b..6523a29c82a 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelIT.java @@ -35,7 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; /** - * Integration tests for observation instrumentation in {@link OpenAiOfficialImageModel}. + * Integration tests for {@link OpenAiOfficialImageModel}. * * @author Julien Dubois */ diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java index 7da96496375..50248975d31 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java @@ -38,7 +38,7 @@ import static org.springframework.ai.image.observation.ImageModelObservationDocumentation.LowCardinalityKeyNames; /** - * Integration tests for {@link OpenAiOfficialImageModel}. + * Integration tests for observation instrumentation in {@link OpenAiOfficialImageModel}. * * @author Julien Dubois */ diff --git a/models/spring-ai-openai-official/src/test/resources/prompts/system-message.st b/models/spring-ai-openai-official/src/test/resources/prompts/system-message.st new file mode 100644 index 00000000000..dd95164675f --- /dev/null +++ b/models/spring-ai-openai-official/src/test/resources/prompts/system-message.st @@ -0,0 +1,4 @@ +You are a helpful AI assistant. Your name is {name}. +You are an AI assistant that helps people find information. +Your name is {name} +You should reply to the user's request with your name and also in the style of a {voice}. \ No newline at end of file From eeba408efabbc82d2df6c51435293bbc7b868ccc Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Mon, 3 Nov 2025 16:25:36 +0100 Subject: [PATCH 06/49] Implementation of the OpenAI Java SDK - Improve authentication, mostly with Azure OpenAI Signed-off-by: Julien Dubois --- .../OpenAiOfficialChatModel.java | 1 - .../setup/OpenAiOfficialSetup.java | 88 ++++++++----------- .../chat/OpenAiOfficialChatModelIT.java | 39 -------- 3 files changed, 38 insertions(+), 90 deletions(-) diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java index 625c9f2f32b..8ec8c68c0b8 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -60,7 +60,6 @@ import org.springframework.ai.tool.definition.ToolDefinition; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; -import org.springframework.util.StringUtils; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.core.scheduler.Schedulers; diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java index ae573f58ebf..37d7011666a 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java @@ -17,7 +17,6 @@ package org.springframework.ai.openaiofficial.setup; import com.openai.azure.AzureOpenAIServiceVersion; -import com.openai.azure.credential.AzureApiKeyCredential; import com.openai.client.OpenAIClient; import com.openai.client.OpenAIClientAsync; import com.openai.client.okhttp.OpenAIOkHttpClient; @@ -45,6 +44,8 @@ public class OpenAiOfficialSetup { static final String OPENAI_URL = "https://api.openai.com/v1"; + static final String OPENAI_API_KEY = "OPENAI_API_KEY"; + static final String AZURE_OPENAI_KEY = "AZURE_OPENAI_KEY"; static final String GITHUB_MODELS_URL = "https://models.inference.ai.azure.com"; static final String GITHUB_TOKEN = "GITHUB_TOKEN"; static final String DEFAULT_USER_AGENT = "spring-ai-openai-official"; @@ -75,21 +76,23 @@ public static OpenAIClient setupSyncClient(String baseUrl, String apiKey, Creden if (maxRetries == null) { maxRetries = DEFAULT_MAX_RETRIES; } - OpenAIOkHttpClient.Builder builder = OpenAIOkHttpClient.builder(); builder .baseUrl(calculateBaseUrl(baseUrl, modelHost, modelName, azureDeploymentName, azureOpenAiServiceVersion)); - Credential calculatedCredential = calculateCredential(modelHost, apiKey, credential); - String calculatedApiKey = calculateApiKey(modelHost, apiKey); - if (calculatedCredential == null && calculatedApiKey == null) { - throw new IllegalArgumentException("Either apiKey or credential must be set to authenticate"); - } - else if (calculatedCredential != null) { - builder.credential(calculatedCredential); + String calculatedApiKey = apiKey != null ? apiKey : detectApiKey(modelHost); + if (calculatedApiKey != null) { + builder.apiKey(calculatedApiKey); } else { - builder.apiKey(calculatedApiKey); + if (credential != null) { + builder.credential(credential); + } + else if (modelHost == ModelHost.AZURE_OPENAI) { + // If no API key is provided for Azure OpenAI, we try to use passwordless + // authentication + builder.credential(azureAuthentication()); + } } builder.organization(organizationId); @@ -131,21 +134,23 @@ public static OpenAIClientAsync setupAsyncClient(String baseUrl, String apiKey, if (maxRetries == null) { maxRetries = DEFAULT_MAX_RETRIES; } - OpenAIOkHttpClientAsync.Builder builder = OpenAIOkHttpClientAsync.builder(); builder .baseUrl(calculateBaseUrl(baseUrl, modelHost, modelName, azureDeploymentName, azureOpenAiServiceVersion)); - Credential calculatedCredential = calculateCredential(modelHost, apiKey, credential); - String calculatedApiKey = calculateApiKey(modelHost, apiKey); - if (calculatedCredential == null && calculatedApiKey == null) { - throw new IllegalArgumentException("Either apiKey or credential must be set to authenticate"); - } - else if (calculatedCredential != null) { - builder.credential(calculatedCredential); + String calculatedApiKey = apiKey != null ? apiKey : detectApiKey(modelHost); + if (calculatedApiKey != null) { + builder.apiKey(calculatedApiKey); } else { - builder.apiKey(calculatedApiKey); + if (credential != null) { + builder.credential(credential); + } + else if (modelHost == ModelHost.AZURE_OPENAI) { + // If no API key is provided for Azure OpenAI, we try to use passwordless + // authentication + builder.credential(azureAuthentication()); + } } builder.organization(organizationId); @@ -241,42 +246,25 @@ else if (modelHost == ModelHost.AZURE_OPENAI) { } } - static Credential calculateCredential(ModelHost modelHost, String apiKey, Credential credential) { - if (apiKey != null) { - if (modelHost == ModelHost.AZURE_OPENAI) { - return AzureApiKeyCredential.create(apiKey); - } - } - else if (credential != null) { - return credential; + static Credential azureAuthentication() { + try { + return AzureInternalOpenAiOfficialHelper.getAzureCredential(); } - else if (modelHost == ModelHost.AZURE_OPENAI) { - try { - return AzureInternalOpenAiOfficialHelper.getAzureCredential(); - } - catch (NoClassDefFoundError e) { - throw new IllegalArgumentException("Azure OpenAI was detected, but no credential was provided. " - + "If you want to use passwordless authentication, you need to add the Azure Identity library (groupId=`com.azure`, artifactId=`azure-identity`) to your classpath."); - } + catch (NoClassDefFoundError e) { + throw new IllegalArgumentException("Azure OpenAI was detected, but no credential was provided. " + + "If you want to use passwordless authentication, you need to add the Azure Identity library (groupId=`com.azure`, artifactId=`azure-identity`) to your classpath."); } - return null; } - static String calculateApiKey(ModelHost modelHost, String apiKey) { - if (apiKey == null) { - var openAiKey = System.getenv("OPENAI_API_KEY"); - if (openAiKey != null) { - apiKey = openAiKey; - logger.debug("OpenAI API Key detected from environment variable OPENAI_API_KEY."); - } - var azureOpenAiKey = System.getenv("AZURE_OPENAI_KEY"); - if (azureOpenAiKey != null) { - apiKey = azureOpenAiKey; - logger.debug("Azure OpenAI Key detected from environment variable AZURE_OPENAI_KEY."); - } + static String detectApiKey(ModelHost modelHost) { + if (modelHost == ModelHost.OPENAI && System.getenv(OPENAI_API_KEY) != null) { + return System.getenv(OPENAI_API_KEY); + } + else if (modelHost == ModelHost.AZURE_OPENAI && System.getenv(AZURE_OPENAI_KEY) != null) { + return System.getenv(AZURE_OPENAI_KEY); } - if (modelHost != ModelHost.AZURE_OPENAI && apiKey != null) { - return apiKey; + else if (modelHost == ModelHost.AZURE_OPENAI && System.getenv(OPENAI_API_KEY) != null) { + return System.getenv(OPENAI_API_KEY); } else if (modelHost == ModelHost.GITHUB_MODELS && System.getenv(GITHUB_TOKEN) != null) { return System.getenv(GITHUB_TOKEN); diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java index 6e3af9a63b3..6b8534ba6ae 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java @@ -16,65 +16,26 @@ package org.springframework.ai.openaiofficial.chat; -import org.assertj.core.data.Percentage; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.ai.chat.client.ChatClient; -import org.springframework.ai.chat.memory.ChatMemory; -import org.springframework.ai.chat.memory.MessageWindowChatMemory; -import org.springframework.ai.chat.messages.AssistantMessage; import org.springframework.ai.chat.messages.Message; -import org.springframework.ai.chat.messages.SystemMessage; import org.springframework.ai.chat.messages.UserMessage; -import org.springframework.ai.chat.metadata.DefaultUsage; -import org.springframework.ai.chat.metadata.EmptyUsage; -import org.springframework.ai.chat.metadata.Usage; import org.springframework.ai.chat.model.ChatResponse; -import org.springframework.ai.chat.model.Generation; -import org.springframework.ai.chat.prompt.ChatOptions; import org.springframework.ai.chat.prompt.Prompt; -import org.springframework.ai.chat.prompt.PromptTemplate; import org.springframework.ai.chat.prompt.SystemPromptTemplate; -import org.springframework.ai.content.Media; -import org.springframework.ai.converter.BeanOutputConverter; -import org.springframework.ai.converter.ListOutputConverter; -import org.springframework.ai.converter.MapOutputConverter; -import org.springframework.ai.model.tool.DefaultToolCallingManager; -import org.springframework.ai.model.tool.ToolCallingChatOptions; -import org.springframework.ai.model.tool.ToolCallingManager; -import org.springframework.ai.model.tool.ToolExecutionResult; import org.springframework.ai.openaiofficial.OpenAiOfficialChatModel; import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfiguration; -import org.springframework.ai.support.ToolCallbacks; -import org.springframework.ai.tool.annotation.Tool; -import org.springframework.ai.tool.function.FunctionToolCallback; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.core.convert.support.DefaultConversionService; -import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; -import org.springframework.util.MimeTypeUtils; -import reactor.core.publisher.Flux; -import java.io.IOException; -import java.net.URI; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.UUID; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; -import java.util.stream.IntStream; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * Integration tests for {@link OpenAiOfficialChatModel}. From 68df074a66cd7ffc6879568e0451bdf4599ac808 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Mon, 3 Nov 2025 17:54:30 +0100 Subject: [PATCH 07/49] Implementation of the OpenAI Java SDK - First tests passing for ChatModel Signed-off-by: Julien Dubois --- .../ai/openaiofficial/OpenAiOfficialChatModel.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java index 8ec8c68c0b8..5c60c30f8dd 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -388,7 +388,7 @@ private Generation buildGeneration(ChatCompletion.Choice choice, Map { return new AssistantMessage.ToolCall(toolCall.function().get().id(), "function", toolCall.function().get().function().name(), toolCall.function().get().function().arguments()); - }).toList()).get(); + }).toList()).orElse(List.of()); var generationMetadataBuilder = ChatGenerationMetadata.builder() .finishReason(choice.finishReason().value().name()); @@ -566,8 +566,7 @@ else if (mimeType.startsWith("audio/")) { else { builder.role(JsonValue.from(MessageType.SYSTEM)); } - Object refusal = message.getMetadata().get("refusal"); - builder.refusal(refusal != null ? JsonValue.from(refusal.toString()) : JsonValue.from("")); + builder.refusal(JsonValue.from(Optional.ofNullable(message.getMetadata().get("refusal")).map(Object::toString).orElse(""))); return List.of(builder.build()); } else if (message.getMessageType() == MessageType.ASSISTANT) { @@ -576,8 +575,12 @@ else if (message.getMessageType() == MessageType.ASSISTANT) { .role(JsonValue.from(MessageType.ASSISTANT)); if (assistantMessage.getText() != null) { - builder.content(ChatCompletionMessage.builder().content(assistantMessage.getText()).build().content()); + builder.content(ChatCompletionMessage.builder() + .content(assistantMessage.getText()) + .refusal(JsonValue.from(Optional.ofNullable(message.getMetadata().get("refusal")).map(Object::toString).orElse(""))) + .build().content()); } + builder.refusal(JsonValue.from(Optional.ofNullable(message.getMetadata().get("refusal")).map(Object::toString).orElse(""))); if (!CollectionUtils.isEmpty(assistantMessage.getToolCalls())) { List toolCalls = assistantMessage.getToolCalls() @@ -604,6 +607,7 @@ else if (message.getMessageType() == MessageType.TOOL) { .map(toolResponse -> ChatCompletionMessage.builder() .role(JsonValue.from(MessageType.TOOL)) .content(ChatCompletionMessage.builder().content(toolResponse.responseData()).build().content()) + .refusal(JsonValue.from(Optional.ofNullable(message.getMetadata().get("refusal")).map(Object::toString).orElse(""))) .build()) .toList(); } From 4653f05066af02e81bda26e121647817585d91d7 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Tue, 4 Nov 2025 16:51:21 +0100 Subject: [PATCH 08/49] Implementation of the OpenAI Java SDK - More tests passing for ChatModel Signed-off-by: Julien Dubois --- .../OpenAiOfficialChatModel.java | 221 ++++--- .../OpenAiOfficialChatOptions.java | 47 +- .../ai/openaiofficial/chat/ActorsFilms.java | 51 ++ .../chat/MockWeatherService.java | 92 +++ .../chat/OpenAiOfficialChatModelIT.java | 551 ++++++++++++++++++ .../src/test/resources/test.png | Bin 0 -> 167772 bytes 6 files changed, 863 insertions(+), 99 deletions(-) create mode 100644 models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/ActorsFilms.java create mode 100644 models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/MockWeatherService.java create mode 100644 models/spring-ai-openai-official/src/test/resources/test.png diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java index 5c60c30f8dd..d4b627d714b 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -19,6 +19,7 @@ import com.openai.client.OpenAIClient; import com.openai.client.OpenAIClientAsync; import com.openai.core.JsonArray; +import com.openai.core.JsonField; import com.openai.core.JsonValue; import com.openai.models.FunctionDefinition; import com.openai.models.FunctionParameters; @@ -61,7 +62,6 @@ import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; import reactor.core.scheduler.Schedulers; import java.util.ArrayList; @@ -92,9 +92,9 @@ public class OpenAiOfficialChatModel implements ChatModel { private final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatModel.class); - private final OpenAIClient openAiClient; + private OpenAIClient openAiClient; - private final OpenAIClientAsync openAiClientAsync; + private OpenAIClientAsync openAiClientAsync; private final OpenAiOfficialChatOptions options; @@ -154,6 +154,7 @@ public OpenAiOfficialChatModel(OpenAIClient openAiClient, OpenAIClientAsync open this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), this.options.getProxy(), this.options.getCustomHeaders())); + this.openAiClientAsync = Objects.requireNonNullElseGet(openAiClientAsync, () -> setupAsyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), this.options.getAzureDeploymentName(), @@ -161,6 +162,7 @@ public OpenAiOfficialChatModel(OpenAIClient openAiClient, OpenAIClientAsync open this.options.isAzure(), this.options.isGitHubModels(), this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), this.options.getProxy(), this.options.getCustomHeaders())); + this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); this.toolCallingManager = Objects.requireNonNullElse(toolCallingManager, DEFAULT_TOOL_CALLING_MANAGER); this.toolExecutionEligibilityPredicate = Objects.requireNonNullElse(toolExecutionEligibilityPredicate, @@ -173,8 +175,10 @@ public OpenAiOfficialChatOptions getOptions() { @Override public ChatResponse call(Prompt prompt) { - // Before moving any further, build the final request Prompt, - // merging runtime and default options. + if (this.openAiClient == null) { + throw new IllegalStateException( + "OpenAI sync client is not configured. Have you set the 'streamUsage' option to false?"); + } Prompt requestPrompt = buildRequestPrompt(prompt); return this.internalCall(requestPrompt, null); } @@ -248,8 +252,10 @@ public ChatResponse internalCall(Prompt prompt, ChatResponse previousChatRespons @Override public Flux stream(Prompt prompt) { - // Before moving any further, build the final request Prompt, - // merging runtime and default options. + if (this.openAiClientAsync == null) { + throw new IllegalStateException( + "OpenAI async client is not configured. Streaming is not supported with the current configuration. Have you set the 'streamUsage' option to true?"); + } Prompt requestPrompt = buildRequestPrompt(prompt); return internalStream(requestPrompt, null); } @@ -273,72 +279,68 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha observation.parentObservation(contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null)).start(); - Flux chatResponse = Flux.empty(); - // Convert the ChatCompletionChunk into a ChatCompletion to be able to reuse - // the function call handling logic. - this.openAiClientAsync.chat().completions().createStreaming(request).subscribe(chunk -> { - ChatCompletion chatCompletion = chunkToChatCompletion(chunk); - Mono.just(chatCompletion).map(chatCompletion2 -> { + Flux chatResponses = Flux.create(sink -> { + this.openAiClientAsync.chat().completions().createStreaming(request).subscribe(chunk -> { try { + ChatCompletion chatCompletion = chunkToChatCompletion(chunk); // If an id is not provided, set to "NO_ID" (for compatible APIs). - chatCompletion2.id(); - String id = chatCompletion2.id(); - - List generations = chatCompletion2.choices().stream().map(choice -> { // @formatter:off - roleMap.putIfAbsent(id, choice.message()._role().asString().isPresent() ? choice.message()._role().asStringOrThrow() : ""); - Map metadata = Map.of( - "id", id, - "role", roleMap.getOrDefault(id, ""), - "index", choice.index(), - "finishReason", choice.finishReason().asString(), - "refusal", choice.message().refusal().isPresent() ? choice.message().refusal() : "", - "annotations", choice.message().annotations().isPresent() ? choice.message().annotations() : List.of()); - return buildGeneration(choice, metadata); - }).toList(); - - Optional usage = chatCompletion2.usage(); - Usage currentChatResponseUsage = usage.isPresent()? getDefaultUsage(usage.get()) : new EmptyUsage(); - Usage accumulatedUsage = UsageCalculator.getCumulativeUsage(currentChatResponseUsage, - previousChatResponse); - return new ChatResponse(generations, from(chatCompletion2, accumulatedUsage)); - } - catch (Exception e) { - logger.error("Error processing chat completion", e); - return new ChatResponse(List.of()); - } - }) - .flux() - .buffer(2, 1) - .map(bufferList -> { - ChatResponse firstResponse = bufferList.get(0); - if (request.streamOptions().isPresent()) { - if (bufferList.size() == 2) { - ChatResponse secondResponse = bufferList.get(1); - if (secondResponse!=null) { - // This is the usage from the final Chat response for a - // given Chat request. - Usage usage = secondResponse.getMetadata().getUsage(); - if (!UsageCalculator.isEmpty(usage)) { - // Store the usage from the final response to the - // penultimate response for accumulation. - return new ChatResponse(firstResponse.getResults(), - from(firstResponse.getMetadata(), usage)); - } + chatCompletion.id(); + String id = chatCompletion.id(); + + List generations = chatCompletion.choices().stream().map(choice -> { // @formatter:off + roleMap.putIfAbsent(id, choice.message()._role().asString().isPresent() ? choice.message()._role().asStringOrThrow() : ""); + Map metadata = Map.of( + "id", id, + "role", roleMap.getOrDefault(id, ""), + "index", choice.index(), + "finishReason", choice.finishReason().asString(), + "refusal", choice.message().refusal().isPresent() ? choice.message().refusal() : "", + "annotations", choice.message().annotations().isPresent() ? choice.message().annotations() : List.of()); + return buildGeneration(choice, metadata); + }).toList(); + + Optional usage = chatCompletion.usage(); + Usage currentChatResponseUsage = usage.isPresent()? getDefaultUsage(usage.get()) : new EmptyUsage(); + Usage accumulatedUsage = UsageCalculator.getCumulativeUsage(currentChatResponseUsage, + previousChatResponse); + ChatResponse response = new ChatResponse(generations, from(chatCompletion, accumulatedUsage)); + sink.next(response); + } + catch (Exception e) { + logger.error("Error processing chat completion", e); + sink.error(e); + } + }).onCompleteFuture().whenComplete((unused, throwable) -> { + if (throwable != null) { + sink.error(throwable); + } else { + sink.complete(); + } + }); + }) + .buffer(2, 1) + .map(bufferList -> { + ChatResponse firstResponse = (ChatResponse) bufferList.get(0); + if (request.streamOptions().isPresent()) { + if (bufferList.size() == 2) { + ChatResponse secondResponse = (ChatResponse) bufferList.get(1); + if (secondResponse!=null) { + // This is the usage from the final Chat response for a + // given Chat request. + Usage usage = secondResponse.getMetadata().getUsage(); + if (!UsageCalculator.isEmpty(usage)) { + // Store the usage from the final response to the + // penultimate response for accumulation. + return new ChatResponse(firstResponse.getResults(), + from(firstResponse.getMetadata(), usage)); } } } - return firstResponse; - }); - }) - .onCompleteFuture() - .whenComplete((unused, error) -> { - if (error != null) { - logger.error(error.getMessage(), error); - throw new RuntimeException(error); - } + } + return firstResponse; }); - Flux flux = chatResponse.flatMap(response -> { + Flux flux = chatResponses.flatMap(response -> { assert prompt.getOptions() != null; if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(prompt.getOptions(), response)) { // FIXME: bounded elastic needs to be used since tool calling @@ -432,11 +434,32 @@ private ChatResponseMetadata from(ChatResponseMetadata chatResponseMetadata, Usa private ChatCompletion chunkToChatCompletion(ChatCompletionChunk chunk) { List choices = chunk.choices() .stream() - .map(chunkChoice -> ChatCompletion.Choice.builder() + .map(chunkChoice -> { + ChatCompletion.Choice.Builder choiceBuilder = ChatCompletion.Choice.builder() .finishReason(ChatCompletion.Choice.FinishReason.of(chunkChoice.finishReason().toString())) - .index(chunkChoice.index()) - .message(ChatCompletionMessage.builder().content(chunkChoice.delta().content()).build()) - .build()) + .index(chunkChoice.index()) + .message(ChatCompletionMessage.builder() + .content(chunkChoice.delta().content()) + .refusal(chunkChoice.delta().refusal()) + .build()); + + // Handle optional logprobs + if (chunkChoice.logprobs().isPresent()) { + var logprobs = chunkChoice.logprobs().get(); + choiceBuilder.logprobs(ChatCompletion.Choice.Logprobs.builder() + .content(logprobs.content()) + .refusal(logprobs.refusal()) + .build()); + } else { + // Provide empty logprobs when not present + choiceBuilder.logprobs(ChatCompletion.Choice.Logprobs.builder() + .content(List.of()) + .refusal(List.of()) + .build()); + } + + return choiceBuilder.build(); + }) .toList(); return ChatCompletion.builder() @@ -444,7 +467,7 @@ private ChatCompletion chunkToChatCompletion(ChatCompletionChunk chunk) { .choices(choices) .created(chunk.created()) .model(chunk.model()) - .usage(Objects.requireNonNull(chunk.usage().orElse(null))) + .usage(chunk.usage().orElse(CompletionUsage.builder().promptTokens(0).completionTokens(0).totalTokens(0).build())) .build(); } @@ -606,7 +629,7 @@ else if (message.getMessageType() == MessageType.TOOL) { .stream() .map(toolResponse -> ChatCompletionMessage.builder() .role(JsonValue.from(MessageType.TOOL)) - .content(ChatCompletionMessage.builder().content(toolResponse.responseData()).build().content()) + .content(ChatCompletionMessage.builder().content(toolResponse.responseData()).refusal(Optional.ofNullable(message.getMetadata().get("refusal")).map(Object::toString).orElse("")).build().content()) .refusal(JsonValue.from(Optional.ofNullable(message.getMetadata().get("refusal")).map(Object::toString).orElse(""))) .build()) .toList(); @@ -710,7 +733,12 @@ else if (requestOptions.getModel() != null) { streamOptionsBuilder.includeObfuscation(requestOptions.getStreamOptions().includeObfuscation().get()); } streamOptionsBuilder.additionalProperties(requestOptions.getStreamOptions()._additionalProperties()); + streamOptionsBuilder.includeUsage(requestOptions.getStreamUsage()); builder.streamOptions(streamOptionsBuilder.build()); + } else { + builder.streamOptions(ChatCompletionStreamOptions.builder() + .includeUsage(true) // Include usage by default for streaming + .build()); } } @@ -752,22 +780,39 @@ else if (mediaContentData instanceof String text) { private List getChatCompletionTools(List toolDefinitions) { return toolDefinitions.stream() - .map(toolDefinition -> { - FunctionParameters.Builder parametersBuilder = FunctionParameters.builder(); - parametersBuilder.putAdditionalProperty("type", JsonValue.from("object")); - if (!toolDefinition.inputSchema().isEmpty()) { - parametersBuilder.putAdditionalProperty("strict", JsonValue.from(true)); // TODO allow to have non-strict schemas - parametersBuilder.putAdditionalProperty("json_schema", JsonValue.from(toolDefinition.inputSchema())); - } - FunctionDefinition functionDefinition = FunctionDefinition.builder() - .name(toolDefinition.name()) - .description(toolDefinition.description()) - .parameters(parametersBuilder.build()) - .build(); - - return ChatCompletionTool.ofFunction(ChatCompletionFunctionTool.builder().function(functionDefinition).build()); - } ) - .toList(); + .map(toolDefinition -> { + FunctionParameters.Builder parametersBuilder = FunctionParameters.builder(); + + if (!toolDefinition.inputSchema().isEmpty()) { + // Parse the schema and add its properties directly + try { + com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); + @SuppressWarnings("unchecked") + Map schemaMap = mapper.readValue(toolDefinition.inputSchema(), Map.class); + + // Add each property from the schema to the parameters + schemaMap.forEach((key, value) -> + parametersBuilder.putAdditionalProperty(key, JsonValue.from(value)) + ); + + // Add strict mode + parametersBuilder.putAdditionalProperty("strict", JsonValue.from(true)); // TODO allow non-strict mode + } catch (Exception e) { + logger.error("Failed to parse tool schema", e); + } + } + + FunctionDefinition functionDefinition = FunctionDefinition.builder() + .name(toolDefinition.name()) + .description(toolDefinition.description()) + .parameters(parametersBuilder.build()) + .build(); + + return ChatCompletionTool.ofFunction( + ChatCompletionFunctionTool.builder().function(functionDefinition).build() + ); + }) + .toList(); } @Override diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java index 1c8bf7330d9..6e44495558a 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java @@ -72,6 +72,8 @@ public class OpenAiOfficialChatOptions extends AbstractOpenAiOfficialOptions imp private ResponseCreateParams.StreamOptions streamOptions; + private Boolean streamUsage; + private Integer seed; private List stop; @@ -199,6 +201,14 @@ public void setStreamOptions(ResponseCreateParams.StreamOptions streamOptions) { this.streamOptions = streamOptions; } + public Boolean getStreamUsage() { + return this.streamUsage; + } + + public void setStreamUsage(Boolean streamUsage) { + this.streamUsage = streamUsage; + } + public Integer getSeed() { return this.seed; } @@ -393,7 +403,8 @@ public boolean equals(Object o) { && Objects.equals(n, options.n) && Objects.equals(outputAudio, options.outputAudio) && Objects.equals(presencePenalty, options.presencePenalty) && Objects.equals(responseFormat, options.responseFormat) - && Objects.equals(streamOptions, options.streamOptions) && Objects.equals(seed, options.seed) + && Objects.equals(streamOptions, options.streamOptions) + && Objects.equals(streamUsage, options.streamUsage) && Objects.equals(seed, options.seed) && Objects.equals(stop, options.stop) && Objects.equals(temperature, options.temperature) && Objects.equals(topP, options.topP) && Objects.equals(tools, options.tools) && Objects.equals(toolChoice, options.toolChoice) && Objects.equals(user, options.user) @@ -409,9 +420,9 @@ public boolean equals(Object o) { @Override public int hashCode() { return Objects.hash(frequencyPenalty, logitBias, logprobs, topLogprobs, maxTokens, n, outputAudio, - presencePenalty, responseFormat, streamOptions, seed, stop, temperature, topP, tools, toolChoice, user, - parallelToolCalls, store, metadata, reasoningEffort, verbosity, serviceTier, toolCallbacks, toolNames, - internalToolExecutionEnabled, httpHeaders, toolContext); + presencePenalty, responseFormat, streamOptions, streamUsage, seed, stop, temperature, topP, tools, + toolChoice, user, parallelToolCalls, store, metadata, reasoningEffort, verbosity, serviceTier, + toolCallbacks, toolNames, internalToolExecutionEnabled, httpHeaders, toolContext); } @Override @@ -419,13 +430,13 @@ public String toString() { return "OpenAiOfficialChatOptions{" + "frequencyPenalty=" + frequencyPenalty + ", logitBias=" + logitBias + ", logprobs=" + logprobs + ", topLogprobs=" + topLogprobs + ", maxTokens=" + maxTokens + ", n=" + n + ", outputAudio=" + outputAudio + ", presencePenalty=" + presencePenalty + ", responseFormat=" - + responseFormat + ", streamOptions=" + streamOptions + ", seed=" + seed + ", stop=" + stop - + ", temperature=" + temperature + ", topP=" + topP + ", tools=" + tools + ", toolChoice=" + toolChoice - + ", user='" + user + '\'' + ", parallelToolCalls=" + parallelToolCalls + ", store=" + store - + ", metadata=" + metadata + ", reasoningEffort='" + reasoningEffort + '\'' + ", verbosity='" - + verbosity + '\'' + ", serviceTier='" + serviceTier + '\'' + ", toolCallbacks=" + toolCallbacks - + ", toolNames=" + toolNames + ", internalToolExecutionEnabled=" + internalToolExecutionEnabled - + ", httpHeaders=" + httpHeaders + ", toolContext=" + toolContext + '}'; + + responseFormat + ", streamOptions=" + streamOptions + ", streamUsage=" + streamUsage + ", seed=" + + seed + ", stop=" + stop + ", temperature=" + temperature + ", topP=" + topP + ", tools=" + tools + + ", toolChoice=" + toolChoice + ", user='" + user + '\'' + ", parallelToolCalls=" + parallelToolCalls + + ", store=" + store + ", metadata=" + metadata + ", reasoningEffort='" + reasoningEffort + '\'' + + ", verbosity='" + verbosity + '\'' + ", serviceTier='" + serviceTier + '\'' + ", toolCallbacks=" + + toolCallbacks + ", toolNames=" + toolNames + ", internalToolExecutionEnabled=" + + internalToolExecutionEnabled + ", httpHeaders=" + httpHeaders + ", toolContext=" + toolContext + '}'; } public static final class Builder { @@ -446,6 +457,7 @@ public Builder from(OpenAiOfficialChatOptions fromOptions) { this.options.setPresencePenalty(fromOptions.getPresencePenalty()); this.options.setResponseFormat(fromOptions.getResponseFormat()); this.options.setStreamOptions(fromOptions.getStreamOptions()); + this.options.setStreamUsage(fromOptions.getStreamUsage()); this.options.setSeed(fromOptions.getSeed()); this.options.setStop(fromOptions.getStop() != null ? new ArrayList<>(fromOptions.getStop()) : null); this.options.setTemperature(fromOptions.getTemperature()); @@ -508,6 +520,9 @@ public Builder merge(OpenAiOfficialChatOptions from) { if (from.getStreamOptions() != null) { this.options.setStreamOptions(from.getStreamOptions()); } + if (from.getStreamUsage() != null) { + this.options.setStreamUsage(from.getStreamUsage()); + } if (from.getSeed() != null) { this.options.setSeed(from.getSeed()); } @@ -639,6 +654,16 @@ public Builder responseFormat(ResponseFormatJsonSchema responseFormat) { return this; } + public Builder streamOptions(ResponseCreateParams.StreamOptions streamOptions) { + this.options.setStreamOptions(streamOptions); + return this; + } + + public Builder streamUsage(Boolean streamUsage) { + this.options.setStreamUsage(streamUsage); + return this; + } + public Builder seed(Integer seed) { this.options.setSeed(seed); return this; diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/ActorsFilms.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/ActorsFilms.java new file mode 100644 index 00000000000..453070320ce --- /dev/null +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/ActorsFilms.java @@ -0,0 +1,51 @@ +/* + * Copyright 2023-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaiofficial.chat; + +import java.util.List; + +public class ActorsFilms { + + private String actor; + + private List movies; + + public ActorsFilms() { + } + + public String getActor() { + return this.actor; + } + + public void setActor(String actor) { + this.actor = actor; + } + + public List getMovies() { + return this.movies; + } + + public void setMovies(List movies) { + this.movies = movies; + } + + @Override + public String toString() { + return "ActorsFilms{" + "actor='" + this.actor + '\'' + ", movies=" + this.movies + '}'; + } + +} diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/MockWeatherService.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/MockWeatherService.java new file mode 100644 index 00000000000..af52c23edf0 --- /dev/null +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/MockWeatherService.java @@ -0,0 +1,92 @@ +/* + * Copyright 2023-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaiofficial.chat; + +import com.fasterxml.jackson.annotation.JsonClassDescription; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyDescription; + +import java.util.function.Function; + +public class MockWeatherService implements Function { + + @Override + public Response apply(Request request) { + + double temperature = 0; + if (request.location().contains("Paris")) { + temperature = 15; + } + else if (request.location().contains("Tokyo")) { + temperature = 10; + } + else if (request.location().contains("San Francisco")) { + temperature = 30; + } + + return new Response(temperature, 15, 20, 2, 53, 45, Unit.C); + } + + /** + * Temperature units. + */ + public enum Unit { + + /** + * Celsius. + */ + C("metric"), + /** + * Fahrenheit. + */ + F("imperial"); + + /** + * Human readable unit name. + */ + public final String unitName; + + Unit(String text) { + this.unitName = text; + } + + } + + /** + * Weather Function request. + */ + @JsonInclude(Include.NON_NULL) + @JsonClassDescription("Weather API request") + public record Request(@JsonProperty(required = true, + value = "location") @JsonPropertyDescription("The city and state e.g. San Francisco, CA") String location, + @JsonProperty(required = true, value = "lat") @JsonPropertyDescription("The city latitude") double lat, + @JsonProperty(required = true, value = "lon") @JsonPropertyDescription("The city longitude") double lon, + @JsonProperty(required = true, value = "unit") @JsonPropertyDescription("Temperature unit") Unit unit) { + + } + + /** + * Weather Function response. + */ + public record Response(double temp, double feels_like, double temp_min, double temp_max, int pressure, int humidity, + Unit unit) { + + } + +} diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java index 6b8534ba6ae..702bc3f7175 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java @@ -16,24 +16,63 @@ package org.springframework.ai.openaiofficial.chat; +import org.assertj.core.data.Percentage; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.ai.chat.client.ChatClient; +import org.springframework.ai.chat.memory.ChatMemory; +import org.springframework.ai.chat.memory.MessageWindowChatMemory; +import org.springframework.ai.chat.messages.AssistantMessage; import org.springframework.ai.chat.messages.Message; +import org.springframework.ai.chat.messages.SystemMessage; import org.springframework.ai.chat.messages.UserMessage; +import org.springframework.ai.chat.metadata.DefaultUsage; +import org.springframework.ai.chat.metadata.EmptyUsage; +import org.springframework.ai.chat.metadata.Usage; import org.springframework.ai.chat.model.ChatResponse; +import org.springframework.ai.chat.model.Generation; +import org.springframework.ai.chat.prompt.ChatOptions; import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.ai.chat.prompt.PromptTemplate; import org.springframework.ai.chat.prompt.SystemPromptTemplate; +import org.springframework.ai.content.Media; +import org.springframework.ai.converter.BeanOutputConverter; +import org.springframework.ai.converter.ListOutputConverter; +import org.springframework.ai.converter.MapOutputConverter; +import org.springframework.ai.model.tool.DefaultToolCallingManager; +import org.springframework.ai.model.tool.ToolCallingChatOptions; +import org.springframework.ai.model.tool.ToolCallingManager; +import org.springframework.ai.model.tool.ToolExecutionResult; import org.springframework.ai.openaiofficial.OpenAiOfficialChatModel; +import org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions; import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfiguration; +import org.springframework.ai.support.ToolCallbacks; +import org.springframework.ai.tool.annotation.Tool; +import org.springframework.ai.tool.function.FunctionToolCallback; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; +import org.springframework.util.MimeTypeUtils; +import reactor.core.publisher.Flux; +import java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.UUID; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import static org.assertj.core.api.Assertions.assertThat; @@ -85,4 +124,516 @@ void testMessageHistory() { assertThat(response.getResult().getOutput().getText()).containsAnyOf("Blackbeard", "Bartholomew"); } + @Test + void streamCompletenessTest() throws InterruptedException { + UserMessage userMessage = new UserMessage( + "List ALL natural numbers in range [1, 1000]. Make sure to not omit any."); + Prompt prompt = new Prompt(List.of(userMessage)); + + StringBuilder answer = new StringBuilder(); + CountDownLatch latch = new CountDownLatch(1); + + Flux chatResponseFlux = this.chatModel.stream(prompt).doOnNext(chatResponse -> { + String responseContent = chatResponse.getResults().get(0).getOutput().getText(); + answer.append(responseContent); + }).doOnComplete(() -> { + logger.info(answer.toString()); + latch.countDown(); + }); + chatResponseFlux.subscribe(); + assertThat(latch.await(120, TimeUnit.SECONDS)).isTrue(); + IntStream.rangeClosed(1, 1000).forEach(n -> assertThat(answer).contains(String.valueOf(n))); + } + + @Test + void streamCompletenessTestWithChatResponse() throws InterruptedException { + UserMessage userMessage = new UserMessage("Who is George Washington? - use first as 1st"); + Prompt prompt = new Prompt(List.of(userMessage)); + + StringBuilder answer = new StringBuilder(); + CountDownLatch latch = new CountDownLatch(1); + + ChatClient chatClient = ChatClient.builder(this.chatModel).build(); + + Flux chatResponseFlux = chatClient.prompt(prompt) + .stream() + .chatResponse() + .doOnNext(chatResponse -> { + if (!chatResponse.getResults().isEmpty()) { + String responseContent = chatResponse.getResults().get(0).getOutput().getText(); + answer.append(responseContent); + } + }) + .doOnComplete(() -> { + logger.info(answer.toString()); + latch.countDown(); + }); + chatResponseFlux.subscribe(); + assertThat(latch.await(120, TimeUnit.SECONDS)).isTrue(); + assertThat(answer).contains("1st "); + } + + @Test + void ensureChatResponseAsContentDoesNotSwallowBlankSpace() throws InterruptedException { + UserMessage userMessage = new UserMessage("Who is George Washington? - use first as 1st"); + Prompt prompt = new Prompt(List.of(userMessage)); + + StringBuilder answer = new StringBuilder(); + CountDownLatch latch = new CountDownLatch(1); + + ChatClient chatClient = ChatClient.builder(this.chatModel).build(); + + Flux chatResponseFlux = chatClient.prompt(prompt) + .stream() + .content() + .doOnNext(answer::append) + .doOnComplete(() -> { + logger.info(answer.toString()); + latch.countDown(); + }); + chatResponseFlux.subscribe(); + assertThat(latch.await(120, TimeUnit.SECONDS)).isTrue(); + assertThat(answer).contains("1st "); + } + + @Test + void streamRoleTest() { + UserMessage userMessage = new UserMessage( + "Tell me about 3 famous pirates from the Golden Age of Piracy and what they did."); + SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(this.systemResource); + Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", "Bob", "voice", "pirate")); + Prompt prompt = new Prompt(List.of(userMessage, systemMessage)); + Flux flux = this.chatModel.stream(prompt); + + List responses = flux.collectList().block(); + assertThat(responses.size()).isGreaterThan(1); + + String stitchedResponseContent = responses.stream() + .map(ChatResponse::getResults) + .flatMap(List::stream) + .map(Generation::getOutput) + .map(AssistantMessage::getText) + .collect(Collectors.joining()); + + assertThat(stitchedResponseContent).contains("Blackbeard"); + + } + + @Test + void streamingWithTokenUsage() { + var promptOptions = OpenAiOfficialChatOptions.builder().streamUsage(true).reasoningEffort("0").seed(1).build(); + + var prompt = new Prompt("List two colors of the Polish flag. Be brief.", promptOptions); + var streamingTokenUsage = this.chatModel.stream(prompt).blockLast().getMetadata().getUsage(); + var referenceTokenUsage = this.chatModel.call(prompt).getMetadata().getUsage(); + + assertThat(streamingTokenUsage.getPromptTokens()).isGreaterThan(0); + assertThat(streamingTokenUsage.getCompletionTokens()).isGreaterThan(0); + assertThat(streamingTokenUsage.getTotalTokens()).isGreaterThan(0); + + assertThat(streamingTokenUsage.getPromptTokens()).isCloseTo(referenceTokenUsage.getPromptTokens(), + Percentage.withPercentage(25)); + assertThat(streamingTokenUsage.getCompletionTokens()).isCloseTo(referenceTokenUsage.getCompletionTokens(), + Percentage.withPercentage(25)); + assertThat(streamingTokenUsage.getTotalTokens()).isCloseTo(referenceTokenUsage.getTotalTokens(), + Percentage.withPercentage(25)); + + } + + @Test + void listOutputConverter() { + DefaultConversionService conversionService = new DefaultConversionService(); + ListOutputConverter outputConverter = new ListOutputConverter(conversionService); + + String format = outputConverter.getFormat(); + String template = """ + List five {subject} + {format} + """; + PromptTemplate promptTemplate = PromptTemplate.builder() + .template(template) + .variables(Map.of("subject", "ice cream flavors", "format", format)) + .build(); + Prompt prompt = new Prompt(promptTemplate.createMessage()); + Generation generation = this.chatModel.call(prompt).getResult(); + + List list = outputConverter.convert(generation.getOutput().getText()); + assertThat(list).hasSize(5); + + } + + @Test + void mapOutputConverter() { + MapOutputConverter outputConverter = new MapOutputConverter(); + + String format = outputConverter.getFormat(); + String template = """ + Provide me a List of {subject} + {format} + """; + PromptTemplate promptTemplate = PromptTemplate.builder() + .template(template) + .variables(Map.of("subject", "numbers from 1 to 9 under they key name 'numbers'", "format", format)) + .build(); + Prompt prompt = new Prompt(promptTemplate.createMessage()); + Generation generation = this.chatModel.call(prompt).getResult(); + + Map result = outputConverter.convert(generation.getOutput().getText()); + assertThat(result.get("numbers")).isEqualTo(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9)); + + } + + @Test + void beanOutputConverter() { + + BeanOutputConverter outputConverter = new BeanOutputConverter<>(ActorsFilms.class); + + String format = outputConverter.getFormat(); + String template = """ + Generate the filmography for a random actor. + {format} + """; + PromptTemplate promptTemplate = PromptTemplate.builder() + .template(template) + .variables(Map.of("format", format)) + .build(); + Prompt prompt = new Prompt(promptTemplate.createMessage()); + Generation generation = this.chatModel.call(prompt).getResult(); + + ActorsFilms actorsFilms = outputConverter.convert(generation.getOutput().getText()); + } + + @Test + void beanOutputConverterRecords() { + + BeanOutputConverter outputConverter = new BeanOutputConverter<>(ActorsFilmsRecord.class); + + String format = outputConverter.getFormat(); + String template = """ + Generate the filmography of 5 movies for Tom Hanks. + {format} + """; + PromptTemplate promptTemplate = PromptTemplate.builder() + .template(template) + .variables(Map.of("format", format)) + .build(); + Prompt prompt = new Prompt(promptTemplate.createMessage()); + Generation generation = this.chatModel.call(prompt).getResult(); + + ActorsFilmsRecord actorsFilms = outputConverter.convert(generation.getOutput().getText()); + logger.info("" + actorsFilms); + assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks"); + assertThat(actorsFilms.movies()).hasSize(5); + } + + @Test + void beanStreamOutputConverterRecords() { + + BeanOutputConverter outputConverter = new BeanOutputConverter<>(ActorsFilmsRecord.class); + + String format = outputConverter.getFormat(); + String template = """ + Generate the filmography of 5 movies for Tom Hanks. + {format} + """; + PromptTemplate promptTemplate = PromptTemplate.builder() + .template(template) + .variables(Map.of("format", format)) + .build(); + Prompt prompt = new Prompt(promptTemplate.createMessage()); + + String generationTextFromStream = this.chatModel.stream(prompt) + .collectList() + .block() + .stream() + .map(ChatResponse::getResults) + .flatMap(List::stream) + .map(Generation::getOutput) + .map(AssistantMessage::getText) + .collect(Collectors.joining()); + + ActorsFilmsRecord actorsFilms = outputConverter.convert(generationTextFromStream); + logger.info("" + actorsFilms); + assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks"); + assertThat(actorsFilms.movies()).hasSize(5); + } + + @Test + void functionCallTest() { + + UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?"); + + List messages = new ArrayList<>(List.of(userMessage)); + + var promptOptions = OpenAiOfficialChatOptions.builder() + .toolCallbacks(List.of(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService()) + .description("Get the weather in location") + .inputType(MockWeatherService.Request.class) + .build())) + .build(); + + ChatResponse response = this.chatModel.call(new Prompt(messages, promptOptions)); + + logger.info("Response: {}", response); + + assertThat(response.getResult().getOutput().getText()).contains("30", "10", "15"); + } + + @Test + void streamFunctionCallTest() { + + UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?"); + + List messages = new ArrayList<>(List.of(userMessage)); + + var promptOptions = OpenAiOfficialChatOptions.builder() + // .withModel(OpenAiApi.ChatModel.GPT_4_TURBO_PREVIEW.getValue()) + .toolCallbacks(List.of(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService()) + .description("Get the weather in location") + .inputType(MockWeatherService.Request.class) + .build())) + .build(); + + Flux response = this.chatModel.stream(new Prompt(messages, promptOptions)); + + String content = response.collectList() + .block() + .stream() + .map(ChatResponse::getResults) + .flatMap(List::stream) + .map(Generation::getOutput) + .map(AssistantMessage::getText) + .collect(Collectors.joining()); + logger.info("Response: {}", content); + + assertThat(content).containsAnyOf("30.0", "30"); + assertThat(content).containsAnyOf("10.0", "10"); + assertThat(content).containsAnyOf("15.0", "15"); + } + + @Test + void functionCallUsageTest() { + + UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?"); + + List messages = new ArrayList<>(List.of(userMessage)); + + var promptOptions = OpenAiOfficialChatOptions.builder() + .toolCallbacks(List.of(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService()) + .description("Get the weather in location") + .inputType(MockWeatherService.Request.class) + .build())) + .build(); + + ChatResponse chatResponse = this.chatModel.call(new Prompt(messages, promptOptions)); + logger.info("Response: {}", chatResponse); + Usage usage = chatResponse.getMetadata().getUsage(); + + logger.info("Usage: {}", usage); + assertThat(usage).isNotNull(); + assertThat(usage).isNotInstanceOf(EmptyUsage.class); + assertThat(usage).isInstanceOf(DefaultUsage.class); + assertThat(usage.getPromptTokens()).isGreaterThan(450).isLessThan(600); + assertThat(usage.getCompletionTokens()).isGreaterThan(230).isLessThan(360); + assertThat(usage.getTotalTokens()).isGreaterThan(680).isLessThan(900); + } + + @Test + void streamFunctionCallUsageTest() { + + UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?"); + + List messages = new ArrayList<>(List.of(userMessage)); + + var promptOptions = OpenAiOfficialChatOptions.builder() + .toolCallbacks(List.of(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService()) + .description("Get the weather in location") + .inputType(MockWeatherService.Request.class) + .build())) + .streamUsage(true) + .build(); + + Flux response = this.chatModel.stream(new Prompt(messages, promptOptions)); + Usage usage = response.last().block().getMetadata().getUsage(); + + logger.info("Usage: {}", usage); + assertThat(usage).isNotNull(); + assertThat(usage).isNotInstanceOf(EmptyUsage.class); + assertThat(usage).isInstanceOf(DefaultUsage.class); + assertThat(usage.getPromptTokens()).isGreaterThan(100).isLessThan(250); + assertThat(usage.getCompletionTokens()).isGreaterThan(300).isLessThan(450); + assertThat(usage.getTotalTokens()).isGreaterThan(400).isLessThan(700); + } + + @Test + void multiModalityEmbeddedImage() throws IOException { + + var imageData = new ClassPathResource("/test.png"); + + var userMessage = UserMessage.builder() + .text("Explain what do you see on this picture?") + .media(List.of(new Media(MimeTypeUtils.IMAGE_PNG, imageData))) + .build(); + + var response = this.chatModel + .call(new Prompt(List.of(userMessage), OpenAiOfficialChatOptions.builder().build())); + + logger.info(response.getResult().getOutput().getText()); + assertThat(response.getResult().getOutput().getText()).containsAnyOf("bananas", "apple", "bowl", "basket", + "fruit stand"); + } + + @Test + void multiModalityImageUrl() throws IOException { + + var userMessage = UserMessage.builder() + .text("Explain what do you see on this picture?") + .media(List.of(Media.builder() + .mimeType(MimeTypeUtils.IMAGE_PNG) + .data(URI.create("https://docs.spring.io/spring-ai/reference/_images/multimodal.test.png")) + .build())) + .build(); + + ChatResponse response = this.chatModel + .call(new Prompt(List.of(userMessage), OpenAiOfficialChatOptions.builder().build())); + + logger.info(response.getResult().getOutput().getText()); + assertThat(response.getResult().getOutput().getText()).containsAnyOf("bananas", "apple", "bowl", "basket", + "fruit stand"); + } + + @Test + void streamingMultiModalityImageUrl() throws IOException { + + var userMessage = UserMessage.builder() + .text("Explain what do you see on this picture?") + .media(List.of(Media.builder() + .mimeType(MimeTypeUtils.IMAGE_PNG) + .data(URI.create("https://docs.spring.io/spring-ai/reference/_images/multimodal.test.png")) + .build())) + .build(); + + Flux response = this.chatModel + .stream(new Prompt(List.of(userMessage), OpenAiOfficialChatOptions.builder().build())); + + String content = response.collectList() + .block() + .stream() + .map(ChatResponse::getResults) + .flatMap(List::stream) + .map(Generation::getOutput) + .map(AssistantMessage::getText) + .collect(Collectors.joining()); + logger.info("Response: {}", content); + assertThat(content).containsAnyOf("bananas", "apple", "bowl", "basket", "fruit stand"); + } + + @Test + void validateCallResponseMetadata() { + String model = OpenAiOfficialChatOptions.DEFAULT_CHAT_MODEL; + // @formatter:off + ChatResponse response = ChatClient.create(this.chatModel).prompt() + .options(OpenAiOfficialChatOptions.builder().model(model).build()) + .user("Tell me about 3 famous pirates from the Golden Age of Piracy and what they did") + .call() + .chatResponse(); + // @formatter:on + + logger.info(response.toString()); + assertThat(response.getMetadata().getId()).isNotEmpty(); + assertThat(response.getMetadata().getModel()).containsIgnoringCase(model); + assertThat(response.getMetadata().getUsage().getPromptTokens()).isPositive(); + assertThat(response.getMetadata().getUsage().getCompletionTokens()).isPositive(); + assertThat(response.getMetadata().getUsage().getTotalTokens()).isPositive(); + } + + @Test + void validateStoreAndMetadata() { + OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() + .store(true) + .metadata(Map.of("type", "dev")) + .build(); + + ChatResponse response = this.chatModel.call(new Prompt("Tell me a joke", options)); + + assertThat(response).isNotNull(); + } + + @Test + void chatMemory() { + ChatMemory memory = MessageWindowChatMemory.builder().build(); + String conversationId = UUID.randomUUID().toString(); + + UserMessage userMessage1 = new UserMessage("My name is James Bond"); + memory.add(conversationId, userMessage1); + ChatResponse response1 = this.chatModel.call(new Prompt(memory.get(conversationId))); + + assertThat(response1).isNotNull(); + memory.add(conversationId, response1.getResult().getOutput()); + + UserMessage userMessage2 = new UserMessage("What is my name?"); + memory.add(conversationId, userMessage2); + ChatResponse response2 = this.chatModel.call(new Prompt(memory.get(conversationId))); + + assertThat(response2).isNotNull(); + memory.add(conversationId, response2.getResult().getOutput()); + + assertThat(response2.getResults()).hasSize(1); + assertThat(response2.getResult().getOutput().getText()).contains("James Bond"); + } + + @Test + void chatMemoryWithTools() { + ToolCallingManager toolCallingManager = DefaultToolCallingManager.builder().build(); + ChatMemory chatMemory = MessageWindowChatMemory.builder().build(); + String conversationId = UUID.randomUUID().toString(); + + ChatOptions chatOptions = ToolCallingChatOptions.builder() + .toolCallbacks(ToolCallbacks.from(new MathTools())) + .internalToolExecutionEnabled(false) + .build(); + Prompt prompt = new Prompt( + List.of(new SystemMessage("You are a helpful assistant."), new UserMessage("What is 6 * 8?")), + chatOptions); + chatMemory.add(conversationId, prompt.getInstructions()); + + Prompt promptWithMemory = new Prompt(chatMemory.get(conversationId), chatOptions); + ChatResponse chatResponse = this.chatModel.call(promptWithMemory); + chatMemory.add(conversationId, chatResponse.getResult().getOutput()); + + while (chatResponse.hasToolCalls()) { + ToolExecutionResult toolExecutionResult = toolCallingManager.executeToolCalls(promptWithMemory, + chatResponse); + chatMemory.add(conversationId, toolExecutionResult.conversationHistory() + .get(toolExecutionResult.conversationHistory().size() - 1)); + promptWithMemory = new Prompt(chatMemory.get(conversationId), chatOptions); + chatResponse = this.chatModel.call(promptWithMemory); + chatMemory.add(conversationId, chatResponse.getResult().getOutput()); + } + + assertThat(chatResponse).isNotNull(); + assertThat(chatResponse.getResult().getOutput().getText()).contains("48"); + + UserMessage newUserMessage = new UserMessage("What did I ask you earlier?"); + chatMemory.add(conversationId, newUserMessage); + + ChatResponse newResponse = this.chatModel.call(new Prompt(chatMemory.get(conversationId))); + + assertThat(newResponse).isNotNull(); + assertThat(newResponse.getResult().getOutput().getText()).contains("6").contains("8"); + } + + record ActorsFilmsRecord(String actor, List movies) { + + } + + static class MathTools { + + @Tool(description = "Multiply the two numbers") + double multiply(double a, double b) { + return a * b; + } + + } + } diff --git a/models/spring-ai-openai-official/src/test/resources/test.png b/models/spring-ai-openai-official/src/test/resources/test.png new file mode 100644 index 0000000000000000000000000000000000000000..8abb4c81aea34f9238ce5abeabfbcc8db7ff0cb3 GIT binary patch literal 167772 zcmV)KK)Sz)P)4Tx0C=38mUmQB*%pV-y*Is3k`RiN&}(Q?0!R(LNRcioF$oY#z>okUHbhi# zL{X8Z2r?+(fTKf^u_B6v0a3B*1Q|rsac~qHmPur-8Q;8l@6DUvANPK1pS{oBXYYO1 zx&V;;g9XA&SP6g(p;#2*=f#MPi)Ua50Sxc}18e}`aI>>Q7WhU2nF4&+jBJ?`_!qsp z4j}paD$_rV!2tiCl(|_VF#u4QjOX(B*<2YH$v8b%oF%tU$(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QR5DCHw`G1jv>;{X5v07*qoM6N<$f=9SukN^Mx literal 0 HcmV?d00001 From e80d37bbe14dae103c01d79e0829db635129d0c9 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Wed, 12 Nov 2025 15:09:42 +0100 Subject: [PATCH 09/49] Implementation of the OpenAI Java SDK - More tests passing for ChatModel Signed-off-by: Julien Dubois --- .../OpenAiOfficialChatModel.java | 98 +++++++++++-------- .../chat/OpenAiOfficialChatModelIT.java | 11 ++- 2 files changed, 61 insertions(+), 48 deletions(-) diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java index d4b627d714b..672a605ee72 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -64,6 +64,7 @@ import reactor.core.publisher.Flux; import reactor.core.scheduler.Schedulers; +import java.net.URI; import java.util.ArrayList; import java.util.Base64; import java.util.HashMap; @@ -74,6 +75,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; +import static java.util.stream.Collectors.toList; import static org.springframework.ai.openaiofficial.setup.OpenAiOfficialSetup.setupAsyncClient; import static org.springframework.ai.openaiofficial.setup.OpenAiOfficialSetup.setupSyncClient; @@ -532,51 +534,57 @@ Prompt buildRequestPrompt(Prompt prompt) { ChatCompletionCreateParams createRequest(Prompt prompt, boolean stream) { - List chatCompletionMessage = prompt.getInstructions().stream().map(message -> { + List chatCompletionMessageParams = prompt.getInstructions().stream().map(message -> { if (message.getMessageType() == MessageType.USER || message.getMessageType() == MessageType.SYSTEM) { // Handle simple text content for user and system messages - ChatCompletionMessage.Builder builder = ChatCompletionMessage.builder(); + ChatCompletionUserMessageParam.Builder builder = ChatCompletionUserMessageParam.builder(); if (message instanceof UserMessage userMessage && !CollectionUtils.isEmpty(userMessage.getMedia())) { // Handle media content (images, audio, files) - List contentParts = new ArrayList<>(); + List parts = new ArrayList<>(); if (!message.getText().isEmpty()) { - contentParts.add(ChatCompletionContentPartText.builder().text(message.getText()).build().text()); + parts.add(ChatCompletionContentPart.ofText(ChatCompletionContentPartText.builder().text(message.getText()).build())); } // Add media content parts userMessage.getMedia().forEach(media -> { String mimeType = media.getMimeType().toString(); if (mimeType.startsWith("image/")) { - if (media.getData() instanceof java.net.URI) { - contentParts.add(ChatCompletionContentPartImage.builder() - .imageUrl(ChatCompletionContentPartImage.ImageUrl.builder() - .url(media.getData().toString()) - .build()) - .build().imageUrl().url()); + if (media.getData() instanceof java.net.URI uri) { + parts.add(ChatCompletionContentPart.ofImageUrl(ChatCompletionContentPartImage.builder() + .imageUrl(ChatCompletionContentPartImage.ImageUrl.builder().url(uri.toString()).build()) + .build())); + } else if (media.getData() instanceof String text) { + // The org.springframework.ai.content.Media object should store the URL as a java.net.URI but it transforms it to String somewhere along the way, + // for example in its Builder class. So, we accept String as well here for image URLs. + parts.add(ChatCompletionContentPart.ofImageUrl(ChatCompletionContentPartImage.builder() + .imageUrl(ChatCompletionContentPartImage.ImageUrl.builder().url(text).build()) + .build())); } else { logger.info("Could not process image media with data of type: {}. Only java.net.URI is supported for image URLs.", media.getData().getClass().getSimpleName()); } } else if (mimeType.startsWith("audio/")) { - contentParts.add(ChatCompletionContentPartInputAudio.builder() - .inputAudio(ChatCompletionContentPartInputAudio.InputAudio - .builder() - .data(fromAudioData(media.getData())) - .format(mimeType.contains("mp3") - ? ChatCompletionContentPartInputAudio.InputAudio.Format.MP3 - : ChatCompletionContentPartInputAudio.InputAudio.Format.WAV) - .build()) - .build().inputAudio().data()); + parts.add(ChatCompletionContentPart.ofInputAudio(ChatCompletionContentPartInputAudio.builder() + .inputAudio(ChatCompletionContentPartInputAudio.builder() + .inputAudio(ChatCompletionContentPartInputAudio.InputAudio.builder() + .data(fromAudioData(media.getData())) + .format(mimeType.contains("mp3") + ? ChatCompletionContentPartInputAudio.InputAudio.Format.MP3 + : ChatCompletionContentPartInputAudio.InputAudio.Format.WAV) + .build()) + .build() + .inputAudio()) + .build())); } else { // Assume it's a file or other media type represented as a data URL - contentParts.add(fromMediaData(media.getMimeType(), media.getData())); + parts.add(ChatCompletionContentPart.ofText(ChatCompletionContentPartText.builder().text(fromMediaData(media.getMimeType(), media.getData())).build())); } }); - builder.content(JsonArray.of(contentParts).asString()); + builder.contentOfArrayOfContentParts(parts); } else { // Simple text message @@ -584,26 +592,24 @@ else if (mimeType.startsWith("audio/")) { } if (message.getMessageType() == MessageType.USER) { - builder.role(JsonValue.from(MessageType.USER)); + builder.role(JsonValue.from(MessageType.USER.getValue())); } else { - builder.role(JsonValue.from(MessageType.SYSTEM)); + builder.role(JsonValue.from(MessageType.SYSTEM.getValue())); } - builder.refusal(JsonValue.from(Optional.ofNullable(message.getMetadata().get("refusal")).map(Object::toString).orElse(""))); - return List.of(builder.build()); + + return ChatCompletionMessageParam.ofUser(builder.build()); } else if (message.getMessageType() == MessageType.ASSISTANT) { var assistantMessage = (AssistantMessage) message; - ChatCompletionMessage.Builder builder = ChatCompletionMessage.builder() - .role(JsonValue.from(MessageType.ASSISTANT)); + ChatCompletionAssistantMessageParam.Builder builder = ChatCompletionAssistantMessageParam.builder() + .role(JsonValue.from(MessageType.ASSISTANT.getValue())); if (assistantMessage.getText() != null) { - builder.content(ChatCompletionMessage.builder() + builder.content(ChatCompletionAssistantMessageParam.builder() .content(assistantMessage.getText()) - .refusal(JsonValue.from(Optional.ofNullable(message.getMetadata().get("refusal")).map(Object::toString).orElse(""))) .build().content()); } - builder.refusal(JsonValue.from(Optional.ofNullable(message.getMetadata().get("refusal")).map(Object::toString).orElse(""))); if (!CollectionUtils.isEmpty(assistantMessage.getToolCalls())) { List toolCalls = assistantMessage.getToolCalls() @@ -617,33 +623,39 @@ else if (message.getMessageType() == MessageType.ASSISTANT) { .build()) ) .toList(); + builder.toolCalls(toolCalls); } - return List.of(builder.build()); + return ChatCompletionMessageParam.ofAssistant(builder.build()); } else if (message.getMessageType() == MessageType.TOOL) { ToolResponseMessage toolMessage = (ToolResponseMessage) message; - return toolMessage.getResponses() - .stream() - .map(toolResponse -> ChatCompletionMessage.builder() - .role(JsonValue.from(MessageType.TOOL)) - .content(ChatCompletionMessage.builder().content(toolResponse.responseData()).refusal(Optional.ofNullable(message.getMetadata().get("refusal")).map(Object::toString).orElse("")).build().content()) - .refusal(JsonValue.from(Optional.ofNullable(message.getMetadata().get("refusal")).map(Object::toString).orElse(""))) - .build()) - .toList(); + ChatCompletionToolMessageParam.Builder builder = ChatCompletionToolMessageParam.builder(); + builder.content(toolMessage.getText() != null ? toolMessage.getText() : ""); + builder.role(JsonValue.from(MessageType.TOOL.getValue())); + + if (toolMessage.getResponses().isEmpty()) { + return ChatCompletionMessageParam.ofTool(builder.build()); + } + String callId = toolMessage.getResponses().get(0).id(); + String callResponse = toolMessage.getResponses().get(0).responseData(); + + return ChatCompletionMessageParam.ofTool(builder + .toolCallId(callId) + .content(callResponse) + .role(JsonValue.from(MessageType.TOOL.getValue())) + .build()); } else { throw new IllegalArgumentException("Unsupported message type: " + message.getMessageType()); } - }).flatMap(List::stream).toList(); + }).toList(); ChatCompletionCreateParams.Builder builder = ChatCompletionCreateParams.builder(); - chatCompletionMessage.forEach(message -> { - builder.addMessage(message); - }); + chatCompletionMessageParams.forEach(builder::addMessage); OpenAiOfficialChatOptions requestOptions = (OpenAiOfficialChatOptions) prompt.getOptions(); diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java index 702bc3f7175..1f08a98e3b4 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java @@ -433,9 +433,9 @@ void functionCallUsageTest() { assertThat(usage).isNotNull(); assertThat(usage).isNotInstanceOf(EmptyUsage.class); assertThat(usage).isInstanceOf(DefaultUsage.class); - assertThat(usage.getPromptTokens()).isGreaterThan(450).isLessThan(600); - assertThat(usage.getCompletionTokens()).isGreaterThan(230).isLessThan(360); - assertThat(usage.getTotalTokens()).isGreaterThan(680).isLessThan(900); + assertThat(usage.getPromptTokens()).isGreaterThan(100).isLessThan(300); + assertThat(usage.getCompletionTokens()).isGreaterThan(230).isLessThan(400); + assertThat(usage.getTotalTokens()).isGreaterThan(450).isLessThan(650); } @Test @@ -451,6 +451,7 @@ void streamFunctionCallUsageTest() { .inputType(MockWeatherService.Request.class) .build())) .streamUsage(true) + .reasoningEffort("0") .build(); Flux response = this.chatModel.stream(new Prompt(messages, promptOptions)); @@ -461,8 +462,8 @@ void streamFunctionCallUsageTest() { assertThat(usage).isNotInstanceOf(EmptyUsage.class); assertThat(usage).isInstanceOf(DefaultUsage.class); assertThat(usage.getPromptTokens()).isGreaterThan(100).isLessThan(250); - assertThat(usage.getCompletionTokens()).isGreaterThan(300).isLessThan(450); - assertThat(usage.getTotalTokens()).isGreaterThan(400).isLessThan(700); + assertThat(usage.getCompletionTokens()).isGreaterThan(200).isLessThan(500); + assertThat(usage.getTotalTokens()).isGreaterThan(300).isLessThan(750); } @Test From dfa333cb6697b9831473faa65d46359b7fe964e3 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Wed, 12 Nov 2025 17:43:42 +0100 Subject: [PATCH 10/49] Implementation of the OpenAI Java SDK - More tests passing for ChatModel Signed-off-by: Julien Dubois --- .../OpenAiOfficialChatModel.java | 18 +++++++++++++++++- .../chat/OpenAiOfficialChatModelIT.java | 15 +++++++++------ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java index 672a605ee72..29a148512e9 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -23,6 +23,7 @@ import com.openai.core.JsonValue; import com.openai.models.FunctionDefinition; import com.openai.models.FunctionParameters; +import com.openai.models.ReasoningEffort; import com.openai.models.chat.completions.*; import com.openai.models.completions.CompletionUsage; import io.micrometer.observation.Observation; @@ -561,6 +562,15 @@ ChatCompletionCreateParams createRequest(Prompt prompt, boolean stream) { parts.add(ChatCompletionContentPart.ofImageUrl(ChatCompletionContentPartImage.builder() .imageUrl(ChatCompletionContentPartImage.ImageUrl.builder().url(text).build()) .build())); + } else if (media.getData() instanceof byte[] bytes) { + // Assume the bytes are an image. So, convert the bytes to a base64 encoded + ChatCompletionContentPartImage.ImageUrl.Builder imageUrlBuilder = ChatCompletionContentPartImage.ImageUrl.builder(); + + imageUrlBuilder.url("data:" + mimeType + ";base64," + + Base64.getEncoder().encodeToString(bytes)); + parts.add(ChatCompletionContentPart.ofImageUrl(ChatCompletionContentPartImage.builder() + .imageUrl(imageUrlBuilder.build()) + .build())); } else { logger.info("Could not process image media with data of type: {}. Only java.net.URI is supported for image URLs.", media.getData().getClass().getSimpleName()); @@ -645,7 +655,6 @@ else if (message.getMessageType() == MessageType.TOOL) { return ChatCompletionMessageParam.ofTool(builder .toolCallId(callId) .content(callResponse) - .role(JsonValue.from(MessageType.TOOL.getValue())) .build()); } else { @@ -724,6 +733,13 @@ else if (requestOptions.getModel() != null) { if (requestOptions.getParallelToolCalls() != null) { builder.parallelToolCalls(requestOptions.getParallelToolCalls()); } + if (requestOptions.getReasoningEffort() != null) { + builder.reasoningEffort(ReasoningEffort.of(requestOptions.getReasoningEffort().toLowerCase())); + } + if (requestOptions.getVerbosity() != null) { + builder.verbosity(ChatCompletionCreateParams.Verbosity.of(requestOptions.getVerbosity())); + } + if (requestOptions.getStore() != null) { builder.store(requestOptions.getStore()); } diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java index 1f08a98e3b4..bd47b696336 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java @@ -16,11 +16,10 @@ package org.springframework.ai.openaiofficial.chat; +import com.openai.models.ReasoningEffort; import org.assertj.core.data.Percentage; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.ai.chat.client.ChatClient; @@ -221,7 +220,11 @@ void streamRoleTest() { @Test void streamingWithTokenUsage() { - var promptOptions = OpenAiOfficialChatOptions.builder().streamUsage(true).reasoningEffort("0").seed(1).build(); + var promptOptions = OpenAiOfficialChatOptions.builder() + .streamUsage(true) + .reasoningEffort(ReasoningEffort.MINIMAL.toString()) + .seed(1) + .build(); var prompt = new Prompt("List two colors of the Polish flag. Be brief.", promptOptions); var streamingTokenUsage = this.chatModel.stream(prompt).blockLast().getMetadata().getUsage(); @@ -451,7 +454,7 @@ void streamFunctionCallUsageTest() { .inputType(MockWeatherService.Request.class) .build())) .streamUsage(true) - .reasoningEffort("0") + .reasoningEffort(ReasoningEffort.MINIMAL.toString()) .build(); Flux response = this.chatModel.stream(new Prompt(messages, promptOptions)); @@ -462,8 +465,8 @@ void streamFunctionCallUsageTest() { assertThat(usage).isNotInstanceOf(EmptyUsage.class); assertThat(usage).isInstanceOf(DefaultUsage.class); assertThat(usage.getPromptTokens()).isGreaterThan(100).isLessThan(250); - assertThat(usage.getCompletionTokens()).isGreaterThan(200).isLessThan(500); - assertThat(usage.getTotalTokens()).isGreaterThan(300).isLessThan(750); + assertThat(usage.getCompletionTokens()).isGreaterThan(100).isLessThan(300); + assertThat(usage.getTotalTokens()).isGreaterThan(250).isLessThan(500); } @Test From d4c675ca2513a2aa5305191da73f1e12ce42a0f2 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Wed, 12 Nov 2025 22:59:57 +0100 Subject: [PATCH 11/49] Implementation of the OpenAI Java SDK - More tests passing for ChatModel Signed-off-by: Julien Dubois --- .../OpenAiOfficialChatModel.java | 8 +- .../OpenAiOfficialChatOptions.java | 28 ++-- ...ialTestConfigurationWithObservability.java | 6 + .../chat/OpenAiOfficialChatModelIT.java | 6 +- .../OpenAiOfficialChatModelObservationIT.java | 135 ++++++++++++++++++ 5 files changed, 161 insertions(+), 22 deletions(-) create mode 100644 models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java index 29a148512e9..869639cb45d 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -18,8 +18,6 @@ import com.openai.client.OpenAIClient; import com.openai.client.OpenAIClientAsync; -import com.openai.core.JsonArray; -import com.openai.core.JsonField; import com.openai.core.JsonValue; import com.openai.models.FunctionDefinition; import com.openai.models.FunctionParameters; @@ -65,7 +63,6 @@ import reactor.core.publisher.Flux; import reactor.core.scheduler.Schedulers; -import java.net.URI; import java.util.ArrayList; import java.util.Base64; import java.util.HashMap; @@ -76,7 +73,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; -import static java.util.stream.Collectors.toList; import static org.springframework.ai.openaiofficial.setup.OpenAiOfficialSetup.setupAsyncClient; import static org.springframework.ai.openaiofficial.setup.OpenAiOfficialSetup.setupSyncClient; @@ -707,8 +703,8 @@ else if (requestOptions.getModel() != null) { if (requestOptions.getPresencePenalty() != null) { builder.presencePenalty(requestOptions.getPresencePenalty()); } - if (requestOptions.getResponseFormat() != null) { - builder.responseFormat(requestOptions.getResponseFormat()); + if (requestOptions.getResponseFormatJsonSchema() != null) { + builder.responseFormat(requestOptions.getResponseFormatJsonSchema()); } if (requestOptions.getSeed() != null) { builder.seed(requestOptions.getSeed()); diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java index 6e44495558a..2abfe77ace3 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java @@ -68,7 +68,7 @@ public class OpenAiOfficialChatOptions extends AbstractOpenAiOfficialOptions imp private Double presencePenalty; - private ResponseFormatJsonSchema responseFormat; + private ResponseFormatJsonSchema responseFormatJsonSchema; private ResponseCreateParams.StreamOptions streamOptions; @@ -185,12 +185,12 @@ public void setPresencePenalty(Double presencePenalty) { this.presencePenalty = presencePenalty; } - public ResponseFormatJsonSchema getResponseFormat() { - return this.responseFormat; + public ResponseFormatJsonSchema getResponseFormatJsonSchema() { + return this.responseFormatJsonSchema; } - public void setResponseFormat(ResponseFormatJsonSchema responseFormat) { - this.responseFormat = responseFormat; + public void setResponseFormatJsonSchema(ResponseFormatJsonSchema responseFormatJsonSchema) { + this.responseFormatJsonSchema = responseFormatJsonSchema; } public ResponseCreateParams.StreamOptions getStreamOptions() { @@ -402,7 +402,7 @@ public boolean equals(Object o) { && Objects.equals(topLogprobs, options.topLogprobs) && Objects.equals(maxTokens, options.maxTokens) && Objects.equals(n, options.n) && Objects.equals(outputAudio, options.outputAudio) && Objects.equals(presencePenalty, options.presencePenalty) - && Objects.equals(responseFormat, options.responseFormat) + && Objects.equals(responseFormatJsonSchema, options.responseFormatJsonSchema) && Objects.equals(streamOptions, options.streamOptions) && Objects.equals(streamUsage, options.streamUsage) && Objects.equals(seed, options.seed) && Objects.equals(stop, options.stop) && Objects.equals(temperature, options.temperature) @@ -420,7 +420,7 @@ public boolean equals(Object o) { @Override public int hashCode() { return Objects.hash(frequencyPenalty, logitBias, logprobs, topLogprobs, maxTokens, n, outputAudio, - presencePenalty, responseFormat, streamOptions, streamUsage, seed, stop, temperature, topP, tools, + presencePenalty, responseFormatJsonSchema, streamOptions, streamUsage, seed, stop, temperature, topP, tools, toolChoice, user, parallelToolCalls, store, metadata, reasoningEffort, verbosity, serviceTier, toolCallbacks, toolNames, internalToolExecutionEnabled, httpHeaders, toolContext); } @@ -429,8 +429,8 @@ public int hashCode() { public String toString() { return "OpenAiOfficialChatOptions{" + "frequencyPenalty=" + frequencyPenalty + ", logitBias=" + logitBias + ", logprobs=" + logprobs + ", topLogprobs=" + topLogprobs + ", maxTokens=" + maxTokens + ", n=" + n - + ", outputAudio=" + outputAudio + ", presencePenalty=" + presencePenalty + ", responseFormat=" - + responseFormat + ", streamOptions=" + streamOptions + ", streamUsage=" + streamUsage + ", seed=" + + ", outputAudio=" + outputAudio + ", presencePenalty=" + presencePenalty + ", responseFormatJsonSchema=" + + responseFormatJsonSchema + ", streamOptions=" + streamOptions + ", streamUsage=" + streamUsage + ", seed=" + seed + ", stop=" + stop + ", temperature=" + temperature + ", topP=" + topP + ", tools=" + tools + ", toolChoice=" + toolChoice + ", user='" + user + '\'' + ", parallelToolCalls=" + parallelToolCalls + ", store=" + store + ", metadata=" + metadata + ", reasoningEffort='" + reasoningEffort + '\'' @@ -455,7 +455,7 @@ public Builder from(OpenAiOfficialChatOptions fromOptions) { this.options.setN(fromOptions.getN()); this.options.setOutputAudio(fromOptions.getOutputAudio()); this.options.setPresencePenalty(fromOptions.getPresencePenalty()); - this.options.setResponseFormat(fromOptions.getResponseFormat()); + this.options.setResponseFormatJsonSchema(fromOptions.getResponseFormatJsonSchema()); this.options.setStreamOptions(fromOptions.getStreamOptions()); this.options.setStreamUsage(fromOptions.getStreamUsage()); this.options.setSeed(fromOptions.getSeed()); @@ -514,8 +514,8 @@ public Builder merge(OpenAiOfficialChatOptions from) { if (from.getPresencePenalty() != null) { this.options.setPresencePenalty(from.getPresencePenalty()); } - if (from.getResponseFormat() != null) { - this.options.setResponseFormat(from.getResponseFormat()); + if (from.getResponseFormatJsonSchema() != null) { + this.options.setResponseFormatJsonSchema(from.getResponseFormatJsonSchema()); } if (from.getStreamOptions() != null) { this.options.setStreamOptions(from.getStreamOptions()); @@ -649,8 +649,8 @@ public Builder presencePenalty(Double presencePenalty) { return this; } - public Builder responseFormat(ResponseFormatJsonSchema responseFormat) { - this.options.setResponseFormat(responseFormat); + public Builder responseFormatJsonSchema(ResponseFormatJsonSchema responseFormatJsonSchema) { + this.options.setResponseFormatJsonSchema(responseFormatJsonSchema); return this; } diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java index 53c6e1adf44..47a93812358 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java @@ -21,6 +21,7 @@ import org.springframework.boot.SpringBootConfiguration; import org.springframework.context.annotation.Bean; +import static org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions.DEFAULT_CHAT_MODEL; import static org.springframework.ai.openaiofficial.OpenAiOfficialEmbeddingOptions.DEFAULT_EMBEDDING_MODEL; import static org.springframework.ai.openaiofficial.OpenAiOfficialImageOptions.DEFAULT_IMAGE_MODEL; @@ -49,4 +50,9 @@ public OpenAiOfficialImageModel openAiImageModel(TestObservationRegistry observa observationRegistry); } + @Bean + public OpenAiOfficialChatModel openAiChatModel(TestObservationRegistry observationRegistry) { + return new OpenAiOfficialChatModel(OpenAiOfficialChatOptions.builder().model(DEFAULT_CHAT_MODEL).build(), + observationRegistry); + } } diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java index bd47b696336..bc26e291e1b 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java @@ -133,8 +133,10 @@ void streamCompletenessTest() throws InterruptedException { CountDownLatch latch = new CountDownLatch(1); Flux chatResponseFlux = this.chatModel.stream(prompt).doOnNext(chatResponse -> { - String responseContent = chatResponse.getResults().get(0).getOutput().getText(); - answer.append(responseContent); + if (!chatResponse.getResults().isEmpty()) { + String responseContent = chatResponse.getResults().get(0).getOutput().getText(); + answer.append(responseContent); + } }).doOnComplete(() -> { logger.info(answer.toString()); latch.countDown(); diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java new file mode 100644 index 00000000000..462470ebf33 --- /dev/null +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java @@ -0,0 +1,135 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaiofficial.chat; + +import io.micrometer.observation.tck.TestObservationRegistry; +import io.micrometer.observation.tck.TestObservationRegistryAssert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import org.springframework.ai.chat.metadata.ChatResponseMetadata; +import org.springframework.ai.chat.model.ChatResponse; +import org.springframework.ai.chat.observation.DefaultChatModelObservationConvention; +import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.ai.observation.conventions.AiOperationType; +import org.springframework.ai.observation.conventions.AiProvider; +import org.springframework.ai.openaiofficial.OpenAiOfficialChatModel; +import org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions; +import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfigurationWithObservability; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import reactor.core.publisher.Flux; + +import java.util.List; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.ai.chat.observation.ChatModelObservationDocumentation.HighCardinalityKeyNames; +import static org.springframework.ai.chat.observation.ChatModelObservationDocumentation.LowCardinalityKeyNames; +import static org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions.DEFAULT_CHAT_MODEL; + +/** + * Integration tests for observation instrumentation in + * {@link OpenAiOfficialChatModel}. + * + * @author Julien Dubois + */ +@SpringBootTest(classes = OpenAiOfficialTestConfigurationWithObservability.class) +@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") +public class OpenAiOfficialChatModelObservationIT { + + @Autowired + TestObservationRegistry observationRegistry; + + @Autowired + private OpenAiOfficialChatModel chatModel; + + @BeforeEach + void setUp() { + this.observationRegistry.clear(); + } + + @Test + void observationForChatOperation() { + + var options = OpenAiOfficialChatOptions.builder() + .model(DEFAULT_CHAT_MODEL) + .build(); + + Prompt prompt = new Prompt("Why does a raven look like a desk?", options); + + ChatResponse chatResponse = this.chatModel.call(prompt); + assertThat(chatResponse.getResult().getOutput().getText()).isNotEmpty(); + + ChatResponseMetadata responseMetadata = chatResponse.getMetadata(); + assertThat(responseMetadata).isNotNull(); + + validate(responseMetadata); + } + + @Test + void observationForStreamingChatOperation() { + var options = OpenAiOfficialChatOptions.builder() + .model(DEFAULT_CHAT_MODEL) + .streamUsage(true) + .build(); + + Prompt prompt = new Prompt("Why does a raven look like a desk?", options); + + Flux chatResponseFlux = this.chatModel.stream(prompt); + + List responses = chatResponseFlux.collectList().block(); + assertThat(responses).isNotEmpty(); + assertThat(responses).hasSizeGreaterThan(10); + + String aggregatedResponse = responses.subList(0, responses.size() - 1) + .stream() + .map(r -> r.getResult() != null ? r.getResult().getOutput().getText() : "") + .collect(Collectors.joining()); + assertThat(aggregatedResponse).isNotEmpty(); + + ChatResponse lastChatResponse = responses.get(responses.size() - 1); + + ChatResponseMetadata responseMetadata = lastChatResponse.getMetadata(); + assertThat(responseMetadata).isNotNull(); + + validate(responseMetadata); + } + + private void validate(ChatResponseMetadata responseMetadata) { + TestObservationRegistryAssert.assertThat(this.observationRegistry) + .doesNotHaveAnyRemainingCurrentObservation() + .hasObservationWithNameEqualTo(DefaultChatModelObservationConvention.DEFAULT_NAME) + .that() + .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_OPERATION_TYPE.asString(), + AiOperationType.CHAT.value()) + .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_PROVIDER.asString(), AiProvider.OPENAI_OFFICIAL.value()) + .hasLowCardinalityKeyValue(LowCardinalityKeyNames.REQUEST_MODEL.asString(), + DEFAULT_CHAT_MODEL) + .hasLowCardinalityKeyValue(LowCardinalityKeyNames.RESPONSE_MODEL.asString(), responseMetadata.getModel()) + .hasHighCardinalityKeyValue(HighCardinalityKeyNames.RESPONSE_ID.asString(), responseMetadata.getId()) + .hasHighCardinalityKeyValue(HighCardinalityKeyNames.RESPONSE_FINISH_REASONS.asString(), "[\"STOP\"]") + .hasHighCardinalityKeyValue(HighCardinalityKeyNames.USAGE_INPUT_TOKENS.asString(), + String.valueOf(responseMetadata.getUsage().getPromptTokens())) + .hasHighCardinalityKeyValue(HighCardinalityKeyNames.USAGE_OUTPUT_TOKENS.asString(), + String.valueOf(responseMetadata.getUsage().getCompletionTokens())) + .hasHighCardinalityKeyValue(HighCardinalityKeyNames.USAGE_TOTAL_TOKENS.asString(), + String.valueOf(responseMetadata.getUsage().getTotalTokens())) + .hasBeenStarted() + .hasBeenStopped(); + } +} From 73f6b1866535ce17bf663484a08b55c9ece282b3 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Thu, 13 Nov 2025 13:54:38 +0100 Subject: [PATCH 12/49] Implementation of the OpenAI Java SDK - More tests passing for ChatModel Signed-off-by: Julien Dubois --- .../OpenAiOfficialChatModel.java | 32 ++- .../OpenAiOfficialChatOptions.java | 28 +- .../OpenAiOfficialChatResponseFormat.java | 95 ++++++ ...ialTestConfigurationWithObservability.java | 1 + .../OpenAiOfficialChatModelObservationIT.java | 19 +- ...enAiOfficialChatModelResponseFormatIT.java | 271 ++++++++++++++++++ 6 files changed, 418 insertions(+), 28 deletions(-) create mode 100644 models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatResponseFormat.java create mode 100644 models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java index 869639cb45d..ed55463710f 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -22,6 +22,9 @@ import com.openai.models.FunctionDefinition; import com.openai.models.FunctionParameters; import com.openai.models.ReasoningEffort; +import com.openai.models.ResponseFormatJsonObject; +import com.openai.models.ResponseFormatJsonSchema; +import com.openai.models.ResponseFormatText; import com.openai.models.chat.completions.*; import com.openai.models.completions.CompletionUsage; import io.micrometer.observation.Observation; @@ -703,8 +706,33 @@ else if (requestOptions.getModel() != null) { if (requestOptions.getPresencePenalty() != null) { builder.presencePenalty(requestOptions.getPresencePenalty()); } - if (requestOptions.getResponseFormatJsonSchema() != null) { - builder.responseFormat(requestOptions.getResponseFormatJsonSchema()); + if (requestOptions.getResponseFormat() != null) { + OpenAiOfficialChatResponseFormat responseFormat = requestOptions.getResponseFormat(); + if (responseFormat.getType().equals(OpenAiOfficialChatResponseFormat.Type.TEXT)) { + builder.responseFormat(ResponseFormatText.builder().build()); + } else if (responseFormat.getType().equals(OpenAiOfficialChatResponseFormat.Type.JSON_OBJECT)) { + builder.responseFormat(ResponseFormatJsonObject.builder().build()); + } else if (responseFormat.getType().equals(OpenAiOfficialChatResponseFormat.Type.JSON_SCHEMA)) { + String jsonSchemaString = responseFormat.getJsonSchema() != null ? responseFormat.getJsonSchema() : ""; + try { + com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); + ResponseFormatJsonSchema.JsonSchema.Builder jsonSchemaBuilder = ResponseFormatJsonSchema.JsonSchema.builder(); + jsonSchemaBuilder.name("json_schema"); + jsonSchemaBuilder.strict(true); + + ResponseFormatJsonSchema.JsonSchema.Schema schema = mapper.readValue(jsonSchemaString, ResponseFormatJsonSchema.JsonSchema.Schema.class); + + jsonSchemaBuilder.schema(schema); + + builder.responseFormat(ResponseFormatJsonSchema.builder() + .jsonSchema(jsonSchemaBuilder.build()) + .build()); + } catch (Exception e) { + throw new IllegalArgumentException("Failed to parse JSON schema: " + jsonSchemaString, e); + } + } else { + throw new IllegalArgumentException("Unsupported response format type: " + responseFormat.getType()); + } } if (requestOptions.getSeed() != null) { builder.seed(requestOptions.getSeed()); diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java index 2abfe77ace3..8ecb2d9e86e 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java @@ -68,7 +68,7 @@ public class OpenAiOfficialChatOptions extends AbstractOpenAiOfficialOptions imp private Double presencePenalty; - private ResponseFormatJsonSchema responseFormatJsonSchema; + private OpenAiOfficialChatResponseFormat responseFormat; private ResponseCreateParams.StreamOptions streamOptions; @@ -185,12 +185,12 @@ public void setPresencePenalty(Double presencePenalty) { this.presencePenalty = presencePenalty; } - public ResponseFormatJsonSchema getResponseFormatJsonSchema() { - return this.responseFormatJsonSchema; + public OpenAiOfficialChatResponseFormat getResponseFormat() { + return this.responseFormat; } - public void setResponseFormatJsonSchema(ResponseFormatJsonSchema responseFormatJsonSchema) { - this.responseFormatJsonSchema = responseFormatJsonSchema; + public void setResponseFormat(OpenAiOfficialChatResponseFormat responseFormat) { + this.responseFormat = responseFormat; } public ResponseCreateParams.StreamOptions getStreamOptions() { @@ -402,7 +402,7 @@ public boolean equals(Object o) { && Objects.equals(topLogprobs, options.topLogprobs) && Objects.equals(maxTokens, options.maxTokens) && Objects.equals(n, options.n) && Objects.equals(outputAudio, options.outputAudio) && Objects.equals(presencePenalty, options.presencePenalty) - && Objects.equals(responseFormatJsonSchema, options.responseFormatJsonSchema) + && Objects.equals(responseFormat, options.responseFormat) && Objects.equals(streamOptions, options.streamOptions) && Objects.equals(streamUsage, options.streamUsage) && Objects.equals(seed, options.seed) && Objects.equals(stop, options.stop) && Objects.equals(temperature, options.temperature) @@ -420,7 +420,7 @@ public boolean equals(Object o) { @Override public int hashCode() { return Objects.hash(frequencyPenalty, logitBias, logprobs, topLogprobs, maxTokens, n, outputAudio, - presencePenalty, responseFormatJsonSchema, streamOptions, streamUsage, seed, stop, temperature, topP, tools, + presencePenalty, responseFormat, streamOptions, streamUsage, seed, stop, temperature, topP, tools, toolChoice, user, parallelToolCalls, store, metadata, reasoningEffort, verbosity, serviceTier, toolCallbacks, toolNames, internalToolExecutionEnabled, httpHeaders, toolContext); } @@ -429,8 +429,8 @@ public int hashCode() { public String toString() { return "OpenAiOfficialChatOptions{" + "frequencyPenalty=" + frequencyPenalty + ", logitBias=" + logitBias + ", logprobs=" + logprobs + ", topLogprobs=" + topLogprobs + ", maxTokens=" + maxTokens + ", n=" + n - + ", outputAudio=" + outputAudio + ", presencePenalty=" + presencePenalty + ", responseFormatJsonSchema=" - + responseFormatJsonSchema + ", streamOptions=" + streamOptions + ", streamUsage=" + streamUsage + ", seed=" + + ", outputAudio=" + outputAudio + ", presencePenalty=" + presencePenalty + ", responseFormat=" + + responseFormat + ", streamOptions=" + streamOptions + ", streamUsage=" + streamUsage + ", seed=" + seed + ", stop=" + stop + ", temperature=" + temperature + ", topP=" + topP + ", tools=" + tools + ", toolChoice=" + toolChoice + ", user='" + user + '\'' + ", parallelToolCalls=" + parallelToolCalls + ", store=" + store + ", metadata=" + metadata + ", reasoningEffort='" + reasoningEffort + '\'' @@ -455,7 +455,7 @@ public Builder from(OpenAiOfficialChatOptions fromOptions) { this.options.setN(fromOptions.getN()); this.options.setOutputAudio(fromOptions.getOutputAudio()); this.options.setPresencePenalty(fromOptions.getPresencePenalty()); - this.options.setResponseFormatJsonSchema(fromOptions.getResponseFormatJsonSchema()); + this.options.setResponseFormat(fromOptions.getResponseFormat()); this.options.setStreamOptions(fromOptions.getStreamOptions()); this.options.setStreamUsage(fromOptions.getStreamUsage()); this.options.setSeed(fromOptions.getSeed()); @@ -514,8 +514,8 @@ public Builder merge(OpenAiOfficialChatOptions from) { if (from.getPresencePenalty() != null) { this.options.setPresencePenalty(from.getPresencePenalty()); } - if (from.getResponseFormatJsonSchema() != null) { - this.options.setResponseFormatJsonSchema(from.getResponseFormatJsonSchema()); + if (from.getResponseFormat() != null) { + this.options.setResponseFormat(from.getResponseFormat()); } if (from.getStreamOptions() != null) { this.options.setStreamOptions(from.getStreamOptions()); @@ -649,8 +649,8 @@ public Builder presencePenalty(Double presencePenalty) { return this; } - public Builder responseFormatJsonSchema(ResponseFormatJsonSchema responseFormatJsonSchema) { - this.options.setResponseFormatJsonSchema(responseFormatJsonSchema); + public Builder responseFormat(OpenAiOfficialChatResponseFormat responseFormat) { + this.options.setResponseFormat(responseFormat); return this; } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatResponseFormat.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatResponseFormat.java new file mode 100644 index 00000000000..cfc593c7946 --- /dev/null +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatResponseFormat.java @@ -0,0 +1,95 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaiofficial; + +/** + * Response format (text, json_object, json_schema) for OpenAiOfficialChatModel responses. + * + * @author Julien Dubois + */ +public class OpenAiOfficialChatResponseFormat { + + private Type type = Type.TEXT; + + private String jsonSchema; + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } + + public String getJsonSchema() { + return jsonSchema; + } + + public void setJsonSchema(String jsonSchema) { + this.jsonSchema = jsonSchema; + } + + public static Builder builder() { + return new Builder(); + } + + public static final class Builder { + + private final OpenAiOfficialChatResponseFormat openAiOfficialChatResponseFormat = new OpenAiOfficialChatResponseFormat(); + + private Builder() { + } + + public Builder type(Type type) { + this.openAiOfficialChatResponseFormat.setType(type); + return this; + } + + public Builder jsonSchema(String jsonSchema) { + this.openAiOfficialChatResponseFormat.setType(Type.JSON_SCHEMA); + this.openAiOfficialChatResponseFormat.setJsonSchema(jsonSchema); + return this; + } + + public OpenAiOfficialChatResponseFormat build() { + return this.openAiOfficialChatResponseFormat; + } + + } + + public enum Type { + + /** + * Generates a text response. (default) + */ + TEXT, + + /** + * Enables JSON mode, which guarantees the message the model generates is valid + * JSON. + */ + JSON_OBJECT, + + /** + * Enables Structured Outputs which guarantees the model will match your supplied + * JSON schema. + */ + JSON_SCHEMA + + } + +} diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java index 47a93812358..3261873aa43 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java @@ -55,4 +55,5 @@ public OpenAiOfficialChatModel openAiChatModel(TestObservationRegistry observati return new OpenAiOfficialChatModel(OpenAiOfficialChatOptions.builder().model(DEFAULT_CHAT_MODEL).build(), observationRegistry); } + } diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java index 462470ebf33..8c2a5419a5c 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java @@ -43,8 +43,7 @@ import static org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions.DEFAULT_CHAT_MODEL; /** - * Integration tests for observation instrumentation in - * {@link OpenAiOfficialChatModel}. + * Integration tests for observation instrumentation in {@link OpenAiOfficialChatModel}. * * @author Julien Dubois */ @@ -66,9 +65,7 @@ void setUp() { @Test void observationForChatOperation() { - var options = OpenAiOfficialChatOptions.builder() - .model(DEFAULT_CHAT_MODEL) - .build(); + var options = OpenAiOfficialChatOptions.builder().model(DEFAULT_CHAT_MODEL).build(); Prompt prompt = new Prompt("Why does a raven look like a desk?", options); @@ -83,10 +80,7 @@ void observationForChatOperation() { @Test void observationForStreamingChatOperation() { - var options = OpenAiOfficialChatOptions.builder() - .model(DEFAULT_CHAT_MODEL) - .streamUsage(true) - .build(); + var options = OpenAiOfficialChatOptions.builder().model(DEFAULT_CHAT_MODEL).streamUsage(true).build(); Prompt prompt = new Prompt("Why does a raven look like a desk?", options); @@ -117,9 +111,9 @@ private void validate(ChatResponseMetadata responseMetadata) { .that() .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_OPERATION_TYPE.asString(), AiOperationType.CHAT.value()) - .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_PROVIDER.asString(), AiProvider.OPENAI_OFFICIAL.value()) - .hasLowCardinalityKeyValue(LowCardinalityKeyNames.REQUEST_MODEL.asString(), - DEFAULT_CHAT_MODEL) + .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_PROVIDER.asString(), + AiProvider.OPENAI_OFFICIAL.value()) + .hasLowCardinalityKeyValue(LowCardinalityKeyNames.REQUEST_MODEL.asString(), DEFAULT_CHAT_MODEL) .hasLowCardinalityKeyValue(LowCardinalityKeyNames.RESPONSE_MODEL.asString(), responseMetadata.getModel()) .hasHighCardinalityKeyValue(HighCardinalityKeyNames.RESPONSE_ID.asString(), responseMetadata.getId()) .hasHighCardinalityKeyValue(HighCardinalityKeyNames.RESPONSE_FINISH_REASONS.asString(), "[\"STOP\"]") @@ -132,4 +126,5 @@ private void validate(ChatResponseMetadata responseMetadata) { .hasBeenStarted() .hasBeenStopped(); } + } diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java new file mode 100644 index 00000000000..f709984f1ac --- /dev/null +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java @@ -0,0 +1,271 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaiofficial.chat; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +import com.fasterxml.jackson.core.JacksonException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.openai.models.ResponseFormatJsonSchema; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.ai.chat.model.ChatResponse; +import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.ai.converter.BeanOutputConverter; +import org.springframework.ai.openaiofficial.OpenAiOfficialChatModel; +import org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions; +import org.springframework.ai.openaiofficial.OpenAiOfficialChatResponseFormat; +import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions.DEFAULT_CHAT_MODEL; + +/** + * Integration tests for the response format in {@link OpenAiOfficialChatModel}. + * + * @author Julien Dubois + */ +@SpringBootTest(classes = OpenAiOfficialTestConfiguration.class) +@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") +public class OpenAiOfficialChatModelResponseFormatIT { + + private static final ObjectMapper MAPPER = new ObjectMapper() + .enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS); + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + @Autowired + private OpenAiOfficialChatModel chatModel; + + public static boolean isValidJson(String json) { + try { + MAPPER.readTree(json); + } + catch (JacksonException e) { + return false; + } + return true; + } + + @Test + void jsonObject() { + + Prompt prompt = new Prompt("List 8 planets. Use JSON response", + OpenAiOfficialChatOptions.builder() + .responseFormat(OpenAiOfficialChatResponseFormat.builder() + .type(OpenAiOfficialChatResponseFormat.Type.JSON_OBJECT) + .build()) + .build()); + + ChatResponse response = this.chatModel.call(prompt); + + assertThat(response).isNotNull(); + + String content = response.getResult().getOutput().getText(); + + logger.info("Response content: {}", content); + + assertThat(isValidJson(content)).isTrue(); + } + + @Test + void jsonSchema() throws JsonMappingException, JsonProcessingException { + + var jsonSchema = """ + { + "type": "object", + "properties": { + "steps": { + "type": "array", + "items": { + "type": "object", + "properties": { + "explanation": { "type": "string" }, + "output": { "type": "string" } + }, + "required": ["explanation", "output"], + "additionalProperties": false + } + }, + "final_answer": { "type": "string" } + }, + "required": ["steps", "final_answer"], + "additionalProperties": false + } + """; + + Prompt prompt = new Prompt("how can I solve 8x + 7 = -23", + OpenAiOfficialChatOptions.builder() + .model(DEFAULT_CHAT_MODEL) + .responseFormat(OpenAiOfficialChatResponseFormat.builder().jsonSchema(jsonSchema).build()) + .build()); + + ChatResponse response = this.chatModel.call(prompt); + + assertThat(response).isNotNull(); + + String content = response.getResult().getOutput().getText(); + + logger.info("Response content: {}", content); + + assertThat(isValidJson(content)).isTrue(); + } + + @Test + void jsonSchemaThroughIndividualSetters() throws JsonProcessingException { + + var jsonSchema = """ + { + "type": "object", + "properties": { + "steps": { + "type": "array", + "items": { + "type": "object", + "properties": { + "explanation": { "type": "string" }, + "output": { "type": "string" } + }, + "required": ["explanation", "output"], + "additionalProperties": false + } + }, + "final_answer": { "type": "string" } + }, + "required": ["steps", "final_answer"], + "additionalProperties": false + } + """; + + ResponseFormatJsonSchema.JsonSchema.Schema responseSchema = MAPPER.readValue(jsonSchema, + ResponseFormatJsonSchema.JsonSchema.Schema.class); + + Prompt prompt = new Prompt("how can I solve 8x + 7 = -23", + OpenAiOfficialChatOptions.builder() + .model(DEFAULT_CHAT_MODEL) + .responseFormat(OpenAiOfficialChatResponseFormat.builder().jsonSchema(jsonSchema).build()) + .build()); + + ChatResponse response = this.chatModel.call(prompt); + + assertThat(response).isNotNull(); + + String content = response.getResult().getOutput().getText(); + + logger.info("Response content: {}", content); + + assertThat(isValidJson(content)).isTrue(); + } + + @Test + void jsonSchemaBeanConverter() throws JsonMappingException, JsonProcessingException { + + @JsonPropertyOrder({ "steps", "final_answer" }) + record MathReasoning(@JsonProperty(required = true, value = "steps") Steps steps, + @JsonProperty(required = true, value = "final_answer") String finalAnswer) { + + record Steps(@JsonProperty(required = true, value = "items") Items[] items) { + + @JsonPropertyOrder({ "output", "explanation" }) + record Items(@JsonProperty(required = true, value = "explanation") String explanation, + @JsonProperty(required = true, value = "output") String output) { + + } + + } + + } + + var outputConverter = new BeanOutputConverter<>(MathReasoning.class); + // @formatter:off + // CHECKSTYLE:OFF + var expectedJsonSchema = """ + { + "$schema" : "https://json-schema.org/draft/2020-12/schema", + "type" : "object", + "properties" : { + "steps" : { + "type" : "object", + "properties" : { + "items" : { + "type" : "array", + "items" : { + "type" : "object", + "properties" : { + "output" : { + "type" : "string" + }, + "explanation" : { + "type" : "string" + } + }, + "required" : [ "output", "explanation" ], + "additionalProperties" : false + } + } + }, + "required" : [ "items" ], + "additionalProperties" : false + }, + "final_answer" : { + "type" : "string" + } + }, + "required" : [ "steps", "final_answer" ], + "additionalProperties" : false + }"""; + // @formatter:on + // CHECKSTYLE:ON + var jsonSchema1 = outputConverter.getJsonSchema(); + + assertThat(jsonSchema1).isNotNull(); + assertThat(jsonSchema1).isEqualTo(expectedJsonSchema); + + Prompt prompt = new Prompt("how can I solve 8x + 7 = -23", + OpenAiOfficialChatOptions.builder() + .model(DEFAULT_CHAT_MODEL) + .responseFormat(OpenAiOfficialChatResponseFormat.builder().jsonSchema(jsonSchema1).build()) + .build()); + + ChatResponse response = this.chatModel.call(prompt); + + assertThat(response).isNotNull(); + + String content = response.getResult().getOutput().getText(); + + logger.info("Response content: {}", content); + + assertThat(isValidJson(content)).isTrue(); + + // Check if the order is correct as specified in the schema. Steps should come + // first before final answer. + assertThat(content.startsWith("{\"steps\":{\"items\":[")); + + MathReasoning mathReasoning = outputConverter.convert(content); + + assertThat(mathReasoning).isNotNull(); + logger.info(mathReasoning.toString()); + } + +} From acc4e284e33e98f6144e87e826b079707762ee67 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Fri, 14 Nov 2025 15:31:31 +0100 Subject: [PATCH 13/49] Implementation of the OpenAI Java SDK - Refactor the ResponseFormat Signed-off-by: Julien Dubois --- .../OpenAiOfficialChatModel.java | 84 +++++++++++++++- .../OpenAiOfficialChatOptions.java | 9 +- .../OpenAiOfficialChatResponseFormat.java | 95 ------------------- ...enAiOfficialChatModelResponseFormatIT.java | 14 +-- 4 files changed, 92 insertions(+), 110 deletions(-) delete mode 100644 models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatResponseFormat.java diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java index ed55463710f..7dcc9bd9bc9 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -707,12 +707,12 @@ else if (requestOptions.getModel() != null) { builder.presencePenalty(requestOptions.getPresencePenalty()); } if (requestOptions.getResponseFormat() != null) { - OpenAiOfficialChatResponseFormat responseFormat = requestOptions.getResponseFormat(); - if (responseFormat.getType().equals(OpenAiOfficialChatResponseFormat.Type.TEXT)) { + ResponseFormat responseFormat = requestOptions.getResponseFormat(); + if (responseFormat.getType().equals(ResponseFormat.Type.TEXT)) { builder.responseFormat(ResponseFormatText.builder().build()); - } else if (responseFormat.getType().equals(OpenAiOfficialChatResponseFormat.Type.JSON_OBJECT)) { + } else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_OBJECT)) { builder.responseFormat(ResponseFormatJsonObject.builder().build()); - } else if (responseFormat.getType().equals(OpenAiOfficialChatResponseFormat.Type.JSON_SCHEMA)) { + } else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_SCHEMA)) { String jsonSchemaString = responseFormat.getJsonSchema() != null ? responseFormat.getJsonSchema() : ""; try { com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); @@ -881,4 +881,80 @@ public void setObservationConvention(ChatModelObservationConvention observationC this.observationConvention = observationConvention; } + /** + * Response format (text, json_object, json_schema) for OpenAiOfficialChatModel responses. + * + * @author Julien Dubois + */ + public static class ResponseFormat { + + private Type type = Type.TEXT; + + private String jsonSchema; + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } + + public String getJsonSchema() { + return jsonSchema; + } + + public void setJsonSchema(String jsonSchema) { + this.jsonSchema = jsonSchema; + } + + public static Builder builder() { + return new Builder(); + } + + public static final class Builder { + + private final ResponseFormat responseFormat = new ResponseFormat(); + + private Builder() { + } + + public Builder type(Type type) { + this.responseFormat.setType(type); + return this; + } + + public Builder jsonSchema(String jsonSchema) { + this.responseFormat.setType(Type.JSON_SCHEMA); + this.responseFormat.setJsonSchema(jsonSchema); + return this; + } + + public ResponseFormat build() { + return this.responseFormat; + } + + } + + public enum Type { + + /** + * Generates a text response. (default) + */ + TEXT, + + /** + * Enables JSON mode, which guarantees the message the model generates is valid + * JSON. + */ + JSON_OBJECT, + + /** + * Enables Structured Outputs which guarantees the model will match your supplied + * JSON schema. + */ + JSON_SCHEMA + + } + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java index 8ecb2d9e86e..55969633317 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java @@ -17,7 +17,6 @@ package org.springframework.ai.openaiofficial; import com.openai.models.FunctionDefinition; -import com.openai.models.ResponseFormatJsonSchema; import com.openai.models.chat.completions.ChatCompletionAudioParam; import com.openai.models.chat.completions.ChatCompletionToolChoiceOption; import com.openai.models.responses.ResponseCreateParams; @@ -68,7 +67,7 @@ public class OpenAiOfficialChatOptions extends AbstractOpenAiOfficialOptions imp private Double presencePenalty; - private OpenAiOfficialChatResponseFormat responseFormat; + private OpenAiOfficialChatModel.ResponseFormat responseFormat; private ResponseCreateParams.StreamOptions streamOptions; @@ -185,11 +184,11 @@ public void setPresencePenalty(Double presencePenalty) { this.presencePenalty = presencePenalty; } - public OpenAiOfficialChatResponseFormat getResponseFormat() { + public OpenAiOfficialChatModel.ResponseFormat getResponseFormat() { return this.responseFormat; } - public void setResponseFormat(OpenAiOfficialChatResponseFormat responseFormat) { + public void setResponseFormat(OpenAiOfficialChatModel.ResponseFormat responseFormat) { this.responseFormat = responseFormat; } @@ -649,7 +648,7 @@ public Builder presencePenalty(Double presencePenalty) { return this; } - public Builder responseFormat(OpenAiOfficialChatResponseFormat responseFormat) { + public Builder responseFormat(OpenAiOfficialChatModel.ResponseFormat responseFormat) { this.options.setResponseFormat(responseFormat); return this; } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatResponseFormat.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatResponseFormat.java deleted file mode 100644 index cfc593c7946..00000000000 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatResponseFormat.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright 2023-2025 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.ai.openaiofficial; - -/** - * Response format (text, json_object, json_schema) for OpenAiOfficialChatModel responses. - * - * @author Julien Dubois - */ -public class OpenAiOfficialChatResponseFormat { - - private Type type = Type.TEXT; - - private String jsonSchema; - - public Type getType() { - return type; - } - - public void setType(Type type) { - this.type = type; - } - - public String getJsonSchema() { - return jsonSchema; - } - - public void setJsonSchema(String jsonSchema) { - this.jsonSchema = jsonSchema; - } - - public static Builder builder() { - return new Builder(); - } - - public static final class Builder { - - private final OpenAiOfficialChatResponseFormat openAiOfficialChatResponseFormat = new OpenAiOfficialChatResponseFormat(); - - private Builder() { - } - - public Builder type(Type type) { - this.openAiOfficialChatResponseFormat.setType(type); - return this; - } - - public Builder jsonSchema(String jsonSchema) { - this.openAiOfficialChatResponseFormat.setType(Type.JSON_SCHEMA); - this.openAiOfficialChatResponseFormat.setJsonSchema(jsonSchema); - return this; - } - - public OpenAiOfficialChatResponseFormat build() { - return this.openAiOfficialChatResponseFormat; - } - - } - - public enum Type { - - /** - * Generates a text response. (default) - */ - TEXT, - - /** - * Enables JSON mode, which guarantees the message the model generates is valid - * JSON. - */ - JSON_OBJECT, - - /** - * Enables Structured Outputs which guarantees the model will match your supplied - * JSON schema. - */ - JSON_SCHEMA - - } - -} diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java index f709984f1ac..c6ec404e348 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java @@ -33,7 +33,6 @@ import org.springframework.ai.converter.BeanOutputConverter; import org.springframework.ai.openaiofficial.OpenAiOfficialChatModel; import org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions; -import org.springframework.ai.openaiofficial.OpenAiOfficialChatResponseFormat; import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfiguration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -73,8 +72,8 @@ void jsonObject() { Prompt prompt = new Prompt("List 8 planets. Use JSON response", OpenAiOfficialChatOptions.builder() - .responseFormat(OpenAiOfficialChatResponseFormat.builder() - .type(OpenAiOfficialChatResponseFormat.Type.JSON_OBJECT) + .responseFormat(OpenAiOfficialChatModel.ResponseFormat.builder() + .type(OpenAiOfficialChatModel.ResponseFormat.Type.JSON_OBJECT) .build()) .build()); @@ -118,7 +117,10 @@ void jsonSchema() throws JsonMappingException, JsonProcessingException { Prompt prompt = new Prompt("how can I solve 8x + 7 = -23", OpenAiOfficialChatOptions.builder() .model(DEFAULT_CHAT_MODEL) - .responseFormat(OpenAiOfficialChatResponseFormat.builder().jsonSchema(jsonSchema).build()) + .responseFormat(OpenAiOfficialChatModel.ResponseFormat.builder() + .type(OpenAiOfficialChatModel.ResponseFormat.Type.JSON_SCHEMA) + .jsonSchema(jsonSchema) + .build()) .build()); ChatResponse response = this.chatModel.call(prompt); @@ -164,7 +166,7 @@ void jsonSchemaThroughIndividualSetters() throws JsonProcessingException { Prompt prompt = new Prompt("how can I solve 8x + 7 = -23", OpenAiOfficialChatOptions.builder() .model(DEFAULT_CHAT_MODEL) - .responseFormat(OpenAiOfficialChatResponseFormat.builder().jsonSchema(jsonSchema).build()) + .responseFormat(OpenAiOfficialChatModel.ResponseFormat.builder().jsonSchema(jsonSchema).build()) .build()); ChatResponse response = this.chatModel.call(prompt); @@ -245,7 +247,7 @@ record Items(@JsonProperty(required = true, value = "explanation") String explan Prompt prompt = new Prompt("how can I solve 8x + 7 = -23", OpenAiOfficialChatOptions.builder() .model(DEFAULT_CHAT_MODEL) - .responseFormat(OpenAiOfficialChatResponseFormat.builder().jsonSchema(jsonSchema1).build()) + .responseFormat(OpenAiOfficialChatModel.ResponseFormat.builder().jsonSchema(jsonSchema1).build()) .build()); ChatResponse response = this.chatModel.call(prompt); From 1e88cb5156a507a4122ddc68247990f6d1669308 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Fri, 14 Nov 2025 16:59:21 +0100 Subject: [PATCH 14/49] Implementation of the OpenAI Java SDK - Fix the tools calling and structured outputs Signed-off-by: Julien Dubois --- .../OpenAiOfficialChatModel.java | 22 ++++++++++--------- .../ai/openaiofficial/chat/ActorsFilms.java | 2 +- .../chat/MockWeatherService.java | 9 ++++++-- .../chat/OpenAiOfficialChatModelIT.java | 6 ++--- ...enAiOfficialChatModelResponseFormatIT.java | 9 ++------ 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java index 7dcc9bd9bc9..2bbd80294bb 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -607,7 +607,7 @@ else if (mimeType.startsWith("audio/")) { builder.role(JsonValue.from(MessageType.SYSTEM.getValue())); } - return ChatCompletionMessageParam.ofUser(builder.build()); + return List.of(ChatCompletionMessageParam.ofUser(builder.build())); } else if (message.getMessageType() == MessageType.ASSISTANT) { var assistantMessage = (AssistantMessage) message; @@ -636,7 +636,7 @@ else if (message.getMessageType() == MessageType.ASSISTANT) { builder.toolCalls(toolCalls); } - return ChatCompletionMessageParam.ofAssistant(builder.build()); + return List.of(ChatCompletionMessageParam.ofAssistant(builder.build())); } else if (message.getMessageType() == MessageType.TOOL) { ToolResponseMessage toolMessage = (ToolResponseMessage) message; @@ -646,20 +646,22 @@ else if (message.getMessageType() == MessageType.TOOL) { builder.role(JsonValue.from(MessageType.TOOL.getValue())); if (toolMessage.getResponses().isEmpty()) { - return ChatCompletionMessageParam.ofTool(builder.build()); + return List.of(ChatCompletionMessageParam.ofTool(builder.build())); } - String callId = toolMessage.getResponses().get(0).id(); - String callResponse = toolMessage.getResponses().get(0).responseData(); + return toolMessage.getResponses().stream().map(response -> { + String callId = response.id(); + String callResponse = response.responseData(); - return ChatCompletionMessageParam.ofTool(builder - .toolCallId(callId) - .content(callResponse) - .build()); + return ChatCompletionMessageParam.ofTool(builder + .toolCallId(callId) + .content(callResponse) + .build()); + }).toList(); } else { throw new IllegalArgumentException("Unsupported message type: " + message.getMessageType()); } - }).toList(); + }).flatMap(List::stream).toList(); ChatCompletionCreateParams.Builder builder = ChatCompletionCreateParams.builder(); diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/ActorsFilms.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/ActorsFilms.java index 453070320ce..0271bb43af3 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/ActorsFilms.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/ActorsFilms.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/MockWeatherService.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/MockWeatherService.java index af52c23edf0..482925e0962 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/MockWeatherService.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/MockWeatherService.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,14 +21,19 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyDescription; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.function.Function; public class MockWeatherService implements Function { + private final Logger logger = LoggerFactory.getLogger(MockWeatherService.class); + @Override public Response apply(Request request) { - + logger.info("--------- Received weather request for location: " + request.location() + ", lat: " + request.lat() + + ", lon: " + request.lon() + ", unit: " + request.unit()); double temperature = 0; if (request.location().contains("Paris")) { temperature = 15; diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java index bc26e291e1b..16f0f8f2d04 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java @@ -438,9 +438,9 @@ void functionCallUsageTest() { assertThat(usage).isNotNull(); assertThat(usage).isNotInstanceOf(EmptyUsage.class); assertThat(usage).isInstanceOf(DefaultUsage.class); - assertThat(usage.getPromptTokens()).isGreaterThan(100).isLessThan(300); - assertThat(usage.getCompletionTokens()).isGreaterThan(230).isLessThan(400); - assertThat(usage.getTotalTokens()).isGreaterThan(450).isLessThan(650); + assertThat(usage.getPromptTokens()).isGreaterThan(500).isLessThan(800); + assertThat(usage.getCompletionTokens()).isGreaterThan(800).isLessThan(1200); + assertThat(usage.getTotalTokens()).isGreaterThan(1500).isLessThan(2000); } @Test diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java index c6ec404e348..90e46926539 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java @@ -21,9 +21,7 @@ import com.fasterxml.jackson.core.JacksonException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.openai.models.ResponseFormatJsonSchema; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; import org.slf4j.Logger; @@ -89,7 +87,7 @@ void jsonObject() { } @Test - void jsonSchema() throws JsonMappingException, JsonProcessingException { + void jsonSchema() { var jsonSchema = """ { @@ -160,9 +158,6 @@ void jsonSchemaThroughIndividualSetters() throws JsonProcessingException { } """; - ResponseFormatJsonSchema.JsonSchema.Schema responseSchema = MAPPER.readValue(jsonSchema, - ResponseFormatJsonSchema.JsonSchema.Schema.class); - Prompt prompt = new Prompt("how can I solve 8x + 7 = -23", OpenAiOfficialChatOptions.builder() .model(DEFAULT_CHAT_MODEL) @@ -181,7 +176,7 @@ void jsonSchemaThroughIndividualSetters() throws JsonProcessingException { } @Test - void jsonSchemaBeanConverter() throws JsonMappingException, JsonProcessingException { + void jsonSchemaBeanConverter() { @JsonPropertyOrder({ "steps", "final_answer" }) record MathReasoning(@JsonProperty(required = true, value = "steps") Steps steps, From cfe0190e42f23e8138628f7524215408616b3c49 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Fri, 14 Nov 2025 17:41:38 +0100 Subject: [PATCH 15/49] Implementation of the OpenAI Java SDK - Fix tests Signed-off-by: Julien Dubois --- .../ai/openaiofficial/chat/MockWeatherService.java | 4 ++-- .../chat/OpenAiOfficialChatModelIT.java | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/MockWeatherService.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/MockWeatherService.java index 482925e0962..50f8ab11067 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/MockWeatherService.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/MockWeatherService.java @@ -32,7 +32,7 @@ public class MockWeatherService implements Function messages = new ArrayList<>(List.of(userMessage)); @@ -387,12 +387,11 @@ void functionCallTest() { @Test void streamFunctionCallTest() { - UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?"); + UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris? Answer in Celsius."); List messages = new ArrayList<>(List.of(userMessage)); var promptOptions = OpenAiOfficialChatOptions.builder() - // .withModel(OpenAiApi.ChatModel.GPT_4_TURBO_PREVIEW.getValue()) .toolCallbacks(List.of(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService()) .description("Get the weather in location") .inputType(MockWeatherService.Request.class) @@ -409,6 +408,7 @@ void streamFunctionCallTest() { .map(Generation::getOutput) .map(AssistantMessage::getText) .collect(Collectors.joining()); + logger.info("Response: {}", content); assertThat(content).containsAnyOf("30.0", "30"); @@ -419,7 +419,7 @@ void streamFunctionCallTest() { @Test void functionCallUsageTest() { - UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?"); + UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris? Answer in Celsius."); List messages = new ArrayList<>(List.of(userMessage)); @@ -446,7 +446,7 @@ void functionCallUsageTest() { @Test void streamFunctionCallUsageTest() { - UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?"); + UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris? Answer in Celsius."); List messages = new ArrayList<>(List.of(userMessage)); From 72ebb33d7bb3da06a52bcd81fc0048c4830c3d0b Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Fri, 14 Nov 2025 19:09:04 +0100 Subject: [PATCH 16/49] Implementation of the OpenAI Java SDK - Fix tests Signed-off-by: Julien Dubois --- .../ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java index 811c4784d7a..72f1769b900 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java @@ -439,8 +439,8 @@ void functionCallUsageTest() { assertThat(usage).isNotInstanceOf(EmptyUsage.class); assertThat(usage).isInstanceOf(DefaultUsage.class); assertThat(usage.getPromptTokens()).isGreaterThan(500).isLessThan(800); - assertThat(usage.getCompletionTokens()).isGreaterThan(800).isLessThan(1200); - assertThat(usage.getTotalTokens()).isGreaterThan(1500).isLessThan(2000); + assertThat(usage.getCompletionTokens()).isGreaterThan(600).isLessThan(1200); + assertThat(usage.getTotalTokens()).isGreaterThan(1200).isLessThan(2000); } @Test From 54f5ceb97e8d2d5df60c57838b91dad8f4f6816d Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Mon, 17 Nov 2025 12:02:52 +0100 Subject: [PATCH 17/49] Implementation of the OpenAI Java SDK - Fix tests Signed-off-by: Julien Dubois --- .../openaiofficial/OpenAiOfficialChatModel.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java index 2bbd80294bb..7c725c1202d 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -285,19 +285,17 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha this.openAiClientAsync.chat().completions().createStreaming(request).subscribe(chunk -> { try { ChatCompletion chatCompletion = chunkToChatCompletion(chunk); - // If an id is not provided, set to "NO_ID" (for compatible APIs). - chatCompletion.id(); String id = chatCompletion.id(); - - List generations = chatCompletion.choices().stream().map(choice -> { // @formatter:off + List generations = chatCompletion.choices().stream().map(choice -> { roleMap.putIfAbsent(id, choice.message()._role().asString().isPresent() ? choice.message()._role().asStringOrThrow() : ""); Map metadata = Map.of( "id", id, "role", roleMap.getOrDefault(id, ""), "index", choice.index(), - "finishReason", choice.finishReason().asString(), + "finishReason", choice.finishReason().value(), "refusal", choice.message().refusal().isPresent() ? choice.message().refusal() : "", "annotations", choice.message().annotations().isPresent() ? choice.message().annotations() : List.of()); + return buildGeneration(choice, metadata); }).toList(); @@ -437,8 +435,13 @@ private ChatCompletion chunkToChatCompletion(ChatCompletionChunk chunk) { List choices = chunk.choices() .stream() .map(chunkChoice -> { + ChatCompletion.Choice.FinishReason finishReason = ChatCompletion.Choice.FinishReason.of(""); + if (chunkChoice.finishReason().isPresent()) { + finishReason = ChatCompletion.Choice.FinishReason.of(chunkChoice.finishReason().get().value().name().toLowerCase()); + } + ChatCompletion.Choice.Builder choiceBuilder = ChatCompletion.Choice.builder() - .finishReason(ChatCompletion.Choice.FinishReason.of(chunkChoice.finishReason().toString())) + .finishReason(finishReason) .index(chunkChoice.index()) .message(ChatCompletionMessage.builder() .content(chunkChoice.delta().content()) From 67f18c95245fb477bebca7dbeec0ee450a1035d0 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Mon, 17 Nov 2025 13:35:59 +0100 Subject: [PATCH 18/49] Implementation of the OpenAI Java SDK - Fix tests Signed-off-by: Julien Dubois --- .../chat/OpenAiOfficialChatModelObservationIT.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java index 8c2a5419a5c..5060d416c28 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java @@ -63,7 +63,7 @@ void setUp() { } @Test - void observationForChatOperation() { + void observationForChatOperation() throws InterruptedException { var options = OpenAiOfficialChatOptions.builder().model(DEFAULT_CHAT_MODEL).build(); @@ -79,7 +79,7 @@ void observationForChatOperation() { } @Test - void observationForStreamingChatOperation() { + void observationForStreamingChatOperation() throws InterruptedException { var options = OpenAiOfficialChatOptions.builder().model(DEFAULT_CHAT_MODEL).streamUsage(true).build(); Prompt prompt = new Prompt("Why does a raven look like a desk?", options); @@ -104,7 +104,9 @@ void observationForStreamingChatOperation() { validate(responseMetadata); } - private void validate(ChatResponseMetadata responseMetadata) { + private void validate(ChatResponseMetadata responseMetadata) throws InterruptedException { + Thread.sleep(100); // Wait for observation to be recorded + TestObservationRegistryAssert.assertThat(this.observationRegistry) .doesNotHaveAnyRemainingCurrentObservation() .hasObservationWithNameEqualTo(DefaultChatModelObservationConvention.DEFAULT_NAME) From df4f135b4cdb862370df8b071aff60112e0c188e Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Mon, 17 Nov 2025 21:34:44 +0100 Subject: [PATCH 19/49] Implementation of the OpenAI Java SDK - Added support for tools calling in streaming mode Signed-off-by: Julien Dubois --- .../OpenAiOfficialChatModel.java | 286 +++++++++++------- .../chat/OpenAiOfficialChatModelIT.java | 6 +- src/checkstyle/checkstyle-suppressions.xml | 1 + 3 files changed, 186 insertions(+), 107 deletions(-) diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java index 7c725c1202d..82cc326efd3 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -63,6 +63,7 @@ import org.springframework.ai.tool.definition.ToolDefinition; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; import reactor.core.publisher.Flux; import reactor.core.scheduler.Schedulers; @@ -262,141 +263,188 @@ public Flux stream(Prompt prompt) { return internalStream(requestPrompt, null); } + public AssistantMessage safeAssistantMessage(ChatResponse response) { + if (response == null) return null; + Generation gen = response.getResult(); + if (gen == null) return null; + if (gen.getOutput() instanceof AssistantMessage) { + return (AssistantMessage) gen.getOutput(); + } + return null; + } + public Flux internalStream(Prompt prompt, ChatResponse previousChatResponse) { return Flux.deferContextual(contextView -> { ChatCompletionCreateParams request = createRequest(prompt, true); - - // For chunked responses, only the first chunk contains the choice role. - // The rest of the chunks with same ID share the same role. ConcurrentHashMap roleMap = new ConcurrentHashMap<>(); - final ChatModelObservationContext observationContext = ChatModelObservationContext.builder() .prompt(prompt) .provider(AiProvider.OPENAI_OFFICIAL.value()) .build(); - Observation observation = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION.observation( - this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, - this.observationRegistry); - + this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, + this.observationRegistry); observation.parentObservation(contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null)).start(); - Flux chatResponses = Flux.create(sink -> { + Flux chatResponses = Flux.create(sink -> { this.openAiClientAsync.chat().completions().createStreaming(request).subscribe(chunk -> { try { ChatCompletion chatCompletion = chunkToChatCompletion(chunk); String id = chatCompletion.id(); List generations = chatCompletion.choices().stream().map(choice -> { - roleMap.putIfAbsent(id, choice.message()._role().asString().isPresent() ? choice.message()._role().asStringOrThrow() : ""); - Map metadata = Map.of( - "id", id, - "role", roleMap.getOrDefault(id, ""), - "index", choice.index(), - "finishReason", choice.finishReason().value(), - "refusal", choice.message().refusal().isPresent() ? choice.message().refusal() : "", - "annotations", choice.message().annotations().isPresent() ? choice.message().annotations() : List.of()); - - return buildGeneration(choice, metadata); - }).toList(); - - Optional usage = chatCompletion.usage(); - Usage currentChatResponseUsage = usage.isPresent()? getDefaultUsage(usage.get()) : new EmptyUsage(); - Usage accumulatedUsage = UsageCalculator.getCumulativeUsage(currentChatResponseUsage, - previousChatResponse); - ChatResponse response = new ChatResponse(generations, from(chatCompletion, accumulatedUsage)); - sink.next(response); - } - catch (Exception e) { - logger.error("Error processing chat completion", e); - sink.error(e); + roleMap.putIfAbsent(id, choice.message()._role().asString().isPresent() ? choice.message()._role().asStringOrThrow() : ""); + Map metadata = new HashMap<>(Map.of( + "id", id, + "role", roleMap.getOrDefault(id, ""), + "index", choice.index(), + "finishReason", choice.finishReason().value(), + "refusal", choice.message().refusal().isPresent() ? choice.message().refusal() : "", + "annotations", choice.message().annotations().isPresent() ? choice.message().annotations() : List.of())); + metadata.put("chunkChoice", chunk.choices().get((int) choice.index())); + return buildGeneration(choice, metadata); + }).toList(); + Optional usage = chatCompletion.usage(); + CompletionUsage usageVal = usage.orElse(null); + Usage currentUsage = usageVal != null ? getDefaultUsage(usageVal) : new EmptyUsage(); + Usage accumulated = UsageCalculator.getCumulativeUsage(currentUsage, previousChatResponse); + sink.next(new ChatResponse(generations, from(chatCompletion, accumulated))); + } + catch (Exception e) { + logger.error("Error processing chat completion", e); + sink.error(e); + } + }).onCompleteFuture().whenComplete((unused, throwable) -> { + if (throwable != null) sink.error(throwable); else sink.complete(); + }); + }) + .buffer(2,1) + .map(buffer -> { + ChatResponse first = buffer.get(0); + if (request.streamOptions().isPresent() && buffer.size()==2) { + ChatResponse second = buffer.get(1); + if (second != null) { + Usage usage = second.getMetadata().getUsage(); + if (!UsageCalculator.isEmpty(usage)) { + return new ChatResponse(first.getResults(), from(first.getMetadata(), usage)); } - }).onCompleteFuture().whenComplete((unused, throwable) -> { - if (throwable != null) { - sink.error(throwable); + } + } + return first; + }); + + Flux flux = chatResponses + .contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation)); + + return flux.collectList().flatMapMany(list -> { + if (list.isEmpty()) return Flux.empty(); + boolean hasToolCalls = list.stream().map(this::safeAssistantMessage).filter(Objects::nonNull) + .anyMatch(am -> !CollectionUtils.isEmpty(am.getToolCalls())); + if (!hasToolCalls) { + observationContext.setResponse(list.get(list.size()-1)); + return Flux.fromIterable(list); + } + Map builders = new HashMap<>(); + StringBuilder text = new StringBuilder(); + ChatResponseMetadata finalMetadata = null; + ChatGenerationMetadata finalGenMetadata = null; + Map props = new HashMap<>(); + for (ChatResponse cr : list) { + AssistantMessage am = safeAssistantMessage(cr); + if (am == null) continue; + if (am.getText()!=null) text.append(am.getText()); + if (am.getMetadata()!=null) props.putAll(am.getMetadata()); + if (!CollectionUtils.isEmpty(am.getToolCalls())) { + Object ccObj = am.getMetadata().get("chunkChoice"); + if (ccObj instanceof ChatCompletionChunk.Choice cc && cc.delta().toolCalls().isPresent()) { + List deltaCalls = cc.delta().toolCalls().get(); + for (int i=0;inew ToolCallBuilder()); + b.merge(tc); + } } else { - sink.complete(); + for (AssistantMessage.ToolCall tc : am.getToolCalls()) { + ToolCallBuilder b = builders.computeIfAbsent(tc.id(), k->new ToolCallBuilder()); + b.merge(tc); + } } - }); - }) - .buffer(2, 1) - .map(bufferList -> { - ChatResponse firstResponse = (ChatResponse) bufferList.get(0); - if (request.streamOptions().isPresent()) { - if (bufferList.size() == 2) { - ChatResponse secondResponse = (ChatResponse) bufferList.get(1); - if (secondResponse!=null) { - // This is the usage from the final Chat response for a - // given Chat request. - Usage usage = secondResponse.getMetadata().getUsage(); - if (!UsageCalculator.isEmpty(usage)) { - // Store the usage from the final response to the - // penultimate response for accumulation. - return new ChatResponse(firstResponse.getResults(), - from(firstResponse.getMetadata(), usage)); - } - } - } - } - return firstResponse; - }); - - Flux flux = chatResponses.flatMap(response -> { - assert prompt.getOptions() != null; - if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(prompt.getOptions(), response)) { - // FIXME: bounded elastic needs to be used since tool calling - // is currently only synchronous + } + Generation g = cr.getResult(); + if (g!=null && g.getMetadata()!=null && g.getMetadata()!=ChatGenerationMetadata.NULL) { + finalGenMetadata = g.getMetadata(); + } + if (cr.getMetadata()!=null) finalMetadata = cr.getMetadata(); + } + List merged = builders.values().stream().map(ToolCallBuilder::build) + .filter(tc -> StringUtils.hasText(tc.name())) + .toList(); + AssistantMessage.Builder amb = AssistantMessage.builder().content(text.toString()).properties(props); + if (!merged.isEmpty()) amb.toolCalls(merged); + AssistantMessage finalMsg = amb.build(); + Generation finalGen = new Generation(finalMsg, finalGenMetadata!=null?finalGenMetadata:ChatGenerationMetadata.NULL); + ChatResponse aggregated = new ChatResponse(List.of(finalGen), finalMetadata); + observationContext.setResponse(aggregated); + if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(prompt.getOptions(), aggregated)) { return Flux.deferContextual(ctx -> { - ToolExecutionResult toolExecutionResult; - try { - ToolCallReactiveContextHolder.setContext(ctx); - toolExecutionResult = this.toolCallingManager.executeToolCalls(prompt, response); - } - finally { - ToolCallReactiveContextHolder.clearContext(); - } - if (toolExecutionResult.returnDirect()) { - // Return tool execution result directly to the client. - return Flux.just(ChatResponse.builder() - .from(response) - .generations(ToolExecutionResult.buildGenerations(toolExecutionResult)) - .build()); - } - else { - // Send the tool execution result back to the model. - return this.internalStream( - new Prompt(toolExecutionResult.conversationHistory(), prompt.getOptions()), - response); - } + ToolExecutionResult ter; + try { ToolCallReactiveContextHolder.setContext(ctx); ter = this.toolCallingManager.executeToolCalls(prompt, aggregated);} finally { ToolCallReactiveContextHolder.clearContext(); } + if (ter.returnDirect()) return Flux.just(ChatResponse.builder().from(aggregated).generations(ToolExecutionResult.buildGenerations(ter)).build()); + return this.internalStream(new Prompt(ter.conversationHistory(), prompt.getOptions()), aggregated); }).subscribeOn(Schedulers.boundedElastic()); } - else { - return Flux.just(response); - } + return Flux.just(aggregated); }) - .doOnError(observation::error) - .doFinally(s -> observation.stop()) - .contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation)); - - return new MessageAggregator().aggregate(flux, observationContext::setResponse); - + .doOnError(observation::error) + .doFinally(s -> observation.stop()); }); } private Generation buildGeneration(ChatCompletion.Choice choice, Map metadata) { ChatCompletionMessage message = choice.message(); - List toolCalls = message.toolCalls() - .map(toolCallsList -> toolCallsList.stream() - .filter(toolCall -> toolCall.function().isPresent()) - .map(toolCall -> { - return new AssistantMessage.ToolCall(toolCall.function().get().id(), "function", toolCall.function().get().function().name(), - toolCall.function().get().function().arguments()); - }).toList()).orElse(List.of()); + List toolCalls = new ArrayList<>(); + + if (metadata.containsKey("chunkChoice")) { + Object chunkChoiceObj = metadata.get("chunkChoice"); + if (chunkChoiceObj instanceof ChatCompletionChunk.Choice chunkChoice) { + if (chunkChoice.delta().toolCalls().isPresent()) { + toolCalls = chunkChoice.delta().toolCalls().get().stream() + .filter(tc -> tc.function().isPresent()) + .map(tc -> { + var funcOpt = tc.function(); + if (funcOpt.isEmpty()) return null; + var func = funcOpt.get(); + String id = tc.id().orElse(""); + String name = func.name().orElse(""); + String arguments = func.arguments().orElse(""); + return new AssistantMessage.ToolCall(id, "function", name, arguments); + }) + .filter(Objects::nonNull) + .toList(); + } + } + } + else { + toolCalls = message.toolCalls().map(list -> list.stream() + .filter(tc -> tc.function().isPresent()) + .map(tc -> { + var opt = tc.function(); + if (opt.isEmpty()) return null; + var funcCall = opt.get(); + var functionDef = funcCall.function(); + String id = funcCall.id(); + String name = functionDef.name(); + String arguments = functionDef.arguments(); + return new AssistantMessage.ToolCall(id, "function", name, arguments); + }) + .filter(Objects::nonNull) + .toList()).orElse(List.of()); + } var generationMetadataBuilder = ChatGenerationMetadata.builder() .finishReason(choice.finishReason().value().name()); - - String textContent = message.content().isPresent() ? message.content().get() : ""; - + String textContent = message.content().orElse(""); var assistantMessage = AssistantMessage.builder() .content(textContent) .properties(metadata) @@ -962,4 +1010,34 @@ public enum Type { } } + + /** + * Helper class to merge streaming tool calls that arrive in pieces across multiple chunks. + * In OpenAI streaming, a tool call's ID, name, and arguments can arrive in separate chunks. + */ + private static class ToolCallBuilder { + private String id = ""; + private String type = "function"; + private String name = ""; + private StringBuilder arguments = new StringBuilder(); + + void merge(AssistantMessage.ToolCall toolCall) { + if (toolCall.id() != null && !toolCall.id().isEmpty()) { + this.id = toolCall.id(); + } + if (toolCall.type() != null && !toolCall.type().isEmpty()) { + this.type = toolCall.type(); + } + if (toolCall.name() != null && !toolCall.name().isEmpty()) { + this.name = toolCall.name(); + } + if (toolCall.arguments() != null && !toolCall.arguments().isEmpty()) { + this.arguments.append(toolCall.arguments()); + } + } + + AssistantMessage.ToolCall build() { + return new AssistantMessage.ToolCall(id, type, name, arguments.toString()); + } + } } diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java index 72f1769b900..7895820c967 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java @@ -466,9 +466,9 @@ void streamFunctionCallUsageTest() { assertThat(usage).isNotNull(); assertThat(usage).isNotInstanceOf(EmptyUsage.class); assertThat(usage).isInstanceOf(DefaultUsage.class); - assertThat(usage.getPromptTokens()).isGreaterThan(100).isLessThan(250); - assertThat(usage.getCompletionTokens()).isGreaterThan(100).isLessThan(300); - assertThat(usage.getTotalTokens()).isGreaterThan(250).isLessThan(500); + assertThat(usage.getPromptTokens()).isGreaterThan(500).isLessThan(800); + assertThat(usage.getCompletionTokens()).isGreaterThan(200).isLessThan(500); + assertThat(usage.getTotalTokens()).isGreaterThan(600).isLessThan(1300); } @Test diff --git a/src/checkstyle/checkstyle-suppressions.xml b/src/checkstyle/checkstyle-suppressions.xml index a6ba36d1ee5..112ef964c87 100644 --- a/src/checkstyle/checkstyle-suppressions.xml +++ b/src/checkstyle/checkstyle-suppressions.xml @@ -41,6 +41,7 @@ + From 91333a0156d9b007dd1ce0383cbf5fd2005c632f Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Tue, 18 Nov 2025 10:18:21 +0100 Subject: [PATCH 20/49] Implementation of the OpenAI Java SDK - All tests are green Signed-off-by: Julien Dubois --- .../OpenAiOfficialChatModel.java | 76 ++++++++++--------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java index 82cc326efd3..03616ff5faa 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -44,7 +44,6 @@ import org.springframework.ai.chat.model.ChatModel; import org.springframework.ai.chat.model.ChatResponse; import org.springframework.ai.chat.model.Generation; -import org.springframework.ai.chat.model.MessageAggregator; import org.springframework.ai.chat.observation.ChatModelObservationContext; import org.springframework.ai.chat.observation.ChatModelObservationConvention; import org.springframework.ai.chat.observation.ChatModelObservationDocumentation; @@ -293,14 +292,16 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha String id = chatCompletion.id(); List generations = chatCompletion.choices().stream().map(choice -> { roleMap.putIfAbsent(id, choice.message()._role().asString().isPresent() ? choice.message()._role().asStringOrThrow() : ""); - Map metadata = new HashMap<>(Map.of( - "id", id, - "role", roleMap.getOrDefault(id, ""), - "index", choice.index(), - "finishReason", choice.finishReason().value(), - "refusal", choice.message().refusal().isPresent() ? choice.message().refusal() : "", - "annotations", choice.message().annotations().isPresent() ? choice.message().annotations() : List.of())); - metadata.put("chunkChoice", chunk.choices().get((int) choice.index())); + + Map metadata = Map.of( + "id", id, + "role", roleMap.getOrDefault(id, ""), + "index", choice.index(), + "finishReason", choice.finishReason().value(), + "refusal", choice.message().refusal().isPresent() ? choice.message().refusal() : "", + "annotations", choice.message().annotations().isPresent() ? choice.message().annotations() : List.of(), + "chunkChoice", chunk.choices().get((int) choice.index())); + return buildGeneration(choice, metadata); }).toList(); Optional usage = chatCompletion.usage(); @@ -340,7 +341,12 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha boolean hasToolCalls = list.stream().map(this::safeAssistantMessage).filter(Objects::nonNull) .anyMatch(am -> !CollectionUtils.isEmpty(am.getToolCalls())); if (!hasToolCalls) { - observationContext.setResponse(list.get(list.size()-1)); + if (list.size() > 2) { + ChatResponse penultimateResponse = list.get(list.size()-2); // Get the finish reason + ChatResponse lastResponse = list.get(list.size()-1); // Get the usage + Usage usage = lastResponse.getMetadata().getUsage(); + observationContext.setResponse(new ChatResponse(penultimateResponse.getResults(), from(penultimateResponse.getMetadata(), usage))); + } return Flux.fromIterable(list); } Map builders = new HashMap<>(); @@ -348,50 +354,52 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha ChatResponseMetadata finalMetadata = null; ChatGenerationMetadata finalGenMetadata = null; Map props = new HashMap<>(); - for (ChatResponse cr : list) { - AssistantMessage am = safeAssistantMessage(cr); + for (ChatResponse chatResponse : list) { + AssistantMessage am = safeAssistantMessage(chatResponse); if (am == null) continue; - if (am.getText()!=null) text.append(am.getText()); - if (am.getMetadata()!=null) props.putAll(am.getMetadata()); + if (am.getText() != null) text.append(am.getText()); + if (am.getMetadata() != null) props.putAll(am.getMetadata()); if (!CollectionUtils.isEmpty(am.getToolCalls())) { Object ccObj = am.getMetadata().get("chunkChoice"); - if (ccObj instanceof ChatCompletionChunk.Choice cc && cc.delta().toolCalls().isPresent()) { - List deltaCalls = cc.delta().toolCalls().get(); - for (int i=0;i deltaCalls = chunkChoice.delta().toolCalls().get(); + for (int i=0; inew ToolCallBuilder()); - b.merge(tc); + String key = chunkChoice.index()+"-"+dtc.index(); + ToolCallBuilder toolCallBuilder = builders.computeIfAbsent(key,k->new ToolCallBuilder()); + toolCallBuilder.merge(tc); } } else { for (AssistantMessage.ToolCall tc : am.getToolCalls()) { - ToolCallBuilder b = builders.computeIfAbsent(tc.id(), k->new ToolCallBuilder()); - b.merge(tc); + ToolCallBuilder toolCallBuilder = builders.computeIfAbsent(tc.id(), k->new ToolCallBuilder()); + toolCallBuilder.merge(tc); } } } - Generation g = cr.getResult(); - if (g!=null && g.getMetadata()!=null && g.getMetadata()!=ChatGenerationMetadata.NULL) { - finalGenMetadata = g.getMetadata(); + Generation generation = chatResponse.getResult(); + if (generation!=null && generation.getMetadata()!=null && generation.getMetadata()!=ChatGenerationMetadata.NULL) { + finalGenMetadata = generation.getMetadata(); } - if (cr.getMetadata()!=null) finalMetadata = cr.getMetadata(); + if (chatResponse.getMetadata()!=null) finalMetadata = chatResponse.getMetadata(); } List merged = builders.values().stream().map(ToolCallBuilder::build) .filter(tc -> StringUtils.hasText(tc.name())) .toList(); - AssistantMessage.Builder amb = AssistantMessage.builder().content(text.toString()).properties(props); - if (!merged.isEmpty()) amb.toolCalls(merged); - AssistantMessage finalMsg = amb.build(); - Generation finalGen = new Generation(finalMsg, finalGenMetadata!=null?finalGenMetadata:ChatGenerationMetadata.NULL); + AssistantMessage.Builder assistantMessageBuilder = AssistantMessage.builder().content(text.toString()).properties(props); + if (!merged.isEmpty()) { + assistantMessageBuilder.toolCalls(merged); + } + AssistantMessage assistantMessage = assistantMessageBuilder.build(); + Generation finalGen = new Generation(assistantMessage, finalGenMetadata!=null? finalGenMetadata : ChatGenerationMetadata.NULL); ChatResponse aggregated = new ChatResponse(List.of(finalGen), finalMetadata); observationContext.setResponse(aggregated); if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(prompt.getOptions(), aggregated)) { return Flux.deferContextual(ctx -> { - ToolExecutionResult ter; - try { ToolCallReactiveContextHolder.setContext(ctx); ter = this.toolCallingManager.executeToolCalls(prompt, aggregated);} finally { ToolCallReactiveContextHolder.clearContext(); } - if (ter.returnDirect()) return Flux.just(ChatResponse.builder().from(aggregated).generations(ToolExecutionResult.buildGenerations(ter)).build()); - return this.internalStream(new Prompt(ter.conversationHistory(), prompt.getOptions()), aggregated); + ToolExecutionResult tetoolExecutionResult; + try { ToolCallReactiveContextHolder.setContext(ctx); tetoolExecutionResult = this.toolCallingManager.executeToolCalls(prompt, aggregated);} finally { ToolCallReactiveContextHolder.clearContext(); } + if (tetoolExecutionResult.returnDirect()) return Flux.just(ChatResponse.builder().from(aggregated).generations(ToolExecutionResult.buildGenerations(tetoolExecutionResult)).build()); + return this.internalStream(new Prompt(tetoolExecutionResult.conversationHistory(), prompt.getOptions()), aggregated); }).subscribeOn(Schedulers.boundedElastic()); } return Flux.just(aggregated); From e337e1cb5342e7234c9c519c607a93dfbdb66eeb Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Tue, 18 Nov 2025 11:59:09 +0100 Subject: [PATCH 21/49] Implementation of the OpenAI Java SDK - Add more tests - Reformat the code Signed-off-by: Julien Dubois --- .../OpenAiOfficialChatModel.java | 606 ++++++++------- .../OpenAiOfficialChatOptions.java | 39 +- .../chat/OpenAiOfficialChatModelIT.java | 12 +- ...enAiOfficialChatModelResponseFormatIT.java | 6 +- .../chat/OpenAiOfficialChatOptionsTests.java | 713 ++++++++++++++++++ 5 files changed, 1088 insertions(+), 288 deletions(-) create mode 100644 models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatOptionsTests.java diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java index 03616ff5faa..f9497af4428 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -94,9 +94,9 @@ public class OpenAiOfficialChatModel implements ChatModel { private final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatModel.class); - private OpenAIClient openAiClient; + private final OpenAIClient openAiClient; - private OpenAIClientAsync openAiClientAsync; + private final OpenAIClientAsync openAiClientAsync; private final OpenAiOfficialChatOptions options; @@ -263,13 +263,12 @@ public Flux stream(Prompt prompt) { } public AssistantMessage safeAssistantMessage(ChatResponse response) { - if (response == null) return null; + if (response == null) + return null; Generation gen = response.getResult(); - if (gen == null) return null; - if (gen.getOutput() instanceof AssistantMessage) { - return (AssistantMessage) gen.getOutput(); - } - return null; + if (gen == null) + return null; + return gen.getOutput(); } public Flux internalStream(Prompt prompt, ChatResponse previousChatResponse) { @@ -281,8 +280,8 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha .provider(AiProvider.OPENAI_OFFICIAL.value()) .build(); Observation observation = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION.observation( - this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, - this.observationRegistry); + this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, + this.observationRegistry); observation.parentObservation(contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null)).start(); Flux chatResponses = Flux.create(sink -> { @@ -291,15 +290,14 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha ChatCompletion chatCompletion = chunkToChatCompletion(chunk); String id = chatCompletion.id(); List generations = chatCompletion.choices().stream().map(choice -> { - roleMap.putIfAbsent(id, choice.message()._role().asString().isPresent() ? choice.message()._role().asStringOrThrow() : ""); - - Map metadata = Map.of( - "id", id, - "role", roleMap.getOrDefault(id, ""), - "index", choice.index(), - "finishReason", choice.finishReason().value(), - "refusal", choice.message().refusal().isPresent() ? choice.message().refusal() : "", - "annotations", choice.message().annotations().isPresent() ? choice.message().annotations() : List.of(), + roleMap.putIfAbsent(id, choice.message()._role().asString().isPresent() + ? choice.message()._role().asStringOrThrow() : ""); + + Map metadata = Map.of("id", id, "role", roleMap.getOrDefault(id, ""), + "index", choice.index(), "finishReason", choice.finishReason().value(), "refusal", + choice.message().refusal().isPresent() ? choice.message().refusal() : "", + "annotations", choice.message().annotations().isPresent() + ? choice.message().annotations() : List.of(), "chunkChoice", chunk.choices().get((int) choice.index())); return buildGeneration(choice, metadata); @@ -315,13 +313,14 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha sink.error(e); } }).onCompleteFuture().whenComplete((unused, throwable) -> { - if (throwable != null) sink.error(throwable); else sink.complete(); + if (throwable != null) + sink.error(throwable); + else + sink.complete(); }); - }) - .buffer(2,1) - .map(buffer -> { + }).buffer(2, 1).map(buffer -> { ChatResponse first = buffer.get(0); - if (request.streamOptions().isPresent() && buffer.size()==2) { + if (request.streamOptions().isPresent() && buffer.size() == 2) { ChatResponse second = buffer.get(1); if (second != null) { Usage usage = second.getMetadata().getUsage(); @@ -337,15 +336,23 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha .contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation)); return flux.collectList().flatMapMany(list -> { - if (list.isEmpty()) return Flux.empty(); - boolean hasToolCalls = list.stream().map(this::safeAssistantMessage).filter(Objects::nonNull) + if (list.isEmpty()) + return Flux.empty(); + boolean hasToolCalls = list.stream() + .map(this::safeAssistantMessage) + .filter(Objects::nonNull) .anyMatch(am -> !CollectionUtils.isEmpty(am.getToolCalls())); if (!hasToolCalls) { if (list.size() > 2) { - ChatResponse penultimateResponse = list.get(list.size()-2); // Get the finish reason - ChatResponse lastResponse = list.get(list.size()-1); // Get the usage + ChatResponse penultimateResponse = list.get(list.size() - 2); // Get + // the + // finish + // reason + ChatResponse lastResponse = list.get(list.size() - 1); // Get the + // usage Usage usage = lastResponse.getMetadata().getUsage(); - observationContext.setResponse(new ChatResponse(penultimateResponse.getResults(), from(penultimateResponse.getMetadata(), usage))); + observationContext.setResponse(new ChatResponse(penultimateResponse.getResults(), + from(penultimateResponse.getMetadata(), usage))); } return Flux.fromIterable(list); } @@ -353,59 +360,85 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha StringBuilder text = new StringBuilder(); ChatResponseMetadata finalMetadata = null; ChatGenerationMetadata finalGenMetadata = null; - Map props = new HashMap<>(); + Map props = new HashMap<>(); for (ChatResponse chatResponse : list) { AssistantMessage am = safeAssistantMessage(chatResponse); - if (am == null) continue; - if (am.getText() != null) text.append(am.getText()); - if (am.getMetadata() != null) props.putAll(am.getMetadata()); + if (am == null) + continue; + if (am.getText() != null) + text.append(am.getText()); + if (am.getMetadata() != null) + props.putAll(am.getMetadata()); if (!CollectionUtils.isEmpty(am.getToolCalls())) { Object ccObj = am.getMetadata().get("chunkChoice"); - if (ccObj instanceof ChatCompletionChunk.Choice chunkChoice && chunkChoice.delta().toolCalls().isPresent()) { - List deltaCalls = chunkChoice.delta().toolCalls().get(); - for (int i=0; i deltaCalls = chunkChoice.delta() + .toolCalls() + .get(); + for (int i = 0; i < am.getToolCalls().size() && i < deltaCalls.size(); i++) { AssistantMessage.ToolCall tc = am.getToolCalls().get(i); ChatCompletionChunk.Choice.Delta.ToolCall dtc = deltaCalls.get(i); - String key = chunkChoice.index()+"-"+dtc.index(); - ToolCallBuilder toolCallBuilder = builders.computeIfAbsent(key,k->new ToolCallBuilder()); + String key = chunkChoice.index() + "-" + dtc.index(); + ToolCallBuilder toolCallBuilder = builders.computeIfAbsent(key, + k -> new ToolCallBuilder()); toolCallBuilder.merge(tc); } - } else { + } + else { for (AssistantMessage.ToolCall tc : am.getToolCalls()) { - ToolCallBuilder toolCallBuilder = builders.computeIfAbsent(tc.id(), k->new ToolCallBuilder()); - toolCallBuilder.merge(tc); + ToolCallBuilder toolCallBuilder = builders.computeIfAbsent(tc.id(), + k -> new ToolCallBuilder()); + toolCallBuilder.merge(tc); } } } Generation generation = chatResponse.getResult(); - if (generation!=null && generation.getMetadata()!=null && generation.getMetadata()!=ChatGenerationMetadata.NULL) { + if (generation != null && generation.getMetadata() != null + && generation.getMetadata() != ChatGenerationMetadata.NULL) { finalGenMetadata = generation.getMetadata(); } - if (chatResponse.getMetadata()!=null) finalMetadata = chatResponse.getMetadata(); + if (chatResponse.getMetadata() != null) + finalMetadata = chatResponse.getMetadata(); } - List merged = builders.values().stream().map(ToolCallBuilder::build) + List merged = builders.values() + .stream() + .map(ToolCallBuilder::build) .filter(tc -> StringUtils.hasText(tc.name())) .toList(); - AssistantMessage.Builder assistantMessageBuilder = AssistantMessage.builder().content(text.toString()).properties(props); + AssistantMessage.Builder assistantMessageBuilder = AssistantMessage.builder() + .content(text.toString()) + .properties(props); if (!merged.isEmpty()) { assistantMessageBuilder.toolCalls(merged); } AssistantMessage assistantMessage = assistantMessageBuilder.build(); - Generation finalGen = new Generation(assistantMessage, finalGenMetadata!=null? finalGenMetadata : ChatGenerationMetadata.NULL); + Generation finalGen = new Generation(assistantMessage, + finalGenMetadata != null ? finalGenMetadata : ChatGenerationMetadata.NULL); ChatResponse aggregated = new ChatResponse(List.of(finalGen), finalMetadata); observationContext.setResponse(aggregated); if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(prompt.getOptions(), aggregated)) { return Flux.deferContextual(ctx -> { ToolExecutionResult tetoolExecutionResult; - try { ToolCallReactiveContextHolder.setContext(ctx); tetoolExecutionResult = this.toolCallingManager.executeToolCalls(prompt, aggregated);} finally { ToolCallReactiveContextHolder.clearContext(); } - if (tetoolExecutionResult.returnDirect()) return Flux.just(ChatResponse.builder().from(aggregated).generations(ToolExecutionResult.buildGenerations(tetoolExecutionResult)).build()); - return this.internalStream(new Prompt(tetoolExecutionResult.conversationHistory(), prompt.getOptions()), aggregated); + try { + ToolCallReactiveContextHolder.setContext(ctx); + tetoolExecutionResult = this.toolCallingManager.executeToolCalls(prompt, aggregated); + } + finally { + ToolCallReactiveContextHolder.clearContext(); + } + if (tetoolExecutionResult.returnDirect()) + return Flux.just(ChatResponse.builder() + .from(aggregated) + .generations(ToolExecutionResult.buildGenerations(tetoolExecutionResult)) + .build()); + return this.internalStream( + new Prompt(tetoolExecutionResult.conversationHistory(), prompt.getOptions()), + aggregated); }).subscribeOn(Schedulers.boundedElastic()); } return Flux.just(aggregated); - }) - .doOnError(observation::error) - .doFinally(s -> observation.stop()); + }).doOnError(observation::error).doFinally(s -> observation.stop()); }); } @@ -417,11 +450,15 @@ private Generation buildGeneration(ChatCompletion.Choice choice, Map tc.function().isPresent()) .map(tc -> { var funcOpt = tc.function(); - if (funcOpt.isEmpty()) return null; + if (funcOpt.isEmpty()) + return null; var func = funcOpt.get(); String id = tc.id().orElse(""); String name = func.name().orElse(""); @@ -434,20 +471,19 @@ private Generation buildGeneration(ChatCompletion.Choice choice, Map list.stream() - .filter(tc -> tc.function().isPresent()) - .map(tc -> { + toolCalls = message.toolCalls() + .map(list -> list.stream().filter(tc -> tc.function().isPresent()).map(tc -> { var opt = tc.function(); - if (opt.isEmpty()) return null; + if (opt.isEmpty()) + return null; var funcCall = opt.get(); var functionDef = funcCall.function(); String id = funcCall.id(); String name = functionDef.name(); String arguments = functionDef.arguments(); return new AssistantMessage.ToolCall(id, "function", name, arguments); - }) - .filter(Objects::nonNull) - .toList()).orElse(List.of()); + }).filter(Objects::nonNull).toList()) + .orElse(List.of()); } var generationMetadataBuilder = ChatGenerationMetadata.builder() @@ -488,47 +524,45 @@ private ChatResponseMetadata from(ChatResponseMetadata chatResponseMetadata, Usa * @return the ChatCompletion */ private ChatCompletion chunkToChatCompletion(ChatCompletionChunk chunk) { - List choices = chunk.choices() - .stream() - .map(chunkChoice -> { - ChatCompletion.Choice.FinishReason finishReason = ChatCompletion.Choice.FinishReason.of(""); - if (chunkChoice.finishReason().isPresent()) { - finishReason = ChatCompletion.Choice.FinishReason.of(chunkChoice.finishReason().get().value().name().toLowerCase()); - } + List choices = chunk.choices().stream().map(chunkChoice -> { + ChatCompletion.Choice.FinishReason finishReason = ChatCompletion.Choice.FinishReason.of(""); + if (chunkChoice.finishReason().isPresent()) { + finishReason = ChatCompletion.Choice.FinishReason + .of(chunkChoice.finishReason().get().value().name().toLowerCase()); + } - ChatCompletion.Choice.Builder choiceBuilder = ChatCompletion.Choice.builder() - .finishReason(finishReason) - .index(chunkChoice.index()) - .message(ChatCompletionMessage.builder() - .content(chunkChoice.delta().content()) - .refusal(chunkChoice.delta().refusal()) - .build()); - - // Handle optional logprobs - if (chunkChoice.logprobs().isPresent()) { - var logprobs = chunkChoice.logprobs().get(); - choiceBuilder.logprobs(ChatCompletion.Choice.Logprobs.builder() - .content(logprobs.content()) - .refusal(logprobs.refusal()) - .build()); - } else { - // Provide empty logprobs when not present - choiceBuilder.logprobs(ChatCompletion.Choice.Logprobs.builder() - .content(List.of()) - .refusal(List.of()) - .build()); - } + ChatCompletion.Choice.Builder choiceBuilder = ChatCompletion.Choice.builder() + .finishReason(finishReason) + .index(chunkChoice.index()) + .message(ChatCompletionMessage.builder() + .content(chunkChoice.delta().content()) + .refusal(chunkChoice.delta().refusal()) + .build()); - return choiceBuilder.build(); - }) - .toList(); + // Handle optional logprobs + if (chunkChoice.logprobs().isPresent()) { + var logprobs = chunkChoice.logprobs().get(); + choiceBuilder.logprobs(ChatCompletion.Choice.Logprobs.builder() + .content(logprobs.content()) + .refusal(logprobs.refusal()) + .build()); + } + else { + // Provide empty logprobs when not present + choiceBuilder + .logprobs(ChatCompletion.Choice.Logprobs.builder().content(List.of()).refusal(List.of()).build()); + } + + return choiceBuilder.build(); + }).toList(); return ChatCompletion.builder() .id(chunk.id()) .choices(choices) .created(chunk.created()) .model(chunk.model()) - .usage(chunk.usage().orElse(CompletionUsage.builder().promptTokens(0).completionTokens(0).totalTokens(0).build())) + .usage(chunk.usage() + .orElse(CompletionUsage.builder().promptTokens(0).completionTokens(0).totalTokens(0).build())) .build(); } @@ -593,134 +627,158 @@ Prompt buildRequestPrompt(Prompt prompt) { ChatCompletionCreateParams createRequest(Prompt prompt, boolean stream) { - List chatCompletionMessageParams = prompt.getInstructions().stream().map(message -> { - if (message.getMessageType() == MessageType.USER || message.getMessageType() == MessageType.SYSTEM) { - // Handle simple text content for user and system messages - ChatCompletionUserMessageParam.Builder builder = ChatCompletionUserMessageParam.builder(); - - if (message instanceof UserMessage userMessage && !CollectionUtils.isEmpty(userMessage.getMedia())) { - // Handle media content (images, audio, files) - List parts = new ArrayList<>(); - - if (!message.getText().isEmpty()) { - parts.add(ChatCompletionContentPart.ofText(ChatCompletionContentPartText.builder().text(message.getText()).build())); - } + List chatCompletionMessageParams = prompt.getInstructions() + .stream() + .map(message -> { + if (message.getMessageType() == MessageType.USER || message.getMessageType() == MessageType.SYSTEM) { + // Handle simple text content for user and system messages + ChatCompletionUserMessageParam.Builder builder = ChatCompletionUserMessageParam.builder(); + + if (message instanceof UserMessage userMessage + && !CollectionUtils.isEmpty(userMessage.getMedia())) { + // Handle media content (images, audio, files) + List parts = new ArrayList<>(); + + if (!message.getText().isEmpty()) { + parts.add(ChatCompletionContentPart + .ofText(ChatCompletionContentPartText.builder().text(message.getText()).build())); + } - // Add media content parts - userMessage.getMedia().forEach(media -> { - String mimeType = media.getMimeType().toString(); - if (mimeType.startsWith("image/")) { - if (media.getData() instanceof java.net.URI uri) { - parts.add(ChatCompletionContentPart.ofImageUrl(ChatCompletionContentPartImage.builder() - .imageUrl(ChatCompletionContentPartImage.ImageUrl.builder().url(uri.toString()).build()) - .build())); - } else if (media.getData() instanceof String text) { - // The org.springframework.ai.content.Media object should store the URL as a java.net.URI but it transforms it to String somewhere along the way, - // for example in its Builder class. So, we accept String as well here for image URLs. - parts.add(ChatCompletionContentPart.ofImageUrl(ChatCompletionContentPartImage.builder() - .imageUrl(ChatCompletionContentPartImage.ImageUrl.builder().url(text).build()) - .build())); - } else if (media.getData() instanceof byte[] bytes) { - // Assume the bytes are an image. So, convert the bytes to a base64 encoded - ChatCompletionContentPartImage.ImageUrl.Builder imageUrlBuilder = ChatCompletionContentPartImage.ImageUrl.builder(); - - imageUrlBuilder.url("data:" + mimeType + ";base64," - + Base64.getEncoder().encodeToString(bytes)); - parts.add(ChatCompletionContentPart.ofImageUrl(ChatCompletionContentPartImage.builder() - .imageUrl(imageUrlBuilder.build()) - .build())); - } else { - logger.info("Could not process image media with data of type: {}. Only java.net.URI is supported for image URLs.", - media.getData().getClass().getSimpleName()); + // Add media content parts + userMessage.getMedia().forEach(media -> { + String mimeType = media.getMimeType().toString(); + if (mimeType.startsWith("image/")) { + if (media.getData() instanceof java.net.URI uri) { + parts.add(ChatCompletionContentPart + .ofImageUrl(ChatCompletionContentPartImage.builder() + .imageUrl(ChatCompletionContentPartImage.ImageUrl.builder() + .url(uri.toString()) + .build()) + .build())); + } + else if (media.getData() instanceof String text) { + // The org.springframework.ai.content.Media object + // should store the URL as a java.net.URI but it + // transforms it to String somewhere along the way, + // for example in its Builder class. So, we accept + // String as well here for image URLs. + parts.add(ChatCompletionContentPart + .ofImageUrl(ChatCompletionContentPartImage.builder() + .imageUrl( + ChatCompletionContentPartImage.ImageUrl.builder().url(text).build()) + .build())); + } + else if (media.getData() instanceof byte[] bytes) { + // Assume the bytes are an image. So, convert the + // bytes to a base64 encoded + ChatCompletionContentPartImage.ImageUrl.Builder imageUrlBuilder = ChatCompletionContentPartImage.ImageUrl + .builder(); + + imageUrlBuilder.url("data:" + mimeType + ";base64," + + Base64.getEncoder().encodeToString(bytes)); + parts.add(ChatCompletionContentPart + .ofImageUrl(ChatCompletionContentPartImage.builder() + .imageUrl(imageUrlBuilder.build()) + .build())); + } + else { + logger.info( + "Could not process image media with data of type: {}. Only java.net.URI is supported for image URLs.", + media.getData().getClass().getSimpleName()); + } } - } - else if (mimeType.startsWith("audio/")) { - parts.add(ChatCompletionContentPart.ofInputAudio(ChatCompletionContentPartInputAudio.builder() - .inputAudio(ChatCompletionContentPartInputAudio.builder() + else if (mimeType.startsWith("audio/")) { + parts.add(ChatCompletionContentPart + .ofInputAudio(ChatCompletionContentPartInputAudio.builder() + .inputAudio(ChatCompletionContentPartInputAudio.builder() .inputAudio(ChatCompletionContentPartInputAudio.InputAudio.builder() - .data(fromAudioData(media.getData())) - .format(mimeType.contains("mp3") - ? ChatCompletionContentPartInputAudio.InputAudio.Format.MP3 - : ChatCompletionContentPartInputAudio.InputAudio.Format.WAV) - .build()) + .data(fromAudioData(media.getData())) + .format(mimeType.contains("mp3") + ? ChatCompletionContentPartInputAudio.InputAudio.Format.MP3 + : ChatCompletionContentPartInputAudio.InputAudio.Format.WAV) + .build()) .build() .inputAudio()) + .build())); + } + else { + // Assume it's a file or other media type represented as a + // data URL + parts.add(ChatCompletionContentPart.ofText(ChatCompletionContentPartText.builder() + .text(fromMediaData(media.getMimeType(), media.getData())) .build())); - } - else { - // Assume it's a file or other media type represented as a data URL - parts.add(ChatCompletionContentPart.ofText(ChatCompletionContentPartText.builder().text(fromMediaData(media.getMimeType(), media.getData())).build())); - } - }); - builder.contentOfArrayOfContentParts(parts); - } - else { - // Simple text message - builder.content(ChatCompletionContentPartText.builder().text(message.getText()).build().text()); - } + } + }); + builder.contentOfArrayOfContentParts(parts); + } + else { + // Simple text message + builder.content(ChatCompletionContentPartText.builder().text(message.getText()).build().text()); + } - if (message.getMessageType() == MessageType.USER) { - builder.role(JsonValue.from(MessageType.USER.getValue())); - } - else { - builder.role(JsonValue.from(MessageType.SYSTEM.getValue())); - } + if (message.getMessageType() == MessageType.USER) { + builder.role(JsonValue.from(MessageType.USER.getValue())); + } + else { + builder.role(JsonValue.from(MessageType.SYSTEM.getValue())); + } - return List.of(ChatCompletionMessageParam.ofUser(builder.build())); - } - else if (message.getMessageType() == MessageType.ASSISTANT) { - var assistantMessage = (AssistantMessage) message; - ChatCompletionAssistantMessageParam.Builder builder = ChatCompletionAssistantMessageParam.builder() - .role(JsonValue.from(MessageType.ASSISTANT.getValue())); + return List.of(ChatCompletionMessageParam.ofUser(builder.build())); + } + else if (message.getMessageType() == MessageType.ASSISTANT) { + var assistantMessage = (AssistantMessage) message; + ChatCompletionAssistantMessageParam.Builder builder = ChatCompletionAssistantMessageParam.builder() + .role(JsonValue.from(MessageType.ASSISTANT.getValue())); - if (assistantMessage.getText() != null) { - builder.content(ChatCompletionAssistantMessageParam.builder() + if (assistantMessage.getText() != null) { + builder.content(ChatCompletionAssistantMessageParam.builder() .content(assistantMessage.getText()) - .build().content()); - } + .build() + .content()); + } - if (!CollectionUtils.isEmpty(assistantMessage.getToolCalls())) { - List toolCalls = assistantMessage.getToolCalls() - .stream() - .map(toolCall -> ChatCompletionMessageToolCall.ofFunction( - ChatCompletionMessageFunctionToolCall.builder() - .id(toolCall.id()) - .function(ChatCompletionMessageFunctionToolCall.Function.builder() - .name(toolCall.name()) - .arguments(toolCall.arguments()).build()) - .build()) - ) - .toList(); + if (!CollectionUtils.isEmpty(assistantMessage.getToolCalls())) { + List toolCalls = assistantMessage.getToolCalls() + .stream() + .map(toolCall -> ChatCompletionMessageToolCall + .ofFunction(ChatCompletionMessageFunctionToolCall.builder() + .id(toolCall.id()) + .function(ChatCompletionMessageFunctionToolCall.Function.builder() + .name(toolCall.name()) + .arguments(toolCall.arguments()) + .build()) + .build())) + .toList(); + + builder.toolCalls(toolCalls); + } - builder.toolCalls(toolCalls); + return List.of(ChatCompletionMessageParam.ofAssistant(builder.build())); } + else if (message.getMessageType() == MessageType.TOOL) { + ToolResponseMessage toolMessage = (ToolResponseMessage) message; - return List.of(ChatCompletionMessageParam.ofAssistant(builder.build())); - } - else if (message.getMessageType() == MessageType.TOOL) { - ToolResponseMessage toolMessage = (ToolResponseMessage) message; + ChatCompletionToolMessageParam.Builder builder = ChatCompletionToolMessageParam.builder(); + builder.content(toolMessage.getText() != null ? toolMessage.getText() : ""); + builder.role(JsonValue.from(MessageType.TOOL.getValue())); - ChatCompletionToolMessageParam.Builder builder = ChatCompletionToolMessageParam.builder(); - builder.content(toolMessage.getText() != null ? toolMessage.getText() : ""); - builder.role(JsonValue.from(MessageType.TOOL.getValue())); + if (toolMessage.getResponses().isEmpty()) { + return List.of(ChatCompletionMessageParam.ofTool(builder.build())); + } + return toolMessage.getResponses().stream().map(response -> { + String callId = response.id(); + String callResponse = response.responseData(); - if (toolMessage.getResponses().isEmpty()) { - return List.of(ChatCompletionMessageParam.ofTool(builder.build())); + return ChatCompletionMessageParam + .ofTool(builder.toolCallId(callId).content(callResponse).build()); + }).toList(); } - return toolMessage.getResponses().stream().map(response -> { - String callId = response.id(); - String callResponse = response.responseData(); - - return ChatCompletionMessageParam.ofTool(builder - .toolCallId(callId) - .content(callResponse) - .build()); - }).toList(); - } - else { - throw new IllegalArgumentException("Unsupported message type: " + message.getMessageType()); - } - }).flatMap(List::stream).toList(); + else { + throw new IllegalArgumentException("Unsupported message type: " + message.getMessageType()); + } + }) + .flatMap(List::stream) + .toList(); ChatCompletionCreateParams.Builder builder = ChatCompletionCreateParams.builder(); @@ -742,9 +800,11 @@ else if (requestOptions.getModel() != null) { } if (requestOptions.getLogitBias() != null) { builder.logitBias(ChatCompletionCreateParams.LogitBias.builder() - .putAllAdditionalProperties(requestOptions.getLogitBias().entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, entry -> JsonValue.from(entry.getValue())))) - .build()); + .putAllAdditionalProperties(requestOptions.getLogitBias() + .entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> JsonValue.from(entry.getValue())))) + .build()); } if (requestOptions.getLogprobs() != null) { builder.logprobs(requestOptions.getLogprobs()); @@ -771,27 +831,32 @@ else if (requestOptions.getModel() != null) { ResponseFormat responseFormat = requestOptions.getResponseFormat(); if (responseFormat.getType().equals(ResponseFormat.Type.TEXT)) { builder.responseFormat(ResponseFormatText.builder().build()); - } else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_OBJECT)) { + } + else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_OBJECT)) { builder.responseFormat(ResponseFormatJsonObject.builder().build()); - } else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_SCHEMA)) { + } + else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_SCHEMA)) { String jsonSchemaString = responseFormat.getJsonSchema() != null ? responseFormat.getJsonSchema() : ""; try { com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); - ResponseFormatJsonSchema.JsonSchema.Builder jsonSchemaBuilder = ResponseFormatJsonSchema.JsonSchema.builder(); + ResponseFormatJsonSchema.JsonSchema.Builder jsonSchemaBuilder = ResponseFormatJsonSchema.JsonSchema + .builder(); jsonSchemaBuilder.name("json_schema"); jsonSchemaBuilder.strict(true); - ResponseFormatJsonSchema.JsonSchema.Schema schema = mapper.readValue(jsonSchemaString, ResponseFormatJsonSchema.JsonSchema.Schema.class); + ResponseFormatJsonSchema.JsonSchema.Schema schema = mapper.readValue(jsonSchemaString, + ResponseFormatJsonSchema.JsonSchema.Schema.class); jsonSchemaBuilder.schema(schema); - builder.responseFormat(ResponseFormatJsonSchema.builder() - .jsonSchema(jsonSchemaBuilder.build()) - .build()); - } catch (Exception e) { + builder.responseFormat( + ResponseFormatJsonSchema.builder().jsonSchema(jsonSchemaBuilder.build()).build()); + } + catch (Exception e) { throw new IllegalArgumentException("Failed to parse JSON schema: " + jsonSchemaString, e); } - } else { + } + else { throw new IllegalArgumentException("Unsupported response format type: " + responseFormat.getType()); } } @@ -828,11 +893,13 @@ else if (requestOptions.getModel() != null) { if (requestOptions.getStore() != null) { builder.store(requestOptions.getStore()); } - if (requestOptions.getMetadata() != null && !requestOptions.getMetadata().isEmpty()) { + if (requestOptions.getMetadata() != null && !requestOptions.getMetadata().isEmpty()) { builder.metadata(ChatCompletionCreateParams.Metadata.builder() - .putAllAdditionalProperties(requestOptions.getMetadata() .entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, entry -> JsonValue.from(entry.getValue())))) - .build()); + .putAllAdditionalProperties(requestOptions.getMetadata() + .entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> JsonValue.from(entry.getValue())))) + .build()); } if (requestOptions.getServiceTier() != null) { builder.serviceTier(ChatCompletionCreateParams.ServiceTier.of(requestOptions.getServiceTier())); @@ -843,15 +910,17 @@ else if (requestOptions.getModel() != null) { ChatCompletionStreamOptions.Builder streamOptionsBuilder = ChatCompletionStreamOptions.builder(); if (requestOptions.getStreamOptions().includeObfuscation().isPresent()) { - streamOptionsBuilder.includeObfuscation(requestOptions.getStreamOptions().includeObfuscation().get()); + streamOptionsBuilder + .includeObfuscation(requestOptions.getStreamOptions().includeObfuscation().get()); } streamOptionsBuilder.additionalProperties(requestOptions.getStreamOptions()._additionalProperties()); streamOptionsBuilder.includeUsage(requestOptions.getStreamUsage()); builder.streamOptions(streamOptionsBuilder.build()); - } else { + } + else { builder.streamOptions(ChatCompletionStreamOptions.builder() - .includeUsage(true) // Include usage by default for streaming - .build()); + .includeUsage(true) // Include usage by default for streaming + .build()); } } @@ -892,40 +961,40 @@ else if (mediaContentData instanceof String text) { } private List getChatCompletionTools(List toolDefinitions) { - return toolDefinitions.stream() - .map(toolDefinition -> { - FunctionParameters.Builder parametersBuilder = FunctionParameters.builder(); + return toolDefinitions.stream().map(toolDefinition -> { + FunctionParameters.Builder parametersBuilder = FunctionParameters.builder(); - if (!toolDefinition.inputSchema().isEmpty()) { - // Parse the schema and add its properties directly - try { - com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); - @SuppressWarnings("unchecked") - Map schemaMap = mapper.readValue(toolDefinition.inputSchema(), Map.class); - - // Add each property from the schema to the parameters - schemaMap.forEach((key, value) -> - parametersBuilder.putAdditionalProperty(key, JsonValue.from(value)) - ); - - // Add strict mode - parametersBuilder.putAdditionalProperty("strict", JsonValue.from(true)); // TODO allow non-strict mode - } catch (Exception e) { - logger.error("Failed to parse tool schema", e); - } - } + if (!toolDefinition.inputSchema().isEmpty()) { + // Parse the schema and add its properties directly + try { + com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); + @SuppressWarnings("unchecked") + Map schemaMap = mapper.readValue(toolDefinition.inputSchema(), Map.class); + + // Add each property from the schema to the parameters + schemaMap + .forEach((key, value) -> parametersBuilder.putAdditionalProperty(key, JsonValue.from(value))); + + // Add strict mode + parametersBuilder.putAdditionalProperty("strict", JsonValue.from(true)); // TODO + // allow + // non-strict + // mode + } + catch (Exception e) { + logger.error("Failed to parse tool schema", e); + } + } + + FunctionDefinition functionDefinition = FunctionDefinition.builder() + .name(toolDefinition.name()) + .description(toolDefinition.description()) + .parameters(parametersBuilder.build()) + .build(); - FunctionDefinition functionDefinition = FunctionDefinition.builder() - .name(toolDefinition.name()) - .description(toolDefinition.description()) - .parameters(parametersBuilder.build()) - .build(); - - return ChatCompletionTool.ofFunction( - ChatCompletionFunctionTool.builder().function(functionDefinition).build() - ); - }) - .toList(); + return ChatCompletionTool + .ofFunction(ChatCompletionFunctionTool.builder().function(functionDefinition).build()); + }).toList(); } @Override @@ -943,7 +1012,8 @@ public void setObservationConvention(ChatModelObservationConvention observationC } /** - * Response format (text, json_object, json_schema) for OpenAiOfficialChatModel responses. + * Response format (text, json_object, json_schema) for OpenAiOfficialChatModel + * responses. * * @author Julien Dubois */ @@ -1005,28 +1075,34 @@ public enum Type { TEXT, /** - * Enables JSON mode, which guarantees the message the model generates is valid - * JSON. + * Enables JSON mode, which guarantees the message the model generates is + * valid JSON. */ JSON_OBJECT, /** - * Enables Structured Outputs which guarantees the model will match your supplied - * JSON schema. + * Enables Structured Outputs which guarantees the model will match your + * supplied JSON schema. */ JSON_SCHEMA } + } /** - * Helper class to merge streaming tool calls that arrive in pieces across multiple chunks. - * In OpenAI streaming, a tool call's ID, name, and arguments can arrive in separate chunks. + * Helper class to merge streaming tool calls that arrive in pieces across multiple + * chunks. In OpenAI streaming, a tool call's ID, name, and arguments can arrive in + * separate chunks. */ private static class ToolCallBuilder { + private String id = ""; + private String type = "function"; + private String name = ""; + private StringBuilder arguments = new StringBuilder(); void merge(AssistantMessage.ToolCall toolCall) { @@ -1047,5 +1123,7 @@ void merge(AssistantMessage.ToolCall toolCall) { AssistantMessage.ToolCall build() { return new AssistantMessage.ToolCall(id, type, name, arguments.toString()); } + } + } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java index 55969633317..ccff03e3a88 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java @@ -396,10 +396,12 @@ public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; OpenAiOfficialChatOptions options = (OpenAiOfficialChatOptions) o; - return Objects.equals(frequencyPenalty, options.frequencyPenalty) + return Objects.equals(getModel(), options.getModel()) + && Objects.equals(frequencyPenalty, options.frequencyPenalty) && Objects.equals(logitBias, options.logitBias) && Objects.equals(logprobs, options.logprobs) && Objects.equals(topLogprobs, options.topLogprobs) && Objects.equals(maxTokens, options.maxTokens) - && Objects.equals(n, options.n) && Objects.equals(outputAudio, options.outputAudio) + && Objects.equals(maxCompletionTokens, options.maxCompletionTokens) && Objects.equals(n, options.n) + && Objects.equals(outputAudio, options.outputAudio) && Objects.equals(presencePenalty, options.presencePenalty) && Objects.equals(responseFormat, options.responseFormat) && Objects.equals(streamOptions, options.streamOptions) @@ -418,16 +420,17 @@ public boolean equals(Object o) { @Override public int hashCode() { - return Objects.hash(frequencyPenalty, logitBias, logprobs, topLogprobs, maxTokens, n, outputAudio, - presencePenalty, responseFormat, streamOptions, streamUsage, seed, stop, temperature, topP, tools, - toolChoice, user, parallelToolCalls, store, metadata, reasoningEffort, verbosity, serviceTier, + return Objects.hash(getModel(), frequencyPenalty, logitBias, logprobs, topLogprobs, maxTokens, maxCompletionTokens, n, + outputAudio, presencePenalty, responseFormat, streamOptions, streamUsage, seed, stop, temperature, topP, + tools, toolChoice, user, parallelToolCalls, store, metadata, reasoningEffort, verbosity, serviceTier, toolCallbacks, toolNames, internalToolExecutionEnabled, httpHeaders, toolContext); } @Override public String toString() { - return "OpenAiOfficialChatOptions{" + "frequencyPenalty=" + frequencyPenalty + ", logitBias=" + logitBias - + ", logprobs=" + logprobs + ", topLogprobs=" + topLogprobs + ", maxTokens=" + maxTokens + ", n=" + n + return "OpenAiOfficialChatOptions{" + "model='" + getModel() + ", frequencyPenalty=" + frequencyPenalty + + ", logitBias=" + logitBias + ", logprobs=" + logprobs + ", topLogprobs=" + topLogprobs + + ", maxTokens=" + maxTokens + ", maxCompletionTokens=" + maxCompletionTokens + ", n=" + n + ", outputAudio=" + outputAudio + ", presencePenalty=" + presencePenalty + ", responseFormat=" + responseFormat + ", streamOptions=" + streamOptions + ", streamUsage=" + streamUsage + ", seed=" + seed + ", stop=" + stop + ", temperature=" + temperature + ", topP=" + topP + ", tools=" + tools @@ -611,22 +614,24 @@ public Builder topLogprobs(Integer topLogprobs) { public Builder maxTokens(Integer maxTokens) { if (maxTokens != null && this.options.getMaxCompletionTokens() != null) { - logger - .warn("Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " - + "The previously set maxCompletionTokens ({}) will be cleared and maxTokens ({}) will be used.", - this.options.getMaxCompletionTokens(), maxTokens); - this.options.setMaxCompletionTokens(null); + logger.warn( + "Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " + + "As maxToken is deprecated, we will ignore it and use maxCompletionToken ({}).", + this.options.getMaxCompletionTokens()); + } + else { + this.options.setMaxTokens(maxTokens); } - this.options.setMaxTokens(maxTokens); return this; } public Builder maxCompletionTokens(Integer maxCompletionTokens) { if (maxCompletionTokens != null && this.options.getMaxTokens() != null) { - logger - .warn("Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " - + "The previously set maxTokens ({}) will be cleared and maxCompletionTokens ({}) will be used.", - this.options.getMaxTokens(), maxCompletionTokens); + logger.warn( + "Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " + + "As maxToken is deprecated, we will use maxCompletionToken ({}).", + maxCompletionTokens); + this.options.setMaxTokens(null); } this.options.setMaxCompletionTokens(maxCompletionTokens); diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java index 7895820c967..d214f62125f 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java @@ -366,7 +366,8 @@ void beanStreamOutputConverterRecords() { @Test void functionCallTest() { - UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris? Answer in Celsius."); + UserMessage userMessage = new UserMessage( + "What's the weather like in San Francisco, Tokyo, and Paris? Answer in Celsius."); List messages = new ArrayList<>(List.of(userMessage)); @@ -387,7 +388,8 @@ void functionCallTest() { @Test void streamFunctionCallTest() { - UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris? Answer in Celsius."); + UserMessage userMessage = new UserMessage( + "What's the weather like in San Francisco, Tokyo, and Paris? Answer in Celsius."); List messages = new ArrayList<>(List.of(userMessage)); @@ -419,7 +421,8 @@ void streamFunctionCallTest() { @Test void functionCallUsageTest() { - UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris? Answer in Celsius."); + UserMessage userMessage = new UserMessage( + "What's the weather like in San Francisco, Tokyo, and Paris? Answer in Celsius."); List messages = new ArrayList<>(List.of(userMessage)); @@ -446,7 +449,8 @@ void functionCallUsageTest() { @Test void streamFunctionCallUsageTest() { - UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris? Answer in Celsius."); + UserMessage userMessage = new UserMessage( + "What's the weather like in San Francisco, Tokyo, and Paris? Answer in Celsius."); List messages = new ArrayList<>(List.of(userMessage)); diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java index 90e46926539..1a3ce76c7af 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java @@ -116,9 +116,9 @@ void jsonSchema() { OpenAiOfficialChatOptions.builder() .model(DEFAULT_CHAT_MODEL) .responseFormat(OpenAiOfficialChatModel.ResponseFormat.builder() - .type(OpenAiOfficialChatModel.ResponseFormat.Type.JSON_SCHEMA) - .jsonSchema(jsonSchema) - .build()) + .type(OpenAiOfficialChatModel.ResponseFormat.Type.JSON_SCHEMA) + .jsonSchema(jsonSchema) + .build()) .build()); ChatResponse response = this.chatModel.call(prompt); diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatOptionsTests.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatOptionsTests.java new file mode 100644 index 00000000000..df9c4565e36 --- /dev/null +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatOptionsTests.java @@ -0,0 +1,713 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaiofficial.chat; + +import com.openai.models.FunctionDefinition; +import org.junit.jupiter.api.Test; +import org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions; +import org.springframework.ai.tool.ToolCallback; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** + * Tests for {@link OpenAiOfficialChatOptions}. + * + * @author Julien Dubois + */ +public class OpenAiOfficialChatOptionsTests { + + @Test + void testBuilderWithAllFields() { + Map logitBias = new HashMap<>(); + logitBias.put("token1", 1); + logitBias.put("token2", -1); + + List stop = List.of("stop1", "stop2"); + List tools = new ArrayList<>(); + Map metadata = Map.of("key1", "value1"); + Map toolContext = Map.of("keyA", "valueA"); + Map httpHeaders = Map.of("header1", "value1"); + + OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() + .model("test-model") + .deploymentName("test-deployment") + .frequencyPenalty(0.5) + .logitBias(logitBias) + .logprobs(true) + .topLogprobs(5) + .maxTokens(100) + .maxCompletionTokens(50) + .N(2) + .presencePenalty(0.8) + .streamUsage(true) + .seed(12345) + .stop(stop) + .temperature(0.7) + .topP(0.9) + .tools(tools) + .user("test-user") + .parallelToolCalls(true) + .store(false) + .metadata(metadata) + .reasoningEffort("medium") + .verbosity("low") + .serviceTier("auto") + .internalToolExecutionEnabled(false) + .httpHeaders(httpHeaders) + .toolContext(toolContext) + .build(); + + assertThat(options.getModel()).isEqualTo("test-model"); + assertThat(options.getDeploymentName()).isEqualTo("test-deployment"); + assertThat(options.getFrequencyPenalty()).isEqualTo(0.5); + assertThat(options.getLogitBias()).isEqualTo(logitBias); + assertThat(options.getLogprobs()).isTrue(); + assertThat(options.getTopLogprobs()).isEqualTo(5); + assertThat(options.getMaxTokens()).isNull(); + assertThat(options.getMaxCompletionTokens()).isEqualTo(50); + assertThat(options.getN()).isEqualTo(2); + assertThat(options.getPresencePenalty()).isEqualTo(0.8); + assertThat(options.getStreamUsage()).isTrue(); + assertThat(options.getSeed()).isEqualTo(12345); + assertThat(options.getStop()).isEqualTo(stop); + assertThat(options.getStopSequences()).isEqualTo(stop); + assertThat(options.getTemperature()).isEqualTo(0.7); + assertThat(options.getTopP()).isEqualTo(0.9); + assertThat(options.getTools()).isEqualTo(tools); + assertThat(options.getUser()).isEqualTo("test-user"); + assertThat(options.getParallelToolCalls()).isTrue(); + assertThat(options.getStore()).isFalse(); + assertThat(options.getMetadata()).isEqualTo(metadata); + assertThat(options.getReasoningEffort()).isEqualTo("medium"); + assertThat(options.getVerbosity()).isEqualTo("low"); + assertThat(options.getServiceTier()).isEqualTo("auto"); + assertThat(options.getInternalToolExecutionEnabled()).isFalse(); + assertThat(options.getHttpHeaders()).isEqualTo(httpHeaders); + assertThat(options.getToolContext()).isEqualTo(toolContext); + } + + @Test + void testCopy() { + Map logitBias = new HashMap<>(); + logitBias.put("token1", 1); + + List stop = List.of("stop1"); + List tools = new ArrayList<>(); + Map metadata = Map.of("key1", "value1"); + + OpenAiOfficialChatOptions originalOptions = OpenAiOfficialChatOptions.builder() + .model("test-model") + .deploymentName("test-deployment") + .frequencyPenalty(0.5) + .logitBias(logitBias) + .logprobs(true) + .topLogprobs(5) + .maxCompletionTokens(50) + .N(2) + .presencePenalty(0.8) + .streamUsage(false) + .seed(12345) + .stop(stop) + .temperature(0.7) + .topP(0.9) + .tools(tools) + .user("test-user") + .parallelToolCalls(false) + .store(true) + .metadata(metadata) + .reasoningEffort("low") + .verbosity("high") + .serviceTier("default") + .internalToolExecutionEnabled(true) + .httpHeaders(Map.of("header1", "value1")) + .build(); + + OpenAiOfficialChatOptions copiedOptions = originalOptions.copy(); + + assertThat(copiedOptions).isNotSameAs(originalOptions).isEqualTo(originalOptions); + // Verify collections are copied + assertThat(copiedOptions.getStop()).isNotSameAs(originalOptions.getStop()); + assertThat(copiedOptions.getHttpHeaders()).isNotSameAs(originalOptions.getHttpHeaders()); + assertThat(copiedOptions.getToolCallbacks()).isNotSameAs(originalOptions.getToolCallbacks()); + assertThat(copiedOptions.getToolNames()).isNotSameAs(originalOptions.getToolNames()); + assertThat(copiedOptions.getToolContext()).isNotSameAs(originalOptions.getToolContext()); + } + + @Test + void testSetters() { + Map logitBias = new HashMap<>(); + logitBias.put("token1", 1); + + List stop = List.of("stop1", "stop2"); + List tools = new ArrayList<>(); + Map metadata = Map.of("key2", "value2"); + + OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + options.setModel("test-model"); + options.setDeploymentName("test-deployment"); + options.setFrequencyPenalty(0.5); + options.setLogitBias(logitBias); + options.setLogprobs(true); + options.setTopLogprobs(5); + options.setMaxTokens(100); + options.setMaxCompletionTokens(50); + options.setN(2); + options.setPresencePenalty(0.8); + options.setStreamUsage(true); + options.setSeed(12345); + options.setStop(stop); + options.setTemperature(0.7); + options.setTopP(0.9); + options.setTools(tools); + options.setUser("test-user"); + options.setParallelToolCalls(true); + options.setStore(false); + options.setMetadata(metadata); + options.setReasoningEffort("high"); + options.setVerbosity("medium"); + options.setServiceTier("auto"); + options.setInternalToolExecutionEnabled(false); + options.setHttpHeaders(Map.of("header2", "value2")); + + assertThat(options.getModel()).isEqualTo("test-model"); + assertThat(options.getDeploymentName()).isEqualTo("test-deployment"); + assertThat(options.getFrequencyPenalty()).isEqualTo(0.5); + assertThat(options.getLogitBias()).isEqualTo(logitBias); + assertThat(options.getLogprobs()).isTrue(); + assertThat(options.getTopLogprobs()).isEqualTo(5); + assertThat(options.getMaxTokens()).isEqualTo(100); + assertThat(options.getMaxCompletionTokens()).isEqualTo(50); + assertThat(options.getN()).isEqualTo(2); + assertThat(options.getPresencePenalty()).isEqualTo(0.8); + assertThat(options.getStreamUsage()).isTrue(); + assertThat(options.getSeed()).isEqualTo(12345); + assertThat(options.getStop()).isEqualTo(stop); + assertThat(options.getTemperature()).isEqualTo(0.7); + assertThat(options.getTopP()).isEqualTo(0.9); + assertThat(options.getTools()).isEqualTo(tools); + assertThat(options.getUser()).isEqualTo("test-user"); + assertThat(options.getParallelToolCalls()).isTrue(); + assertThat(options.getStore()).isFalse(); + assertThat(options.getMetadata()).isEqualTo(metadata); + assertThat(options.getReasoningEffort()).isEqualTo("high"); + assertThat(options.getVerbosity()).isEqualTo("medium"); + assertThat(options.getServiceTier()).isEqualTo("auto"); + assertThat(options.getInternalToolExecutionEnabled()).isFalse(); + assertThat(options.getHttpHeaders()).isEqualTo(Map.of("header2", "value2")); + } + + @Test + void testDefaultValues() { + OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + + assertThat(options.getModel()).isNull(); + assertThat(options.getDeploymentName()).isNull(); + assertThat(options.getFrequencyPenalty()).isNull(); + assertThat(options.getLogitBias()).isNull(); + assertThat(options.getLogprobs()).isNull(); + assertThat(options.getTopLogprobs()).isNull(); + assertThat(options.getMaxTokens()).isNull(); + assertThat(options.getMaxCompletionTokens()).isNull(); + assertThat(options.getN()).isNull(); + assertThat(options.getOutputAudio()).isNull(); + assertThat(options.getPresencePenalty()).isNull(); + assertThat(options.getResponseFormat()).isNull(); + assertThat(options.getStreamOptions()).isNull(); + assertThat(options.getStreamUsage()).isNull(); + assertThat(options.getSeed()).isNull(); + assertThat(options.getStop()).isNull(); + assertThat(options.getStopSequences()).isNull(); + assertThat(options.getTemperature()).isNull(); + assertThat(options.getTopP()).isNull(); + assertThat(options.getTopK()).isNull(); + assertThat(options.getTools()).isNull(); + assertThat(options.getToolChoice()).isNull(); + assertThat(options.getUser()).isNull(); + assertThat(options.getParallelToolCalls()).isNull(); + assertThat(options.getStore()).isNull(); + assertThat(options.getMetadata()).isNull(); + assertThat(options.getReasoningEffort()).isNull(); + assertThat(options.getVerbosity()).isNull(); + assertThat(options.getServiceTier()).isNull(); + assertThat(options.getToolCallbacks()).isNotNull().isEmpty(); + assertThat(options.getToolNames()).isNotNull().isEmpty(); + assertThat(options.getInternalToolExecutionEnabled()).isNull(); + assertThat(options.getHttpHeaders()).isNotNull().isEmpty(); + assertThat(options.getToolContext()).isNotNull().isEmpty(); + } + + @Test + void testEqualsAndHashCode() { + OpenAiOfficialChatOptions options1 = OpenAiOfficialChatOptions.builder() + .model("test-model") + .temperature(0.7) + .maxTokens(100) + .build(); + + OpenAiOfficialChatOptions options2 = OpenAiOfficialChatOptions.builder() + .model("test-model") + .temperature(0.7) + .maxTokens(100) + .build(); + + OpenAiOfficialChatOptions options3 = OpenAiOfficialChatOptions.builder() + .model("different-model") + .temperature(0.7) + .maxTokens(100) + .build(); + + // Test equals + assertThat(options1).isEqualTo(options2); + assertThat(options1).isNotEqualTo(options3); + assertThat(options1).isNotEqualTo(null); + + // Test hashCode + assertThat(options1.hashCode()).isEqualTo(options2.hashCode()); + } + + @Test + void testBuilderWithNullValues() { + OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() + .temperature(null) + .logitBias(null) + .stop(null) + .tools(null) + .metadata(null) + .httpHeaders(null) + .build(); + + assertThat(options.getModel()).isNull(); + assertThat(options.getTemperature()).isNull(); + assertThat(options.getLogitBias()).isNull(); + assertThat(options.getStop()).isNull(); + assertThat(options.getTools()).isNull(); + assertThat(options.getMetadata()).isNull(); + assertThat(options.getHttpHeaders()).isNull(); + } + + @Test + void testBuilderChaining() { + OpenAiOfficialChatOptions.Builder builder = OpenAiOfficialChatOptions.builder(); + + OpenAiOfficialChatOptions.Builder result = builder.model("test-model").temperature(0.7).maxTokens(100); + + assertThat(result).isSameAs(builder); + + OpenAiOfficialChatOptions options = result.build(); + assertThat(options.getModel()).isEqualTo("test-model"); + assertThat(options.getTemperature()).isEqualTo(0.7); + assertThat(options.getMaxTokens()).isEqualTo(100); + } + + @Test + void testNullAndEmptyCollections() { + OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + + // Test setting null collections + options.setLogitBias(null); + options.setStop(null); + options.setTools(null); + options.setMetadata(null); + options.setHttpHeaders(null); + + assertThat(options.getLogitBias()).isNull(); + assertThat(options.getStop()).isNull(); + assertThat(options.getTools()).isNull(); + assertThat(options.getMetadata()).isNull(); + assertThat(options.getHttpHeaders()).isNull(); + + // Test setting empty collections + options.setLogitBias(new HashMap<>()); + options.setStop(new ArrayList<>()); + options.setTools(new ArrayList<>()); + options.setMetadata(new HashMap<>()); + options.setHttpHeaders(new HashMap<>()); + + assertThat(options.getLogitBias()).isEmpty(); + assertThat(options.getStop()).isEmpty(); + assertThat(options.getTools()).isEmpty(); + assertThat(options.getMetadata()).isEmpty(); + assertThat(options.getHttpHeaders()).isEmpty(); + } + + @Test + void testStopSequencesAlias() { + OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + List stopSequences = List.of("stop1", "stop2"); + + // Setting stopSequences should also set stop + options.setStopSequences(stopSequences); + assertThat(options.getStopSequences()).isEqualTo(stopSequences); + assertThat(options.getStop()).isEqualTo(stopSequences); + + // Setting stop should also update stopSequences + List newStop = List.of("stop3", "stop4"); + options.setStop(newStop); + assertThat(options.getStop()).isEqualTo(newStop); + assertThat(options.getStopSequences()).isEqualTo(newStop); + } + + @Test + void testCopyChangeIndependence() { + OpenAiOfficialChatOptions original = OpenAiOfficialChatOptions.builder() + .model("original-model") + .temperature(0.5) + .build(); + + OpenAiOfficialChatOptions copied = original.copy(); + + // Modify original + original.setModel("modified-model"); + original.setTemperature(0.9); + + // Verify copy is unchanged + assertThat(copied.getModel()).isEqualTo("original-model"); + assertThat(copied.getTemperature()).isEqualTo(0.5); + } + + @Test + void testMaxTokensIsDeprectaed() { + // Test that setting maxCompletionTokens takes precedence over maxTokens in + // builder + OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() + .maxCompletionTokens(100) + .maxTokens(50) + .build(); + + assertThat(options.getMaxTokens()).isNull(); + assertThat(options.getMaxCompletionTokens()).isEqualTo(100); + } + + @Test + void testMaxCompletionTokensMutualExclusivityValidation() { + // Test that setting maxCompletionTokens clears maxTokens in builder + OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() + .maxTokens(50) + .maxCompletionTokens(100) + .build(); + + assertThat(options.getMaxTokens()).isNull(); + assertThat(options.getMaxCompletionTokens()).isEqualTo(100); + } + + @Test + void testMaxTokensWithNullDoesNotClearMaxCompletionTokens() { + // Test that setting maxTokens to null doesn't trigger validation + OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() + .maxCompletionTokens(100) + .maxTokens(null) + .build(); + + assertThat(options.getMaxTokens()).isNull(); + assertThat(options.getMaxCompletionTokens()).isEqualTo(100); + } + + @Test + void testMaxCompletionTokensWithNullDoesNotClearMaxTokens() { + // Test that setting maxCompletionTokens to null doesn't trigger validation + OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() + .maxTokens(50) + .maxCompletionTokens(null) + .build(); + + assertThat(options.getMaxTokens()).isEqualTo(50); + assertThat(options.getMaxCompletionTokens()).isNull(); + } + + @Test + void testBuilderCanSetOnlyMaxTokens() { + OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder().maxTokens(100).build(); + + assertThat(options.getMaxTokens()).isEqualTo(100); + assertThat(options.getMaxCompletionTokens()).isNull(); + } + + @Test + void testBuilderCanSetOnlyMaxCompletionTokens() { + OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder().maxCompletionTokens(150).build(); + + assertThat(options.getMaxTokens()).isNull(); + assertThat(options.getMaxCompletionTokens()).isEqualTo(150); + } + + @Test + void testSettersMutualExclusivityNotEnforced() { + // Test that direct setters do NOT enforce mutual exclusivity (only builder does) + OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + options.setMaxTokens(50); + options.setMaxCompletionTokens(100); + + // Both should be set when using setters directly + assertThat(options.getMaxTokens()).isEqualTo(50); + assertThat(options.getMaxCompletionTokens()).isEqualTo(100); + } + + @Test + void testToolCallbacksAndNames() { + ToolCallback callback1 = new ToolCallback() { + @Override + public org.springframework.ai.tool.definition.ToolDefinition getToolDefinition() { + return org.springframework.ai.tool.definition.DefaultToolDefinition.builder() + .name("tool1") + .description("desc1") + .inputSchema("{}") + .build(); + } + + @Override + public String call(String toolInput) { + return "result1"; + } + }; + + ToolCallback callback2 = new ToolCallback() { + @Override + public org.springframework.ai.tool.definition.ToolDefinition getToolDefinition() { + return org.springframework.ai.tool.definition.DefaultToolDefinition.builder() + .name("tool2") + .description("desc2") + .inputSchema("{}") + .build(); + } + + @Override + public String call(String toolInput) { + return "result2"; + } + }; + + OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() + .toolCallbacks(callback1, callback2) + .toolNames("tool1", "tool2") + .build(); + + assertThat(options.getToolCallbacks()).hasSize(2).containsExactly(callback1, callback2); + assertThat(options.getToolNames()).hasSize(2).contains("tool1", "tool2"); + } + + @Test + void testToolCallbacksList() { + ToolCallback callback = new ToolCallback() { + @Override + public org.springframework.ai.tool.definition.ToolDefinition getToolDefinition() { + return org.springframework.ai.tool.definition.DefaultToolDefinition.builder() + .name("tool") + .description("desc") + .inputSchema("{}") + .build(); + } + + @Override + public String call(String toolInput) { + return "result"; + } + }; + List callbacks = List.of(callback); + + OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder().toolCallbacks(callbacks).build(); + + assertThat(options.getToolCallbacks()).hasSize(1).containsExactly(callback); + } + + @Test + void testToolNamesSet() { + Set toolNames = new HashSet<>(Arrays.asList("tool1", "tool2", "tool3")); + + OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder().toolNames(toolNames).build(); + + assertThat(options.getToolNames()).hasSize(3).containsExactlyInAnyOrder("tool1", "tool2", "tool3"); + } + + @Test + @SuppressWarnings("DataFlowIssue") + void testSetToolCallbacksValidation() { + OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + + // Test null validation + assertThatThrownBy(() -> options.setToolCallbacks(null)).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("toolCallbacks cannot be null"); + + // Test null elements validation + List callbacksWithNull = new ArrayList<>(); + callbacksWithNull.add(null); + assertThatThrownBy(() -> options.setToolCallbacks(callbacksWithNull)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("toolCallbacks cannot contain null elements"); + } + + @Test + @SuppressWarnings("DataFlowIssue") + void testSetToolNamesValidation() { + OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + + // Test null validation + assertThatThrownBy(() -> options.setToolNames(null)).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("toolNames cannot be null"); + + // Test null elements validation + Set toolNamesWithNull = new HashSet<>(); + toolNamesWithNull.add(null); + assertThatThrownBy(() -> options.setToolNames(toolNamesWithNull)).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("toolNames cannot contain null elements"); + + // Test empty string validation + Set toolNamesWithEmpty = new HashSet<>(); + toolNamesWithEmpty.add(""); + assertThatThrownBy(() -> options.setToolNames(toolNamesWithEmpty)).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("toolNames cannot contain empty elements"); + + // Test whitespace string validation + Set toolNamesWithWhitespace = new HashSet<>(); + toolNamesWithWhitespace.add(" "); + assertThatThrownBy(() -> options.setToolNames(toolNamesWithWhitespace)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("toolNames cannot contain empty elements"); + } + + @Test + void testBuilderMerge() { + OpenAiOfficialChatOptions base = OpenAiOfficialChatOptions.builder() + .model("base-model") + .temperature(0.5) + .maxTokens(100) + .build(); + + OpenAiOfficialChatOptions override = OpenAiOfficialChatOptions.builder() + .model("override-model") + .topP(0.9) + .build(); + + OpenAiOfficialChatOptions merged = OpenAiOfficialChatOptions.builder().from(base).merge(override).build(); + + // Model should be overridden + assertThat(merged.getModel()).isEqualTo("override-model"); + // Temperature should be preserved from base + assertThat(merged.getTemperature()).isEqualTo(0.5); + // MaxTokens should be preserved from base + assertThat(merged.getMaxTokens()).isEqualTo(100); + // TopP should come from override + assertThat(merged.getTopP()).isEqualTo(0.9); + } + + @Test + void testBuilderFrom() { + Map logitBias = Map.of("token", 1); + List stop = List.of("stop"); + Map metadata = Map.of("key", "value"); + + OpenAiOfficialChatOptions source = OpenAiOfficialChatOptions.builder() + .model("source-model") + .temperature(0.7) + .maxTokens(100) + .logitBias(logitBias) + .stop(stop) + .metadata(metadata) + .build(); + + OpenAiOfficialChatOptions copy = OpenAiOfficialChatOptions.builder().from(source).build(); + + assertThat(copy.getModel()).isEqualTo("source-model"); + assertThat(copy.getTemperature()).isEqualTo(0.7); + assertThat(copy.getMaxTokens()).isEqualTo(100); + assertThat(copy.getLogitBias()).isEqualTo(logitBias); + assertThat(copy.getStop()).isEqualTo(stop); + assertThat(copy.getMetadata()).isEqualTo(metadata); + // Verify collections are copied + assertThat(copy.getStop()).isNotSameAs(source.getStop()); + } + + @Test + void testMergeDoesNotOverrideWithNull() { + OpenAiOfficialChatOptions base = OpenAiOfficialChatOptions.builder() + .model("base-model") + .temperature(0.5) + .maxTokens(100) + .build(); + + OpenAiOfficialChatOptions override = OpenAiOfficialChatOptions.builder().model(null).temperature(null).build(); + + OpenAiOfficialChatOptions merged = OpenAiOfficialChatOptions.builder().from(base).merge(override).build(); + + // Null values should not override + assertThat(merged.getModel()).isEqualTo("base-model"); + assertThat(merged.getTemperature()).isEqualTo(0.5); + assertThat(merged.getMaxTokens()).isEqualTo(100); + } + + @Test + void testMergeWithEmptyCollections() { + ToolCallback callback = new ToolCallback() { + @Override + public org.springframework.ai.tool.definition.ToolDefinition getToolDefinition() { + return org.springframework.ai.tool.definition.DefaultToolDefinition.builder() + .name("tool") + .description("desc") + .inputSchema("{}") + .build(); + } + + @Override + public String call(String toolInput) { + return "result"; + } + }; + + OpenAiOfficialChatOptions base = OpenAiOfficialChatOptions.builder() + .toolCallbacks(callback) + .toolNames("tool1") + .toolContext(Map.of("key", "value")) + .build(); + + OpenAiOfficialChatOptions override = new OpenAiOfficialChatOptions(); + + OpenAiOfficialChatOptions merged = OpenAiOfficialChatOptions.builder().from(base).merge(override).build(); + + // Empty collections should not override + assertThat(merged.getToolCallbacks()).hasSize(1); + assertThat(merged.getToolNames()).hasSize(1); + assertThat(merged.getToolContext()).hasSize(1); + } + + @Test + void testToString() { + OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() + .model("test-model") + .temperature(0.7) + .build(); + + String toString = options.toString(); + assertThat(toString).contains("OpenAiOfficialChatOptions"); + assertThat(toString).contains("test-model"); + assertThat(toString).contains("0.7"); + } + + @Test + void testTopKReturnsNull() { + OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + // TopK is not supported by OpenAI, should always return null + assertThat(options.getTopK()).isNull(); + } +} From dfa8e311b68aca812a91d1c36122760c9dc4744d Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Tue, 18 Nov 2025 16:51:12 +0100 Subject: [PATCH 22/49] Implementation of the OpenAI Java SDK - Update the parent pom version number, following sync up with the main branch Signed-off-by: Julien Dubois --- models/spring-ai-openai-official/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/spring-ai-openai-official/pom.xml b/models/spring-ai-openai-official/pom.xml index 99817cb4973..20feab8233f 100644 --- a/models/spring-ai-openai-official/pom.xml +++ b/models/spring-ai-openai-official/pom.xml @@ -20,7 +20,7 @@ org.springframework.ai spring-ai-parent - 1.1.0-SNAPSHOT + 2.0.0-SNAPSHOT ../../pom.xml spring-ai-openai-official From d781d4da21104eb59dfd8b3be73ae21469125a2e Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Tue, 18 Nov 2025 18:55:31 +0100 Subject: [PATCH 23/49] Implementation of the OpenAI Java SDK - Fix some of the Checkstyle issues Signed-off-by: Julien Dubois --- .../AbstractOpenAiOfficialOptions.java | 370 +-- .../OpenAiOfficialChatModel.java | 2151 +++++++++-------- .../OpenAiOfficialChatOptions.java | 1585 ++++++------ .../OpenAiOfficialEmbeddingModel.java | 359 +-- .../OpenAiOfficialEmbeddingOptions.java | 266 +- .../OpenAiOfficialImageModel.java | 290 ++- .../OpenAiOfficialImageOptions.java | 588 ++--- ...OpenAiOfficialImageGenerationMetadata.java | 68 +- .../OpenAiOfficialImageResponseMetadata.java | 69 +- .../openaiofficial/metadata/package-info.java | 26 + .../ai/openaiofficial/package-info.java | 26 + .../AzureInternalOpenAiOfficialHelper.java | 8 +- .../setup/OpenAiOfficialSetup.java | 456 ++-- .../ai/openaiofficial/setup/package-info.java | 27 + .../chat/OpenAiOfficialChatOptionsTests.java | 1 + ...OpenAiOfficialImageModelObservationIT.java | 4 +- 16 files changed, 3378 insertions(+), 2916 deletions(-) create mode 100644 models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/package-info.java create mode 100644 models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/package-info.java create mode 100644 models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/package-info.java diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java index 0044fd27645..65c4fb94508 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java @@ -25,190 +25,190 @@ public class AbstractOpenAiOfficialOptions { - /** - * The deployment URL to connect to OpenAI. - */ - private String baseUrl; - - /** - * The API key to connect to OpenAI. - */ - private String apiKey; - - /** - * Credentials used to connect to Azure OpenAI. - */ - private Credential credential; - - /** - * The model name used. When using Azure AI Foundry, this is also used as the default - * deployment name. - */ - private String model; - - /** - * The deployment name as defined in Azure AI Foundry. On Azure AI Foundry, the - * default deployment name is the same as the model name. When using OpenAI directly, - * this value isn't used. - */ - private String azureDeploymentName; - - /** - * The Azure OpenAI Service version to use when connecting to Azure AI Foundry. - */ - private AzureOpenAIServiceVersion azureOpenAIServiceVersion; - - /** - * The organization ID to use when connecting to Azure OpenAI. - */ - private String organizationId; - - /** - * Whether Azure OpenAI is detected. - */ - private boolean isAzure; - - /** - * Whether GitHub Models is detected. - */ - private boolean isGitHubModels; - - /** - * Request timeout for OpenAI client. - */ - private Duration timeout; - - /** - * Maximum number of retries for OpenAI client. - */ - private Integer maxRetries; - - /** - * Proxy settings for OpenAI client. - */ - private Proxy proxy; - - /** - * Custom headers to add to OpenAI client requests. - */ - private Map customHeaders; - - public String getBaseUrl() { - return baseUrl; - } - - public void setBaseUrl(String baseUrl) { - this.baseUrl = baseUrl; - } - - public String getApiKey() { - return apiKey; - } - - public void setApiKey(String apiKey) { - this.apiKey = apiKey; - } - - public Credential getCredential() { - return credential; - } - - public void setCredential(Credential credential) { - this.credential = credential; - } - - public String getModel() { - return model; - } - - public void setModel(String model) { - this.model = model; - } - - public String getAzureDeploymentName() { - return azureDeploymentName; - } - - public void setAzureDeploymentName(String azureDeploymentName) { - this.azureDeploymentName = azureDeploymentName; - } - - /** - * Alias for getAzureDeploymentName() - */ - public String getDeploymentName() { - return azureDeploymentName; - } - - /** - * Alias for setAzureDeploymentName() - */ - public void setDeploymentName(String azureDeploymentName) { - this.azureDeploymentName = azureDeploymentName; - } - - public AzureOpenAIServiceVersion getAzureOpenAIServiceVersion() { - return azureOpenAIServiceVersion; - } - - public void setAzureOpenAIServiceVersion(AzureOpenAIServiceVersion azureOpenAIServiceVersion) { - this.azureOpenAIServiceVersion = azureOpenAIServiceVersion; - } - - public String getOrganizationId() { - return organizationId; - } - - public void setOrganizationId(String organizationId) { - this.organizationId = organizationId; - } - - public boolean isAzure() { - return isAzure; - } - - public void setAzure(boolean azure) { - isAzure = azure; - } - - public boolean isGitHubModels() { - return isGitHubModels; - } - - public void setGitHubModels(boolean gitHubModels) { - isGitHubModels = gitHubModels; - } - - public Duration getTimeout() { - return timeout; - } - - public void setTimeout(Duration timeout) { - this.timeout = timeout; - } - - public Integer getMaxRetries() { - return maxRetries; - } - - public void setMaxRetries(Integer maxRetries) { - this.maxRetries = maxRetries; - } - - public Proxy getProxy() { - return proxy; - } - - public void setProxy(Proxy proxy) { - this.proxy = proxy; - } - - public Map getCustomHeaders() { - return customHeaders; - } - - public void setCustomHeaders(Map customHeaders) { - this.customHeaders = customHeaders; - } + /** + * The deployment URL to connect to OpenAI. + */ + private String baseUrl; + + /** + * The API key to connect to OpenAI. + */ + private String apiKey; + + /** + * Credentials used to connect to Azure OpenAI. + */ + private Credential credential; + + /** + * The model name used. When using Azure AI Foundry, this is also used as the default + * deployment name. + */ + private String model; + + /** + * The deployment name as defined in Azure AI Foundry. On Azure AI Foundry, the + * default deployment name is the same as the model name. When using OpenAI directly, + * this value isn't used. + */ + private String azureDeploymentName; + + /** + * The Azure OpenAI Service version to use when connecting to Azure AI Foundry. + */ + private AzureOpenAIServiceVersion azureOpenAIServiceVersion; + + /** + * The organization ID to use when connecting to Azure OpenAI. + */ + private String organizationId; + + /** + * Whether Azure OpenAI is detected. + */ + private boolean isAzure; + + /** + * Whether GitHub Models is detected. + */ + private boolean isGitHubModels; + + /** + * Request timeout for OpenAI client. + */ + private Duration timeout; + + /** + * Maximum number of retries for OpenAI client. + */ + private Integer maxRetries; + + /** + * Proxy settings for OpenAI client. + */ + private Proxy proxy; + + /** + * Custom headers to add to OpenAI client requests. + */ + private Map customHeaders; + + public String getBaseUrl() { + return baseUrl; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + + public String getApiKey() { + return apiKey; + } + + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + } + + public Credential getCredential() { + return credential; + } + + public void setCredential(Credential credential) { + this.credential = credential; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getAzureDeploymentName() { + return azureDeploymentName; + } + + public void setAzureDeploymentName(String azureDeploymentName) { + this.azureDeploymentName = azureDeploymentName; + } + + /** + * Alias for getAzureDeploymentName() + */ + public String getDeploymentName() { + return azureDeploymentName; + } + + /** + * Alias for setAzureDeploymentName() + */ + public void setDeploymentName(String azureDeploymentName) { + this.azureDeploymentName = azureDeploymentName; + } + + public AzureOpenAIServiceVersion getAzureOpenAIServiceVersion() { + return azureOpenAIServiceVersion; + } + + public void setAzureOpenAIServiceVersion(AzureOpenAIServiceVersion azureOpenAIServiceVersion) { + this.azureOpenAIServiceVersion = azureOpenAIServiceVersion; + } + + public String getOrganizationId() { + return organizationId; + } + + public void setOrganizationId(String organizationId) { + this.organizationId = organizationId; + } + + public boolean isAzure() { + return isAzure; + } + + public void setAzure(boolean azure) { + isAzure = azure; + } + + public boolean isGitHubModels() { + return isGitHubModels; + } + + public void setGitHubModels(boolean gitHubModels) { + isGitHubModels = gitHubModels; + } + + public Duration getTimeout() { + return timeout; + } + + public void setTimeout(Duration timeout) { + this.timeout = timeout; + } + + public Integer getMaxRetries() { + return maxRetries; + } + + public void setMaxRetries(Integer maxRetries) { + this.maxRetries = maxRetries; + } + + public Proxy getProxy() { + return proxy; + } + + public void setProxy(Proxy proxy) { + this.proxy = proxy; + } + + public Map getCustomHeaders() { + return customHeaders; + } + + public void setCustomHeaders(Map customHeaders) { + this.customHeaders = customHeaders; + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java index f9497af4428..948bafcb414 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -86,1044 +86,1121 @@ */ public class OpenAiOfficialChatModel implements ChatModel { - private static final String DEFAULT_MODEL_NAME = OpenAiOfficialChatOptions.DEFAULT_CHAT_MODEL; + private static final String DEFAULT_MODEL_NAME = OpenAiOfficialChatOptions.DEFAULT_CHAT_MODEL; + + private static final ChatModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultChatModelObservationConvention(); + + private static final ToolCallingManager DEFAULT_TOOL_CALLING_MANAGER = ToolCallingManager.builder().build(); + + private final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatModel.class); + + private final OpenAIClient openAiClient; + + private final OpenAIClientAsync openAiClientAsync; + + private final OpenAiOfficialChatOptions options; + + private final ObservationRegistry observationRegistry; + + private final ToolCallingManager toolCallingManager; + + private final ToolExecutionEligibilityPredicate toolExecutionEligibilityPredicate; + + private ChatModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; + + /** + * Creates a new OpenAiOfficialChatModel with default options. + */ + public OpenAiOfficialChatModel() { + this(null, null, null, null, null, null); + } + + /** + * Creates a new OpenAiOfficialChatModel with the given options. + * @param options the chat options + */ + public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options) { + this(null, null, options, null, null, null); + } + + /** + * Creates a new OpenAiOfficialChatModel with the given options and observation registry. + * @param options the chat options + * @param observationRegistry the observation registry + */ + public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options, ObservationRegistry observationRegistry) { + this(null, null, options, null, observationRegistry, null); + } + + /** + * Creates a new OpenAiOfficialChatModel with the given options, tool calling manager, and observation registry. + * @param options the chat options + * @param toolCallingManager the tool calling manager + * @param observationRegistry the observation registry + */ + public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options, ToolCallingManager toolCallingManager, + ObservationRegistry observationRegistry) { + this(null, null, options, toolCallingManager, observationRegistry, null); + } + + /** + * Creates a new OpenAiOfficialChatModel with the given OpenAI clients. + * @param openAIClient the synchronous OpenAI client + * @param openAiClientAsync the asynchronous OpenAI client + */ + public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync) { + this(openAIClient, openAiClientAsync, null, null, null, null); + } + + /** + * Creates a new OpenAiOfficialChatModel with the given OpenAI clients and options. + * @param openAIClient the synchronous OpenAI client + * @param openAiClientAsync the asynchronous OpenAI client + * @param options the chat options + */ + public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync, + OpenAiOfficialChatOptions options) { + this(openAIClient, openAiClientAsync, options, null, null, null); + } + + /** + * Creates a new OpenAiOfficialChatModel with the given OpenAI clients, options, and observation registry. + * @param openAIClient the synchronous OpenAI client + * @param openAiClientAsync the asynchronous OpenAI client + * @param options the chat options + * @param observationRegistry the observation registry + */ + public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync, + OpenAiOfficialChatOptions options, ObservationRegistry observationRegistry) { + this(openAIClient, openAiClientAsync, options, null, observationRegistry, null); + } + + /** + * Creates a new OpenAiOfficialChatModel with all configuration options. + * @param openAiClient the synchronous OpenAI client + * @param openAiClientAsync the asynchronous OpenAI client + * @param options the chat options + * @param toolCallingManager the tool calling manager + * @param observationRegistry the observation registry + * @param toolExecutionEligibilityPredicate the predicate to determine tool execution eligibility + */ + public OpenAiOfficialChatModel(OpenAIClient openAiClient, OpenAIClientAsync openAiClientAsync, + OpenAiOfficialChatOptions options, ToolCallingManager toolCallingManager, + ObservationRegistry observationRegistry, + ToolExecutionEligibilityPredicate toolExecutionEligibilityPredicate) { + + if (options == null) { + this.options = OpenAiOfficialChatOptions.builder().model(DEFAULT_MODEL_NAME).build(); + } + else { + this.options = options; + } + this.openAiClient = Objects.requireNonNullElseGet(openAiClient, + () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), + this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), + this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), + this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), + this.options.getProxy(), this.options.getCustomHeaders())); + + this.openAiClientAsync = Objects.requireNonNullElseGet(openAiClientAsync, + () -> setupAsyncClient(this.options.getBaseUrl(), this.options.getApiKey(), + this.options.getCredential(), this.options.getAzureDeploymentName(), + this.options.getAzureOpenAIServiceVersion(), this.options.getOrganizationId(), + this.options.isAzure(), this.options.isGitHubModels(), this.options.getModel(), + this.options.getTimeout(), this.options.getMaxRetries(), this.options.getProxy(), + this.options.getCustomHeaders())); + + this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); + this.toolCallingManager = Objects.requireNonNullElse(toolCallingManager, DEFAULT_TOOL_CALLING_MANAGER); + this.toolExecutionEligibilityPredicate = Objects.requireNonNullElse(toolExecutionEligibilityPredicate, + new DefaultToolExecutionEligibilityPredicate()); + } + + /** + * Gets the chat options for this model. + * @return the chat options + */ + public OpenAiOfficialChatOptions getOptions() { + return this.options; + } + + @Override + public ChatResponse call(Prompt prompt) { + if (this.openAiClient == null) { + throw new IllegalStateException( + "OpenAI sync client is not configured. Have you set the 'streamUsage' option to false?"); + } + Prompt requestPrompt = buildRequestPrompt(prompt); + return this.internalCall(requestPrompt, null); + } + + /** + * Internal method to handle chat completion calls with tool execution support. + * @param prompt the prompt for the chat completion + * @param previousChatResponse the previous chat response for accumulating usage + * @return the chat response + */ + public ChatResponse internalCall(Prompt prompt, ChatResponse previousChatResponse) { + + ChatCompletionCreateParams request = createRequest(prompt, false); + + ChatModelObservationContext observationContext = ChatModelObservationContext.builder() + .prompt(prompt) + .provider(AiProvider.OPENAI_OFFICIAL.value()) + .build(); + + ChatResponse response = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION + .observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, + this.observationRegistry) + .observe(() -> { + + ChatCompletion chatCompletion = this.openAiClient.chat().completions().create(request); + + List choices = chatCompletion.choices(); + if (choices.isEmpty()) { + logger.warn("No choices returned for prompt: {}", prompt); + return new ChatResponse(List.of()); + } + + List generations = choices.stream().map(choice -> { + chatCompletion.id(); + choice.finishReason(); + Map metadata = Map.of("id", chatCompletion.id(), "role", + choice.message()._role().asString().isPresent() ? choice.message()._role().asStringOrThrow() + : "", + "index", choice.index(), "finishReason", choice.finishReason().value().toString(), + "refusal", choice.message().refusal().isPresent() ? choice.message().refusal() : "", + "annotations", choice.message().annotations().isPresent() ? choice.message().annotations() + : List.of(Map.of())); + return buildGeneration(choice, metadata); + }).toList(); + + // Current usage + CompletionUsage usage = chatCompletion.usage().orElse(null); + Usage currentChatResponseUsage = usage != null ? getDefaultUsage(usage) : new EmptyUsage(); + Usage accumulatedUsage = UsageCalculator.getCumulativeUsage(currentChatResponseUsage, + previousChatResponse); + ChatResponse chatResponse = new ChatResponse(generations, from(chatCompletion, accumulatedUsage)); + + observationContext.setResponse(chatResponse); + + return chatResponse; + + }); + + if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(prompt.getOptions(), response)) { + var toolExecutionResult = this.toolCallingManager.executeToolCalls(prompt, response); + if (toolExecutionResult.returnDirect()) { + // Return tool execution result directly to the client. + return ChatResponse.builder() + .from(response) + .generations(ToolExecutionResult.buildGenerations(toolExecutionResult)) + .build(); + } + else { + // Send the tool execution result back to the model. + return this.internalCall(new Prompt(toolExecutionResult.conversationHistory(), prompt.getOptions()), + response); + } + } + + return response; + } + + @Override + public Flux stream(Prompt prompt) { + if (this.openAiClientAsync == null) { + throw new IllegalStateException( + "OpenAI async client is not configured. Streaming is not supported with the current configuration. Have you set the 'streamUsage' option to true?"); + } + Prompt requestPrompt = buildRequestPrompt(prompt); + return internalStream(requestPrompt, null); + } + + /** + * Safely extracts the assistant message from a chat response. + * @param response the chat response + * @return the assistant message, or null if not available + */ + public AssistantMessage safeAssistantMessage(ChatResponse response) { + if (response == null) + return null; + Generation gen = response.getResult(); + if (gen == null) + return null; + return gen.getOutput(); + } + + /** + * Internal method to handle streaming chat completion calls with tool execution support. + * @param prompt the prompt for the chat completion + * @param previousChatResponse the previous chat response for accumulating usage + * @return a Flux of chat responses + */ + public Flux internalStream(Prompt prompt, ChatResponse previousChatResponse) { + return Flux.deferContextual(contextView -> { + ChatCompletionCreateParams request = createRequest(prompt, true); + ConcurrentHashMap roleMap = new ConcurrentHashMap<>(); + final ChatModelObservationContext observationContext = ChatModelObservationContext.builder() + .prompt(prompt) + .provider(AiProvider.OPENAI_OFFICIAL.value()) + .build(); + Observation observation = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION.observation( + this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, + this.observationRegistry); + observation.parentObservation(contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null)).start(); + + Flux chatResponses = Flux.create(sink -> { + this.openAiClientAsync.chat().completions().createStreaming(request).subscribe(chunk -> { + try { + ChatCompletion chatCompletion = chunkToChatCompletion(chunk); + String id = chatCompletion.id(); + List generations = chatCompletion.choices().stream().map(choice -> { + roleMap.putIfAbsent(id, choice.message()._role().asString().isPresent() + ? choice.message()._role().asStringOrThrow() : ""); + + Map metadata = Map.of("id", id, "role", roleMap.getOrDefault(id, ""), + "index", choice.index(), "finishReason", choice.finishReason().value(), "refusal", + choice.message().refusal().isPresent() ? choice.message().refusal() : "", + "annotations", choice.message().annotations().isPresent() + ? choice.message().annotations() : List.of(), + "chunkChoice", chunk.choices().get((int) choice.index())); + + return buildGeneration(choice, metadata); + }).toList(); + Optional usage = chatCompletion.usage(); + CompletionUsage usageVal = usage.orElse(null); + Usage currentUsage = usageVal != null ? getDefaultUsage(usageVal) : new EmptyUsage(); + Usage accumulated = UsageCalculator.getCumulativeUsage(currentUsage, previousChatResponse); + sink.next(new ChatResponse(generations, from(chatCompletion, accumulated))); + } + catch (Exception e) { + logger.error("Error processing chat completion", e); + sink.error(e); + } + }).onCompleteFuture().whenComplete((unused, throwable) -> { + if (throwable != null) + sink.error(throwable); + else + sink.complete(); + }); + }).buffer(2, 1).map(buffer -> { + ChatResponse first = buffer.get(0); + if (request.streamOptions().isPresent() && buffer.size() == 2) { + ChatResponse second = buffer.get(1); + if (second != null) { + Usage usage = second.getMetadata().getUsage(); + if (!UsageCalculator.isEmpty(usage)) { + return new ChatResponse(first.getResults(), from(first.getMetadata(), usage)); + } + } + } + return first; + }); + + Flux flux = chatResponses + .contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation)); + + return flux.collectList().flatMapMany(list -> { + if (list.isEmpty()) + return Flux.empty(); + boolean hasToolCalls = list.stream() + .map(this::safeAssistantMessage) + .filter(Objects::nonNull) + .anyMatch(am -> !CollectionUtils.isEmpty(am.getToolCalls())); + if (!hasToolCalls) { + if (list.size() > 2) { + ChatResponse penultimateResponse = list.get(list.size() - 2); // Get + // the + // finish + // reason + ChatResponse lastResponse = list.get(list.size() - 1); // Get the + // usage + Usage usage = lastResponse.getMetadata().getUsage(); + observationContext.setResponse(new ChatResponse(penultimateResponse.getResults(), + from(penultimateResponse.getMetadata(), usage))); + } + return Flux.fromIterable(list); + } + Map builders = new HashMap<>(); + StringBuilder text = new StringBuilder(); + ChatResponseMetadata finalMetadata = null; + ChatGenerationMetadata finalGenMetadata = null; + Map props = new HashMap<>(); + for (ChatResponse chatResponse : list) { + AssistantMessage am = safeAssistantMessage(chatResponse); + if (am == null) + continue; + if (am.getText() != null) + text.append(am.getText()); + if (am.getMetadata() != null) + props.putAll(am.getMetadata()); + if (!CollectionUtils.isEmpty(am.getToolCalls())) { + Object ccObj = am.getMetadata().get("chunkChoice"); + if (ccObj instanceof ChatCompletionChunk.Choice chunkChoice + && chunkChoice.delta().toolCalls().isPresent()) { + List deltaCalls = chunkChoice.delta() + .toolCalls() + .get(); + for (int i = 0; i < am.getToolCalls().size() && i < deltaCalls.size(); i++) { + AssistantMessage.ToolCall tc = am.getToolCalls().get(i); + ChatCompletionChunk.Choice.Delta.ToolCall dtc = deltaCalls.get(i); + String key = chunkChoice.index() + "-" + dtc.index(); + ToolCallBuilder toolCallBuilder = builders.computeIfAbsent(key, + k -> new ToolCallBuilder()); + toolCallBuilder.merge(tc); + } + } + else { + for (AssistantMessage.ToolCall tc : am.getToolCalls()) { + ToolCallBuilder toolCallBuilder = builders.computeIfAbsent(tc.id(), + k -> new ToolCallBuilder()); + toolCallBuilder.merge(tc); + } + } + } + Generation generation = chatResponse.getResult(); + if (generation != null && generation.getMetadata() != null + && generation.getMetadata() != ChatGenerationMetadata.NULL) { + finalGenMetadata = generation.getMetadata(); + } + if (chatResponse.getMetadata() != null) + finalMetadata = chatResponse.getMetadata(); + } + List merged = builders.values() + .stream() + .map(ToolCallBuilder::build) + .filter(tc -> StringUtils.hasText(tc.name())) + .toList(); + AssistantMessage.Builder assistantMessageBuilder = AssistantMessage.builder() + .content(text.toString()) + .properties(props); + if (!merged.isEmpty()) { + assistantMessageBuilder.toolCalls(merged); + } + AssistantMessage assistantMessage = assistantMessageBuilder.build(); + Generation finalGen = new Generation(assistantMessage, + finalGenMetadata != null ? finalGenMetadata : ChatGenerationMetadata.NULL); + ChatResponse aggregated = new ChatResponse(List.of(finalGen), finalMetadata); + observationContext.setResponse(aggregated); + if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(prompt.getOptions(), aggregated)) { + return Flux.deferContextual(ctx -> { + ToolExecutionResult tetoolExecutionResult; + try { + ToolCallReactiveContextHolder.setContext(ctx); + tetoolExecutionResult = this.toolCallingManager.executeToolCalls(prompt, aggregated); + } + finally { + ToolCallReactiveContextHolder.clearContext(); + } + if (tetoolExecutionResult.returnDirect()) + return Flux.just(ChatResponse.builder() + .from(aggregated) + .generations(ToolExecutionResult.buildGenerations(tetoolExecutionResult)) + .build()); + return this.internalStream( + new Prompt(tetoolExecutionResult.conversationHistory(), prompt.getOptions()), + aggregated); + }).subscribeOn(Schedulers.boundedElastic()); + } + return Flux.just(aggregated); + }).doOnError(observation::error).doFinally(s -> observation.stop()); + }); + } + + private Generation buildGeneration(ChatCompletion.Choice choice, Map metadata) { + ChatCompletionMessage message = choice.message(); + List toolCalls = new ArrayList<>(); + + if (metadata.containsKey("chunkChoice")) { + Object chunkChoiceObj = metadata.get("chunkChoice"); + if (chunkChoiceObj instanceof ChatCompletionChunk.Choice chunkChoice) { + if (chunkChoice.delta().toolCalls().isPresent()) { + toolCalls = chunkChoice.delta() + .toolCalls() + .get() + .stream() + .filter(tc -> tc.function().isPresent()) + .map(tc -> { + var funcOpt = tc.function(); + if (funcOpt.isEmpty()) + return null; + var func = funcOpt.get(); + String id = tc.id().orElse(""); + String name = func.name().orElse(""); + String arguments = func.arguments().orElse(""); + return new AssistantMessage.ToolCall(id, "function", name, arguments); + }) + .filter(Objects::nonNull) + .toList(); + } + } + } + else { + toolCalls = message.toolCalls() + .map(list -> list.stream().filter(tc -> tc.function().isPresent()).map(tc -> { + var opt = tc.function(); + if (opt.isEmpty()) + return null; + var funcCall = opt.get(); + var functionDef = funcCall.function(); + String id = funcCall.id(); + String name = functionDef.name(); + String arguments = functionDef.arguments(); + return new AssistantMessage.ToolCall(id, "function", name, arguments); + }).filter(Objects::nonNull).toList()) + .orElse(List.of()); + } + + var generationMetadataBuilder = ChatGenerationMetadata.builder() + .finishReason(choice.finishReason().value().name()); + String textContent = message.content().orElse(""); + var assistantMessage = AssistantMessage.builder() + .content(textContent) + .properties(metadata) + .toolCalls(toolCalls) + .build(); + return new Generation(assistantMessage, generationMetadataBuilder.build()); + } + + private ChatResponseMetadata from(ChatCompletion result, Usage usage) { + Assert.notNull(result, "OpenAI ChatCompletion must not be null"); + result.model(); + result.id(); + return ChatResponseMetadata.builder() + .id(result.id()) + .usage(usage) + .model(result.model()) + .keyValue("created", result.created()) + .build(); + } + + private ChatResponseMetadata from(ChatResponseMetadata chatResponseMetadata, Usage usage) { + Assert.notNull(chatResponseMetadata, "OpenAI ChatResponseMetadata must not be null"); + return ChatResponseMetadata.builder() + .id(chatResponseMetadata.getId() != null ? chatResponseMetadata.getId() : "") + .usage(usage) + .model(chatResponseMetadata.getModel() != null ? chatResponseMetadata.getModel() : "") + .build(); + } + + /** + * Convert the ChatCompletionChunk into a ChatCompletion. The Usage is set to null. + * @param chunk the ChatCompletionChunk to convert + * @return the ChatCompletion + */ + private ChatCompletion chunkToChatCompletion(ChatCompletionChunk chunk) { + List choices = chunk.choices().stream().map(chunkChoice -> { + ChatCompletion.Choice.FinishReason finishReason = ChatCompletion.Choice.FinishReason.of(""); + if (chunkChoice.finishReason().isPresent()) { + finishReason = ChatCompletion.Choice.FinishReason + .of(chunkChoice.finishReason().get().value().name().toLowerCase()); + } + + ChatCompletion.Choice.Builder choiceBuilder = ChatCompletion.Choice.builder() + .finishReason(finishReason) + .index(chunkChoice.index()) + .message(ChatCompletionMessage.builder() + .content(chunkChoice.delta().content()) + .refusal(chunkChoice.delta().refusal()) + .build()); + + // Handle optional logprobs + if (chunkChoice.logprobs().isPresent()) { + var logprobs = chunkChoice.logprobs().get(); + choiceBuilder.logprobs(ChatCompletion.Choice.Logprobs.builder() + .content(logprobs.content()) + .refusal(logprobs.refusal()) + .build()); + } + else { + // Provide empty logprobs when not present + choiceBuilder + .logprobs(ChatCompletion.Choice.Logprobs.builder().content(List.of()).refusal(List.of()).build()); + } + + return choiceBuilder.build(); + }).toList(); + + return ChatCompletion.builder() + .id(chunk.id()) + .choices(choices) + .created(chunk.created()) + .model(chunk.model()) + .usage(chunk.usage() + .orElse(CompletionUsage.builder().promptTokens(0).completionTokens(0).totalTokens(0).build())) + .build(); + } + + private DefaultUsage getDefaultUsage(CompletionUsage usage) { + return new DefaultUsage(Math.toIntExact(usage.promptTokens()), Math.toIntExact(usage.completionTokens()), + Math.toIntExact(usage.totalTokens()), usage); + } + + /** + * Builds the request prompt by merging runtime options with default options. + * @param prompt the original prompt + * @return the prompt with merged options + */ + Prompt buildRequestPrompt(Prompt prompt) { + // Process runtime options + OpenAiOfficialChatOptions runtimeOptions = null; + if (prompt.getOptions() != null) { + if (prompt.getOptions() instanceof ToolCallingChatOptions toolCallingChatOptions) { + runtimeOptions = ModelOptionsUtils.copyToTarget(toolCallingChatOptions, ToolCallingChatOptions.class, + OpenAiOfficialChatOptions.class); + } + else { + runtimeOptions = ModelOptionsUtils.copyToTarget(prompt.getOptions(), ChatOptions.class, + OpenAiOfficialChatOptions.class); + } + } + + // Define request options by merging runtime options and default options + OpenAiOfficialChatOptions requestOptions = OpenAiOfficialChatOptions.builder() + .from(this.options) + .merge(runtimeOptions != null ? runtimeOptions : OpenAiOfficialChatOptions.builder().build()) + .build(); + + // Merge @JsonIgnore-annotated options explicitly since they are ignored by + // Jackson, used by ModelOptionsUtils. + if (runtimeOptions != null) { + if (runtimeOptions.getTopK() != null) { + logger.warn("The topK option is not supported by OpenAI chat models. Ignoring."); + } + + Map mergedHttpHeaders = new HashMap<>(this.options.getHttpHeaders()); + mergedHttpHeaders.putAll(runtimeOptions.getHttpHeaders()); + requestOptions.setHttpHeaders(mergedHttpHeaders); + + requestOptions.setInternalToolExecutionEnabled(runtimeOptions.getInternalToolExecutionEnabled() != null + ? runtimeOptions.getInternalToolExecutionEnabled() + : this.options.getInternalToolExecutionEnabled()); + requestOptions.setToolNames( + ToolCallingChatOptions.mergeToolNames(runtimeOptions.getToolNames(), this.options.getToolNames())); + requestOptions.setToolCallbacks(ToolCallingChatOptions.mergeToolCallbacks(runtimeOptions.getToolCallbacks(), + this.options.getToolCallbacks())); + requestOptions.setToolContext(ToolCallingChatOptions.mergeToolContext(runtimeOptions.getToolContext(), + this.options.getToolContext())); + } + else { + requestOptions.setHttpHeaders(this.options.getHttpHeaders()); + requestOptions.setInternalToolExecutionEnabled(this.options.getInternalToolExecutionEnabled()); + requestOptions.setToolNames(this.options.getToolNames()); + requestOptions.setToolCallbacks(this.options.getToolCallbacks()); + requestOptions.setToolContext(this.options.getToolContext()); + } + + ToolCallingChatOptions.validateToolCallbacks(requestOptions.getToolCallbacks()); + + return new Prompt(prompt.getInstructions(), requestOptions); + } + + /** + * Creates a chat completion request from the given prompt. + * @param prompt the prompt containing messages and options + * @param stream whether this is a streaming request + * @return the chat completion create parameters + */ + ChatCompletionCreateParams createRequest(Prompt prompt, boolean stream) { + + List chatCompletionMessageParams = prompt.getInstructions() + .stream() + .map(message -> { + if (message.getMessageType() == MessageType.USER || message.getMessageType() == MessageType.SYSTEM) { + // Handle simple text content for user and system messages + ChatCompletionUserMessageParam.Builder builder = ChatCompletionUserMessageParam.builder(); + + if (message instanceof UserMessage userMessage + && !CollectionUtils.isEmpty(userMessage.getMedia())) { + // Handle media content (images, audio, files) + List parts = new ArrayList<>(); + + if (!message.getText().isEmpty()) { + parts.add(ChatCompletionContentPart + .ofText(ChatCompletionContentPartText.builder().text(message.getText()).build())); + } + + // Add media content parts + userMessage.getMedia().forEach(media -> { + String mimeType = media.getMimeType().toString(); + if (mimeType.startsWith("image/")) { + if (media.getData() instanceof java.net.URI uri) { + parts.add(ChatCompletionContentPart + .ofImageUrl(ChatCompletionContentPartImage.builder() + .imageUrl(ChatCompletionContentPartImage.ImageUrl.builder() + .url(uri.toString()) + .build()) + .build())); + } + else if (media.getData() instanceof String text) { + // The org.springframework.ai.content.Media object + // should store the URL as a java.net.URI but it + // transforms it to String somewhere along the way, + // for example in its Builder class. So, we accept + // String as well here for image URLs. + parts.add(ChatCompletionContentPart + .ofImageUrl(ChatCompletionContentPartImage.builder() + .imageUrl( + ChatCompletionContentPartImage.ImageUrl.builder().url(text).build()) + .build())); + } + else if (media.getData() instanceof byte[] bytes) { + // Assume the bytes are an image. So, convert the + // bytes to a base64 encoded + ChatCompletionContentPartImage.ImageUrl.Builder imageUrlBuilder = ChatCompletionContentPartImage.ImageUrl + .builder(); + + imageUrlBuilder.url("data:" + mimeType + ";base64," + + Base64.getEncoder().encodeToString(bytes)); + parts.add(ChatCompletionContentPart + .ofImageUrl(ChatCompletionContentPartImage.builder() + .imageUrl(imageUrlBuilder.build()) + .build())); + } + else { + logger.info( + "Could not process image media with data of type: {}. Only java.net.URI is supported for image URLs.", + media.getData().getClass().getSimpleName()); + } + } + else if (mimeType.startsWith("audio/")) { + parts.add(ChatCompletionContentPart + .ofInputAudio(ChatCompletionContentPartInputAudio.builder() + .inputAudio(ChatCompletionContentPartInputAudio.builder() + .inputAudio(ChatCompletionContentPartInputAudio.InputAudio.builder() + .data(fromAudioData(media.getData())) + .format(mimeType.contains("mp3") + ? ChatCompletionContentPartInputAudio.InputAudio.Format.MP3 + : ChatCompletionContentPartInputAudio.InputAudio.Format.WAV) + .build()) + .build() + .inputAudio()) + .build())); + } + else { + // Assume it's a file or other media type represented as a + // data URL + parts.add(ChatCompletionContentPart.ofText(ChatCompletionContentPartText.builder() + .text(fromMediaData(media.getMimeType(), media.getData())) + .build())); + } + }); + builder.contentOfArrayOfContentParts(parts); + } + else { + // Simple text message + builder.content(ChatCompletionContentPartText.builder().text(message.getText()).build().text()); + } + + if (message.getMessageType() == MessageType.USER) { + builder.role(JsonValue.from(MessageType.USER.getValue())); + } + else { + builder.role(JsonValue.from(MessageType.SYSTEM.getValue())); + } + + return List.of(ChatCompletionMessageParam.ofUser(builder.build())); + } + else if (message.getMessageType() == MessageType.ASSISTANT) { + var assistantMessage = (AssistantMessage) message; + ChatCompletionAssistantMessageParam.Builder builder = ChatCompletionAssistantMessageParam.builder() + .role(JsonValue.from(MessageType.ASSISTANT.getValue())); + + if (assistantMessage.getText() != null) { + builder.content(ChatCompletionAssistantMessageParam.builder() + .content(assistantMessage.getText()) + .build() + .content()); + } + + if (!CollectionUtils.isEmpty(assistantMessage.getToolCalls())) { + List toolCalls = assistantMessage.getToolCalls() + .stream() + .map(toolCall -> ChatCompletionMessageToolCall + .ofFunction(ChatCompletionMessageFunctionToolCall.builder() + .id(toolCall.id()) + .function(ChatCompletionMessageFunctionToolCall.Function.builder() + .name(toolCall.name()) + .arguments(toolCall.arguments()) + .build()) + .build())) + .toList(); + + builder.toolCalls(toolCalls); + } + + return List.of(ChatCompletionMessageParam.ofAssistant(builder.build())); + } + else if (message.getMessageType() == MessageType.TOOL) { + ToolResponseMessage toolMessage = (ToolResponseMessage) message; + + ChatCompletionToolMessageParam.Builder builder = ChatCompletionToolMessageParam.builder(); + builder.content(toolMessage.getText() != null ? toolMessage.getText() : ""); + builder.role(JsonValue.from(MessageType.TOOL.getValue())); + + if (toolMessage.getResponses().isEmpty()) { + return List.of(ChatCompletionMessageParam.ofTool(builder.build())); + } + return toolMessage.getResponses().stream().map(response -> { + String callId = response.id(); + String callResponse = response.responseData(); + + return ChatCompletionMessageParam + .ofTool(builder.toolCallId(callId).content(callResponse).build()); + }).toList(); + } + else { + throw new IllegalArgumentException("Unsupported message type: " + message.getMessageType()); + } + }) + .flatMap(List::stream) + .toList(); + + ChatCompletionCreateParams.Builder builder = ChatCompletionCreateParams.builder(); + + chatCompletionMessageParams.forEach(builder::addMessage); + + OpenAiOfficialChatOptions requestOptions = (OpenAiOfficialChatOptions) prompt.getOptions(); + + // Use deployment name if available (for Azure AI Foundry), otherwise use model + // name + if (requestOptions.getDeploymentName() != null) { + builder.model(requestOptions.getDeploymentName()); + } + else if (requestOptions.getModel() != null) { + builder.model(requestOptions.getModel()); + } + + if (requestOptions.getFrequencyPenalty() != null) { + builder.frequencyPenalty(requestOptions.getFrequencyPenalty()); + } + if (requestOptions.getLogitBias() != null) { + builder.logitBias(ChatCompletionCreateParams.LogitBias.builder() + .putAllAdditionalProperties(requestOptions.getLogitBias() + .entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> JsonValue.from(entry.getValue())))) + .build()); + } + if (requestOptions.getLogprobs() != null) { + builder.logprobs(requestOptions.getLogprobs()); + } + if (requestOptions.getTopLogprobs() != null) { + builder.topLogprobs(requestOptions.getTopLogprobs()); + } + if (requestOptions.getMaxTokens() != null) { + builder.maxTokens(requestOptions.getMaxTokens()); + } + if (requestOptions.getMaxCompletionTokens() != null) { + builder.maxCompletionTokens(requestOptions.getMaxCompletionTokens()); + } + if (requestOptions.getN() != null) { + builder.n(requestOptions.getN()); + } + if (requestOptions.getOutputAudio() != null) { + builder.audio(requestOptions.getOutputAudio()); + } + if (requestOptions.getPresencePenalty() != null) { + builder.presencePenalty(requestOptions.getPresencePenalty()); + } + if (requestOptions.getResponseFormat() != null) { + ResponseFormat responseFormat = requestOptions.getResponseFormat(); + if (responseFormat.getType().equals(ResponseFormat.Type.TEXT)) { + builder.responseFormat(ResponseFormatText.builder().build()); + } + else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_OBJECT)) { + builder.responseFormat(ResponseFormatJsonObject.builder().build()); + } + else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_SCHEMA)) { + String jsonSchemaString = responseFormat.getJsonSchema() != null ? responseFormat.getJsonSchema() : ""; + try { + com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); + ResponseFormatJsonSchema.JsonSchema.Builder jsonSchemaBuilder = ResponseFormatJsonSchema.JsonSchema + .builder(); + jsonSchemaBuilder.name("json_schema"); + jsonSchemaBuilder.strict(true); + + ResponseFormatJsonSchema.JsonSchema.Schema schema = mapper.readValue(jsonSchemaString, + ResponseFormatJsonSchema.JsonSchema.Schema.class); + + jsonSchemaBuilder.schema(schema); + + builder.responseFormat( + ResponseFormatJsonSchema.builder().jsonSchema(jsonSchemaBuilder.build()).build()); + } + catch (Exception e) { + throw new IllegalArgumentException("Failed to parse JSON schema: " + jsonSchemaString, e); + } + } + else { + throw new IllegalArgumentException("Unsupported response format type: " + responseFormat.getType()); + } + } + if (requestOptions.getSeed() != null) { + builder.seed(requestOptions.getSeed()); + } + if (requestOptions.getStop() != null && !requestOptions.getStop().isEmpty()) { + if (requestOptions.getStop().size() == 1) { + builder.stop(ChatCompletionCreateParams.Stop.ofString(requestOptions.getStop().get(0))); + } + else { + builder.stop(ChatCompletionCreateParams.Stop.ofStrings(requestOptions.getStop())); + } + } + if (requestOptions.getTemperature() != null) { + builder.temperature(requestOptions.getTemperature()); + } + if (requestOptions.getTopP() != null) { + builder.topP(requestOptions.getTopP()); + } + if (requestOptions.getUser() != null) { + builder.user(requestOptions.getUser()); + } + if (requestOptions.getParallelToolCalls() != null) { + builder.parallelToolCalls(requestOptions.getParallelToolCalls()); + } + if (requestOptions.getReasoningEffort() != null) { + builder.reasoningEffort(ReasoningEffort.of(requestOptions.getReasoningEffort().toLowerCase())); + } + if (requestOptions.getVerbosity() != null) { + builder.verbosity(ChatCompletionCreateParams.Verbosity.of(requestOptions.getVerbosity())); + } + + if (requestOptions.getStore() != null) { + builder.store(requestOptions.getStore()); + } + if (requestOptions.getMetadata() != null && !requestOptions.getMetadata().isEmpty()) { + builder.metadata(ChatCompletionCreateParams.Metadata.builder() + .putAllAdditionalProperties(requestOptions.getMetadata() + .entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> JsonValue.from(entry.getValue())))) + .build()); + } + if (requestOptions.getServiceTier() != null) { + builder.serviceTier(ChatCompletionCreateParams.ServiceTier.of(requestOptions.getServiceTier())); + } + + if (stream) { + if (requestOptions.getStreamOptions() != null) { + ChatCompletionStreamOptions.Builder streamOptionsBuilder = ChatCompletionStreamOptions.builder(); + + if (requestOptions.getStreamOptions().includeObfuscation().isPresent()) { + streamOptionsBuilder + .includeObfuscation(requestOptions.getStreamOptions().includeObfuscation().get()); + } + streamOptionsBuilder.additionalProperties(requestOptions.getStreamOptions()._additionalProperties()); + streamOptionsBuilder.includeUsage(requestOptions.getStreamUsage()); + builder.streamOptions(streamOptionsBuilder.build()); + } + else { + builder.streamOptions(ChatCompletionStreamOptions.builder() + .includeUsage(true) // Include usage by default for streaming + .build()); + } + } + + // Add the tool definitions to the request's tools parameter. + List toolDefinitions = this.toolCallingManager.resolveToolDefinitions(requestOptions); + if (!CollectionUtils.isEmpty(toolDefinitions)) { + builder.tools(getChatCompletionTools(toolDefinitions)); + } + + if (requestOptions.getToolChoice() != null) { + builder.toolChoice(requestOptions.getToolChoice()); + } + + return builder.build(); + } + + private String fromAudioData(Object audioData) { + if (audioData instanceof byte[] bytes) { + return Base64.getEncoder().encodeToString(bytes); + } + throw new IllegalArgumentException("Unsupported audio data type: " + audioData.getClass().getSimpleName()); + } + + private String fromMediaData(org.springframework.util.MimeType mimeType, Object mediaContentData) { + if (mediaContentData instanceof byte[] bytes) { + // Assume the bytes are an image. So, convert the bytes to a base64 encoded + // following the prefix pattern. + return String.format("data:%s;base64,%s", mimeType.toString(), Base64.getEncoder().encodeToString(bytes)); + } + else if (mediaContentData instanceof String text) { + // Assume the text is a URLs or a base64 encoded image prefixed by the user. + return text; + } + else { + throw new IllegalArgumentException( + "Unsupported media data type: " + mediaContentData.getClass().getSimpleName()); + } + } + + private List getChatCompletionTools(List toolDefinitions) { + return toolDefinitions.stream().map(toolDefinition -> { + FunctionParameters.Builder parametersBuilder = FunctionParameters.builder(); + + if (!toolDefinition.inputSchema().isEmpty()) { + // Parse the schema and add its properties directly + try { + com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); + @SuppressWarnings("unchecked") + Map schemaMap = mapper.readValue(toolDefinition.inputSchema(), Map.class); + + // Add each property from the schema to the parameters + schemaMap + .forEach((key, value) -> parametersBuilder.putAdditionalProperty(key, JsonValue.from(value))); + + // Add strict mode + parametersBuilder.putAdditionalProperty("strict", JsonValue.from(true)); // TODO + // allow + // non-strict + // mode + } + catch (Exception e) { + logger.error("Failed to parse tool schema", e); + } + } + + FunctionDefinition functionDefinition = FunctionDefinition.builder() + .name(toolDefinition.name()) + .description(toolDefinition.description()) + .parameters(parametersBuilder.build()) + .build(); + + return ChatCompletionTool + .ofFunction(ChatCompletionFunctionTool.builder().function(functionDefinition).build()); + }).toList(); + } + + @Override + public ChatOptions getDefaultOptions() { + return this.options.copy(); + } + + /** + * Use the provided convention for reporting observation data + * @param observationConvention The provided convention + */ + public void setObservationConvention(ChatModelObservationConvention observationConvention) { + Assert.notNull(observationConvention, "observationConvention cannot be null"); + this.observationConvention = observationConvention; + } + + /** + * Response format (text, json_object, json_schema) for OpenAiOfficialChatModel + * responses. + * + * @author Julien Dubois + */ + public static class ResponseFormat { + + private Type type = Type.TEXT; + + private String jsonSchema; + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } + + public String getJsonSchema() { + return jsonSchema; + } + + public void setJsonSchema(String jsonSchema) { + this.jsonSchema = jsonSchema; + } + + public static Builder builder() { + return new Builder(); + } + + public static final class Builder { + + private final ResponseFormat responseFormat = new ResponseFormat(); + + private Builder() { + } + + public Builder type(Type type) { + this.responseFormat.setType(type); + return this; + } + + public Builder jsonSchema(String jsonSchema) { + this.responseFormat.setType(Type.JSON_SCHEMA); + this.responseFormat.setJsonSchema(jsonSchema); + return this; + } + + public ResponseFormat build() { + return this.responseFormat; + } + + } + + public enum Type { + + /** + * Generates a text response. (default) + */ + TEXT, + + /** + * Enables JSON mode, which guarantees the message the model generates is + * valid JSON. + */ + JSON_OBJECT, + + /** + * Enables Structured Outputs which guarantees the model will match your + * supplied JSON schema. + */ + JSON_SCHEMA + + } + + } + + /** + * Helper class to merge streaming tool calls that arrive in pieces across multiple + * chunks. In OpenAI streaming, a tool call's ID, name, and arguments can arrive in + * separate chunks. + */ + private static class ToolCallBuilder { + + private String id = ""; + + private String type = "function"; + + private String name = ""; + + private StringBuilder arguments = new StringBuilder(); + + void merge(AssistantMessage.ToolCall toolCall) { + if (toolCall.id() != null && !toolCall.id().isEmpty()) { + this.id = toolCall.id(); + } + if (toolCall.type() != null && !toolCall.type().isEmpty()) { + this.type = toolCall.type(); + } + if (toolCall.name() != null && !toolCall.name().isEmpty()) { + this.name = toolCall.name(); + } + if (toolCall.arguments() != null && !toolCall.arguments().isEmpty()) { + this.arguments.append(toolCall.arguments()); + } + } - private static final ChatModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultChatModelObservationConvention(); + AssistantMessage.ToolCall build() { + return new AssistantMessage.ToolCall(id, type, name, arguments.toString()); + } - private static final ToolCallingManager DEFAULT_TOOL_CALLING_MANAGER = ToolCallingManager.builder().build(); - - private final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatModel.class); - - private final OpenAIClient openAiClient; - - private final OpenAIClientAsync openAiClientAsync; - - private final OpenAiOfficialChatOptions options; - - private final ObservationRegistry observationRegistry; - - private final ToolCallingManager toolCallingManager; - - private final ToolExecutionEligibilityPredicate toolExecutionEligibilityPredicate; - - private ChatModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; - - public OpenAiOfficialChatModel() { - this(null, null, null, null, null, null); - } - - public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options) { - this(null, null, options, null, null, null); - } - - public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options, ObservationRegistry observationRegistry) { - this(null, null, options, null, observationRegistry, null); - } - - public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options, ToolCallingManager toolCallingManager, - ObservationRegistry observationRegistry) { - this(null, null, options, toolCallingManager, observationRegistry, null); - } - - public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync) { - this(openAIClient, openAiClientAsync, null, null, null, null); - } - - public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync, - OpenAiOfficialChatOptions options) { - this(openAIClient, openAiClientAsync, options, null, null, null); - } - - public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync, - OpenAiOfficialChatOptions options, ObservationRegistry observationRegistry) { - this(openAIClient, openAiClientAsync, options, null, observationRegistry, null); - } - - public OpenAiOfficialChatModel(OpenAIClient openAiClient, OpenAIClientAsync openAiClientAsync, - OpenAiOfficialChatOptions options, ToolCallingManager toolCallingManager, - ObservationRegistry observationRegistry, - ToolExecutionEligibilityPredicate toolExecutionEligibilityPredicate) { - - if (options == null) { - this.options = OpenAiOfficialChatOptions.builder().model(DEFAULT_MODEL_NAME).build(); - } - else { - this.options = options; - } - this.openAiClient = Objects.requireNonNullElseGet(openAiClient, - () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), - this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), - this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), - this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), - this.options.getProxy(), this.options.getCustomHeaders())); - - this.openAiClientAsync = Objects.requireNonNullElseGet(openAiClientAsync, - () -> setupAsyncClient(this.options.getBaseUrl(), this.options.getApiKey(), - this.options.getCredential(), this.options.getAzureDeploymentName(), - this.options.getAzureOpenAIServiceVersion(), this.options.getOrganizationId(), - this.options.isAzure(), this.options.isGitHubModels(), this.options.getModel(), - this.options.getTimeout(), this.options.getMaxRetries(), this.options.getProxy(), - this.options.getCustomHeaders())); - - this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); - this.toolCallingManager = Objects.requireNonNullElse(toolCallingManager, DEFAULT_TOOL_CALLING_MANAGER); - this.toolExecutionEligibilityPredicate = Objects.requireNonNullElse(toolExecutionEligibilityPredicate, - new DefaultToolExecutionEligibilityPredicate()); - } - - public OpenAiOfficialChatOptions getOptions() { - return this.options; - } - - @Override - public ChatResponse call(Prompt prompt) { - if (this.openAiClient == null) { - throw new IllegalStateException( - "OpenAI sync client is not configured. Have you set the 'streamUsage' option to false?"); - } - Prompt requestPrompt = buildRequestPrompt(prompt); - return this.internalCall(requestPrompt, null); - } - - public ChatResponse internalCall(Prompt prompt, ChatResponse previousChatResponse) { - - ChatCompletionCreateParams request = createRequest(prompt, false); - - ChatModelObservationContext observationContext = ChatModelObservationContext.builder() - .prompt(prompt) - .provider(AiProvider.OPENAI_OFFICIAL.value()) - .build(); - - ChatResponse response = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION - .observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, - this.observationRegistry) - .observe(() -> { - - ChatCompletion chatCompletion = this.openAiClient.chat().completions().create(request); - - List choices = chatCompletion.choices(); - if (choices.isEmpty()) { - logger.warn("No choices returned for prompt: {}", prompt); - return new ChatResponse(List.of()); - } - - List generations = choices.stream().map(choice -> { - chatCompletion.id(); - choice.finishReason(); - Map metadata = Map.of("id", chatCompletion.id(), "role", - choice.message()._role().asString().isPresent() ? choice.message()._role().asStringOrThrow() - : "", - "index", choice.index(), "finishReason", choice.finishReason().value().toString(), - "refusal", choice.message().refusal().isPresent() ? choice.message().refusal() : "", - "annotations", choice.message().annotations().isPresent() ? choice.message().annotations() - : List.of(Map.of())); - return buildGeneration(choice, metadata); - }).toList(); - - // Current usage - CompletionUsage usage = chatCompletion.usage().orElse(null); - Usage currentChatResponseUsage = usage != null ? getDefaultUsage(usage) : new EmptyUsage(); - Usage accumulatedUsage = UsageCalculator.getCumulativeUsage(currentChatResponseUsage, - previousChatResponse); - ChatResponse chatResponse = new ChatResponse(generations, from(chatCompletion, accumulatedUsage)); - - observationContext.setResponse(chatResponse); - - return chatResponse; - - }); - - if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(prompt.getOptions(), response)) { - var toolExecutionResult = this.toolCallingManager.executeToolCalls(prompt, response); - if (toolExecutionResult.returnDirect()) { - // Return tool execution result directly to the client. - return ChatResponse.builder() - .from(response) - .generations(ToolExecutionResult.buildGenerations(toolExecutionResult)) - .build(); - } - else { - // Send the tool execution result back to the model. - return this.internalCall(new Prompt(toolExecutionResult.conversationHistory(), prompt.getOptions()), - response); - } - } - - return response; - } - - @Override - public Flux stream(Prompt prompt) { - if (this.openAiClientAsync == null) { - throw new IllegalStateException( - "OpenAI async client is not configured. Streaming is not supported with the current configuration. Have you set the 'streamUsage' option to true?"); - } - Prompt requestPrompt = buildRequestPrompt(prompt); - return internalStream(requestPrompt, null); - } - - public AssistantMessage safeAssistantMessage(ChatResponse response) { - if (response == null) - return null; - Generation gen = response.getResult(); - if (gen == null) - return null; - return gen.getOutput(); - } - - public Flux internalStream(Prompt prompt, ChatResponse previousChatResponse) { - return Flux.deferContextual(contextView -> { - ChatCompletionCreateParams request = createRequest(prompt, true); - ConcurrentHashMap roleMap = new ConcurrentHashMap<>(); - final ChatModelObservationContext observationContext = ChatModelObservationContext.builder() - .prompt(prompt) - .provider(AiProvider.OPENAI_OFFICIAL.value()) - .build(); - Observation observation = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION.observation( - this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, - this.observationRegistry); - observation.parentObservation(contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null)).start(); - - Flux chatResponses = Flux.create(sink -> { - this.openAiClientAsync.chat().completions().createStreaming(request).subscribe(chunk -> { - try { - ChatCompletion chatCompletion = chunkToChatCompletion(chunk); - String id = chatCompletion.id(); - List generations = chatCompletion.choices().stream().map(choice -> { - roleMap.putIfAbsent(id, choice.message()._role().asString().isPresent() - ? choice.message()._role().asStringOrThrow() : ""); - - Map metadata = Map.of("id", id, "role", roleMap.getOrDefault(id, ""), - "index", choice.index(), "finishReason", choice.finishReason().value(), "refusal", - choice.message().refusal().isPresent() ? choice.message().refusal() : "", - "annotations", choice.message().annotations().isPresent() - ? choice.message().annotations() : List.of(), - "chunkChoice", chunk.choices().get((int) choice.index())); - - return buildGeneration(choice, metadata); - }).toList(); - Optional usage = chatCompletion.usage(); - CompletionUsage usageVal = usage.orElse(null); - Usage currentUsage = usageVal != null ? getDefaultUsage(usageVal) : new EmptyUsage(); - Usage accumulated = UsageCalculator.getCumulativeUsage(currentUsage, previousChatResponse); - sink.next(new ChatResponse(generations, from(chatCompletion, accumulated))); - } - catch (Exception e) { - logger.error("Error processing chat completion", e); - sink.error(e); - } - }).onCompleteFuture().whenComplete((unused, throwable) -> { - if (throwable != null) - sink.error(throwable); - else - sink.complete(); - }); - }).buffer(2, 1).map(buffer -> { - ChatResponse first = buffer.get(0); - if (request.streamOptions().isPresent() && buffer.size() == 2) { - ChatResponse second = buffer.get(1); - if (second != null) { - Usage usage = second.getMetadata().getUsage(); - if (!UsageCalculator.isEmpty(usage)) { - return new ChatResponse(first.getResults(), from(first.getMetadata(), usage)); - } - } - } - return first; - }); - - Flux flux = chatResponses - .contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation)); - - return flux.collectList().flatMapMany(list -> { - if (list.isEmpty()) - return Flux.empty(); - boolean hasToolCalls = list.stream() - .map(this::safeAssistantMessage) - .filter(Objects::nonNull) - .anyMatch(am -> !CollectionUtils.isEmpty(am.getToolCalls())); - if (!hasToolCalls) { - if (list.size() > 2) { - ChatResponse penultimateResponse = list.get(list.size() - 2); // Get - // the - // finish - // reason - ChatResponse lastResponse = list.get(list.size() - 1); // Get the - // usage - Usage usage = lastResponse.getMetadata().getUsage(); - observationContext.setResponse(new ChatResponse(penultimateResponse.getResults(), - from(penultimateResponse.getMetadata(), usage))); - } - return Flux.fromIterable(list); - } - Map builders = new HashMap<>(); - StringBuilder text = new StringBuilder(); - ChatResponseMetadata finalMetadata = null; - ChatGenerationMetadata finalGenMetadata = null; - Map props = new HashMap<>(); - for (ChatResponse chatResponse : list) { - AssistantMessage am = safeAssistantMessage(chatResponse); - if (am == null) - continue; - if (am.getText() != null) - text.append(am.getText()); - if (am.getMetadata() != null) - props.putAll(am.getMetadata()); - if (!CollectionUtils.isEmpty(am.getToolCalls())) { - Object ccObj = am.getMetadata().get("chunkChoice"); - if (ccObj instanceof ChatCompletionChunk.Choice chunkChoice - && chunkChoice.delta().toolCalls().isPresent()) { - List deltaCalls = chunkChoice.delta() - .toolCalls() - .get(); - for (int i = 0; i < am.getToolCalls().size() && i < deltaCalls.size(); i++) { - AssistantMessage.ToolCall tc = am.getToolCalls().get(i); - ChatCompletionChunk.Choice.Delta.ToolCall dtc = deltaCalls.get(i); - String key = chunkChoice.index() + "-" + dtc.index(); - ToolCallBuilder toolCallBuilder = builders.computeIfAbsent(key, - k -> new ToolCallBuilder()); - toolCallBuilder.merge(tc); - } - } - else { - for (AssistantMessage.ToolCall tc : am.getToolCalls()) { - ToolCallBuilder toolCallBuilder = builders.computeIfAbsent(tc.id(), - k -> new ToolCallBuilder()); - toolCallBuilder.merge(tc); - } - } - } - Generation generation = chatResponse.getResult(); - if (generation != null && generation.getMetadata() != null - && generation.getMetadata() != ChatGenerationMetadata.NULL) { - finalGenMetadata = generation.getMetadata(); - } - if (chatResponse.getMetadata() != null) - finalMetadata = chatResponse.getMetadata(); - } - List merged = builders.values() - .stream() - .map(ToolCallBuilder::build) - .filter(tc -> StringUtils.hasText(tc.name())) - .toList(); - AssistantMessage.Builder assistantMessageBuilder = AssistantMessage.builder() - .content(text.toString()) - .properties(props); - if (!merged.isEmpty()) { - assistantMessageBuilder.toolCalls(merged); - } - AssistantMessage assistantMessage = assistantMessageBuilder.build(); - Generation finalGen = new Generation(assistantMessage, - finalGenMetadata != null ? finalGenMetadata : ChatGenerationMetadata.NULL); - ChatResponse aggregated = new ChatResponse(List.of(finalGen), finalMetadata); - observationContext.setResponse(aggregated); - if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(prompt.getOptions(), aggregated)) { - return Flux.deferContextual(ctx -> { - ToolExecutionResult tetoolExecutionResult; - try { - ToolCallReactiveContextHolder.setContext(ctx); - tetoolExecutionResult = this.toolCallingManager.executeToolCalls(prompt, aggregated); - } - finally { - ToolCallReactiveContextHolder.clearContext(); - } - if (tetoolExecutionResult.returnDirect()) - return Flux.just(ChatResponse.builder() - .from(aggregated) - .generations(ToolExecutionResult.buildGenerations(tetoolExecutionResult)) - .build()); - return this.internalStream( - new Prompt(tetoolExecutionResult.conversationHistory(), prompt.getOptions()), - aggregated); - }).subscribeOn(Schedulers.boundedElastic()); - } - return Flux.just(aggregated); - }).doOnError(observation::error).doFinally(s -> observation.stop()); - }); - } - - private Generation buildGeneration(ChatCompletion.Choice choice, Map metadata) { - ChatCompletionMessage message = choice.message(); - List toolCalls = new ArrayList<>(); - - if (metadata.containsKey("chunkChoice")) { - Object chunkChoiceObj = metadata.get("chunkChoice"); - if (chunkChoiceObj instanceof ChatCompletionChunk.Choice chunkChoice) { - if (chunkChoice.delta().toolCalls().isPresent()) { - toolCalls = chunkChoice.delta() - .toolCalls() - .get() - .stream() - .filter(tc -> tc.function().isPresent()) - .map(tc -> { - var funcOpt = tc.function(); - if (funcOpt.isEmpty()) - return null; - var func = funcOpt.get(); - String id = tc.id().orElse(""); - String name = func.name().orElse(""); - String arguments = func.arguments().orElse(""); - return new AssistantMessage.ToolCall(id, "function", name, arguments); - }) - .filter(Objects::nonNull) - .toList(); - } - } - } - else { - toolCalls = message.toolCalls() - .map(list -> list.stream().filter(tc -> tc.function().isPresent()).map(tc -> { - var opt = tc.function(); - if (opt.isEmpty()) - return null; - var funcCall = opt.get(); - var functionDef = funcCall.function(); - String id = funcCall.id(); - String name = functionDef.name(); - String arguments = functionDef.arguments(); - return new AssistantMessage.ToolCall(id, "function", name, arguments); - }).filter(Objects::nonNull).toList()) - .orElse(List.of()); - } - - var generationMetadataBuilder = ChatGenerationMetadata.builder() - .finishReason(choice.finishReason().value().name()); - String textContent = message.content().orElse(""); - var assistantMessage = AssistantMessage.builder() - .content(textContent) - .properties(metadata) - .toolCalls(toolCalls) - .build(); - return new Generation(assistantMessage, generationMetadataBuilder.build()); - } - - private ChatResponseMetadata from(ChatCompletion result, Usage usage) { - Assert.notNull(result, "OpenAI ChatCompletion must not be null"); - result.model(); - result.id(); - return ChatResponseMetadata.builder() - .id(result.id()) - .usage(usage) - .model(result.model()) - .keyValue("created", result.created()) - .build(); - } - - private ChatResponseMetadata from(ChatResponseMetadata chatResponseMetadata, Usage usage) { - Assert.notNull(chatResponseMetadata, "OpenAI ChatResponseMetadata must not be null"); - return ChatResponseMetadata.builder() - .id(chatResponseMetadata.getId() != null ? chatResponseMetadata.getId() : "") - .usage(usage) - .model(chatResponseMetadata.getModel() != null ? chatResponseMetadata.getModel() : "") - .build(); - } - - /** - * Convert the ChatCompletionChunk into a ChatCompletion. The Usage is set to null. - * @param chunk the ChatCompletionChunk to convert - * @return the ChatCompletion - */ - private ChatCompletion chunkToChatCompletion(ChatCompletionChunk chunk) { - List choices = chunk.choices().stream().map(chunkChoice -> { - ChatCompletion.Choice.FinishReason finishReason = ChatCompletion.Choice.FinishReason.of(""); - if (chunkChoice.finishReason().isPresent()) { - finishReason = ChatCompletion.Choice.FinishReason - .of(chunkChoice.finishReason().get().value().name().toLowerCase()); - } - - ChatCompletion.Choice.Builder choiceBuilder = ChatCompletion.Choice.builder() - .finishReason(finishReason) - .index(chunkChoice.index()) - .message(ChatCompletionMessage.builder() - .content(chunkChoice.delta().content()) - .refusal(chunkChoice.delta().refusal()) - .build()); - - // Handle optional logprobs - if (chunkChoice.logprobs().isPresent()) { - var logprobs = chunkChoice.logprobs().get(); - choiceBuilder.logprobs(ChatCompletion.Choice.Logprobs.builder() - .content(logprobs.content()) - .refusal(logprobs.refusal()) - .build()); - } - else { - // Provide empty logprobs when not present - choiceBuilder - .logprobs(ChatCompletion.Choice.Logprobs.builder().content(List.of()).refusal(List.of()).build()); - } - - return choiceBuilder.build(); - }).toList(); - - return ChatCompletion.builder() - .id(chunk.id()) - .choices(choices) - .created(chunk.created()) - .model(chunk.model()) - .usage(chunk.usage() - .orElse(CompletionUsage.builder().promptTokens(0).completionTokens(0).totalTokens(0).build())) - .build(); - } - - private DefaultUsage getDefaultUsage(CompletionUsage usage) { - return new DefaultUsage(Math.toIntExact(usage.promptTokens()), Math.toIntExact(usage.completionTokens()), - Math.toIntExact(usage.totalTokens()), usage); - } - - Prompt buildRequestPrompt(Prompt prompt) { - // Process runtime options - OpenAiOfficialChatOptions runtimeOptions = null; - if (prompt.getOptions() != null) { - if (prompt.getOptions() instanceof ToolCallingChatOptions toolCallingChatOptions) { - runtimeOptions = ModelOptionsUtils.copyToTarget(toolCallingChatOptions, ToolCallingChatOptions.class, - OpenAiOfficialChatOptions.class); - } - else { - runtimeOptions = ModelOptionsUtils.copyToTarget(prompt.getOptions(), ChatOptions.class, - OpenAiOfficialChatOptions.class); - } - } - - // Define request options by merging runtime options and default options - OpenAiOfficialChatOptions requestOptions = OpenAiOfficialChatOptions.builder() - .from(this.options) - .merge(runtimeOptions != null ? runtimeOptions : OpenAiOfficialChatOptions.builder().build()) - .build(); - - // Merge @JsonIgnore-annotated options explicitly since they are ignored by - // Jackson, used by ModelOptionsUtils. - if (runtimeOptions != null) { - if (runtimeOptions.getTopK() != null) { - logger.warn("The topK option is not supported by OpenAI chat models. Ignoring."); - } - - Map mergedHttpHeaders = new HashMap<>(this.options.getHttpHeaders()); - mergedHttpHeaders.putAll(runtimeOptions.getHttpHeaders()); - requestOptions.setHttpHeaders(mergedHttpHeaders); - - requestOptions.setInternalToolExecutionEnabled(runtimeOptions.getInternalToolExecutionEnabled() != null - ? runtimeOptions.getInternalToolExecutionEnabled() - : this.options.getInternalToolExecutionEnabled()); - requestOptions.setToolNames( - ToolCallingChatOptions.mergeToolNames(runtimeOptions.getToolNames(), this.options.getToolNames())); - requestOptions.setToolCallbacks(ToolCallingChatOptions.mergeToolCallbacks(runtimeOptions.getToolCallbacks(), - this.options.getToolCallbacks())); - requestOptions.setToolContext(ToolCallingChatOptions.mergeToolContext(runtimeOptions.getToolContext(), - this.options.getToolContext())); - } - else { - requestOptions.setHttpHeaders(this.options.getHttpHeaders()); - requestOptions.setInternalToolExecutionEnabled(this.options.getInternalToolExecutionEnabled()); - requestOptions.setToolNames(this.options.getToolNames()); - requestOptions.setToolCallbacks(this.options.getToolCallbacks()); - requestOptions.setToolContext(this.options.getToolContext()); - } - - ToolCallingChatOptions.validateToolCallbacks(requestOptions.getToolCallbacks()); - - return new Prompt(prompt.getInstructions(), requestOptions); - } - - ChatCompletionCreateParams createRequest(Prompt prompt, boolean stream) { - - List chatCompletionMessageParams = prompt.getInstructions() - .stream() - .map(message -> { - if (message.getMessageType() == MessageType.USER || message.getMessageType() == MessageType.SYSTEM) { - // Handle simple text content for user and system messages - ChatCompletionUserMessageParam.Builder builder = ChatCompletionUserMessageParam.builder(); - - if (message instanceof UserMessage userMessage - && !CollectionUtils.isEmpty(userMessage.getMedia())) { - // Handle media content (images, audio, files) - List parts = new ArrayList<>(); - - if (!message.getText().isEmpty()) { - parts.add(ChatCompletionContentPart - .ofText(ChatCompletionContentPartText.builder().text(message.getText()).build())); - } - - // Add media content parts - userMessage.getMedia().forEach(media -> { - String mimeType = media.getMimeType().toString(); - if (mimeType.startsWith("image/")) { - if (media.getData() instanceof java.net.URI uri) { - parts.add(ChatCompletionContentPart - .ofImageUrl(ChatCompletionContentPartImage.builder() - .imageUrl(ChatCompletionContentPartImage.ImageUrl.builder() - .url(uri.toString()) - .build()) - .build())); - } - else if (media.getData() instanceof String text) { - // The org.springframework.ai.content.Media object - // should store the URL as a java.net.URI but it - // transforms it to String somewhere along the way, - // for example in its Builder class. So, we accept - // String as well here for image URLs. - parts.add(ChatCompletionContentPart - .ofImageUrl(ChatCompletionContentPartImage.builder() - .imageUrl( - ChatCompletionContentPartImage.ImageUrl.builder().url(text).build()) - .build())); - } - else if (media.getData() instanceof byte[] bytes) { - // Assume the bytes are an image. So, convert the - // bytes to a base64 encoded - ChatCompletionContentPartImage.ImageUrl.Builder imageUrlBuilder = ChatCompletionContentPartImage.ImageUrl - .builder(); - - imageUrlBuilder.url("data:" + mimeType + ";base64," - + Base64.getEncoder().encodeToString(bytes)); - parts.add(ChatCompletionContentPart - .ofImageUrl(ChatCompletionContentPartImage.builder() - .imageUrl(imageUrlBuilder.build()) - .build())); - } - else { - logger.info( - "Could not process image media with data of type: {}. Only java.net.URI is supported for image URLs.", - media.getData().getClass().getSimpleName()); - } - } - else if (mimeType.startsWith("audio/")) { - parts.add(ChatCompletionContentPart - .ofInputAudio(ChatCompletionContentPartInputAudio.builder() - .inputAudio(ChatCompletionContentPartInputAudio.builder() - .inputAudio(ChatCompletionContentPartInputAudio.InputAudio.builder() - .data(fromAudioData(media.getData())) - .format(mimeType.contains("mp3") - ? ChatCompletionContentPartInputAudio.InputAudio.Format.MP3 - : ChatCompletionContentPartInputAudio.InputAudio.Format.WAV) - .build()) - .build() - .inputAudio()) - .build())); - } - else { - // Assume it's a file or other media type represented as a - // data URL - parts.add(ChatCompletionContentPart.ofText(ChatCompletionContentPartText.builder() - .text(fromMediaData(media.getMimeType(), media.getData())) - .build())); - } - }); - builder.contentOfArrayOfContentParts(parts); - } - else { - // Simple text message - builder.content(ChatCompletionContentPartText.builder().text(message.getText()).build().text()); - } - - if (message.getMessageType() == MessageType.USER) { - builder.role(JsonValue.from(MessageType.USER.getValue())); - } - else { - builder.role(JsonValue.from(MessageType.SYSTEM.getValue())); - } - - return List.of(ChatCompletionMessageParam.ofUser(builder.build())); - } - else if (message.getMessageType() == MessageType.ASSISTANT) { - var assistantMessage = (AssistantMessage) message; - ChatCompletionAssistantMessageParam.Builder builder = ChatCompletionAssistantMessageParam.builder() - .role(JsonValue.from(MessageType.ASSISTANT.getValue())); - - if (assistantMessage.getText() != null) { - builder.content(ChatCompletionAssistantMessageParam.builder() - .content(assistantMessage.getText()) - .build() - .content()); - } - - if (!CollectionUtils.isEmpty(assistantMessage.getToolCalls())) { - List toolCalls = assistantMessage.getToolCalls() - .stream() - .map(toolCall -> ChatCompletionMessageToolCall - .ofFunction(ChatCompletionMessageFunctionToolCall.builder() - .id(toolCall.id()) - .function(ChatCompletionMessageFunctionToolCall.Function.builder() - .name(toolCall.name()) - .arguments(toolCall.arguments()) - .build()) - .build())) - .toList(); - - builder.toolCalls(toolCalls); - } - - return List.of(ChatCompletionMessageParam.ofAssistant(builder.build())); - } - else if (message.getMessageType() == MessageType.TOOL) { - ToolResponseMessage toolMessage = (ToolResponseMessage) message; - - ChatCompletionToolMessageParam.Builder builder = ChatCompletionToolMessageParam.builder(); - builder.content(toolMessage.getText() != null ? toolMessage.getText() : ""); - builder.role(JsonValue.from(MessageType.TOOL.getValue())); - - if (toolMessage.getResponses().isEmpty()) { - return List.of(ChatCompletionMessageParam.ofTool(builder.build())); - } - return toolMessage.getResponses().stream().map(response -> { - String callId = response.id(); - String callResponse = response.responseData(); - - return ChatCompletionMessageParam - .ofTool(builder.toolCallId(callId).content(callResponse).build()); - }).toList(); - } - else { - throw new IllegalArgumentException("Unsupported message type: " + message.getMessageType()); - } - }) - .flatMap(List::stream) - .toList(); - - ChatCompletionCreateParams.Builder builder = ChatCompletionCreateParams.builder(); - - chatCompletionMessageParams.forEach(builder::addMessage); - - OpenAiOfficialChatOptions requestOptions = (OpenAiOfficialChatOptions) prompt.getOptions(); - - // Use deployment name if available (for Azure AI Foundry), otherwise use model - // name - if (requestOptions.getDeploymentName() != null) { - builder.model(requestOptions.getDeploymentName()); - } - else if (requestOptions.getModel() != null) { - builder.model(requestOptions.getModel()); - } - - if (requestOptions.getFrequencyPenalty() != null) { - builder.frequencyPenalty(requestOptions.getFrequencyPenalty()); - } - if (requestOptions.getLogitBias() != null) { - builder.logitBias(ChatCompletionCreateParams.LogitBias.builder() - .putAllAdditionalProperties(requestOptions.getLogitBias() - .entrySet() - .stream() - .collect(Collectors.toMap(Map.Entry::getKey, entry -> JsonValue.from(entry.getValue())))) - .build()); - } - if (requestOptions.getLogprobs() != null) { - builder.logprobs(requestOptions.getLogprobs()); - } - if (requestOptions.getTopLogprobs() != null) { - builder.topLogprobs(requestOptions.getTopLogprobs()); - } - if (requestOptions.getMaxTokens() != null) { - builder.maxTokens(requestOptions.getMaxTokens()); - } - if (requestOptions.getMaxCompletionTokens() != null) { - builder.maxCompletionTokens(requestOptions.getMaxCompletionTokens()); - } - if (requestOptions.getN() != null) { - builder.n(requestOptions.getN()); - } - if (requestOptions.getOutputAudio() != null) { - builder.audio(requestOptions.getOutputAudio()); - } - if (requestOptions.getPresencePenalty() != null) { - builder.presencePenalty(requestOptions.getPresencePenalty()); - } - if (requestOptions.getResponseFormat() != null) { - ResponseFormat responseFormat = requestOptions.getResponseFormat(); - if (responseFormat.getType().equals(ResponseFormat.Type.TEXT)) { - builder.responseFormat(ResponseFormatText.builder().build()); - } - else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_OBJECT)) { - builder.responseFormat(ResponseFormatJsonObject.builder().build()); - } - else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_SCHEMA)) { - String jsonSchemaString = responseFormat.getJsonSchema() != null ? responseFormat.getJsonSchema() : ""; - try { - com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); - ResponseFormatJsonSchema.JsonSchema.Builder jsonSchemaBuilder = ResponseFormatJsonSchema.JsonSchema - .builder(); - jsonSchemaBuilder.name("json_schema"); - jsonSchemaBuilder.strict(true); - - ResponseFormatJsonSchema.JsonSchema.Schema schema = mapper.readValue(jsonSchemaString, - ResponseFormatJsonSchema.JsonSchema.Schema.class); - - jsonSchemaBuilder.schema(schema); - - builder.responseFormat( - ResponseFormatJsonSchema.builder().jsonSchema(jsonSchemaBuilder.build()).build()); - } - catch (Exception e) { - throw new IllegalArgumentException("Failed to parse JSON schema: " + jsonSchemaString, e); - } - } - else { - throw new IllegalArgumentException("Unsupported response format type: " + responseFormat.getType()); - } - } - if (requestOptions.getSeed() != null) { - builder.seed(requestOptions.getSeed()); - } - if (requestOptions.getStop() != null && !requestOptions.getStop().isEmpty()) { - if (requestOptions.getStop().size() == 1) { - builder.stop(ChatCompletionCreateParams.Stop.ofString(requestOptions.getStop().get(0))); - } - else { - builder.stop(ChatCompletionCreateParams.Stop.ofStrings(requestOptions.getStop())); - } - } - if (requestOptions.getTemperature() != null) { - builder.temperature(requestOptions.getTemperature()); - } - if (requestOptions.getTopP() != null) { - builder.topP(requestOptions.getTopP()); - } - if (requestOptions.getUser() != null) { - builder.user(requestOptions.getUser()); - } - if (requestOptions.getParallelToolCalls() != null) { - builder.parallelToolCalls(requestOptions.getParallelToolCalls()); - } - if (requestOptions.getReasoningEffort() != null) { - builder.reasoningEffort(ReasoningEffort.of(requestOptions.getReasoningEffort().toLowerCase())); - } - if (requestOptions.getVerbosity() != null) { - builder.verbosity(ChatCompletionCreateParams.Verbosity.of(requestOptions.getVerbosity())); - } - - if (requestOptions.getStore() != null) { - builder.store(requestOptions.getStore()); - } - if (requestOptions.getMetadata() != null && !requestOptions.getMetadata().isEmpty()) { - builder.metadata(ChatCompletionCreateParams.Metadata.builder() - .putAllAdditionalProperties(requestOptions.getMetadata() - .entrySet() - .stream() - .collect(Collectors.toMap(Map.Entry::getKey, entry -> JsonValue.from(entry.getValue())))) - .build()); - } - if (requestOptions.getServiceTier() != null) { - builder.serviceTier(ChatCompletionCreateParams.ServiceTier.of(requestOptions.getServiceTier())); - } - - if (stream) { - if (requestOptions.getStreamOptions() != null) { - ChatCompletionStreamOptions.Builder streamOptionsBuilder = ChatCompletionStreamOptions.builder(); - - if (requestOptions.getStreamOptions().includeObfuscation().isPresent()) { - streamOptionsBuilder - .includeObfuscation(requestOptions.getStreamOptions().includeObfuscation().get()); - } - streamOptionsBuilder.additionalProperties(requestOptions.getStreamOptions()._additionalProperties()); - streamOptionsBuilder.includeUsage(requestOptions.getStreamUsage()); - builder.streamOptions(streamOptionsBuilder.build()); - } - else { - builder.streamOptions(ChatCompletionStreamOptions.builder() - .includeUsage(true) // Include usage by default for streaming - .build()); - } - } - - // Add the tool definitions to the request's tools parameter. - List toolDefinitions = this.toolCallingManager.resolveToolDefinitions(requestOptions); - if (!CollectionUtils.isEmpty(toolDefinitions)) { - builder.tools(getChatCompletionTools(toolDefinitions)); - } - - if (requestOptions.getToolChoice() != null) { - builder.toolChoice(requestOptions.getToolChoice()); - } - - return builder.build(); - } - - private String fromAudioData(Object audioData) { - if (audioData instanceof byte[] bytes) { - return Base64.getEncoder().encodeToString(bytes); - } - throw new IllegalArgumentException("Unsupported audio data type: " + audioData.getClass().getSimpleName()); - } - - private String fromMediaData(org.springframework.util.MimeType mimeType, Object mediaContentData) { - if (mediaContentData instanceof byte[] bytes) { - // Assume the bytes are an image. So, convert the bytes to a base64 encoded - // following the prefix pattern. - return String.format("data:%s;base64,%s", mimeType.toString(), Base64.getEncoder().encodeToString(bytes)); - } - else if (mediaContentData instanceof String text) { - // Assume the text is a URLs or a base64 encoded image prefixed by the user. - return text; - } - else { - throw new IllegalArgumentException( - "Unsupported media data type: " + mediaContentData.getClass().getSimpleName()); - } - } - - private List getChatCompletionTools(List toolDefinitions) { - return toolDefinitions.stream().map(toolDefinition -> { - FunctionParameters.Builder parametersBuilder = FunctionParameters.builder(); - - if (!toolDefinition.inputSchema().isEmpty()) { - // Parse the schema and add its properties directly - try { - com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); - @SuppressWarnings("unchecked") - Map schemaMap = mapper.readValue(toolDefinition.inputSchema(), Map.class); - - // Add each property from the schema to the parameters - schemaMap - .forEach((key, value) -> parametersBuilder.putAdditionalProperty(key, JsonValue.from(value))); - - // Add strict mode - parametersBuilder.putAdditionalProperty("strict", JsonValue.from(true)); // TODO - // allow - // non-strict - // mode - } - catch (Exception e) { - logger.error("Failed to parse tool schema", e); - } - } - - FunctionDefinition functionDefinition = FunctionDefinition.builder() - .name(toolDefinition.name()) - .description(toolDefinition.description()) - .parameters(parametersBuilder.build()) - .build(); - - return ChatCompletionTool - .ofFunction(ChatCompletionFunctionTool.builder().function(functionDefinition).build()); - }).toList(); - } - - @Override - public ChatOptions getDefaultOptions() { - return this.options.copy(); - } - - /** - * Use the provided convention for reporting observation data - * @param observationConvention The provided convention - */ - public void setObservationConvention(ChatModelObservationConvention observationConvention) { - Assert.notNull(observationConvention, "observationConvention cannot be null"); - this.observationConvention = observationConvention; - } - - /** - * Response format (text, json_object, json_schema) for OpenAiOfficialChatModel - * responses. - * - * @author Julien Dubois - */ - public static class ResponseFormat { - - private Type type = Type.TEXT; - - private String jsonSchema; - - public Type getType() { - return type; - } - - public void setType(Type type) { - this.type = type; - } - - public String getJsonSchema() { - return jsonSchema; - } - - public void setJsonSchema(String jsonSchema) { - this.jsonSchema = jsonSchema; - } - - public static Builder builder() { - return new Builder(); - } - - public static final class Builder { - - private final ResponseFormat responseFormat = new ResponseFormat(); - - private Builder() { - } - - public Builder type(Type type) { - this.responseFormat.setType(type); - return this; - } - - public Builder jsonSchema(String jsonSchema) { - this.responseFormat.setType(Type.JSON_SCHEMA); - this.responseFormat.setJsonSchema(jsonSchema); - return this; - } - - public ResponseFormat build() { - return this.responseFormat; - } - - } - - public enum Type { - - /** - * Generates a text response. (default) - */ - TEXT, - - /** - * Enables JSON mode, which guarantees the message the model generates is - * valid JSON. - */ - JSON_OBJECT, - - /** - * Enables Structured Outputs which guarantees the model will match your - * supplied JSON schema. - */ - JSON_SCHEMA - - } - - } - - /** - * Helper class to merge streaming tool calls that arrive in pieces across multiple - * chunks. In OpenAI streaming, a tool call's ID, name, and arguments can arrive in - * separate chunks. - */ - private static class ToolCallBuilder { - - private String id = ""; - - private String type = "function"; - - private String name = ""; - - private StringBuilder arguments = new StringBuilder(); - - void merge(AssistantMessage.ToolCall toolCall) { - if (toolCall.id() != null && !toolCall.id().isEmpty()) { - this.id = toolCall.id(); - } - if (toolCall.type() != null && !toolCall.type().isEmpty()) { - this.type = toolCall.type(); - } - if (toolCall.name() != null && !toolCall.name().isEmpty()) { - this.name = toolCall.name(); - } - if (toolCall.arguments() != null && !toolCall.arguments().isEmpty()) { - this.arguments.append(toolCall.arguments()); - } - } - - AssistantMessage.ToolCall build() { - return new AssistantMessage.ToolCall(id, type, name, arguments.toString()); - } - - } + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java index ccff03e3a88..b051df91c90 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java @@ -45,735 +45,932 @@ */ public class OpenAiOfficialChatOptions extends AbstractOpenAiOfficialOptions implements ToolCallingChatOptions { - public static final String DEFAULT_CHAT_MODEL = GPT_5_MINI.asString(); + public static final String DEFAULT_CHAT_MODEL = GPT_5_MINI.asString(); - private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatOptions.class); + private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatOptions.class); - private Double frequencyPenalty; + private Double frequencyPenalty; - private Map logitBias; + private Map logitBias; - private Boolean logprobs; + private Boolean logprobs; - private Integer topLogprobs; + private Integer topLogprobs; - private Integer maxTokens; + private Integer maxTokens; - private Integer maxCompletionTokens; + private Integer maxCompletionTokens; - private Integer n; + private Integer n; - private ChatCompletionAudioParam outputAudio; + private ChatCompletionAudioParam outputAudio; - private Double presencePenalty; + private Double presencePenalty; - private OpenAiOfficialChatModel.ResponseFormat responseFormat; + private OpenAiOfficialChatModel.ResponseFormat responseFormat; - private ResponseCreateParams.StreamOptions streamOptions; + private ResponseCreateParams.StreamOptions streamOptions; - private Boolean streamUsage; + private Boolean streamUsage; - private Integer seed; + private Integer seed; - private List stop; + private List stop; - private Double temperature; + private Double temperature; - private Double topP; + private Double topP; - private List tools; + private List tools; - private ChatCompletionToolChoiceOption toolChoice; + private ChatCompletionToolChoiceOption toolChoice; - private String user; + private String user; - private Boolean parallelToolCalls; + private Boolean parallelToolCalls; - private Boolean store; + private Boolean store; - private Map metadata; + private Map metadata; - private String reasoningEffort; + private String reasoningEffort; - private String verbosity; + private String verbosity; - private String serviceTier; + private String serviceTier; - private List toolCallbacks = new ArrayList<>(); + private List toolCallbacks = new ArrayList<>(); - private Set toolNames = new HashSet<>(); + private Set toolNames = new HashSet<>(); - private Boolean internalToolExecutionEnabled; + private Boolean internalToolExecutionEnabled; - private Map httpHeaders = new HashMap<>(); + private Map httpHeaders = new HashMap<>(); - private Map toolContext = new HashMap<>(); + private Map toolContext = new HashMap<>(); - @Override - public Double getFrequencyPenalty() { - return this.frequencyPenalty; - } + /** + * Gets the frequency penalty parameter. + * @return the frequency penalty + */ + @Override + public Double getFrequencyPenalty() { + return this.frequencyPenalty; + } - public void setFrequencyPenalty(Double frequencyPenalty) { - this.frequencyPenalty = frequencyPenalty; - } + /** + * Sets the frequency penalty parameter. + * @param frequencyPenalty the frequency penalty to set + */ + public void setFrequencyPenalty(Double frequencyPenalty) { + this.frequencyPenalty = frequencyPenalty; + } - public Map getLogitBias() { - return this.logitBias; - } + /** + * Gets the logit bias map. + * @return the logit bias map + */ + public Map getLogitBias() { + return this.logitBias; + } - public void setLogitBias(Map logitBias) { - this.logitBias = logitBias; - } + /** + * Sets the logit bias map. + * @param logitBias the logit bias map to set + */ + public void setLogitBias(Map logitBias) { + this.logitBias = logitBias; + } - public Boolean getLogprobs() { - return this.logprobs; - } - - public void setLogprobs(Boolean logprobs) { - this.logprobs = logprobs; - } - - public Integer getTopLogprobs() { - return this.topLogprobs; - } - - public void setTopLogprobs(Integer topLogprobs) { - this.topLogprobs = topLogprobs; - } - - @Override - public Integer getMaxTokens() { - return this.maxTokens; - } - - public void setMaxTokens(Integer maxTokens) { - this.maxTokens = maxTokens; - } - - public Integer getMaxCompletionTokens() { - return this.maxCompletionTokens; - } - - public void setMaxCompletionTokens(Integer maxCompletionTokens) { - this.maxCompletionTokens = maxCompletionTokens; - } - - public Integer getN() { - return this.n; - } - - public void setN(Integer n) { - this.n = n; - } - - public ChatCompletionAudioParam getOutputAudio() { - return this.outputAudio; - } - - public void setOutputAudio(ChatCompletionAudioParam outputAudio) { - this.outputAudio = outputAudio; - } - - @Override - public Double getPresencePenalty() { - return this.presencePenalty; - } - - public void setPresencePenalty(Double presencePenalty) { - this.presencePenalty = presencePenalty; - } - - public OpenAiOfficialChatModel.ResponseFormat getResponseFormat() { - return this.responseFormat; - } - - public void setResponseFormat(OpenAiOfficialChatModel.ResponseFormat responseFormat) { - this.responseFormat = responseFormat; - } - - public ResponseCreateParams.StreamOptions getStreamOptions() { - return this.streamOptions; - } - - public void setStreamOptions(ResponseCreateParams.StreamOptions streamOptions) { - this.streamOptions = streamOptions; - } - - public Boolean getStreamUsage() { - return this.streamUsage; - } - - public void setStreamUsage(Boolean streamUsage) { - this.streamUsage = streamUsage; - } - - public Integer getSeed() { - return this.seed; - } - - public void setSeed(Integer seed) { - this.seed = seed; - } - - public List getStop() { - return this.stop; - } - - public void setStop(List stop) { - this.stop = stop; - } - - @Override - public List getStopSequences() { - return getStop(); - } - - public void setStopSequences(List stopSequences) { - setStop(stopSequences); - } - - @Override - public Double getTemperature() { - return this.temperature; - } - - public void setTemperature(Double temperature) { - this.temperature = temperature; - } - - @Override - public Double getTopP() { - return this.topP; - } - - public void setTopP(Double topP) { - this.topP = topP; - } - - public List getTools() { - return this.tools; - } - - public void setTools(List tools) { - this.tools = tools; - } - - public ChatCompletionToolChoiceOption getToolChoice() { - return this.toolChoice; - } - - public void setToolChoice(ChatCompletionToolChoiceOption toolChoice) { - this.toolChoice = toolChoice; - } - - public String getUser() { - return this.user; - } - - public void setUser(String user) { - this.user = user; - } - - public Boolean getParallelToolCalls() { - return this.parallelToolCalls; - } - - public void setParallelToolCalls(Boolean parallelToolCalls) { - this.parallelToolCalls = parallelToolCalls; - } - - public Boolean getStore() { - return this.store; - } - - public void setStore(Boolean store) { - this.store = store; - } - - public Map getMetadata() { - return this.metadata; - } - - public void setMetadata(Map metadata) { - this.metadata = metadata; - } - - public String getReasoningEffort() { - return this.reasoningEffort; - } - - public void setReasoningEffort(String reasoningEffort) { - this.reasoningEffort = reasoningEffort; - } - - public String getVerbosity() { - return this.verbosity; - } - - public void setVerbosity(String verbosity) { - this.verbosity = verbosity; - } - - public String getServiceTier() { - return this.serviceTier; - } - - public void setServiceTier(String serviceTier) { - this.serviceTier = serviceTier; - } - - @Override - public List getToolCallbacks() { - return this.toolCallbacks; - } - - @Override - public void setToolCallbacks(List toolCallbacks) { - Assert.notNull(toolCallbacks, "toolCallbacks cannot be null"); - Assert.noNullElements(toolCallbacks, "toolCallbacks cannot contain null elements"); - this.toolCallbacks = toolCallbacks; - } - - @Override - public Set getToolNames() { - return this.toolNames; - } - - @Override - public void setToolNames(Set toolNames) { - Assert.notNull(toolNames, "toolNames cannot be null"); - Assert.noNullElements(toolNames, "toolNames cannot contain null elements"); - toolNames.forEach(tool -> Assert.hasText(tool, "toolNames cannot contain empty elements")); - this.toolNames = toolNames; - } - - @Override - @Nullable - public Boolean getInternalToolExecutionEnabled() { - return this.internalToolExecutionEnabled; - } - - @Override - public void setInternalToolExecutionEnabled(@Nullable Boolean internalToolExecutionEnabled) { - this.internalToolExecutionEnabled = internalToolExecutionEnabled; - } - - public Map getHttpHeaders() { - return this.httpHeaders; - } - - public void setHttpHeaders(Map httpHeaders) { - this.httpHeaders = httpHeaders; - } - - @Override - public Map getToolContext() { - return this.toolContext; - } - - @Override - public void setToolContext(Map toolContext) { - this.toolContext = toolContext; - } - - @Override - public Integer getTopK() { - return null; - } - - public static Builder builder() { - return new Builder(); - } - - @Override - public OpenAiOfficialChatOptions copy() { - return builder().from(this).build(); - } - - @Override - public boolean equals(Object o) { - if (o == null || getClass() != o.getClass()) - return false; - OpenAiOfficialChatOptions options = (OpenAiOfficialChatOptions) o; - return Objects.equals(getModel(), options.getModel()) - && Objects.equals(frequencyPenalty, options.frequencyPenalty) - && Objects.equals(logitBias, options.logitBias) && Objects.equals(logprobs, options.logprobs) - && Objects.equals(topLogprobs, options.topLogprobs) && Objects.equals(maxTokens, options.maxTokens) - && Objects.equals(maxCompletionTokens, options.maxCompletionTokens) && Objects.equals(n, options.n) - && Objects.equals(outputAudio, options.outputAudio) - && Objects.equals(presencePenalty, options.presencePenalty) - && Objects.equals(responseFormat, options.responseFormat) - && Objects.equals(streamOptions, options.streamOptions) - && Objects.equals(streamUsage, options.streamUsage) && Objects.equals(seed, options.seed) - && Objects.equals(stop, options.stop) && Objects.equals(temperature, options.temperature) - && Objects.equals(topP, options.topP) && Objects.equals(tools, options.tools) - && Objects.equals(toolChoice, options.toolChoice) && Objects.equals(user, options.user) - && Objects.equals(parallelToolCalls, options.parallelToolCalls) && Objects.equals(store, options.store) - && Objects.equals(metadata, options.metadata) - && Objects.equals(reasoningEffort, options.reasoningEffort) - && Objects.equals(verbosity, options.verbosity) && Objects.equals(serviceTier, options.serviceTier) - && Objects.equals(toolCallbacks, options.toolCallbacks) && Objects.equals(toolNames, options.toolNames) - && Objects.equals(internalToolExecutionEnabled, options.internalToolExecutionEnabled) - && Objects.equals(httpHeaders, options.httpHeaders) && Objects.equals(toolContext, options.toolContext); - } - - @Override - public int hashCode() { - return Objects.hash(getModel(), frequencyPenalty, logitBias, logprobs, topLogprobs, maxTokens, maxCompletionTokens, n, - outputAudio, presencePenalty, responseFormat, streamOptions, streamUsage, seed, stop, temperature, topP, - tools, toolChoice, user, parallelToolCalls, store, metadata, reasoningEffort, verbosity, serviceTier, - toolCallbacks, toolNames, internalToolExecutionEnabled, httpHeaders, toolContext); - } - - @Override - public String toString() { - return "OpenAiOfficialChatOptions{" + "model='" + getModel() + ", frequencyPenalty=" + frequencyPenalty - + ", logitBias=" + logitBias + ", logprobs=" + logprobs + ", topLogprobs=" + topLogprobs - + ", maxTokens=" + maxTokens + ", maxCompletionTokens=" + maxCompletionTokens + ", n=" + n - + ", outputAudio=" + outputAudio + ", presencePenalty=" + presencePenalty + ", responseFormat=" - + responseFormat + ", streamOptions=" + streamOptions + ", streamUsage=" + streamUsage + ", seed=" - + seed + ", stop=" + stop + ", temperature=" + temperature + ", topP=" + topP + ", tools=" + tools - + ", toolChoice=" + toolChoice + ", user='" + user + '\'' + ", parallelToolCalls=" + parallelToolCalls - + ", store=" + store + ", metadata=" + metadata + ", reasoningEffort='" + reasoningEffort + '\'' - + ", verbosity='" + verbosity + '\'' + ", serviceTier='" + serviceTier + '\'' + ", toolCallbacks=" - + toolCallbacks + ", toolNames=" + toolNames + ", internalToolExecutionEnabled=" - + internalToolExecutionEnabled + ", httpHeaders=" + httpHeaders + ", toolContext=" + toolContext + '}'; - } - - public static final class Builder { - - private final OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); - - public Builder from(OpenAiOfficialChatOptions fromOptions) { - this.options.setModel(fromOptions.getModel()); - this.options.setDeploymentName(fromOptions.getDeploymentName()); - this.options.setFrequencyPenalty(fromOptions.getFrequencyPenalty()); - this.options.setLogitBias(fromOptions.getLogitBias()); - this.options.setLogprobs(fromOptions.getLogprobs()); - this.options.setTopLogprobs(fromOptions.getTopLogprobs()); - this.options.setMaxTokens(fromOptions.getMaxTokens()); - this.options.setMaxCompletionTokens(fromOptions.getMaxCompletionTokens()); - this.options.setN(fromOptions.getN()); - this.options.setOutputAudio(fromOptions.getOutputAudio()); - this.options.setPresencePenalty(fromOptions.getPresencePenalty()); - this.options.setResponseFormat(fromOptions.getResponseFormat()); - this.options.setStreamOptions(fromOptions.getStreamOptions()); - this.options.setStreamUsage(fromOptions.getStreamUsage()); - this.options.setSeed(fromOptions.getSeed()); - this.options.setStop(fromOptions.getStop() != null ? new ArrayList<>(fromOptions.getStop()) : null); - this.options.setTemperature(fromOptions.getTemperature()); - this.options.setTopP(fromOptions.getTopP()); - this.options.setTools(fromOptions.getTools()); - this.options.setToolChoice(fromOptions.getToolChoice()); - this.options.setUser(fromOptions.getUser()); - this.options.setParallelToolCalls(fromOptions.getParallelToolCalls()); - this.options.setToolCallbacks(new ArrayList<>(fromOptions.getToolCallbacks())); - this.options.setToolNames(new HashSet<>(fromOptions.getToolNames())); - this.options.setHttpHeaders( - fromOptions.getHttpHeaders() != null ? new HashMap<>(fromOptions.getHttpHeaders()) : null); - this.options.setInternalToolExecutionEnabled(fromOptions.getInternalToolExecutionEnabled()); - this.options.setToolContext(new HashMap<>(fromOptions.getToolContext())); - this.options.setStore(fromOptions.getStore()); - this.options.setMetadata(fromOptions.getMetadata()); - this.options.setReasoningEffort(fromOptions.getReasoningEffort()); - this.options.setVerbosity(fromOptions.getVerbosity()); - this.options.setServiceTier(fromOptions.getServiceTier()); - return this; - } - - public Builder merge(OpenAiOfficialChatOptions from) { - if (from.getModel() != null) { - this.options.setModel(from.getModel()); - } - if (from.getDeploymentName() != null) { - this.options.setDeploymentName(from.getDeploymentName()); - } - if (from.getFrequencyPenalty() != null) { - this.options.setFrequencyPenalty(from.getFrequencyPenalty()); - } - if (from.getLogitBias() != null) { - this.options.setLogitBias(from.getLogitBias()); - } - if (from.getLogprobs() != null) { - this.options.setLogprobs(from.getLogprobs()); - } - if (from.getTopLogprobs() != null) { - this.options.setTopLogprobs(from.getTopLogprobs()); - } - if (from.getMaxTokens() != null) { - this.options.setMaxTokens(from.getMaxTokens()); - } - if (from.getMaxCompletionTokens() != null) { - this.options.setMaxCompletionTokens(from.getMaxCompletionTokens()); - } - if (from.getN() != null) { - this.options.setN(from.getN()); - } - if (from.getOutputAudio() != null) { - this.options.setOutputAudio(from.getOutputAudio()); - } - if (from.getPresencePenalty() != null) { - this.options.setPresencePenalty(from.getPresencePenalty()); - } - if (from.getResponseFormat() != null) { - this.options.setResponseFormat(from.getResponseFormat()); - } - if (from.getStreamOptions() != null) { - this.options.setStreamOptions(from.getStreamOptions()); - } - if (from.getStreamUsage() != null) { - this.options.setStreamUsage(from.getStreamUsage()); - } - if (from.getSeed() != null) { - this.options.setSeed(from.getSeed()); - } - if (from.getStop() != null) { - this.options.setStop(new ArrayList<>(from.getStop())); - } - if (from.getTemperature() != null) { - this.options.setTemperature(from.getTemperature()); - } - if (from.getTopP() != null) { - this.options.setTopP(from.getTopP()); - } - if (from.getTools() != null) { - this.options.setTools(from.getTools()); - } - if (from.getToolChoice() != null) { - this.options.setToolChoice(from.getToolChoice()); - } - if (from.getUser() != null) { - this.options.setUser(from.getUser()); - } - if (from.getParallelToolCalls() != null) { - this.options.setParallelToolCalls(from.getParallelToolCalls()); - } - if (!from.getToolCallbacks().isEmpty()) { - this.options.setToolCallbacks(new ArrayList<>(from.getToolCallbacks())); - } - if (!from.getToolNames().isEmpty()) { - this.options.setToolNames(new HashSet<>(from.getToolNames())); - } - if (from.getHttpHeaders() != null) { - this.options.setHttpHeaders(new HashMap<>(from.getHttpHeaders())); - } - if (from.getInternalToolExecutionEnabled() != null) { - this.options.setInternalToolExecutionEnabled(from.getInternalToolExecutionEnabled()); - } - if (!from.getToolContext().isEmpty()) { - this.options.setToolContext(new HashMap<>(from.getToolContext())); - } - if (from.getStore() != null) { - this.options.setStore(from.getStore()); - } - if (from.getMetadata() != null) { - this.options.setMetadata(from.getMetadata()); - } - if (from.getReasoningEffort() != null) { - this.options.setReasoningEffort(from.getReasoningEffort()); - } - if (from.getVerbosity() != null) { - this.options.setVerbosity(from.getVerbosity()); - } - if (from.getServiceTier() != null) { - this.options.setServiceTier(from.getServiceTier()); - } - return this; - } - - public Builder model(String model) { - this.options.setModel(model); - return this; - } - - public Builder deploymentName(String deploymentName) { - this.options.setDeploymentName(deploymentName); - return this; - } - - public Builder frequencyPenalty(Double frequencyPenalty) { - this.options.setFrequencyPenalty(frequencyPenalty); - return this; - } - - public Builder logitBias(Map logitBias) { - this.options.setLogitBias(logitBias); - return this; - } - - public Builder logprobs(Boolean logprobs) { - this.options.setLogprobs(logprobs); - return this; - } - - public Builder topLogprobs(Integer topLogprobs) { - this.options.setTopLogprobs(topLogprobs); - return this; - } - - public Builder maxTokens(Integer maxTokens) { - if (maxTokens != null && this.options.getMaxCompletionTokens() != null) { - logger.warn( - "Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " - + "As maxToken is deprecated, we will ignore it and use maxCompletionToken ({}).", - this.options.getMaxCompletionTokens()); - } - else { - this.options.setMaxTokens(maxTokens); - } - return this; - } - - public Builder maxCompletionTokens(Integer maxCompletionTokens) { - if (maxCompletionTokens != null && this.options.getMaxTokens() != null) { - logger.warn( - "Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " - + "As maxToken is deprecated, we will use maxCompletionToken ({}).", - maxCompletionTokens); - - this.options.setMaxTokens(null); - } - this.options.setMaxCompletionTokens(maxCompletionTokens); - return this; - } - - public Builder N(Integer n) { - this.options.setN(n); - return this; - } - - public Builder outputAudio(ChatCompletionAudioParam audio) { - this.options.setOutputAudio(audio); - return this; - } - - public Builder presencePenalty(Double presencePenalty) { - this.options.setPresencePenalty(presencePenalty); - return this; - } - - public Builder responseFormat(OpenAiOfficialChatModel.ResponseFormat responseFormat) { - this.options.setResponseFormat(responseFormat); - return this; - } - - public Builder streamOptions(ResponseCreateParams.StreamOptions streamOptions) { - this.options.setStreamOptions(streamOptions); - return this; - } - - public Builder streamUsage(Boolean streamUsage) { - this.options.setStreamUsage(streamUsage); - return this; - } - - public Builder seed(Integer seed) { - this.options.setSeed(seed); - return this; - } - - public Builder stop(List stop) { - this.options.setStop(stop); - return this; - } - - public Builder temperature(Double temperature) { - this.options.setTemperature(temperature); - return this; - } - - public Builder topP(Double topP) { - this.options.setTopP(topP); - return this; - } - - public Builder tools(List tools) { - this.options.setTools(tools); - return this; - } - - public Builder toolChoice(ChatCompletionToolChoiceOption toolChoice) { - this.options.setToolChoice(toolChoice); - return this; - } - - public Builder user(String user) { - this.options.setUser(user); - return this; - } - - public Builder parallelToolCalls(Boolean parallelToolCalls) { - this.options.setParallelToolCalls(parallelToolCalls); - return this; - } - - public Builder toolCallbacks(List toolCallbacks) { - this.options.setToolCallbacks(toolCallbacks); - return this; - } - - public Builder toolCallbacks(ToolCallback... toolCallbacks) { - this.options.setToolCallbacks(Arrays.asList(toolCallbacks)); - return this; - } - - public Builder toolNames(Set toolNames) { - Assert.notNull(toolNames, "toolNames cannot be null"); - this.options.setToolNames(toolNames); - return this; - } - - public Builder toolNames(String... toolNames) { - Assert.notNull(toolNames, "toolNames cannot be null"); - this.options.setToolNames(new HashSet<>(Arrays.asList(toolNames))); - return this; - } - - public Builder httpHeaders(Map httpHeaders) { - this.options.setHttpHeaders(httpHeaders); - return this; - } - - public Builder internalToolExecutionEnabled(@Nullable Boolean internalToolExecutionEnabled) { - this.options.setInternalToolExecutionEnabled(internalToolExecutionEnabled); - return this; - } - - public Builder toolContext(Map toolContext) { - this.options.setToolContext(toolContext); - return this; - } - - public Builder store(Boolean store) { - this.options.setStore(store); - return this; - } - - public Builder metadata(Map metadata) { - this.options.setMetadata(metadata); - return this; - } - - public Builder reasoningEffort(String reasoningEffort) { - this.options.setReasoningEffort(reasoningEffort); - return this; - } - - public Builder verbosity(String verbosity) { - this.options.setVerbosity(verbosity); - return this; - } - - public Builder serviceTier(String serviceTier) { - this.options.setServiceTier(serviceTier); - return this; - } - - public OpenAiOfficialChatOptions build() { - return this.options; - } - - } + /** + * Gets whether to return log probabilities. + * @return true if log probabilities should be returned + */ + public Boolean getLogprobs() { + return this.logprobs; + } + + /** + * Sets whether to return log probabilities. + * @param logprobs whether to return log probabilities + */ + public void setLogprobs(Boolean logprobs) { + this.logprobs = logprobs; + } + + /** + * Gets the number of top log probabilities to return. + * @return the number of top log probabilities + */ + public Integer getTopLogprobs() { + return this.topLogprobs; + } + + /** + * Sets the number of top log probabilities to return. + * @param topLogprobs the number of top log probabilities + */ + public void setTopLogprobs(Integer topLogprobs) { + this.topLogprobs = topLogprobs; + } + + @Override + public Integer getMaxTokens() { + return this.maxTokens; + } + + /** + * Sets the maximum number of tokens to generate. + * @param maxTokens the maximum number of tokens + */ + public void setMaxTokens(Integer maxTokens) { + this.maxTokens = maxTokens; + } + + /** + * Gets the maximum number of completion tokens. + * @return the maximum number of completion tokens + */ + public Integer getMaxCompletionTokens() { + return this.maxCompletionTokens; + } + + /** + * Sets the maximum number of completion tokens. + * @param maxCompletionTokens the maximum number of completion tokens + */ + public void setMaxCompletionTokens(Integer maxCompletionTokens) { + this.maxCompletionTokens = maxCompletionTokens; + } + + /** + * Gets the number of completions to generate. + * @return the number of completions + */ + public Integer getN() { + return this.n; + } + + /** + * Sets the number of completions to generate. + * @param n the number of completions + */ + public void setN(Integer n) { + this.n = n; + } + + /** + * Gets the output audio parameters. + * @return the output audio parameters + */ + public ChatCompletionAudioParam getOutputAudio() { + return this.outputAudio; + } + + /** + * Sets the output audio parameters. + * @param outputAudio the output audio parameters + */ + public void setOutputAudio(ChatCompletionAudioParam outputAudio) { + this.outputAudio = outputAudio; + } + + @Override + public Double getPresencePenalty() { + return this.presencePenalty; + } + + /** + * Sets the presence penalty parameter. + * @param presencePenalty the presence penalty to set + */ + public void setPresencePenalty(Double presencePenalty) { + this.presencePenalty = presencePenalty; + } + + /** + * Gets the response format configuration. + * @return the response format + */ + public OpenAiOfficialChatModel.ResponseFormat getResponseFormat() { + return this.responseFormat; + } + + /** + * Sets the response format configuration. + * @param responseFormat the response format to set + */ + public void setResponseFormat(OpenAiOfficialChatModel.ResponseFormat responseFormat) { + this.responseFormat = responseFormat; + } + + /** + * Gets the stream options. + * @return the stream options + */ + public ResponseCreateParams.StreamOptions getStreamOptions() { + return this.streamOptions; + } + + /** + * Sets the stream options. + * @param streamOptions the stream options to set + */ + public void setStreamOptions(ResponseCreateParams.StreamOptions streamOptions) { + this.streamOptions = streamOptions; + } + + /** + * Gets whether to include usage information in streaming responses. + * @return true if usage should be included in streams + */ + public Boolean getStreamUsage() { + return this.streamUsage; + } + + /** + * Sets whether to include usage information in streaming responses. + * @param streamUsage whether to include usage in streams + */ + public void setStreamUsage(Boolean streamUsage) { + this.streamUsage = streamUsage; + } + + /** + * Gets the random seed for deterministic generation. + * @return the random seed + */ + public Integer getSeed() { + return this.seed; + } + + /** + * Sets the random seed for deterministic generation. + * @param seed the random seed + */ + public void setSeed(Integer seed) { + this.seed = seed; + } + + /** + * Gets the stop sequences. + * @return the list of stop sequences + */ + public List getStop() { + return this.stop; + } + + /** + * Sets the stop sequences. + * @param stop the list of stop sequences + */ + public void setStop(List stop) { + this.stop = stop; + } + + @Override + public List getStopSequences() { + return getStop(); + } + + /** + * Sets the stop sequences. + * @param stopSequences the list of stop sequences + */ + public void setStopSequences(List stopSequences) { + setStop(stopSequences); + } + + @Override + public Double getTemperature() { + return this.temperature; + } + + /** + * Sets the temperature for sampling. + * @param temperature the temperature value + */ + public void setTemperature(Double temperature) { + this.temperature = temperature; + } + + @Override + public Double getTopP() { + return this.topP; + } + + /** + * Sets the top-p nucleus sampling parameter. + * @param topP the top-p value + */ + public void setTopP(Double topP) { + this.topP = topP; + } + + /** + * Gets the list of tool definitions. + * @return the list of tools + */ + public List getTools() { + return this.tools; + } + + /** + * Sets the list of tool definitions. + * @param tools the list of tools + */ + public void setTools(List tools) { + this.tools = tools; + } + + /** + * Gets the tool choice configuration. + * @return the tool choice option + */ + public ChatCompletionToolChoiceOption getToolChoice() { + return this.toolChoice; + } + + /** + * Sets the tool choice configuration. + * @param toolChoice the tool choice option + */ + public void setToolChoice(ChatCompletionToolChoiceOption toolChoice) { + this.toolChoice = toolChoice; + } + + /** + * Gets the user identifier. + * @return the user identifier + */ + public String getUser() { + return this.user; + } + + /** + * Sets the user identifier. + * @param user the user identifier + */ + public void setUser(String user) { + this.user = user; + } + + /** + * Gets whether to enable parallel tool calls. + * @return true if parallel tool calls are enabled + */ + public Boolean getParallelToolCalls() { + return this.parallelToolCalls; + } + + /** + * Sets whether to enable parallel tool calls. + * @param parallelToolCalls whether to enable parallel tool calls + */ + public void setParallelToolCalls(Boolean parallelToolCalls) { + this.parallelToolCalls = parallelToolCalls; + } + + /** + * Gets whether to store the conversation. + * @return true if the conversation should be stored + */ + public Boolean getStore() { + return this.store; + } + + /** + * Sets whether to store the conversation. + * @param store whether to store the conversation + */ + public void setStore(Boolean store) { + this.store = store; + } + + /** + * Gets the metadata map. + * @return the metadata map + */ + public Map getMetadata() { + return this.metadata; + } + + /** + * Sets the metadata map. + * @param metadata the metadata map + */ + public void setMetadata(Map metadata) { + this.metadata = metadata; + } + + /** + * Gets the reasoning effort level. + * @return the reasoning effort level + */ + public String getReasoningEffort() { + return this.reasoningEffort; + } + + /** + * Sets the reasoning effort level. + * @param reasoningEffort the reasoning effort level + */ + public void setReasoningEffort(String reasoningEffort) { + this.reasoningEffort = reasoningEffort; + } + + /** + * Gets the verbosity level. + * @return the verbosity level + */ + public String getVerbosity() { + return this.verbosity; + } + + /** + * Sets the verbosity level. + * @param verbosity the verbosity level + */ + public void setVerbosity(String verbosity) { + this.verbosity = verbosity; + } + + /** + * Gets the service tier. + * @return the service tier + */ + public String getServiceTier() { + return this.serviceTier; + } + + /** + * Sets the service tier. + * @param serviceTier the service tier + */ + public void setServiceTier(String serviceTier) { + this.serviceTier = serviceTier; + } + + @Override + public List getToolCallbacks() { + return this.toolCallbacks; + } + + @Override + public void setToolCallbacks(List toolCallbacks) { + Assert.notNull(toolCallbacks, "toolCallbacks cannot be null"); + Assert.noNullElements(toolCallbacks, "toolCallbacks cannot contain null elements"); + this.toolCallbacks = toolCallbacks; + } + + @Override + public Set getToolNames() { + return this.toolNames; + } + + @Override + public void setToolNames(Set toolNames) { + Assert.notNull(toolNames, "toolNames cannot be null"); + Assert.noNullElements(toolNames, "toolNames cannot contain null elements"); + toolNames.forEach(tool -> Assert.hasText(tool, "toolNames cannot contain empty elements")); + this.toolNames = toolNames; + } + + @Override + @Nullable + public Boolean getInternalToolExecutionEnabled() { + return this.internalToolExecutionEnabled; + } + + @Override + public void setInternalToolExecutionEnabled(@Nullable Boolean internalToolExecutionEnabled) { + this.internalToolExecutionEnabled = internalToolExecutionEnabled; + } + + /** + * Gets the HTTP headers to include in requests. + * @return the HTTP headers map + */ + public Map getHttpHeaders() { + return this.httpHeaders; + } + + /** + * Sets the HTTP headers to include in requests. + * @param httpHeaders the HTTP headers map + */ + public void setHttpHeaders(Map httpHeaders) { + this.httpHeaders = httpHeaders; + } + + @Override + public Map getToolContext() { + return this.toolContext; + } + + @Override + public void setToolContext(Map toolContext) { + this.toolContext = toolContext; + } + + @Override + public Integer getTopK() { + return null; + } + + public static Builder builder() { + return new Builder(); + } + + @Override + public OpenAiOfficialChatOptions copy() { + return builder().from(this).build(); + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) + return false; + OpenAiOfficialChatOptions options = (OpenAiOfficialChatOptions) o; + return Objects.equals(getModel(), options.getModel()) + && Objects.equals(frequencyPenalty, options.frequencyPenalty) + && Objects.equals(logitBias, options.logitBias) && Objects.equals(logprobs, options.logprobs) + && Objects.equals(topLogprobs, options.topLogprobs) && Objects.equals(maxTokens, options.maxTokens) + && Objects.equals(maxCompletionTokens, options.maxCompletionTokens) && Objects.equals(n, options.n) + && Objects.equals(outputAudio, options.outputAudio) + && Objects.equals(presencePenalty, options.presencePenalty) + && Objects.equals(responseFormat, options.responseFormat) + && Objects.equals(streamOptions, options.streamOptions) + && Objects.equals(streamUsage, options.streamUsage) && Objects.equals(seed, options.seed) + && Objects.equals(stop, options.stop) && Objects.equals(temperature, options.temperature) + && Objects.equals(topP, options.topP) && Objects.equals(tools, options.tools) + && Objects.equals(toolChoice, options.toolChoice) && Objects.equals(user, options.user) + && Objects.equals(parallelToolCalls, options.parallelToolCalls) && Objects.equals(store, options.store) + && Objects.equals(metadata, options.metadata) + && Objects.equals(reasoningEffort, options.reasoningEffort) + && Objects.equals(verbosity, options.verbosity) && Objects.equals(serviceTier, options.serviceTier) + && Objects.equals(toolCallbacks, options.toolCallbacks) && Objects.equals(toolNames, options.toolNames) + && Objects.equals(internalToolExecutionEnabled, options.internalToolExecutionEnabled) + && Objects.equals(httpHeaders, options.httpHeaders) && Objects.equals(toolContext, options.toolContext); + } + + @Override + public int hashCode() { + return Objects.hash(getModel(), frequencyPenalty, logitBias, logprobs, topLogprobs, maxTokens, + maxCompletionTokens, n, outputAudio, presencePenalty, responseFormat, streamOptions, streamUsage, seed, + stop, temperature, topP, tools, toolChoice, user, parallelToolCalls, store, metadata, reasoningEffort, + verbosity, serviceTier, toolCallbacks, toolNames, internalToolExecutionEnabled, httpHeaders, + toolContext); + } + + @Override + public String toString() { + return "OpenAiOfficialChatOptions{" + "model='" + getModel() + ", frequencyPenalty=" + frequencyPenalty + + ", logitBias=" + logitBias + ", logprobs=" + logprobs + ", topLogprobs=" + topLogprobs + + ", maxTokens=" + maxTokens + ", maxCompletionTokens=" + maxCompletionTokens + ", n=" + n + + ", outputAudio=" + outputAudio + ", presencePenalty=" + presencePenalty + ", responseFormat=" + + responseFormat + ", streamOptions=" + streamOptions + ", streamUsage=" + streamUsage + ", seed=" + + seed + ", stop=" + stop + ", temperature=" + temperature + ", topP=" + topP + ", tools=" + tools + + ", toolChoice=" + toolChoice + ", user='" + user + '\'' + ", parallelToolCalls=" + parallelToolCalls + + ", store=" + store + ", metadata=" + metadata + ", reasoningEffort='" + reasoningEffort + '\'' + + ", verbosity='" + verbosity + '\'' + ", serviceTier='" + serviceTier + '\'' + ", toolCallbacks=" + + toolCallbacks + ", toolNames=" + toolNames + ", internalToolExecutionEnabled=" + + internalToolExecutionEnabled + ", httpHeaders=" + httpHeaders + ", toolContext=" + toolContext + '}'; + } + + public static final class Builder { + + private final OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + + public Builder from(OpenAiOfficialChatOptions fromOptions) { + this.options.setModel(fromOptions.getModel()); + this.options.setDeploymentName(fromOptions.getDeploymentName()); + this.options.setFrequencyPenalty(fromOptions.getFrequencyPenalty()); + this.options.setLogitBias(fromOptions.getLogitBias()); + this.options.setLogprobs(fromOptions.getLogprobs()); + this.options.setTopLogprobs(fromOptions.getTopLogprobs()); + this.options.setMaxTokens(fromOptions.getMaxTokens()); + this.options.setMaxCompletionTokens(fromOptions.getMaxCompletionTokens()); + this.options.setN(fromOptions.getN()); + this.options.setOutputAudio(fromOptions.getOutputAudio()); + this.options.setPresencePenalty(fromOptions.getPresencePenalty()); + this.options.setResponseFormat(fromOptions.getResponseFormat()); + this.options.setStreamOptions(fromOptions.getStreamOptions()); + this.options.setStreamUsage(fromOptions.getStreamUsage()); + this.options.setSeed(fromOptions.getSeed()); + this.options.setStop(fromOptions.getStop() != null ? new ArrayList<>(fromOptions.getStop()) : null); + this.options.setTemperature(fromOptions.getTemperature()); + this.options.setTopP(fromOptions.getTopP()); + this.options.setTools(fromOptions.getTools()); + this.options.setToolChoice(fromOptions.getToolChoice()); + this.options.setUser(fromOptions.getUser()); + this.options.setParallelToolCalls(fromOptions.getParallelToolCalls()); + this.options.setToolCallbacks(new ArrayList<>(fromOptions.getToolCallbacks())); + this.options.setToolNames(new HashSet<>(fromOptions.getToolNames())); + this.options.setHttpHeaders( + fromOptions.getHttpHeaders() != null ? new HashMap<>(fromOptions.getHttpHeaders()) : null); + this.options.setInternalToolExecutionEnabled(fromOptions.getInternalToolExecutionEnabled()); + this.options.setToolContext(new HashMap<>(fromOptions.getToolContext())); + this.options.setStore(fromOptions.getStore()); + this.options.setMetadata(fromOptions.getMetadata()); + this.options.setReasoningEffort(fromOptions.getReasoningEffort()); + this.options.setVerbosity(fromOptions.getVerbosity()); + this.options.setServiceTier(fromOptions.getServiceTier()); + return this; + } + + public Builder merge(OpenAiOfficialChatOptions from) { + if (from.getModel() != null) { + this.options.setModel(from.getModel()); + } + if (from.getDeploymentName() != null) { + this.options.setDeploymentName(from.getDeploymentName()); + } + if (from.getFrequencyPenalty() != null) { + this.options.setFrequencyPenalty(from.getFrequencyPenalty()); + } + if (from.getLogitBias() != null) { + this.options.setLogitBias(from.getLogitBias()); + } + if (from.getLogprobs() != null) { + this.options.setLogprobs(from.getLogprobs()); + } + if (from.getTopLogprobs() != null) { + this.options.setTopLogprobs(from.getTopLogprobs()); + } + if (from.getMaxTokens() != null) { + this.options.setMaxTokens(from.getMaxTokens()); + } + if (from.getMaxCompletionTokens() != null) { + this.options.setMaxCompletionTokens(from.getMaxCompletionTokens()); + } + if (from.getN() != null) { + this.options.setN(from.getN()); + } + if (from.getOutputAudio() != null) { + this.options.setOutputAudio(from.getOutputAudio()); + } + if (from.getPresencePenalty() != null) { + this.options.setPresencePenalty(from.getPresencePenalty()); + } + if (from.getResponseFormat() != null) { + this.options.setResponseFormat(from.getResponseFormat()); + } + if (from.getStreamOptions() != null) { + this.options.setStreamOptions(from.getStreamOptions()); + } + if (from.getStreamUsage() != null) { + this.options.setStreamUsage(from.getStreamUsage()); + } + if (from.getSeed() != null) { + this.options.setSeed(from.getSeed()); + } + if (from.getStop() != null) { + this.options.setStop(new ArrayList<>(from.getStop())); + } + if (from.getTemperature() != null) { + this.options.setTemperature(from.getTemperature()); + } + if (from.getTopP() != null) { + this.options.setTopP(from.getTopP()); + } + if (from.getTools() != null) { + this.options.setTools(from.getTools()); + } + if (from.getToolChoice() != null) { + this.options.setToolChoice(from.getToolChoice()); + } + if (from.getUser() != null) { + this.options.setUser(from.getUser()); + } + if (from.getParallelToolCalls() != null) { + this.options.setParallelToolCalls(from.getParallelToolCalls()); + } + if (!from.getToolCallbacks().isEmpty()) { + this.options.setToolCallbacks(new ArrayList<>(from.getToolCallbacks())); + } + if (!from.getToolNames().isEmpty()) { + this.options.setToolNames(new HashSet<>(from.getToolNames())); + } + if (from.getHttpHeaders() != null) { + this.options.setHttpHeaders(new HashMap<>(from.getHttpHeaders())); + } + if (from.getInternalToolExecutionEnabled() != null) { + this.options.setInternalToolExecutionEnabled(from.getInternalToolExecutionEnabled()); + } + if (!from.getToolContext().isEmpty()) { + this.options.setToolContext(new HashMap<>(from.getToolContext())); + } + if (from.getStore() != null) { + this.options.setStore(from.getStore()); + } + if (from.getMetadata() != null) { + this.options.setMetadata(from.getMetadata()); + } + if (from.getReasoningEffort() != null) { + this.options.setReasoningEffort(from.getReasoningEffort()); + } + if (from.getVerbosity() != null) { + this.options.setVerbosity(from.getVerbosity()); + } + if (from.getServiceTier() != null) { + this.options.setServiceTier(from.getServiceTier()); + } + return this; + } + + public Builder model(String model) { + this.options.setModel(model); + return this; + } + + public Builder deploymentName(String deploymentName) { + this.options.setDeploymentName(deploymentName); + return this; + } + + public Builder frequencyPenalty(Double frequencyPenalty) { + this.options.setFrequencyPenalty(frequencyPenalty); + return this; + } + + public Builder logitBias(Map logitBias) { + this.options.setLogitBias(logitBias); + return this; + } + + public Builder logprobs(Boolean logprobs) { + this.options.setLogprobs(logprobs); + return this; + } + + public Builder topLogprobs(Integer topLogprobs) { + this.options.setTopLogprobs(topLogprobs); + return this; + } + + public Builder maxTokens(Integer maxTokens) { + if (maxTokens != null && this.options.getMaxCompletionTokens() != null) { + logger.warn( + "Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " + + "As maxToken is deprecated, we will ignore it and use maxCompletionToken ({}).", + this.options.getMaxCompletionTokens()); + } + else { + this.options.setMaxTokens(maxTokens); + } + return this; + } + + public Builder maxCompletionTokens(Integer maxCompletionTokens) { + if (maxCompletionTokens != null && this.options.getMaxTokens() != null) { + logger.warn( + "Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " + + "As maxToken is deprecated, we will use maxCompletionToken ({}).", + maxCompletionTokens); + + this.options.setMaxTokens(null); + } + this.options.setMaxCompletionTokens(maxCompletionTokens); + return this; + } + + public Builder N(Integer n) { + this.options.setN(n); + return this; + } + + public Builder outputAudio(ChatCompletionAudioParam audio) { + this.options.setOutputAudio(audio); + return this; + } + + public Builder presencePenalty(Double presencePenalty) { + this.options.setPresencePenalty(presencePenalty); + return this; + } + + public Builder responseFormat(OpenAiOfficialChatModel.ResponseFormat responseFormat) { + this.options.setResponseFormat(responseFormat); + return this; + } + + public Builder streamOptions(ResponseCreateParams.StreamOptions streamOptions) { + this.options.setStreamOptions(streamOptions); + return this; + } + + public Builder streamUsage(Boolean streamUsage) { + this.options.setStreamUsage(streamUsage); + return this; + } + + public Builder seed(Integer seed) { + this.options.setSeed(seed); + return this; + } + + public Builder stop(List stop) { + this.options.setStop(stop); + return this; + } + + public Builder temperature(Double temperature) { + this.options.setTemperature(temperature); + return this; + } + + public Builder topP(Double topP) { + this.options.setTopP(topP); + return this; + } + + public Builder tools(List tools) { + this.options.setTools(tools); + return this; + } + + public Builder toolChoice(ChatCompletionToolChoiceOption toolChoice) { + this.options.setToolChoice(toolChoice); + return this; + } + + public Builder user(String user) { + this.options.setUser(user); + return this; + } + + public Builder parallelToolCalls(Boolean parallelToolCalls) { + this.options.setParallelToolCalls(parallelToolCalls); + return this; + } + + public Builder toolCallbacks(List toolCallbacks) { + this.options.setToolCallbacks(toolCallbacks); + return this; + } + + public Builder toolCallbacks(ToolCallback... toolCallbacks) { + this.options.setToolCallbacks(Arrays.asList(toolCallbacks)); + return this; + } + + public Builder toolNames(Set toolNames) { + Assert.notNull(toolNames, "toolNames cannot be null"); + this.options.setToolNames(toolNames); + return this; + } + + public Builder toolNames(String... toolNames) { + Assert.notNull(toolNames, "toolNames cannot be null"); + this.options.setToolNames(new HashSet<>(Arrays.asList(toolNames))); + return this; + } + + public Builder httpHeaders(Map httpHeaders) { + this.options.setHttpHeaders(httpHeaders); + return this; + } + + public Builder internalToolExecutionEnabled(@Nullable Boolean internalToolExecutionEnabled) { + this.options.setInternalToolExecutionEnabled(internalToolExecutionEnabled); + return this; + } + + public Builder toolContext(Map toolContext) { + this.options.setToolContext(toolContext); + return this; + } + + public Builder store(Boolean store) { + this.options.setStore(store); + return this; + } + + public Builder metadata(Map metadata) { + this.options.setMetadata(metadata); + return this; + } + + public Builder reasoningEffort(String reasoningEffort) { + this.options.setReasoningEffort(reasoningEffort); + return this; + } + + public Builder verbosity(String verbosity) { + this.options.setVerbosity(verbosity); + return this; + } + + public Builder serviceTier(String serviceTier) { + this.options.setServiceTier(serviceTier); + return this; + } + + public OpenAiOfficialChatOptions build() { + return this.options; + } + + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java index cb5f54b3592..53ee176bc87 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java @@ -52,160 +52,209 @@ */ public class OpenAiOfficialEmbeddingModel extends AbstractEmbeddingModel { - private static final String DEFAULT_MODEL_NAME = OpenAiOfficialEmbeddingOptions.DEFAULT_EMBEDDING_MODEL; - - private static final EmbeddingModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultEmbeddingModelObservationConvention(); - - private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialEmbeddingModel.class); - - private final OpenAIClient openAiClient; - - private final OpenAiOfficialEmbeddingOptions options; - - private final MetadataMode metadataMode; - - private final ObservationRegistry observationRegistry; - - private EmbeddingModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; - - public OpenAiOfficialEmbeddingModel() { - this(null, null, null, null); - } - - public OpenAiOfficialEmbeddingModel(OpenAiOfficialEmbeddingOptions options) { - this(null, null, options, null); - } - - public OpenAiOfficialEmbeddingModel(MetadataMode metadataMode, OpenAiOfficialEmbeddingOptions options) { - this(null, metadataMode, options, null); - } - - public OpenAiOfficialEmbeddingModel(OpenAiOfficialEmbeddingOptions options, - ObservationRegistry observationRegistry) { - this(null, null, options, observationRegistry); - } - - public OpenAiOfficialEmbeddingModel(MetadataMode metadataMode, OpenAiOfficialEmbeddingOptions options, - ObservationRegistry observationRegistry) { - this(null, metadataMode, options, observationRegistry); - } - - public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient) { - this(openAiClient, null, null, null); - } - - public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode) { - this(openAiClient, metadataMode, null, null); - } - - public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, - OpenAiOfficialEmbeddingOptions options) { - this(openAiClient, metadataMode, options, null); - } - - public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, - OpenAiOfficialEmbeddingOptions options, ObservationRegistry observationRegistry) { - - if (options == null) { - this.options = OpenAiOfficialEmbeddingOptions.builder().model(DEFAULT_MODEL_NAME).build(); - } - else { - this.options = options; - } - this.openAiClient = Objects.requireNonNullElseGet(openAiClient, - () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), - this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), - this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), - this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), - this.options.getProxy(), this.options.getCustomHeaders())); - this.metadataMode = Objects.requireNonNullElse(metadataMode, MetadataMode.EMBED); - this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); - } - - @Override - public float[] embed(Document document) { - EmbeddingResponse response = this - .call(new EmbeddingRequest(List.of(document.getFormattedContent(this.metadataMode)), null)); - - if (CollectionUtils.isEmpty(response.getResults())) { - return new float[0]; - } - return response.getResults().get(0).getOutput(); - } - - @Override - public EmbeddingResponse call(EmbeddingRequest embeddingRequest) { - OpenAiOfficialEmbeddingOptions options = OpenAiOfficialEmbeddingOptions.builder() - .from(this.options) - .merge(embeddingRequest.getOptions()) - .build(); - - EmbeddingRequest embeddingRequestWithMergedOptions = new EmbeddingRequest(embeddingRequest.getInstructions(), - options); - - EmbeddingCreateParams embeddingCreateParams = options - .toOpenAiCreateParams(embeddingRequestWithMergedOptions.getInstructions()); - - if (logger.isTraceEnabled()) { - logger.trace("OpenAiOfficialEmbeddingModel call {} with the following options : {} ", options.getModel(), - embeddingCreateParams); - } - - var observationContext = EmbeddingModelObservationContext.builder() - .embeddingRequest(embeddingRequestWithMergedOptions) - .provider(AiProvider.OPENAI_OFFICIAL.value()) - .build(); - - return Objects.requireNonNull( - EmbeddingModelObservationDocumentation.EMBEDDING_MODEL_OPERATION - .observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, - this.observationRegistry) - .observe(() -> { - CreateEmbeddingResponse response = this.openAiClient.embeddings().create(embeddingCreateParams); - - var embeddingResponse = generateEmbeddingResponse(response); - observationContext.setResponse(embeddingResponse); - return embeddingResponse; - })); - } - - private EmbeddingResponse generateEmbeddingResponse(CreateEmbeddingResponse response) { - - List data = generateEmbeddingList(response.data()); - EmbeddingResponseMetadata metadata = new EmbeddingResponseMetadata(); - metadata.setModel(response.model()); - metadata.setUsage(getDefaultUsage(response.usage())); - return new EmbeddingResponse(data, metadata); - } - - private DefaultUsage getDefaultUsage(CreateEmbeddingResponse.Usage nativeUsage) { - return new DefaultUsage(Math.toIntExact(nativeUsage.promptTokens()), 0, - Math.toIntExact(nativeUsage.totalTokens()), nativeUsage); - } - - private List generateEmbeddingList(List nativeData) { - List data = new ArrayList<>(); - for (com.openai.models.embeddings.Embedding nativeDatum : nativeData) { - List nativeDatumEmbedding = nativeDatum.embedding(); - long nativeIndex = nativeDatum.index(); - Embedding embedding = new Embedding(EmbeddingUtils.toPrimitive(nativeDatumEmbedding), - Math.toIntExact(nativeIndex)); - data.add(embedding); - } - return data; - } - - public OpenAiOfficialEmbeddingOptions getOptions() { - return this.options; - } - - /** - * Use the provided convention for reporting observation data - * @param observationConvention The provided convention - */ - public void setObservationConvention(EmbeddingModelObservationConvention observationConvention) { - Assert.notNull(observationConvention, "observationConvention cannot be null"); - this.observationConvention = observationConvention; - } + private static final String DEFAULT_MODEL_NAME = OpenAiOfficialEmbeddingOptions.DEFAULT_EMBEDDING_MODEL; + + private static final EmbeddingModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultEmbeddingModelObservationConvention(); + + private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialEmbeddingModel.class); + + private final OpenAIClient openAiClient; + + private final OpenAiOfficialEmbeddingOptions options; + + private final MetadataMode metadataMode; + + private final ObservationRegistry observationRegistry; + + private EmbeddingModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; + + /** + * Creates a new OpenAiOfficialEmbeddingModel with default options. + */ + public OpenAiOfficialEmbeddingModel() { + this(null, null, null, null); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with the given options. + * @param options the embedding options + */ + public OpenAiOfficialEmbeddingModel(OpenAiOfficialEmbeddingOptions options) { + this(null, null, options, null); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with the given metadata mode and options. + * @param metadataMode the metadata mode + * @param options the embedding options + */ + public OpenAiOfficialEmbeddingModel(MetadataMode metadataMode, OpenAiOfficialEmbeddingOptions options) { + this(null, metadataMode, options, null); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with the given options and observation registry. + * @param options the embedding options + * @param observationRegistry the observation registry + */ + public OpenAiOfficialEmbeddingModel(OpenAiOfficialEmbeddingOptions options, + ObservationRegistry observationRegistry) { + this(null, null, options, observationRegistry); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with the given metadata mode, options, and observation registry. + * @param metadataMode the metadata mode + * @param options the embedding options + * @param observationRegistry the observation registry + */ + public OpenAiOfficialEmbeddingModel(MetadataMode metadataMode, OpenAiOfficialEmbeddingOptions options, + ObservationRegistry observationRegistry) { + this(null, metadataMode, options, observationRegistry); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with the given OpenAI client. + * @param openAiClient the OpenAI client + */ + public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient) { + this(openAiClient, null, null, null); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with the given OpenAI client and metadata mode. + * @param openAiClient the OpenAI client + * @param metadataMode the metadata mode + */ + public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode) { + this(openAiClient, metadataMode, null, null); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with all configuration options. + * @param openAiClient the OpenAI client + * @param metadataMode the metadata mode + * @param options the embedding options + */ + public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, + OpenAiOfficialEmbeddingOptions options) { + this(openAiClient, metadataMode, options, null); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with all configuration options. + * @param openAiClient the OpenAI client + * @param metadataMode the metadata mode + * @param options the embedding options + * @param observationRegistry the observation registry + */ + public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, + OpenAiOfficialEmbeddingOptions options, ObservationRegistry observationRegistry) { + + if (options == null) { + this.options = OpenAiOfficialEmbeddingOptions.builder().model(DEFAULT_MODEL_NAME).build(); + } + else { + this.options = options; + } + this.openAiClient = Objects.requireNonNullElseGet(openAiClient, + () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), + this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), + this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), + this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), + this.options.getProxy(), this.options.getCustomHeaders())); + this.metadataMode = Objects.requireNonNullElse(metadataMode, MetadataMode.EMBED); + this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); + } + + @Override + public float[] embed(Document document) { + EmbeddingResponse response = this + .call(new EmbeddingRequest(List.of(document.getFormattedContent(this.metadataMode)), null)); + + if (CollectionUtils.isEmpty(response.getResults())) { + return new float[0]; + } + return response.getResults().get(0).getOutput(); + } + + @Override + public EmbeddingResponse call(EmbeddingRequest embeddingRequest) { + OpenAiOfficialEmbeddingOptions options = OpenAiOfficialEmbeddingOptions.builder() + .from(this.options) + .merge(embeddingRequest.getOptions()) + .build(); + + EmbeddingRequest embeddingRequestWithMergedOptions = new EmbeddingRequest(embeddingRequest.getInstructions(), + options); + + EmbeddingCreateParams embeddingCreateParams = options + .toOpenAiCreateParams(embeddingRequestWithMergedOptions.getInstructions()); + + if (logger.isTraceEnabled()) { + logger.trace("OpenAiOfficialEmbeddingModel call {} with the following options : {} ", options.getModel(), + embeddingCreateParams); + } + + var observationContext = EmbeddingModelObservationContext.builder() + .embeddingRequest(embeddingRequestWithMergedOptions) + .provider(AiProvider.OPENAI_OFFICIAL.value()) + .build(); + + return Objects.requireNonNull( + EmbeddingModelObservationDocumentation.EMBEDDING_MODEL_OPERATION + .observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, + this.observationRegistry) + .observe(() -> { + CreateEmbeddingResponse response = this.openAiClient.embeddings().create(embeddingCreateParams); + + var embeddingResponse = generateEmbeddingResponse(response); + observationContext.setResponse(embeddingResponse); + return embeddingResponse; + })); + } + + private EmbeddingResponse generateEmbeddingResponse(CreateEmbeddingResponse response) { + + List data = generateEmbeddingList(response.data()); + EmbeddingResponseMetadata metadata = new EmbeddingResponseMetadata(); + metadata.setModel(response.model()); + metadata.setUsage(getDefaultUsage(response.usage())); + return new EmbeddingResponse(data, metadata); + } + + private DefaultUsage getDefaultUsage(CreateEmbeddingResponse.Usage nativeUsage) { + return new DefaultUsage(Math.toIntExact(nativeUsage.promptTokens()), 0, + Math.toIntExact(nativeUsage.totalTokens()), nativeUsage); + } + + private List generateEmbeddingList(List nativeData) { + List data = new ArrayList<>(); + for (com.openai.models.embeddings.Embedding nativeDatum : nativeData) { + List nativeDatumEmbedding = nativeDatum.embedding(); + long nativeIndex = nativeDatum.index(); + Embedding embedding = new Embedding(EmbeddingUtils.toPrimitive(nativeDatumEmbedding), + Math.toIntExact(nativeIndex)); + data.add(embedding); + } + return data; + } + + /** + * Gets the embedding options for this model. + * @return the embedding options + */ + public OpenAiOfficialEmbeddingOptions getOptions() { + return this.options; + } + + /** + * Use the provided convention for reporting observation data + * @param observationConvention The provided convention + */ + public void setObservationConvention(EmbeddingModelObservationConvention observationConvention) { + Assert.notNull(observationConvention, "observationConvention cannot be null"); + this.observationConvention = observationConvention; + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingOptions.java index 403ed446869..bf6f583a073 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingOptions.java @@ -31,138 +31,138 @@ */ public class OpenAiOfficialEmbeddingOptions extends AbstractOpenAiOfficialOptions implements EmbeddingOptions { - public static final String DEFAULT_EMBEDDING_MODEL = TEXT_EMBEDDING_ADA_002.asString(); - - /** - * An identifier for the caller or end user of the operation. This may be used for - * tracking or rate-limiting purposes. - */ - private String user; - - /* - * The number of dimensions the resulting output embeddings should have. Only - * supported in `text-embedding-3` and later models. - */ - private Integer dimensions; - - public static Builder builder() { - return new Builder(); - } - - public String getUser() { - return this.user; - } - - public void setUser(String user) { - this.user = user; - } - - @Override - public Integer getDimensions() { - return this.dimensions; - } - - public void setDimensions(Integer dimensions) { - this.dimensions = dimensions; - } - - @Override - public String toString() { - return "OpenAiOfficialEmbeddingOptions{" + "user='" + user + '\'' + ", model='" + getModel() + '\'' - + ", deploymentName='" + getDeploymentName() + '\'' + ", dimensions=" + dimensions + '}'; - } - - public EmbeddingCreateParams toOpenAiCreateParams(List instructions) { - - EmbeddingCreateParams.Builder builder = EmbeddingCreateParams.builder(); - - // Use deployment name if available (for Azure AI Foundry), otherwise use model - // name - if (this.getDeploymentName() != null) { - builder.model(this.getDeploymentName()); - } - else if (this.getModel() != null) { - builder.model(this.getModel()); - } - - if (instructions != null && !instructions.isEmpty()) { - builder.input(EmbeddingCreateParams.Input.ofArrayOfStrings(instructions)); - } - if (this.getUser() != null) { - builder.user(this.getUser()); - } - if (this.getDimensions() != null) { - builder.dimensions(this.getDimensions()); - } - return builder.build(); - } - - public static final class Builder { - - private final OpenAiOfficialEmbeddingOptions options = new OpenAiOfficialEmbeddingOptions(); - - public Builder from(OpenAiOfficialEmbeddingOptions fromOptions) { - this.options.setUser(fromOptions.getUser()); - this.options.setModel(fromOptions.getModel()); - this.options.setDeploymentName(fromOptions.getDeploymentName()); - this.options.setDimensions(fromOptions.getDimensions()); - return this; - } - - public Builder merge(EmbeddingOptions from) { - if (from instanceof OpenAiOfficialEmbeddingOptions castFrom) { - - if (castFrom.getUser() != null) { - this.options.setUser(castFrom.getUser()); - } - if (castFrom.getModel() != null) { - this.options.setModel(castFrom.getModel()); - } - if (castFrom.getDeploymentName() != null) { - this.options.setDeploymentName(castFrom.getDeploymentName()); - } - if (castFrom.getDimensions() != null) { - this.options.setDimensions(castFrom.getDimensions()); - } - } - return this; - } - - public Builder from(EmbeddingCreateParams openAiCreateParams) { - - if (openAiCreateParams.user().isPresent()) { - this.options.setUser(openAiCreateParams.user().get()); - } - if (openAiCreateParams.dimensions().isPresent()) { - this.options.setDimensions(Math.toIntExact(openAiCreateParams.dimensions().get())); - } - return this; - } - - public Builder user(String user) { - this.options.setUser(user); - return this; - } - - public Builder deploymentName(String deploymentName) { - this.options.setDeploymentName(deploymentName); - return this; - } - - public Builder model(String model) { - this.options.setModel(model); - return this; - } - - public Builder dimensions(Integer dimensions) { - this.options.dimensions = dimensions; - return this; - } - - public OpenAiOfficialEmbeddingOptions build() { - return this.options; - } - - } + public static final String DEFAULT_EMBEDDING_MODEL = TEXT_EMBEDDING_ADA_002.asString(); + + /** + * An identifier for the caller or end user of the operation. This may be used for + * tracking or rate-limiting purposes. + */ + private String user; + + /* + * The number of dimensions the resulting output embeddings should have. Only + * supported in `text-embedding-3` and later models. + */ + private Integer dimensions; + + public static Builder builder() { + return new Builder(); + } + + public String getUser() { + return this.user; + } + + public void setUser(String user) { + this.user = user; + } + + @Override + public Integer getDimensions() { + return this.dimensions; + } + + public void setDimensions(Integer dimensions) { + this.dimensions = dimensions; + } + + @Override + public String toString() { + return "OpenAiOfficialEmbeddingOptions{" + "user='" + user + '\'' + ", model='" + getModel() + '\'' + + ", deploymentName='" + getDeploymentName() + '\'' + ", dimensions=" + dimensions + '}'; + } + + public EmbeddingCreateParams toOpenAiCreateParams(List instructions) { + + EmbeddingCreateParams.Builder builder = EmbeddingCreateParams.builder(); + + // Use deployment name if available (for Azure AI Foundry), otherwise use model + // name + if (this.getDeploymentName() != null) { + builder.model(this.getDeploymentName()); + } + else if (this.getModel() != null) { + builder.model(this.getModel()); + } + + if (instructions != null && !instructions.isEmpty()) { + builder.input(EmbeddingCreateParams.Input.ofArrayOfStrings(instructions)); + } + if (this.getUser() != null) { + builder.user(this.getUser()); + } + if (this.getDimensions() != null) { + builder.dimensions(this.getDimensions()); + } + return builder.build(); + } + + public static final class Builder { + + private final OpenAiOfficialEmbeddingOptions options = new OpenAiOfficialEmbeddingOptions(); + + public Builder from(OpenAiOfficialEmbeddingOptions fromOptions) { + this.options.setUser(fromOptions.getUser()); + this.options.setModel(fromOptions.getModel()); + this.options.setDeploymentName(fromOptions.getDeploymentName()); + this.options.setDimensions(fromOptions.getDimensions()); + return this; + } + + public Builder merge(EmbeddingOptions from) { + if (from instanceof OpenAiOfficialEmbeddingOptions castFrom) { + + if (castFrom.getUser() != null) { + this.options.setUser(castFrom.getUser()); + } + if (castFrom.getModel() != null) { + this.options.setModel(castFrom.getModel()); + } + if (castFrom.getDeploymentName() != null) { + this.options.setDeploymentName(castFrom.getDeploymentName()); + } + if (castFrom.getDimensions() != null) { + this.options.setDimensions(castFrom.getDimensions()); + } + } + return this; + } + + public Builder from(EmbeddingCreateParams openAiCreateParams) { + + if (openAiCreateParams.user().isPresent()) { + this.options.setUser(openAiCreateParams.user().get()); + } + if (openAiCreateParams.dimensions().isPresent()) { + this.options.setDimensions(Math.toIntExact(openAiCreateParams.dimensions().get())); + } + return this; + } + + public Builder user(String user) { + this.options.setUser(user); + return this; + } + + public Builder deploymentName(String deploymentName) { + this.options.setDeploymentName(deploymentName); + return this; + } + + public Builder model(String model) { + this.options.setModel(model); + return this; + } + + public Builder dimensions(Integer dimensions) { + this.options.dimensions = dimensions; + return this; + } + + public OpenAiOfficialEmbeddingOptions build() { + return this.options; + } + + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageModel.java index fcc9def4afb..5296ff6c937 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageModel.java @@ -48,130 +48,170 @@ */ public class OpenAiOfficialImageModel implements ImageModel { - private static final String DEFAULT_MODEL_NAME = OpenAiOfficialImageOptions.DEFAULT_IMAGE_MODEL; - - private static final ImageModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultImageModelObservationConvention(); - - private final Logger logger = LoggerFactory.getLogger(OpenAiOfficialImageModel.class); - - private final OpenAIClient openAiClient; - - private final OpenAiOfficialImageOptions options; - - private final ObservationRegistry observationRegistry; - - private ImageModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; - - public OpenAiOfficialImageModel() { - this(null, null, null); - } - - public OpenAiOfficialImageModel(OpenAiOfficialImageOptions options) { - this(null, options, null); - } - - public OpenAiOfficialImageModel(ObservationRegistry observationRegistry) { - this(null, null, observationRegistry); - } - - public OpenAiOfficialImageModel(OpenAiOfficialImageOptions options, ObservationRegistry observationRegistry) { - this(null, options, observationRegistry); - } - - public OpenAiOfficialImageModel(OpenAIClient openAIClient) { - this(openAIClient, null, null); - } - - public OpenAiOfficialImageModel(OpenAIClient openAIClient, OpenAiOfficialImageOptions options) { - this(openAIClient, options, null); - } - - public OpenAiOfficialImageModel(OpenAIClient openAIClient, ObservationRegistry observationRegistry) { - this(openAIClient, null, observationRegistry); - } - - public OpenAiOfficialImageModel(OpenAIClient openAiClient, OpenAiOfficialImageOptions options, - ObservationRegistry observationRegistry) { - - if (options == null) { - this.options = OpenAiOfficialImageOptions.builder().model(DEFAULT_MODEL_NAME).build(); - } - else { - this.options = options; - } - this.openAiClient = Objects.requireNonNullElseGet(openAiClient, - () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), - this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), - this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), - this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), - this.options.getProxy(), this.options.getCustomHeaders())); - this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); - } - - public OpenAiOfficialImageOptions getOptions() { - return this.options; - } - - @Override - public ImageResponse call(ImagePrompt imagePrompt) { - OpenAiOfficialImageOptions options = OpenAiOfficialImageOptions.builder() - .from(this.options) - .merge(imagePrompt.getOptions()) - .build(); - - ImageGenerateParams imageGenerateParams = options.toOpenAiImageGenerateParams(imagePrompt); - - if (logger.isTraceEnabled()) { - logger.trace("OpenAiOfficialImageOptions call {} with the following options : {} ", options.getModel(), - imageGenerateParams); - } - - var observationContext = ImageModelObservationContext.builder() - .imagePrompt(imagePrompt) - .provider(AiProvider.OPENAI_OFFICIAL.value()) - .build(); - - return Objects.requireNonNull( - ImageModelObservationDocumentation.IMAGE_MODEL_OPERATION - .observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, - this.observationRegistry) - .observe(() -> { - var images = this.openAiClient.images().generate(imageGenerateParams); - - if (images.data().isEmpty() && images.data().get().isEmpty()) { - throw new IllegalArgumentException("Image generation failed: no image returned"); - } - - List imageGenerations = images.data().get().stream().map(nativeImage -> { - Image image; - if (nativeImage.url().isPresent()) { - image = new Image(nativeImage.url().get(), null); - } - else if (nativeImage.b64Json().isPresent()) { - image = new Image(null, nativeImage.b64Json().get()); - } - else { - throw new IllegalArgumentException( - "Image generation failed: image entry missing url and b64_json"); - } - var metadata = new OpenAiOfficialImageGenerationMetadata(nativeImage.revisedPrompt()); - return new ImageGeneration(image, metadata); - }).toList(); - ImageResponseMetadata openAiImageResponseMetadata = OpenAiOfficialImageResponseMetadata - .from(images); - ImageResponse imageResponse = new ImageResponse(imageGenerations, openAiImageResponseMetadata); - observationContext.setResponse(imageResponse); - return imageResponse; - })); - } - - /** - * Use the provided convention for reporting observation data - * @param observationConvention The provided convention - */ - public void setObservationConvention(ImageModelObservationConvention observationConvention) { - Assert.notNull(observationConvention, "observationConvention cannot be null"); - this.observationConvention = observationConvention; - } + private static final String DEFAULT_MODEL_NAME = OpenAiOfficialImageOptions.DEFAULT_IMAGE_MODEL; + + private static final ImageModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultImageModelObservationConvention(); + + private final Logger logger = LoggerFactory.getLogger(OpenAiOfficialImageModel.class); + + private final OpenAIClient openAiClient; + + private final OpenAiOfficialImageOptions options; + + private final ObservationRegistry observationRegistry; + + private ImageModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; + + /** + * Creates a new OpenAiOfficialImageModel with default options. + */ + public OpenAiOfficialImageModel() { + this(null, null, null); + } + + /** + * Creates a new OpenAiOfficialImageModel with the given options. + * @param options the image options + */ + public OpenAiOfficialImageModel(OpenAiOfficialImageOptions options) { + this(null, options, null); + } + + /** + * Creates a new OpenAiOfficialImageModel with the given observation registry. + * @param observationRegistry the observation registry + */ + public OpenAiOfficialImageModel(ObservationRegistry observationRegistry) { + this(null, null, observationRegistry); + } + + /** + * Creates a new OpenAiOfficialImageModel with the given options and observation registry. + * @param options the image options + * @param observationRegistry the observation registry + */ + public OpenAiOfficialImageModel(OpenAiOfficialImageOptions options, ObservationRegistry observationRegistry) { + this(null, options, observationRegistry); + } + + /** + * Creates a new OpenAiOfficialImageModel with the given OpenAI client. + * @param openAIClient the OpenAI client + */ + public OpenAiOfficialImageModel(OpenAIClient openAIClient) { + this(openAIClient, null, null); + } + + /** + * Creates a new OpenAiOfficialImageModel with the given OpenAI client and options. + * @param openAIClient the OpenAI client + * @param options the image options + */ + public OpenAiOfficialImageModel(OpenAIClient openAIClient, OpenAiOfficialImageOptions options) { + this(openAIClient, options, null); + } + + /** + * Creates a new OpenAiOfficialImageModel with the given OpenAI client and observation registry. + * @param openAIClient the OpenAI client + * @param observationRegistry the observation registry + */ + public OpenAiOfficialImageModel(OpenAIClient openAIClient, ObservationRegistry observationRegistry) { + this(openAIClient, null, observationRegistry); + } + + /** + * Creates a new OpenAiOfficialImageModel with all configuration options. + * @param openAiClient the OpenAI client + * @param options the image options + * @param observationRegistry the observation registry + */ + public OpenAiOfficialImageModel(OpenAIClient openAiClient, OpenAiOfficialImageOptions options, + ObservationRegistry observationRegistry) { + + if (options == null) { + this.options = OpenAiOfficialImageOptions.builder().model(DEFAULT_MODEL_NAME).build(); + } + else { + this.options = options; + } + this.openAiClient = Objects.requireNonNullElseGet(openAiClient, + () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), + this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), + this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), + this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), + this.options.getProxy(), this.options.getCustomHeaders())); + this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); + } + + /** + * Gets the image options for this model. + * @return the image options + */ + public OpenAiOfficialImageOptions getOptions() { + return this.options; + } + + @Override + public ImageResponse call(ImagePrompt imagePrompt) { + OpenAiOfficialImageOptions options = OpenAiOfficialImageOptions.builder() + .from(this.options) + .merge(imagePrompt.getOptions()) + .build(); + + ImageGenerateParams imageGenerateParams = options.toOpenAiImageGenerateParams(imagePrompt); + + if (logger.isTraceEnabled()) { + logger.trace("OpenAiOfficialImageOptions call {} with the following options : {} ", options.getModel(), + imageGenerateParams); + } + + var observationContext = ImageModelObservationContext.builder() + .imagePrompt(imagePrompt) + .provider(AiProvider.OPENAI_OFFICIAL.value()) + .build(); + + return Objects.requireNonNull( + ImageModelObservationDocumentation.IMAGE_MODEL_OPERATION + .observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, + this.observationRegistry) + .observe(() -> { + var images = this.openAiClient.images().generate(imageGenerateParams); + + if (images.data().isEmpty() && images.data().get().isEmpty()) { + throw new IllegalArgumentException("Image generation failed: no image returned"); + } + + List imageGenerations = images.data().get().stream().map(nativeImage -> { + Image image; + if (nativeImage.url().isPresent()) { + image = new Image(nativeImage.url().get(), null); + } + else if (nativeImage.b64Json().isPresent()) { + image = new Image(null, nativeImage.b64Json().get()); + } + else { + throw new IllegalArgumentException( + "Image generation failed: image entry missing url and b64_json"); + } + var metadata = new OpenAiOfficialImageGenerationMetadata(nativeImage.revisedPrompt()); + return new ImageGeneration(image, metadata); + }).toList(); + ImageResponseMetadata openAiImageResponseMetadata = OpenAiOfficialImageResponseMetadata + .from(images); + ImageResponse imageResponse = new ImageResponse(imageGenerations, openAiImageResponseMetadata); + observationContext.setResponse(imageResponse); + return imageResponse; + })); + } + + /** + * Use the provided convention for reporting observation data + * @param observationConvention The provided convention + */ + public void setObservationConvention(ImageModelObservationConvention observationConvention) { + Assert.notNull(observationConvention, "observationConvention cannot be null"); + this.observationConvention = observationConvention; + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageOptions.java index 651de12cca0..bb36e4199dd 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageOptions.java @@ -29,299 +29,299 @@ */ public class OpenAiOfficialImageOptions extends AbstractOpenAiOfficialOptions implements ImageOptions { - public static final String DEFAULT_IMAGE_MODEL = ImageModel.DALL_E_3.toString(); - - /** - * The number of images to generate. Must be between 1 and 10. For dall-e-3, only n=1 - * is supported. - */ - private Integer n; - - /** - * The width of the generated images. Must be one of 256, 512, or 1024 for dall-e-2. - */ - private Integer width; - - /** - * The height of the generated images. Must be one of 256, 512, or 1024 for dall-e-2. - */ - private Integer height; - - /** - * The quality of the image that will be generated. hd creates images with finer - * details and greater consistency across the image. This param is only supported for - * dall-e-3. standard or hd - */ - private String quality; - - /** - * The format in which the generated images are returned. Must be one of url or - * b64_json. - */ - private String responseFormat; - - /** - * The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024 for - * dall-e-2. Must be one of 1024x1024, 1792x1024, or 1024x1792 for dall-e-3 models. - */ - private String size; - - /** - * The style of the generated images. Must be one of vivid or natural. Vivid causes - * the model to lean towards generating hyper-real and dramatic images. Natural causes - * the model to produce more natural, less hyper-real looking images. This param is - * only supported for dall-e-3. natural or vivid - */ - private String style; - - /** - * A unique identifier representing your end-user, which can help OpenAI to monitor - * and detect abuse. - */ - private String user; - - public static Builder builder() { - return new Builder(); - } - - @Override - public Integer getN() { - return this.n; - } - - public void setN(Integer n) { - this.n = n; - } - - @Override - public Integer getWidth() { - return this.width; - } - - public void setWidth(Integer width) { - this.width = width; - this.size = this.width + "x" + this.height; - } - - @Override - public Integer getHeight() { - return this.height; - } - - public void setHeight(Integer height) { - this.height = height; - this.size = this.width + "x" + this.height; - } - - @Override - public String getResponseFormat() { - return this.responseFormat; - } - - public void setResponseFormat(String responseFormat) { - this.responseFormat = responseFormat; - } - - public String getSize() { - if (this.size != null) { - return this.size; - } - return (this.width != null && this.height != null) ? this.width + "x" + this.height : null; - } - - public void setSize(String size) { - this.size = size; - } - - public String getUser() { - return this.user; - } - - public void setUser(String user) { - this.user = user; - } - - public String getQuality() { - return this.quality; - } - - public void setQuality(String quality) { - this.quality = quality; - } - - @Override - public String getStyle() { - return this.style; - } - - public void setStyle(String style) { - this.style = style; - } - - @Override - public boolean equals(Object o) { - if (o == null || getClass() != o.getClass()) - return false; - OpenAiOfficialImageOptions that = (OpenAiOfficialImageOptions) o; - return Objects.equals(n, that.n) && Objects.equals(width, that.width) && Objects.equals(height, that.height) - && Objects.equals(quality, that.quality) && Objects.equals(responseFormat, that.responseFormat) - && Objects.equals(size, that.size) && Objects.equals(style, that.style) - && Objects.equals(user, that.user); - } - - @Override - public int hashCode() { - return Objects.hash(n, width, height, quality, responseFormat, size, style, user); - } - - @Override - public String toString() { - return "OpenAiOfficialImageOptions{" + "n=" + n + ", width=" + width + ", height=" + height + ", quality='" - + quality + '\'' + ", responseFormat='" + responseFormat + '\'' + ", size='" + size + '\'' + ", style='" - + style + '\'' + ", user='" + user + '\'' + '}'; - } - - public ImageGenerateParams toOpenAiImageGenerateParams(ImagePrompt imagePrompt) { - if (imagePrompt.getInstructions().isEmpty()) { - throw new IllegalArgumentException("Image prompt instructions cannot be empty"); - } - - String prompt = imagePrompt.getInstructions().get(0).getText(); - ImageGenerateParams.Builder builder = ImageGenerateParams.builder().prompt(prompt); - - // Use deployment name if available (for Azure AI Foundry), otherwise use model - // name - if (this.getDeploymentName() != null) { - builder.model(this.getDeploymentName()); - } - else if (this.getModel() != null) { - builder.model(this.getModel()); - } - - if (this.getN() != null) { - builder.n(this.getN().longValue()); - } - if (this.getQuality() != null) { - builder.quality(ImageGenerateParams.Quality.of(this.getQuality().toLowerCase())); - } - if (this.getResponseFormat() != null) { - builder.responseFormat(ImageGenerateParams.ResponseFormat.of(this.getResponseFormat().toLowerCase())); - } - if (this.getSize() != null) { - builder.size(ImageGenerateParams.Size.of(this.getSize())); - } - if (this.getStyle() != null) { - builder.style(ImageGenerateParams.Style.of(this.getStyle().toLowerCase())); - } - if (this.getUser() != null) { - builder.user(this.getUser()); - } - - return builder.build(); - } - - public static final class Builder { - - private final OpenAiOfficialImageOptions options; - - private Builder() { - this.options = new OpenAiOfficialImageOptions(); - } - - public Builder from(OpenAiOfficialImageOptions fromOptions) { - this.options.setN(fromOptions.getN()); - this.options.setModel(fromOptions.getModel()); - this.options.setDeploymentName(fromOptions.getDeploymentName()); - this.options.setWidth(fromOptions.getWidth()); - this.options.setHeight(fromOptions.getHeight()); - this.options.setQuality(fromOptions.getQuality()); - this.options.setResponseFormat(fromOptions.getResponseFormat()); - this.options.setSize(fromOptions.getSize()); - this.options.setStyle(fromOptions.getStyle()); - this.options.setUser(fromOptions.getUser()); - return this; - } - - public Builder merge(ImageOptions from) { - if (from instanceof OpenAiOfficialImageOptions castFrom) { - if (castFrom.getN() != null) { - this.options.setN(castFrom.getN()); - } - if (castFrom.getModel() != null) { - this.options.setModel(castFrom.getModel()); - } - if (castFrom.getDeploymentName() != null) { - this.options.setDeploymentName(castFrom.getDeploymentName()); - } - if (castFrom.getWidth() != null) { - this.options.setWidth(castFrom.getWidth()); - } - if (castFrom.getHeight() != null) { - this.options.setHeight(castFrom.getHeight()); - } - if (castFrom.getQuality() != null) { - this.options.setQuality(castFrom.getQuality()); - } - if (castFrom.getResponseFormat() != null) { - this.options.setResponseFormat(castFrom.getResponseFormat()); - } - if (castFrom.getSize() != null) { - this.options.setSize(castFrom.getSize()); - } - if (castFrom.getStyle() != null) { - this.options.setStyle(castFrom.getStyle()); - } - if (castFrom.getUser() != null) { - this.options.setUser(castFrom.getUser()); - } - } - return this; - } - - public Builder N(Integer n) { - this.options.setN(n); - return this; - } - - public Builder model(String model) { - this.options.setModel(model); - return this; - } - - public Builder deploymentName(String deploymentName) { - this.options.setDeploymentName(deploymentName); - return this; - } - - public Builder responseFormat(String responseFormat) { - this.options.setResponseFormat(responseFormat); - return this; - } - - public Builder width(Integer width) { - this.options.setWidth(width); - return this; - } - - public Builder height(Integer height) { - this.options.setHeight(height); - return this; - } - - public Builder user(String user) { - this.options.setUser(user); - return this; - } - - public Builder style(String style) { - this.options.setStyle(style); - return this; - } - - public OpenAiOfficialImageOptions build() { - return this.options; - } - - } + public static final String DEFAULT_IMAGE_MODEL = ImageModel.DALL_E_3.toString(); + + /** + * The number of images to generate. Must be between 1 and 10. For dall-e-3, only n=1 + * is supported. + */ + private Integer n; + + /** + * The width of the generated images. Must be one of 256, 512, or 1024 for dall-e-2. + */ + private Integer width; + + /** + * The height of the generated images. Must be one of 256, 512, or 1024 for dall-e-2. + */ + private Integer height; + + /** + * The quality of the image that will be generated. hd creates images with finer + * details and greater consistency across the image. This param is only supported for + * dall-e-3. standard or hd + */ + private String quality; + + /** + * The format in which the generated images are returned. Must be one of url or + * b64_json. + */ + private String responseFormat; + + /** + * The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024 for + * dall-e-2. Must be one of 1024x1024, 1792x1024, or 1024x1792 for dall-e-3 models. + */ + private String size; + + /** + * The style of the generated images. Must be one of vivid or natural. Vivid causes + * the model to lean towards generating hyper-real and dramatic images. Natural causes + * the model to produce more natural, less hyper-real looking images. This param is + * only supported for dall-e-3. natural or vivid + */ + private String style; + + /** + * A unique identifier representing your end-user, which can help OpenAI to monitor + * and detect abuse. + */ + private String user; + + public static Builder builder() { + return new Builder(); + } + + @Override + public Integer getN() { + return this.n; + } + + public void setN(Integer n) { + this.n = n; + } + + @Override + public Integer getWidth() { + return this.width; + } + + public void setWidth(Integer width) { + this.width = width; + this.size = this.width + "x" + this.height; + } + + @Override + public Integer getHeight() { + return this.height; + } + + public void setHeight(Integer height) { + this.height = height; + this.size = this.width + "x" + this.height; + } + + @Override + public String getResponseFormat() { + return this.responseFormat; + } + + public void setResponseFormat(String responseFormat) { + this.responseFormat = responseFormat; + } + + public String getSize() { + if (this.size != null) { + return this.size; + } + return (this.width != null && this.height != null) ? this.width + "x" + this.height : null; + } + + public void setSize(String size) { + this.size = size; + } + + public String getUser() { + return this.user; + } + + public void setUser(String user) { + this.user = user; + } + + public String getQuality() { + return this.quality; + } + + public void setQuality(String quality) { + this.quality = quality; + } + + @Override + public String getStyle() { + return this.style; + } + + public void setStyle(String style) { + this.style = style; + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) + return false; + OpenAiOfficialImageOptions that = (OpenAiOfficialImageOptions) o; + return Objects.equals(n, that.n) && Objects.equals(width, that.width) && Objects.equals(height, that.height) + && Objects.equals(quality, that.quality) && Objects.equals(responseFormat, that.responseFormat) + && Objects.equals(size, that.size) && Objects.equals(style, that.style) + && Objects.equals(user, that.user); + } + + @Override + public int hashCode() { + return Objects.hash(n, width, height, quality, responseFormat, size, style, user); + } + + @Override + public String toString() { + return "OpenAiOfficialImageOptions{" + "n=" + n + ", width=" + width + ", height=" + height + ", quality='" + + quality + '\'' + ", responseFormat='" + responseFormat + '\'' + ", size='" + size + '\'' + ", style='" + + style + '\'' + ", user='" + user + '\'' + '}'; + } + + public ImageGenerateParams toOpenAiImageGenerateParams(ImagePrompt imagePrompt) { + if (imagePrompt.getInstructions().isEmpty()) { + throw new IllegalArgumentException("Image prompt instructions cannot be empty"); + } + + String prompt = imagePrompt.getInstructions().get(0).getText(); + ImageGenerateParams.Builder builder = ImageGenerateParams.builder().prompt(prompt); + + // Use deployment name if available (for Azure AI Foundry), otherwise use model + // name + if (this.getDeploymentName() != null) { + builder.model(this.getDeploymentName()); + } + else if (this.getModel() != null) { + builder.model(this.getModel()); + } + + if (this.getN() != null) { + builder.n(this.getN().longValue()); + } + if (this.getQuality() != null) { + builder.quality(ImageGenerateParams.Quality.of(this.getQuality().toLowerCase())); + } + if (this.getResponseFormat() != null) { + builder.responseFormat(ImageGenerateParams.ResponseFormat.of(this.getResponseFormat().toLowerCase())); + } + if (this.getSize() != null) { + builder.size(ImageGenerateParams.Size.of(this.getSize())); + } + if (this.getStyle() != null) { + builder.style(ImageGenerateParams.Style.of(this.getStyle().toLowerCase())); + } + if (this.getUser() != null) { + builder.user(this.getUser()); + } + + return builder.build(); + } + + public static final class Builder { + + private final OpenAiOfficialImageOptions options; + + private Builder() { + this.options = new OpenAiOfficialImageOptions(); + } + + public Builder from(OpenAiOfficialImageOptions fromOptions) { + this.options.setN(fromOptions.getN()); + this.options.setModel(fromOptions.getModel()); + this.options.setDeploymentName(fromOptions.getDeploymentName()); + this.options.setWidth(fromOptions.getWidth()); + this.options.setHeight(fromOptions.getHeight()); + this.options.setQuality(fromOptions.getQuality()); + this.options.setResponseFormat(fromOptions.getResponseFormat()); + this.options.setSize(fromOptions.getSize()); + this.options.setStyle(fromOptions.getStyle()); + this.options.setUser(fromOptions.getUser()); + return this; + } + + public Builder merge(ImageOptions from) { + if (from instanceof OpenAiOfficialImageOptions castFrom) { + if (castFrom.getN() != null) { + this.options.setN(castFrom.getN()); + } + if (castFrom.getModel() != null) { + this.options.setModel(castFrom.getModel()); + } + if (castFrom.getDeploymentName() != null) { + this.options.setDeploymentName(castFrom.getDeploymentName()); + } + if (castFrom.getWidth() != null) { + this.options.setWidth(castFrom.getWidth()); + } + if (castFrom.getHeight() != null) { + this.options.setHeight(castFrom.getHeight()); + } + if (castFrom.getQuality() != null) { + this.options.setQuality(castFrom.getQuality()); + } + if (castFrom.getResponseFormat() != null) { + this.options.setResponseFormat(castFrom.getResponseFormat()); + } + if (castFrom.getSize() != null) { + this.options.setSize(castFrom.getSize()); + } + if (castFrom.getStyle() != null) { + this.options.setStyle(castFrom.getStyle()); + } + if (castFrom.getUser() != null) { + this.options.setUser(castFrom.getUser()); + } + } + return this; + } + + public Builder N(Integer n) { + this.options.setN(n); + return this; + } + + public Builder model(String model) { + this.options.setModel(model); + return this; + } + + public Builder deploymentName(String deploymentName) { + this.options.setDeploymentName(deploymentName); + return this; + } + + public Builder responseFormat(String responseFormat) { + this.options.setResponseFormat(responseFormat); + return this; + } + + public Builder width(Integer width) { + this.options.setWidth(width); + return this; + } + + public Builder height(Integer height) { + this.options.setHeight(height); + return this; + } + + public Builder user(String user) { + this.options.setUser(user); + return this; + } + + public Builder style(String style) { + this.options.setStyle(style); + return this; + } + + public OpenAiOfficialImageOptions build() { + return this.options; + } + + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageGenerationMetadata.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageGenerationMetadata.java index 742a2d95f56..7e446e3eac3 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageGenerationMetadata.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageGenerationMetadata.java @@ -28,40 +28,48 @@ */ public class OpenAiOfficialImageGenerationMetadata implements ImageGenerationMetadata { - private final String revisedPrompt; + private final String revisedPrompt; - public OpenAiOfficialImageGenerationMetadata(Optional revisedPrompt) { - if (revisedPrompt.isPresent()) { - this.revisedPrompt = revisedPrompt.get(); - } - else { - this.revisedPrompt = null; - } - } + /** + * Creates a new OpenAiOfficialImageGenerationMetadata. + * @param revisedPrompt the revised prompt used for generation + */ + public OpenAiOfficialImageGenerationMetadata(Optional revisedPrompt) { + if (revisedPrompt.isPresent()) { + this.revisedPrompt = revisedPrompt.get(); + } + else { + this.revisedPrompt = null; + } + } - public String getRevisedPrompt() { - return this.revisedPrompt; - } + /** + * Gets the revised prompt that was used for image generation. + * @return the revised prompt, or null if not available + */ + public String getRevisedPrompt() { + return this.revisedPrompt; + } - @Override - public String toString() { - return "OpenAiOfficialImageGenerationMetadata{" + "revisedPrompt='" + revisedPrompt + '\'' + '}'; - } + @Override + public String toString() { + return "OpenAiOfficialImageGenerationMetadata{" + "revisedPrompt='" + revisedPrompt + '\'' + '}'; + } - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof OpenAiOfficialImageGenerationMetadata that)) { - return false; - } - return Objects.equals(this.revisedPrompt, that.revisedPrompt); - } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof OpenAiOfficialImageGenerationMetadata that)) { + return false; + } + return Objects.equals(this.revisedPrompt, that.revisedPrompt); + } - @Override - public int hashCode() { - return Objects.hash(this.revisedPrompt); - } + @Override + public int hashCode() { + return Objects.hash(this.revisedPrompt); + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageResponseMetadata.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageResponseMetadata.java index 2ecf121d095..34567225799 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageResponseMetadata.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageResponseMetadata.java @@ -29,41 +29,50 @@ */ public class OpenAiOfficialImageResponseMetadata extends ImageResponseMetadata { - private final Long created; + private final Long created; - protected OpenAiOfficialImageResponseMetadata(Long created) { - this.created = created; - } + /** + * Creates a new OpenAiOfficialImageResponseMetadata. + * @param created the creation timestamp + */ + protected OpenAiOfficialImageResponseMetadata(Long created) { + this.created = created; + } - public static OpenAiOfficialImageResponseMetadata from(ImagesResponse imagesResponse) { - Assert.notNull(imagesResponse, "imagesResponse must not be null"); - return new OpenAiOfficialImageResponseMetadata(imagesResponse.created()); - } + /** + * Creates metadata from an ImagesResponse. + * @param imagesResponse the OpenAI images response + * @return the metadata instance + */ + public static OpenAiOfficialImageResponseMetadata from(ImagesResponse imagesResponse) { + Assert.notNull(imagesResponse, "imagesResponse must not be null"); + return new OpenAiOfficialImageResponseMetadata(imagesResponse.created()); + } - @Override - public Long getCreated() { - return this.created; - } + @Override + public Long getCreated() { + return this.created; + } - @Override - public String toString() { - return "OpenAiOfficialImageResponseMetadata{" + "created=" + created + '}'; - } + @Override + public String toString() { + return "OpenAiOfficialImageResponseMetadata{" + "created=" + created + '}'; + } - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof OpenAiOfficialImageResponseMetadata that)) { - return false; - } - return Objects.equals(this.created, that.created); - } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof OpenAiOfficialImageResponseMetadata that)) { + return false; + } + return Objects.equals(this.created, that.created); + } - @Override - public int hashCode() { - return Objects.hash(this.created); - } + @Override + public int hashCode() { + return Objects.hash(this.created); + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/package-info.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/package-info.java new file mode 100644 index 00000000000..dc9412f493b --- /dev/null +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/package-info.java @@ -0,0 +1,26 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Metadata classes for OpenAI Official model responses. + *

+ * This package contains metadata implementations for chat, embedding, + * and image model responses. + * + * @author Julien Dubois + */ +package org.springframework.ai.openaiofficial.metadata; + diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/package-info.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/package-info.java new file mode 100644 index 00000000000..e3277b6d0a8 --- /dev/null +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/package-info.java @@ -0,0 +1,26 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Spring AI integration with the official OpenAI Java SDK. + *

+ * This package provides chat, embedding, and image model implementations + * using the official OpenAI Java client library. + * + * @author Julien Dubois + */ +package org.springframework.ai.openaiofficial; + diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java index 3215ffbb937..e99f8a72c6b 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java @@ -31,9 +31,9 @@ */ class AzureInternalOpenAiOfficialHelper { - static Credential getAzureCredential() { - return BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier( - new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default")); - } + static Credential getAzureCredential() { + return BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier( + new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default")); + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java index 37d7011666a..50f9e44cb1b 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java @@ -43,233 +43,233 @@ */ public class OpenAiOfficialSetup { - static final String OPENAI_URL = "https://api.openai.com/v1"; - static final String OPENAI_API_KEY = "OPENAI_API_KEY"; - static final String AZURE_OPENAI_KEY = "AZURE_OPENAI_KEY"; - static final String GITHUB_MODELS_URL = "https://models.inference.ai.azure.com"; - static final String GITHUB_TOKEN = "GITHUB_TOKEN"; - static final String DEFAULT_USER_AGENT = "spring-ai-openai-official"; - - private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialSetup.class); - - private static final Duration DEFAULT_DURATION = ofSeconds(60); - - private static final int DEFAULT_MAX_RETRIES = 3; - - public enum ModelHost { - - OPENAI, AZURE_OPENAI, GITHUB_MODELS - - } - - public static OpenAIClient setupSyncClient(String baseUrl, String apiKey, Credential credential, - String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAiServiceVersion, String organizationId, - boolean isAzure, boolean isGitHubModels, String modelName, Duration timeout, Integer maxRetries, - Proxy proxy, Map customHeaders) { - - baseUrl = detectBaseUrlFromEnv(baseUrl); - var modelHost = detectModelHost(isAzure, isGitHubModels, baseUrl, azureDeploymentName, - azureOpenAiServiceVersion); - if (timeout == null) { - timeout = DEFAULT_DURATION; - } - if (maxRetries == null) { - maxRetries = DEFAULT_MAX_RETRIES; - } - OpenAIOkHttpClient.Builder builder = OpenAIOkHttpClient.builder(); - builder - .baseUrl(calculateBaseUrl(baseUrl, modelHost, modelName, azureDeploymentName, azureOpenAiServiceVersion)); - - String calculatedApiKey = apiKey != null ? apiKey : detectApiKey(modelHost); - if (calculatedApiKey != null) { - builder.apiKey(calculatedApiKey); - } - else { - if (credential != null) { - builder.credential(credential); - } - else if (modelHost == ModelHost.AZURE_OPENAI) { - // If no API key is provided for Azure OpenAI, we try to use passwordless - // authentication - builder.credential(azureAuthentication()); - } - } - builder.organization(organizationId); - - if (azureOpenAiServiceVersion != null) { - builder.azureServiceVersion(azureOpenAiServiceVersion); - } - - if (proxy != null) { - builder.proxy(proxy); - } - - builder.putHeader("User-Agent", DEFAULT_USER_AGENT); - if (customHeaders != null) { - builder.putAllHeaders(customHeaders.entrySet() - .stream() - .collect(Collectors.toMap(Map.Entry::getKey, entry -> Collections.singletonList(entry.getValue())))); - } - - builder.timeout(timeout); - builder.maxRetries(maxRetries); - return builder.build(); - } - - /** - * The asynchronous client setup is the same as the synchronous one in the OpenAI Java - * SDK, but uses a different client implementation. - */ - public static OpenAIClientAsync setupAsyncClient(String baseUrl, String apiKey, Credential credential, - String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAiServiceVersion, String organizationId, - boolean isAzure, boolean isGitHubModels, String modelName, Duration timeout, Integer maxRetries, - Proxy proxy, Map customHeaders) { - - baseUrl = detectBaseUrlFromEnv(baseUrl); - var modelHost = detectModelHost(isAzure, isGitHubModels, baseUrl, azureDeploymentName, - azureOpenAiServiceVersion); - if (timeout == null) { - timeout = DEFAULT_DURATION; - } - if (maxRetries == null) { - maxRetries = DEFAULT_MAX_RETRIES; - } - OpenAIOkHttpClientAsync.Builder builder = OpenAIOkHttpClientAsync.builder(); - builder - .baseUrl(calculateBaseUrl(baseUrl, modelHost, modelName, azureDeploymentName, azureOpenAiServiceVersion)); - - String calculatedApiKey = apiKey != null ? apiKey : detectApiKey(modelHost); - if (calculatedApiKey != null) { - builder.apiKey(calculatedApiKey); - } - else { - if (credential != null) { - builder.credential(credential); - } - else if (modelHost == ModelHost.AZURE_OPENAI) { - // If no API key is provided for Azure OpenAI, we try to use passwordless - // authentication - builder.credential(azureAuthentication()); - } - } - builder.organization(organizationId); - - if (azureOpenAiServiceVersion != null) { - builder.azureServiceVersion(azureOpenAiServiceVersion); - } - - if (proxy != null) { - builder.proxy(proxy); - } - - builder.putHeader("User-Agent", DEFAULT_USER_AGENT); - if (customHeaders != null) { - builder.putAllHeaders(customHeaders.entrySet() - .stream() - .collect(Collectors.toMap(Map.Entry::getKey, entry -> Collections.singletonList(entry.getValue())))); - } - - builder.timeout(timeout); - builder.maxRetries(maxRetries); - return builder.build(); - } - - static String detectBaseUrlFromEnv(String baseUrl) { - if (baseUrl == null) { - var openAiBaseUrl = System.getenv("OPENAI_BASE_URL"); - if (openAiBaseUrl != null) { - baseUrl = openAiBaseUrl; - logger.debug("OpenAI Base URL detected from environment variable OPENAI_BASE_URL."); - } - var azureOpenAiBaseUrl = System.getenv("AZURE_OPENAI_BASE_URL"); - if (azureOpenAiBaseUrl != null) { - baseUrl = azureOpenAiBaseUrl; - logger.debug("Azure OpenAI Base URL detected from environment variable AZURE_OPENAI_BASE_URL."); - } - } - return baseUrl; - } - - static ModelHost detectModelHost(boolean isAzure, boolean isGitHubModels, String baseUrl, - String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAIServiceVersion) { - - if (isAzure) { - return ModelHost.AZURE_OPENAI; // Forced by the user - } - if (isGitHubModels) { - return ModelHost.GITHUB_MODELS; // Forced by the user - } - if (baseUrl != null) { - if (baseUrl.endsWith("openai.azure.com") || baseUrl.endsWith("openai.azure.com/") - || baseUrl.endsWith("cognitiveservices.azure.com") - || baseUrl.endsWith("cognitiveservices.azure.com/")) { - return ModelHost.AZURE_OPENAI; - } - else if (baseUrl.startsWith(GITHUB_MODELS_URL)) { - return ModelHost.GITHUB_MODELS; - } - } - if (azureDeploymentName != null || azureOpenAIServiceVersion != null) { - return ModelHost.AZURE_OPENAI; - } - return ModelHost.OPENAI; - } - - static String calculateBaseUrl(String baseUrl, ModelHost modelHost, String modelName, String azureDeploymentName, - AzureOpenAIServiceVersion azureOpenAiServiceVersion) { - - if (modelHost == ModelHost.OPENAI) { - if (baseUrl == null || baseUrl.isBlank()) { - return OPENAI_URL; - } - return baseUrl; - } - else if (modelHost == ModelHost.GITHUB_MODELS) { - return GITHUB_MODELS_URL; - } - else if (modelHost == ModelHost.AZURE_OPENAI) { - // Using Azure OpenAI - String tmpUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl; - // If the Azure deployment name is not configured, the model name will be used - // by default by the OpenAI Java - // SDK - if (azureDeploymentName != null && !azureDeploymentName.equals(modelName)) { - tmpUrl += "/openai/deployments/" + azureDeploymentName; - } - if (azureOpenAiServiceVersion != null) { - tmpUrl += "?api-version=" + azureOpenAiServiceVersion.value(); - } - return tmpUrl; - } - else { - throw new IllegalArgumentException("Unknown model host: " + modelHost); - } - } - - static Credential azureAuthentication() { - try { - return AzureInternalOpenAiOfficialHelper.getAzureCredential(); - } - catch (NoClassDefFoundError e) { - throw new IllegalArgumentException("Azure OpenAI was detected, but no credential was provided. " - + "If you want to use passwordless authentication, you need to add the Azure Identity library (groupId=`com.azure`, artifactId=`azure-identity`) to your classpath."); - } - } - - static String detectApiKey(ModelHost modelHost) { - if (modelHost == ModelHost.OPENAI && System.getenv(OPENAI_API_KEY) != null) { - return System.getenv(OPENAI_API_KEY); - } - else if (modelHost == ModelHost.AZURE_OPENAI && System.getenv(AZURE_OPENAI_KEY) != null) { - return System.getenv(AZURE_OPENAI_KEY); - } - else if (modelHost == ModelHost.AZURE_OPENAI && System.getenv(OPENAI_API_KEY) != null) { - return System.getenv(OPENAI_API_KEY); - } - else if (modelHost == ModelHost.GITHUB_MODELS && System.getenv(GITHUB_TOKEN) != null) { - return System.getenv(GITHUB_TOKEN); - } - return null; - } + static final String OPENAI_URL = "https://api.openai.com/v1"; + static final String OPENAI_API_KEY = "OPENAI_API_KEY"; + static final String AZURE_OPENAI_KEY = "AZURE_OPENAI_KEY"; + static final String GITHUB_MODELS_URL = "https://models.inference.ai.azure.com"; + static final String GITHUB_TOKEN = "GITHUB_TOKEN"; + static final String DEFAULT_USER_AGENT = "spring-ai-openai-official"; + + private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialSetup.class); + + private static final Duration DEFAULT_DURATION = ofSeconds(60); + + private static final int DEFAULT_MAX_RETRIES = 3; + + public enum ModelHost { + + OPENAI, AZURE_OPENAI, GITHUB_MODELS + + } + + public static OpenAIClient setupSyncClient(String baseUrl, String apiKey, Credential credential, + String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAiServiceVersion, String organizationId, + boolean isAzure, boolean isGitHubModels, String modelName, Duration timeout, Integer maxRetries, + Proxy proxy, Map customHeaders) { + + baseUrl = detectBaseUrlFromEnv(baseUrl); + var modelHost = detectModelHost(isAzure, isGitHubModels, baseUrl, azureDeploymentName, + azureOpenAiServiceVersion); + if (timeout == null) { + timeout = DEFAULT_DURATION; + } + if (maxRetries == null) { + maxRetries = DEFAULT_MAX_RETRIES; + } + OpenAIOkHttpClient.Builder builder = OpenAIOkHttpClient.builder(); + builder + .baseUrl(calculateBaseUrl(baseUrl, modelHost, modelName, azureDeploymentName, azureOpenAiServiceVersion)); + + String calculatedApiKey = apiKey != null ? apiKey : detectApiKey(modelHost); + if (calculatedApiKey != null) { + builder.apiKey(calculatedApiKey); + } + else { + if (credential != null) { + builder.credential(credential); + } + else if (modelHost == ModelHost.AZURE_OPENAI) { + // If no API key is provided for Azure OpenAI, we try to use passwordless + // authentication + builder.credential(azureAuthentication()); + } + } + builder.organization(organizationId); + + if (azureOpenAiServiceVersion != null) { + builder.azureServiceVersion(azureOpenAiServiceVersion); + } + + if (proxy != null) { + builder.proxy(proxy); + } + + builder.putHeader("User-Agent", DEFAULT_USER_AGENT); + if (customHeaders != null) { + builder.putAllHeaders(customHeaders.entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> Collections.singletonList(entry.getValue())))); + } + + builder.timeout(timeout); + builder.maxRetries(maxRetries); + return builder.build(); + } + + /** + * The asynchronous client setup is the same as the synchronous one in the OpenAI Java + * SDK, but uses a different client implementation. + */ + public static OpenAIClientAsync setupAsyncClient(String baseUrl, String apiKey, Credential credential, + String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAiServiceVersion, String organizationId, + boolean isAzure, boolean isGitHubModels, String modelName, Duration timeout, Integer maxRetries, + Proxy proxy, Map customHeaders) { + + baseUrl = detectBaseUrlFromEnv(baseUrl); + var modelHost = detectModelHost(isAzure, isGitHubModels, baseUrl, azureDeploymentName, + azureOpenAiServiceVersion); + if (timeout == null) { + timeout = DEFAULT_DURATION; + } + if (maxRetries == null) { + maxRetries = DEFAULT_MAX_RETRIES; + } + OpenAIOkHttpClientAsync.Builder builder = OpenAIOkHttpClientAsync.builder(); + builder + .baseUrl(calculateBaseUrl(baseUrl, modelHost, modelName, azureDeploymentName, azureOpenAiServiceVersion)); + + String calculatedApiKey = apiKey != null ? apiKey : detectApiKey(modelHost); + if (calculatedApiKey != null) { + builder.apiKey(calculatedApiKey); + } + else { + if (credential != null) { + builder.credential(credential); + } + else if (modelHost == ModelHost.AZURE_OPENAI) { + // If no API key is provided for Azure OpenAI, we try to use passwordless + // authentication + builder.credential(azureAuthentication()); + } + } + builder.organization(organizationId); + + if (azureOpenAiServiceVersion != null) { + builder.azureServiceVersion(azureOpenAiServiceVersion); + } + + if (proxy != null) { + builder.proxy(proxy); + } + + builder.putHeader("User-Agent", DEFAULT_USER_AGENT); + if (customHeaders != null) { + builder.putAllHeaders(customHeaders.entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> Collections.singletonList(entry.getValue())))); + } + + builder.timeout(timeout); + builder.maxRetries(maxRetries); + return builder.build(); + } + + static String detectBaseUrlFromEnv(String baseUrl) { + if (baseUrl == null) { + var openAiBaseUrl = System.getenv("OPENAI_BASE_URL"); + if (openAiBaseUrl != null) { + baseUrl = openAiBaseUrl; + logger.debug("OpenAI Base URL detected from environment variable OPENAI_BASE_URL."); + } + var azureOpenAiBaseUrl = System.getenv("AZURE_OPENAI_BASE_URL"); + if (azureOpenAiBaseUrl != null) { + baseUrl = azureOpenAiBaseUrl; + logger.debug("Azure OpenAI Base URL detected from environment variable AZURE_OPENAI_BASE_URL."); + } + } + return baseUrl; + } + + static ModelHost detectModelHost(boolean isAzure, boolean isGitHubModels, String baseUrl, + String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAIServiceVersion) { + + if (isAzure) { + return ModelHost.AZURE_OPENAI; // Forced by the user + } + if (isGitHubModels) { + return ModelHost.GITHUB_MODELS; // Forced by the user + } + if (baseUrl != null) { + if (baseUrl.endsWith("openai.azure.com") || baseUrl.endsWith("openai.azure.com/") + || baseUrl.endsWith("cognitiveservices.azure.com") + || baseUrl.endsWith("cognitiveservices.azure.com/")) { + return ModelHost.AZURE_OPENAI; + } + else if (baseUrl.startsWith(GITHUB_MODELS_URL)) { + return ModelHost.GITHUB_MODELS; + } + } + if (azureDeploymentName != null || azureOpenAIServiceVersion != null) { + return ModelHost.AZURE_OPENAI; + } + return ModelHost.OPENAI; + } + + static String calculateBaseUrl(String baseUrl, ModelHost modelHost, String modelName, String azureDeploymentName, + AzureOpenAIServiceVersion azureOpenAiServiceVersion) { + + if (modelHost == ModelHost.OPENAI) { + if (baseUrl == null || baseUrl.isBlank()) { + return OPENAI_URL; + } + return baseUrl; + } + else if (modelHost == ModelHost.GITHUB_MODELS) { + return GITHUB_MODELS_URL; + } + else if (modelHost == ModelHost.AZURE_OPENAI) { + // Using Azure OpenAI + String tmpUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl; + // If the Azure deployment name is not configured, the model name will be used + // by default by the OpenAI Java + // SDK + if (azureDeploymentName != null && !azureDeploymentName.equals(modelName)) { + tmpUrl += "/openai/deployments/" + azureDeploymentName; + } + if (azureOpenAiServiceVersion != null) { + tmpUrl += "?api-version=" + azureOpenAiServiceVersion.value(); + } + return tmpUrl; + } + else { + throw new IllegalArgumentException("Unknown model host: " + modelHost); + } + } + + static Credential azureAuthentication() { + try { + return AzureInternalOpenAiOfficialHelper.getAzureCredential(); + } + catch (NoClassDefFoundError e) { + throw new IllegalArgumentException("Azure OpenAI was detected, but no credential was provided. " + + "If you want to use passwordless authentication, you need to add the Azure Identity library (groupId=`com.azure`, artifactId=`azure-identity`) to your classpath."); + } + } + + static String detectApiKey(ModelHost modelHost) { + if (modelHost == ModelHost.OPENAI && System.getenv(OPENAI_API_KEY) != null) { + return System.getenv(OPENAI_API_KEY); + } + else if (modelHost == ModelHost.AZURE_OPENAI && System.getenv(AZURE_OPENAI_KEY) != null) { + return System.getenv(AZURE_OPENAI_KEY); + } + else if (modelHost == ModelHost.AZURE_OPENAI && System.getenv(OPENAI_API_KEY) != null) { + return System.getenv(OPENAI_API_KEY); + } + else if (modelHost == ModelHost.GITHUB_MODELS && System.getenv(GITHUB_TOKEN) != null) { + return System.getenv(GITHUB_TOKEN); + } + return null; + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/package-info.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/package-info.java new file mode 100644 index 00000000000..202da167925 --- /dev/null +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/package-info.java @@ -0,0 +1,27 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Setup and configuration utilities for OpenAI Official clients. + *

+ * This package contains helper classes for configuring and setting up + * OpenAI clients for different environments including OpenAI, Azure OpenAI, + * and GitHub Models. + * + * @author Julien Dubois + */ +package org.springframework.ai.openaiofficial.setup; + diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatOptionsTests.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatOptionsTests.java index df9c4565e36..311361d314d 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatOptionsTests.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatOptionsTests.java @@ -710,4 +710,5 @@ void testTopKReturnsNull() { // TopK is not supported by OpenAI, should always return null assertThat(options.getTopK()).isNull(); } + } diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java index 50248975d31..d19b6e2ad6c 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java @@ -58,7 +58,7 @@ void setUp() { } @Test - void observationForImageOperation() { + void observationForImageOperation() throws InterruptedException { var options = OpenAiOfficialImageOptions.builder() .model(DALL_E_3.asString()) .height(1024) @@ -76,6 +76,8 @@ void observationForImageOperation() { ImageResponse imageResponse = this.imageModel.call(imagePrompt); assertThat(imageResponse.getResults()).hasSize(1); + Thread.sleep(100); // Wait for observation to be recorded + TestObservationRegistryAssert.assertThat(this.observationRegistry) .doesNotHaveAnyRemainingCurrentObservation() .hasObservationWithNameEqualTo(DefaultImageModelObservationConvention.DEFAULT_NAME) From 8c35a84450173b7079b7e1970707377f4fd50787 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Tue, 18 Nov 2025 22:00:39 +0100 Subject: [PATCH 24/49] Implementation of the OpenAI Java SDK - Update to OpenAI Java SDK 4.8.0 Signed-off-by: Julien Dubois --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 05ef393ca05..4d90bbcabcf 100644 --- a/pom.xml +++ b/pom.xml @@ -278,7 +278,7 @@ 4.0.0 4.3.4 1.0.0-beta.16 - 4.6.1 + 4.8.0 1.15.4 1.1.0 2.2.21 From 7eb40b93369e4ca869ccc890454663363938156e Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Wed, 19 Nov 2025 16:23:16 +0100 Subject: [PATCH 25/49] Implementation of the OpenAI Java SDK - Refactor ModelHost to ModelProvider Signed-off-by: Julien Dubois --- .../AzureInternalOpenAiOfficialHelper.java | 7 ++- .../setup/OpenAiOfficialSetup.java | 56 +++++++++---------- ...OpenAiOfficialImageModelObservationIT.java | 2 +- .../setup/OpenAiOfficialSetupTests.java | 34 +++++------ 4 files changed, 51 insertions(+), 48 deletions(-) diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java index e99f8a72c6b..cd335b45c5b 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java @@ -22,8 +22,11 @@ import com.openai.credential.Credential; /** - * Helps configure the OpenAI Java SDK, depending on the platform used. This code is - * inspired by LangChain4j's + * Specific configuration for authenticating on Azure. + * This is in a separate class to avoid needing the Azure SDK dependencies + * when not using Azure as a platform. + * + * This code is inspired by LangChain4j's * `dev.langchain4j.model.openaiofficial.AzureInternalOpenAiOfficialHelper` class, which * is coded by the same author (Julien Dubois, from Microsoft). * diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java index 50f9e44cb1b..ee46439eb14 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java @@ -56,7 +56,7 @@ public class OpenAiOfficialSetup { private static final int DEFAULT_MAX_RETRIES = 3; - public enum ModelHost { + public enum ModelProvider { OPENAI, AZURE_OPENAI, GITHUB_MODELS @@ -68,7 +68,7 @@ public static OpenAIClient setupSyncClient(String baseUrl, String apiKey, Creden Proxy proxy, Map customHeaders) { baseUrl = detectBaseUrlFromEnv(baseUrl); - var modelHost = detectModelHost(isAzure, isGitHubModels, baseUrl, azureDeploymentName, + var modelProvider = detectModelProvider(isAzure, isGitHubModels, baseUrl, azureDeploymentName, azureOpenAiServiceVersion); if (timeout == null) { timeout = DEFAULT_DURATION; @@ -78,9 +78,9 @@ public static OpenAIClient setupSyncClient(String baseUrl, String apiKey, Creden } OpenAIOkHttpClient.Builder builder = OpenAIOkHttpClient.builder(); builder - .baseUrl(calculateBaseUrl(baseUrl, modelHost, modelName, azureDeploymentName, azureOpenAiServiceVersion)); + .baseUrl(calculateBaseUrl(baseUrl, modelProvider, modelName, azureDeploymentName, azureOpenAiServiceVersion)); - String calculatedApiKey = apiKey != null ? apiKey : detectApiKey(modelHost); + String calculatedApiKey = apiKey != null ? apiKey : detectApiKey(modelProvider); if (calculatedApiKey != null) { builder.apiKey(calculatedApiKey); } @@ -88,7 +88,7 @@ public static OpenAIClient setupSyncClient(String baseUrl, String apiKey, Creden if (credential != null) { builder.credential(credential); } - else if (modelHost == ModelHost.AZURE_OPENAI) { + else if (modelProvider == ModelProvider.AZURE_OPENAI) { // If no API key is provided for Azure OpenAI, we try to use passwordless // authentication builder.credential(azureAuthentication()); @@ -126,7 +126,7 @@ public static OpenAIClientAsync setupAsyncClient(String baseUrl, String apiKey, Proxy proxy, Map customHeaders) { baseUrl = detectBaseUrlFromEnv(baseUrl); - var modelHost = detectModelHost(isAzure, isGitHubModels, baseUrl, azureDeploymentName, + var modelProvider = detectModelProvider(isAzure, isGitHubModels, baseUrl, azureDeploymentName, azureOpenAiServiceVersion); if (timeout == null) { timeout = DEFAULT_DURATION; @@ -136,9 +136,9 @@ public static OpenAIClientAsync setupAsyncClient(String baseUrl, String apiKey, } OpenAIOkHttpClientAsync.Builder builder = OpenAIOkHttpClientAsync.builder(); builder - .baseUrl(calculateBaseUrl(baseUrl, modelHost, modelName, azureDeploymentName, azureOpenAiServiceVersion)); + .baseUrl(calculateBaseUrl(baseUrl, modelProvider, modelName, azureDeploymentName, azureOpenAiServiceVersion)); - String calculatedApiKey = apiKey != null ? apiKey : detectApiKey(modelHost); + String calculatedApiKey = apiKey != null ? apiKey : detectApiKey(modelProvider); if (calculatedApiKey != null) { builder.apiKey(calculatedApiKey); } @@ -146,7 +146,7 @@ public static OpenAIClientAsync setupAsyncClient(String baseUrl, String apiKey, if (credential != null) { builder.credential(credential); } - else if (modelHost == ModelHost.AZURE_OPENAI) { + else if (modelProvider == ModelProvider.AZURE_OPENAI) { // If no API key is provided for Azure OpenAI, we try to use passwordless // authentication builder.credential(azureAuthentication()); @@ -190,44 +190,44 @@ static String detectBaseUrlFromEnv(String baseUrl) { return baseUrl; } - static ModelHost detectModelHost(boolean isAzure, boolean isGitHubModels, String baseUrl, - String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAIServiceVersion) { + static ModelProvider detectModelProvider(boolean isAzure, boolean isGitHubModels, String baseUrl, + String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAIServiceVersion) { if (isAzure) { - return ModelHost.AZURE_OPENAI; // Forced by the user + return ModelProvider.AZURE_OPENAI; // Forced by the user } if (isGitHubModels) { - return ModelHost.GITHUB_MODELS; // Forced by the user + return ModelProvider.GITHUB_MODELS; // Forced by the user } if (baseUrl != null) { if (baseUrl.endsWith("openai.azure.com") || baseUrl.endsWith("openai.azure.com/") || baseUrl.endsWith("cognitiveservices.azure.com") || baseUrl.endsWith("cognitiveservices.azure.com/")) { - return ModelHost.AZURE_OPENAI; + return ModelProvider.AZURE_OPENAI; } else if (baseUrl.startsWith(GITHUB_MODELS_URL)) { - return ModelHost.GITHUB_MODELS; + return ModelProvider.GITHUB_MODELS; } } if (azureDeploymentName != null || azureOpenAIServiceVersion != null) { - return ModelHost.AZURE_OPENAI; + return ModelProvider.AZURE_OPENAI; } - return ModelHost.OPENAI; + return ModelProvider.OPENAI; } - static String calculateBaseUrl(String baseUrl, ModelHost modelHost, String modelName, String azureDeploymentName, - AzureOpenAIServiceVersion azureOpenAiServiceVersion) { + static String calculateBaseUrl(String baseUrl, ModelProvider modelProvider, String modelName, String azureDeploymentName, + AzureOpenAIServiceVersion azureOpenAiServiceVersion) { - if (modelHost == ModelHost.OPENAI) { + if (modelProvider == ModelProvider.OPENAI) { if (baseUrl == null || baseUrl.isBlank()) { return OPENAI_URL; } return baseUrl; } - else if (modelHost == ModelHost.GITHUB_MODELS) { + else if (modelProvider == ModelProvider.GITHUB_MODELS) { return GITHUB_MODELS_URL; } - else if (modelHost == ModelHost.AZURE_OPENAI) { + else if (modelProvider == ModelProvider.AZURE_OPENAI) { // Using Azure OpenAI String tmpUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl; // If the Azure deployment name is not configured, the model name will be used @@ -242,7 +242,7 @@ else if (modelHost == ModelHost.AZURE_OPENAI) { return tmpUrl; } else { - throw new IllegalArgumentException("Unknown model host: " + modelHost); + throw new IllegalArgumentException("Unknown model host: " + modelProvider); } } @@ -256,17 +256,17 @@ static Credential azureAuthentication() { } } - static String detectApiKey(ModelHost modelHost) { - if (modelHost == ModelHost.OPENAI && System.getenv(OPENAI_API_KEY) != null) { + static String detectApiKey(ModelProvider modelProvider) { + if (modelProvider == ModelProvider.OPENAI && System.getenv(OPENAI_API_KEY) != null) { return System.getenv(OPENAI_API_KEY); } - else if (modelHost == ModelHost.AZURE_OPENAI && System.getenv(AZURE_OPENAI_KEY) != null) { + else if (modelProvider == ModelProvider.AZURE_OPENAI && System.getenv(AZURE_OPENAI_KEY) != null) { return System.getenv(AZURE_OPENAI_KEY); } - else if (modelHost == ModelHost.AZURE_OPENAI && System.getenv(OPENAI_API_KEY) != null) { + else if (modelProvider == ModelProvider.AZURE_OPENAI && System.getenv(OPENAI_API_KEY) != null) { return System.getenv(OPENAI_API_KEY); } - else if (modelHost == ModelHost.GITHUB_MODELS && System.getenv(GITHUB_TOKEN) != null) { + else if (modelProvider == ModelProvider.GITHUB_MODELS && System.getenv(GITHUB_TOKEN) != null) { return System.getenv(GITHUB_TOKEN); } return null; diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java index d19b6e2ad6c..1fbfeabbff5 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java @@ -76,7 +76,7 @@ void observationForImageOperation() throws InterruptedException { ImageResponse imageResponse = this.imageModel.call(imagePrompt); assertThat(imageResponse.getResults()).hasSize(1); - Thread.sleep(100); // Wait for observation to be recorded + Thread.sleep(200); // Wait for observation to be recorded TestObservationRegistryAssert.assertThat(this.observationRegistry) .doesNotHaveAnyRemainingCurrentObservation() diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetupTests.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetupTests.java index 101539a068d..c66b3d3ae97 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetupTests.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetupTests.java @@ -13,40 +13,40 @@ public class OpenAiOfficialSetupTests { @Test - void detectModelHost_returnsAzureOpenAI_whenAzureFlagIsTrue() { - OpenAiOfficialSetup.ModelHost result = OpenAiOfficialSetup.detectModelHost(true, false, null, null, null); + void detectModelProvider_returnsAzureOpenAI_whenAzureFlagIsTrue() { + OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(true, false, null, null, null); - assertEquals(OpenAiOfficialSetup.ModelHost.AZURE_OPENAI, result); + assertEquals(OpenAiOfficialSetup.ModelProvider.AZURE_OPENAI, result); } @Test - void detectModelHost_returnsGitHubModels_whenGitHubFlagIsTrue() { - OpenAiOfficialSetup.ModelHost result = OpenAiOfficialSetup.detectModelHost(false, true, null, null, null); + void detectModelProvider_returnsGitHubModels_whenGitHubFlagIsTrue() { + OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(false, true, null, null, null); - assertEquals(OpenAiOfficialSetup.ModelHost.GITHUB_MODELS, result); + assertEquals(OpenAiOfficialSetup.ModelProvider.GITHUB_MODELS, result); } @Test - void detectModelHost_returnsAzureOpenAI_whenBaseUrlMatchesAzure() { - OpenAiOfficialSetup.ModelHost result = OpenAiOfficialSetup.detectModelHost(false, false, + void detectModelProvider_returnsAzureOpenAI_whenBaseUrlMatchesAzure() { + OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(false, false, "https://example.openai.azure.com", null, null); - assertEquals(OpenAiOfficialSetup.ModelHost.AZURE_OPENAI, result); + assertEquals(OpenAiOfficialSetup.ModelProvider.AZURE_OPENAI, result); } @Test - void detectModelHost_returnsGitHubModels_whenBaseUrlMatchesGitHub() { - OpenAiOfficialSetup.ModelHost result = OpenAiOfficialSetup.detectModelHost(false, false, + void detectModelProvider_returnsGitHubModels_whenBaseUrlMatchesGitHub() { + OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(false, false, "https://models.inference.ai.azure.com", null, null); - assertEquals(OpenAiOfficialSetup.ModelHost.GITHUB_MODELS, result); + assertEquals(OpenAiOfficialSetup.ModelProvider.GITHUB_MODELS, result); } @Test - void detectModelHost_returnsOpenAI_whenNoConditionsMatch() { - OpenAiOfficialSetup.ModelHost result = OpenAiOfficialSetup.detectModelHost(false, false, null, null, null); + void detectModelProvider_returnsOpenAI_whenNoConditionsMatch() { + OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(false, false, null, null, null); - assertEquals(OpenAiOfficialSetup.ModelHost.OPENAI, result); + assertEquals(OpenAiOfficialSetup.ModelProvider.OPENAI, result); } @Test @@ -69,7 +69,7 @@ void setupSyncClient_appliesCustomHeaders_whenProvided() { @Test void calculateBaseUrl_returnsDefaultOpenAIUrl_whenBaseUrlIsNull() { - String result = OpenAiOfficialSetup.calculateBaseUrl(null, OpenAiOfficialSetup.ModelHost.OPENAI, null, null, + String result = OpenAiOfficialSetup.calculateBaseUrl(null, OpenAiOfficialSetup.ModelProvider.OPENAI, null, null, null); assertEquals(OpenAiOfficialSetup.OPENAI_URL, result); @@ -77,7 +77,7 @@ void calculateBaseUrl_returnsDefaultOpenAIUrl_whenBaseUrlIsNull() { @Test void calculateBaseUrl_returnsGitHubUrl_whenModelHostIsGitHub() { - String result = OpenAiOfficialSetup.calculateBaseUrl(null, OpenAiOfficialSetup.ModelHost.GITHUB_MODELS, null, + String result = OpenAiOfficialSetup.calculateBaseUrl(null, OpenAiOfficialSetup.ModelProvider.GITHUB_MODELS, null, null, null); assertEquals(OpenAiOfficialSetup.GITHUB_MODELS_URL, result); From 532a776328ba03c053a8f8d0e9cf21f6237345f9 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Wed, 19 Nov 2025 20:36:36 +0100 Subject: [PATCH 26/49] Implementation of the OpenAI Java SDK - Update the Azure authentication logic - Run the Spring formatter again, even if it clashes with Checkstyle Signed-off-by: Julien Dubois --- .../AbstractOpenAiOfficialOptions.java | 370 +-- .../OpenAiOfficialChatModel.java | 2233 +++++++++-------- .../OpenAiOfficialChatOptions.java | 1782 ++++++------- .../OpenAiOfficialEmbeddingModel.java | 412 +-- .../OpenAiOfficialEmbeddingOptions.java | 266 +- .../OpenAiOfficialImageModel.java | 332 +-- .../OpenAiOfficialImageOptions.java | 588 ++--- ...OpenAiOfficialImageGenerationMetadata.java | 76 +- .../OpenAiOfficialImageResponseMetadata.java | 78 +- .../openaiofficial/metadata/package-info.java | 5 +- .../ai/openaiofficial/package-info.java | 5 +- .../AzureInternalOpenAiOfficialHelper.java | 13 +- .../setup/OpenAiOfficialSetup.java | 453 ++-- .../ai/openaiofficial/setup/package-info.java | 6 +- .../setup/OpenAiOfficialSetupTests.java | 19 +- 15 files changed, 3322 insertions(+), 3316 deletions(-) diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java index 65c4fb94508..0044fd27645 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java @@ -25,190 +25,190 @@ public class AbstractOpenAiOfficialOptions { - /** - * The deployment URL to connect to OpenAI. - */ - private String baseUrl; - - /** - * The API key to connect to OpenAI. - */ - private String apiKey; - - /** - * Credentials used to connect to Azure OpenAI. - */ - private Credential credential; - - /** - * The model name used. When using Azure AI Foundry, this is also used as the default - * deployment name. - */ - private String model; - - /** - * The deployment name as defined in Azure AI Foundry. On Azure AI Foundry, the - * default deployment name is the same as the model name. When using OpenAI directly, - * this value isn't used. - */ - private String azureDeploymentName; - - /** - * The Azure OpenAI Service version to use when connecting to Azure AI Foundry. - */ - private AzureOpenAIServiceVersion azureOpenAIServiceVersion; - - /** - * The organization ID to use when connecting to Azure OpenAI. - */ - private String organizationId; - - /** - * Whether Azure OpenAI is detected. - */ - private boolean isAzure; - - /** - * Whether GitHub Models is detected. - */ - private boolean isGitHubModels; - - /** - * Request timeout for OpenAI client. - */ - private Duration timeout; - - /** - * Maximum number of retries for OpenAI client. - */ - private Integer maxRetries; - - /** - * Proxy settings for OpenAI client. - */ - private Proxy proxy; - - /** - * Custom headers to add to OpenAI client requests. - */ - private Map customHeaders; - - public String getBaseUrl() { - return baseUrl; - } - - public void setBaseUrl(String baseUrl) { - this.baseUrl = baseUrl; - } - - public String getApiKey() { - return apiKey; - } - - public void setApiKey(String apiKey) { - this.apiKey = apiKey; - } - - public Credential getCredential() { - return credential; - } - - public void setCredential(Credential credential) { - this.credential = credential; - } - - public String getModel() { - return model; - } - - public void setModel(String model) { - this.model = model; - } - - public String getAzureDeploymentName() { - return azureDeploymentName; - } - - public void setAzureDeploymentName(String azureDeploymentName) { - this.azureDeploymentName = azureDeploymentName; - } - - /** - * Alias for getAzureDeploymentName() - */ - public String getDeploymentName() { - return azureDeploymentName; - } - - /** - * Alias for setAzureDeploymentName() - */ - public void setDeploymentName(String azureDeploymentName) { - this.azureDeploymentName = azureDeploymentName; - } - - public AzureOpenAIServiceVersion getAzureOpenAIServiceVersion() { - return azureOpenAIServiceVersion; - } - - public void setAzureOpenAIServiceVersion(AzureOpenAIServiceVersion azureOpenAIServiceVersion) { - this.azureOpenAIServiceVersion = azureOpenAIServiceVersion; - } - - public String getOrganizationId() { - return organizationId; - } - - public void setOrganizationId(String organizationId) { - this.organizationId = organizationId; - } - - public boolean isAzure() { - return isAzure; - } - - public void setAzure(boolean azure) { - isAzure = azure; - } - - public boolean isGitHubModels() { - return isGitHubModels; - } - - public void setGitHubModels(boolean gitHubModels) { - isGitHubModels = gitHubModels; - } - - public Duration getTimeout() { - return timeout; - } - - public void setTimeout(Duration timeout) { - this.timeout = timeout; - } - - public Integer getMaxRetries() { - return maxRetries; - } - - public void setMaxRetries(Integer maxRetries) { - this.maxRetries = maxRetries; - } - - public Proxy getProxy() { - return proxy; - } - - public void setProxy(Proxy proxy) { - this.proxy = proxy; - } - - public Map getCustomHeaders() { - return customHeaders; - } - - public void setCustomHeaders(Map customHeaders) { - this.customHeaders = customHeaders; - } + /** + * The deployment URL to connect to OpenAI. + */ + private String baseUrl; + + /** + * The API key to connect to OpenAI. + */ + private String apiKey; + + /** + * Credentials used to connect to Azure OpenAI. + */ + private Credential credential; + + /** + * The model name used. When using Azure AI Foundry, this is also used as the default + * deployment name. + */ + private String model; + + /** + * The deployment name as defined in Azure AI Foundry. On Azure AI Foundry, the + * default deployment name is the same as the model name. When using OpenAI directly, + * this value isn't used. + */ + private String azureDeploymentName; + + /** + * The Azure OpenAI Service version to use when connecting to Azure AI Foundry. + */ + private AzureOpenAIServiceVersion azureOpenAIServiceVersion; + + /** + * The organization ID to use when connecting to Azure OpenAI. + */ + private String organizationId; + + /** + * Whether Azure OpenAI is detected. + */ + private boolean isAzure; + + /** + * Whether GitHub Models is detected. + */ + private boolean isGitHubModels; + + /** + * Request timeout for OpenAI client. + */ + private Duration timeout; + + /** + * Maximum number of retries for OpenAI client. + */ + private Integer maxRetries; + + /** + * Proxy settings for OpenAI client. + */ + private Proxy proxy; + + /** + * Custom headers to add to OpenAI client requests. + */ + private Map customHeaders; + + public String getBaseUrl() { + return baseUrl; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + + public String getApiKey() { + return apiKey; + } + + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + } + + public Credential getCredential() { + return credential; + } + + public void setCredential(Credential credential) { + this.credential = credential; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getAzureDeploymentName() { + return azureDeploymentName; + } + + public void setAzureDeploymentName(String azureDeploymentName) { + this.azureDeploymentName = azureDeploymentName; + } + + /** + * Alias for getAzureDeploymentName() + */ + public String getDeploymentName() { + return azureDeploymentName; + } + + /** + * Alias for setAzureDeploymentName() + */ + public void setDeploymentName(String azureDeploymentName) { + this.azureDeploymentName = azureDeploymentName; + } + + public AzureOpenAIServiceVersion getAzureOpenAIServiceVersion() { + return azureOpenAIServiceVersion; + } + + public void setAzureOpenAIServiceVersion(AzureOpenAIServiceVersion azureOpenAIServiceVersion) { + this.azureOpenAIServiceVersion = azureOpenAIServiceVersion; + } + + public String getOrganizationId() { + return organizationId; + } + + public void setOrganizationId(String organizationId) { + this.organizationId = organizationId; + } + + public boolean isAzure() { + return isAzure; + } + + public void setAzure(boolean azure) { + isAzure = azure; + } + + public boolean isGitHubModels() { + return isGitHubModels; + } + + public void setGitHubModels(boolean gitHubModels) { + isGitHubModels = gitHubModels; + } + + public Duration getTimeout() { + return timeout; + } + + public void setTimeout(Duration timeout) { + this.timeout = timeout; + } + + public Integer getMaxRetries() { + return maxRetries; + } + + public void setMaxRetries(Integer maxRetries) { + this.maxRetries = maxRetries; + } + + public Proxy getProxy() { + return proxy; + } + + public void setProxy(Proxy proxy) { + this.proxy = proxy; + } + + public Map getCustomHeaders() { + return customHeaders; + } + + public void setCustomHeaders(Map customHeaders) { + this.customHeaders = customHeaders; + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java index 948bafcb414..01777ec2e87 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java @@ -86,1121 +86,1126 @@ */ public class OpenAiOfficialChatModel implements ChatModel { - private static final String DEFAULT_MODEL_NAME = OpenAiOfficialChatOptions.DEFAULT_CHAT_MODEL; - - private static final ChatModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultChatModelObservationConvention(); - - private static final ToolCallingManager DEFAULT_TOOL_CALLING_MANAGER = ToolCallingManager.builder().build(); - - private final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatModel.class); - - private final OpenAIClient openAiClient; - - private final OpenAIClientAsync openAiClientAsync; - - private final OpenAiOfficialChatOptions options; - - private final ObservationRegistry observationRegistry; - - private final ToolCallingManager toolCallingManager; - - private final ToolExecutionEligibilityPredicate toolExecutionEligibilityPredicate; - - private ChatModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; - - /** - * Creates a new OpenAiOfficialChatModel with default options. - */ - public OpenAiOfficialChatModel() { - this(null, null, null, null, null, null); - } - - /** - * Creates a new OpenAiOfficialChatModel with the given options. - * @param options the chat options - */ - public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options) { - this(null, null, options, null, null, null); - } - - /** - * Creates a new OpenAiOfficialChatModel with the given options and observation registry. - * @param options the chat options - * @param observationRegistry the observation registry - */ - public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options, ObservationRegistry observationRegistry) { - this(null, null, options, null, observationRegistry, null); - } - - /** - * Creates a new OpenAiOfficialChatModel with the given options, tool calling manager, and observation registry. - * @param options the chat options - * @param toolCallingManager the tool calling manager - * @param observationRegistry the observation registry - */ - public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options, ToolCallingManager toolCallingManager, - ObservationRegistry observationRegistry) { - this(null, null, options, toolCallingManager, observationRegistry, null); - } - - /** - * Creates a new OpenAiOfficialChatModel with the given OpenAI clients. - * @param openAIClient the synchronous OpenAI client - * @param openAiClientAsync the asynchronous OpenAI client - */ - public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync) { - this(openAIClient, openAiClientAsync, null, null, null, null); - } - - /** - * Creates a new OpenAiOfficialChatModel with the given OpenAI clients and options. - * @param openAIClient the synchronous OpenAI client - * @param openAiClientAsync the asynchronous OpenAI client - * @param options the chat options - */ - public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync, - OpenAiOfficialChatOptions options) { - this(openAIClient, openAiClientAsync, options, null, null, null); - } - - /** - * Creates a new OpenAiOfficialChatModel with the given OpenAI clients, options, and observation registry. - * @param openAIClient the synchronous OpenAI client - * @param openAiClientAsync the asynchronous OpenAI client - * @param options the chat options - * @param observationRegistry the observation registry - */ - public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync, - OpenAiOfficialChatOptions options, ObservationRegistry observationRegistry) { - this(openAIClient, openAiClientAsync, options, null, observationRegistry, null); - } - - /** - * Creates a new OpenAiOfficialChatModel with all configuration options. - * @param openAiClient the synchronous OpenAI client - * @param openAiClientAsync the asynchronous OpenAI client - * @param options the chat options - * @param toolCallingManager the tool calling manager - * @param observationRegistry the observation registry - * @param toolExecutionEligibilityPredicate the predicate to determine tool execution eligibility - */ - public OpenAiOfficialChatModel(OpenAIClient openAiClient, OpenAIClientAsync openAiClientAsync, - OpenAiOfficialChatOptions options, ToolCallingManager toolCallingManager, - ObservationRegistry observationRegistry, - ToolExecutionEligibilityPredicate toolExecutionEligibilityPredicate) { - - if (options == null) { - this.options = OpenAiOfficialChatOptions.builder().model(DEFAULT_MODEL_NAME).build(); - } - else { - this.options = options; - } - this.openAiClient = Objects.requireNonNullElseGet(openAiClient, - () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), - this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), - this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), - this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), - this.options.getProxy(), this.options.getCustomHeaders())); - - this.openAiClientAsync = Objects.requireNonNullElseGet(openAiClientAsync, - () -> setupAsyncClient(this.options.getBaseUrl(), this.options.getApiKey(), - this.options.getCredential(), this.options.getAzureDeploymentName(), - this.options.getAzureOpenAIServiceVersion(), this.options.getOrganizationId(), - this.options.isAzure(), this.options.isGitHubModels(), this.options.getModel(), - this.options.getTimeout(), this.options.getMaxRetries(), this.options.getProxy(), - this.options.getCustomHeaders())); - - this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); - this.toolCallingManager = Objects.requireNonNullElse(toolCallingManager, DEFAULT_TOOL_CALLING_MANAGER); - this.toolExecutionEligibilityPredicate = Objects.requireNonNullElse(toolExecutionEligibilityPredicate, - new DefaultToolExecutionEligibilityPredicate()); - } - - /** - * Gets the chat options for this model. - * @return the chat options - */ - public OpenAiOfficialChatOptions getOptions() { - return this.options; - } - - @Override - public ChatResponse call(Prompt prompt) { - if (this.openAiClient == null) { - throw new IllegalStateException( - "OpenAI sync client is not configured. Have you set the 'streamUsage' option to false?"); - } - Prompt requestPrompt = buildRequestPrompt(prompt); - return this.internalCall(requestPrompt, null); - } - - /** - * Internal method to handle chat completion calls with tool execution support. - * @param prompt the prompt for the chat completion - * @param previousChatResponse the previous chat response for accumulating usage - * @return the chat response - */ - public ChatResponse internalCall(Prompt prompt, ChatResponse previousChatResponse) { - - ChatCompletionCreateParams request = createRequest(prompt, false); - - ChatModelObservationContext observationContext = ChatModelObservationContext.builder() - .prompt(prompt) - .provider(AiProvider.OPENAI_OFFICIAL.value()) - .build(); - - ChatResponse response = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION - .observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, - this.observationRegistry) - .observe(() -> { - - ChatCompletion chatCompletion = this.openAiClient.chat().completions().create(request); - - List choices = chatCompletion.choices(); - if (choices.isEmpty()) { - logger.warn("No choices returned for prompt: {}", prompt); - return new ChatResponse(List.of()); - } - - List generations = choices.stream().map(choice -> { - chatCompletion.id(); - choice.finishReason(); - Map metadata = Map.of("id", chatCompletion.id(), "role", - choice.message()._role().asString().isPresent() ? choice.message()._role().asStringOrThrow() - : "", - "index", choice.index(), "finishReason", choice.finishReason().value().toString(), - "refusal", choice.message().refusal().isPresent() ? choice.message().refusal() : "", - "annotations", choice.message().annotations().isPresent() ? choice.message().annotations() - : List.of(Map.of())); - return buildGeneration(choice, metadata); - }).toList(); - - // Current usage - CompletionUsage usage = chatCompletion.usage().orElse(null); - Usage currentChatResponseUsage = usage != null ? getDefaultUsage(usage) : new EmptyUsage(); - Usage accumulatedUsage = UsageCalculator.getCumulativeUsage(currentChatResponseUsage, - previousChatResponse); - ChatResponse chatResponse = new ChatResponse(generations, from(chatCompletion, accumulatedUsage)); - - observationContext.setResponse(chatResponse); - - return chatResponse; - - }); - - if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(prompt.getOptions(), response)) { - var toolExecutionResult = this.toolCallingManager.executeToolCalls(prompt, response); - if (toolExecutionResult.returnDirect()) { - // Return tool execution result directly to the client. - return ChatResponse.builder() - .from(response) - .generations(ToolExecutionResult.buildGenerations(toolExecutionResult)) - .build(); - } - else { - // Send the tool execution result back to the model. - return this.internalCall(new Prompt(toolExecutionResult.conversationHistory(), prompt.getOptions()), - response); - } - } - - return response; - } - - @Override - public Flux stream(Prompt prompt) { - if (this.openAiClientAsync == null) { - throw new IllegalStateException( - "OpenAI async client is not configured. Streaming is not supported with the current configuration. Have you set the 'streamUsage' option to true?"); - } - Prompt requestPrompt = buildRequestPrompt(prompt); - return internalStream(requestPrompt, null); - } - - /** - * Safely extracts the assistant message from a chat response. - * @param response the chat response - * @return the assistant message, or null if not available - */ - public AssistantMessage safeAssistantMessage(ChatResponse response) { - if (response == null) - return null; - Generation gen = response.getResult(); - if (gen == null) - return null; - return gen.getOutput(); - } - - /** - * Internal method to handle streaming chat completion calls with tool execution support. - * @param prompt the prompt for the chat completion - * @param previousChatResponse the previous chat response for accumulating usage - * @return a Flux of chat responses - */ - public Flux internalStream(Prompt prompt, ChatResponse previousChatResponse) { - return Flux.deferContextual(contextView -> { - ChatCompletionCreateParams request = createRequest(prompt, true); - ConcurrentHashMap roleMap = new ConcurrentHashMap<>(); - final ChatModelObservationContext observationContext = ChatModelObservationContext.builder() - .prompt(prompt) - .provider(AiProvider.OPENAI_OFFICIAL.value()) - .build(); - Observation observation = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION.observation( - this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, - this.observationRegistry); - observation.parentObservation(contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null)).start(); - - Flux chatResponses = Flux.create(sink -> { - this.openAiClientAsync.chat().completions().createStreaming(request).subscribe(chunk -> { - try { - ChatCompletion chatCompletion = chunkToChatCompletion(chunk); - String id = chatCompletion.id(); - List generations = chatCompletion.choices().stream().map(choice -> { - roleMap.putIfAbsent(id, choice.message()._role().asString().isPresent() - ? choice.message()._role().asStringOrThrow() : ""); - - Map metadata = Map.of("id", id, "role", roleMap.getOrDefault(id, ""), - "index", choice.index(), "finishReason", choice.finishReason().value(), "refusal", - choice.message().refusal().isPresent() ? choice.message().refusal() : "", - "annotations", choice.message().annotations().isPresent() - ? choice.message().annotations() : List.of(), - "chunkChoice", chunk.choices().get((int) choice.index())); - - return buildGeneration(choice, metadata); - }).toList(); - Optional usage = chatCompletion.usage(); - CompletionUsage usageVal = usage.orElse(null); - Usage currentUsage = usageVal != null ? getDefaultUsage(usageVal) : new EmptyUsage(); - Usage accumulated = UsageCalculator.getCumulativeUsage(currentUsage, previousChatResponse); - sink.next(new ChatResponse(generations, from(chatCompletion, accumulated))); - } - catch (Exception e) { - logger.error("Error processing chat completion", e); - sink.error(e); - } - }).onCompleteFuture().whenComplete((unused, throwable) -> { - if (throwable != null) - sink.error(throwable); - else - sink.complete(); - }); - }).buffer(2, 1).map(buffer -> { - ChatResponse first = buffer.get(0); - if (request.streamOptions().isPresent() && buffer.size() == 2) { - ChatResponse second = buffer.get(1); - if (second != null) { - Usage usage = second.getMetadata().getUsage(); - if (!UsageCalculator.isEmpty(usage)) { - return new ChatResponse(first.getResults(), from(first.getMetadata(), usage)); - } - } - } - return first; - }); - - Flux flux = chatResponses - .contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation)); - - return flux.collectList().flatMapMany(list -> { - if (list.isEmpty()) - return Flux.empty(); - boolean hasToolCalls = list.stream() - .map(this::safeAssistantMessage) - .filter(Objects::nonNull) - .anyMatch(am -> !CollectionUtils.isEmpty(am.getToolCalls())); - if (!hasToolCalls) { - if (list.size() > 2) { - ChatResponse penultimateResponse = list.get(list.size() - 2); // Get - // the - // finish - // reason - ChatResponse lastResponse = list.get(list.size() - 1); // Get the - // usage - Usage usage = lastResponse.getMetadata().getUsage(); - observationContext.setResponse(new ChatResponse(penultimateResponse.getResults(), - from(penultimateResponse.getMetadata(), usage))); - } - return Flux.fromIterable(list); - } - Map builders = new HashMap<>(); - StringBuilder text = new StringBuilder(); - ChatResponseMetadata finalMetadata = null; - ChatGenerationMetadata finalGenMetadata = null; - Map props = new HashMap<>(); - for (ChatResponse chatResponse : list) { - AssistantMessage am = safeAssistantMessage(chatResponse); - if (am == null) - continue; - if (am.getText() != null) - text.append(am.getText()); - if (am.getMetadata() != null) - props.putAll(am.getMetadata()); - if (!CollectionUtils.isEmpty(am.getToolCalls())) { - Object ccObj = am.getMetadata().get("chunkChoice"); - if (ccObj instanceof ChatCompletionChunk.Choice chunkChoice - && chunkChoice.delta().toolCalls().isPresent()) { - List deltaCalls = chunkChoice.delta() - .toolCalls() - .get(); - for (int i = 0; i < am.getToolCalls().size() && i < deltaCalls.size(); i++) { - AssistantMessage.ToolCall tc = am.getToolCalls().get(i); - ChatCompletionChunk.Choice.Delta.ToolCall dtc = deltaCalls.get(i); - String key = chunkChoice.index() + "-" + dtc.index(); - ToolCallBuilder toolCallBuilder = builders.computeIfAbsent(key, - k -> new ToolCallBuilder()); - toolCallBuilder.merge(tc); - } - } - else { - for (AssistantMessage.ToolCall tc : am.getToolCalls()) { - ToolCallBuilder toolCallBuilder = builders.computeIfAbsent(tc.id(), - k -> new ToolCallBuilder()); - toolCallBuilder.merge(tc); - } - } - } - Generation generation = chatResponse.getResult(); - if (generation != null && generation.getMetadata() != null - && generation.getMetadata() != ChatGenerationMetadata.NULL) { - finalGenMetadata = generation.getMetadata(); - } - if (chatResponse.getMetadata() != null) - finalMetadata = chatResponse.getMetadata(); - } - List merged = builders.values() - .stream() - .map(ToolCallBuilder::build) - .filter(tc -> StringUtils.hasText(tc.name())) - .toList(); - AssistantMessage.Builder assistantMessageBuilder = AssistantMessage.builder() - .content(text.toString()) - .properties(props); - if (!merged.isEmpty()) { - assistantMessageBuilder.toolCalls(merged); - } - AssistantMessage assistantMessage = assistantMessageBuilder.build(); - Generation finalGen = new Generation(assistantMessage, - finalGenMetadata != null ? finalGenMetadata : ChatGenerationMetadata.NULL); - ChatResponse aggregated = new ChatResponse(List.of(finalGen), finalMetadata); - observationContext.setResponse(aggregated); - if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(prompt.getOptions(), aggregated)) { - return Flux.deferContextual(ctx -> { - ToolExecutionResult tetoolExecutionResult; - try { - ToolCallReactiveContextHolder.setContext(ctx); - tetoolExecutionResult = this.toolCallingManager.executeToolCalls(prompt, aggregated); - } - finally { - ToolCallReactiveContextHolder.clearContext(); - } - if (tetoolExecutionResult.returnDirect()) - return Flux.just(ChatResponse.builder() - .from(aggregated) - .generations(ToolExecutionResult.buildGenerations(tetoolExecutionResult)) - .build()); - return this.internalStream( - new Prompt(tetoolExecutionResult.conversationHistory(), prompt.getOptions()), - aggregated); - }).subscribeOn(Schedulers.boundedElastic()); - } - return Flux.just(aggregated); - }).doOnError(observation::error).doFinally(s -> observation.stop()); - }); - } - - private Generation buildGeneration(ChatCompletion.Choice choice, Map metadata) { - ChatCompletionMessage message = choice.message(); - List toolCalls = new ArrayList<>(); - - if (metadata.containsKey("chunkChoice")) { - Object chunkChoiceObj = metadata.get("chunkChoice"); - if (chunkChoiceObj instanceof ChatCompletionChunk.Choice chunkChoice) { - if (chunkChoice.delta().toolCalls().isPresent()) { - toolCalls = chunkChoice.delta() - .toolCalls() - .get() - .stream() - .filter(tc -> tc.function().isPresent()) - .map(tc -> { - var funcOpt = tc.function(); - if (funcOpt.isEmpty()) - return null; - var func = funcOpt.get(); - String id = tc.id().orElse(""); - String name = func.name().orElse(""); - String arguments = func.arguments().orElse(""); - return new AssistantMessage.ToolCall(id, "function", name, arguments); - }) - .filter(Objects::nonNull) - .toList(); - } - } - } - else { - toolCalls = message.toolCalls() - .map(list -> list.stream().filter(tc -> tc.function().isPresent()).map(tc -> { - var opt = tc.function(); - if (opt.isEmpty()) - return null; - var funcCall = opt.get(); - var functionDef = funcCall.function(); - String id = funcCall.id(); - String name = functionDef.name(); - String arguments = functionDef.arguments(); - return new AssistantMessage.ToolCall(id, "function", name, arguments); - }).filter(Objects::nonNull).toList()) - .orElse(List.of()); - } - - var generationMetadataBuilder = ChatGenerationMetadata.builder() - .finishReason(choice.finishReason().value().name()); - String textContent = message.content().orElse(""); - var assistantMessage = AssistantMessage.builder() - .content(textContent) - .properties(metadata) - .toolCalls(toolCalls) - .build(); - return new Generation(assistantMessage, generationMetadataBuilder.build()); - } - - private ChatResponseMetadata from(ChatCompletion result, Usage usage) { - Assert.notNull(result, "OpenAI ChatCompletion must not be null"); - result.model(); - result.id(); - return ChatResponseMetadata.builder() - .id(result.id()) - .usage(usage) - .model(result.model()) - .keyValue("created", result.created()) - .build(); - } - - private ChatResponseMetadata from(ChatResponseMetadata chatResponseMetadata, Usage usage) { - Assert.notNull(chatResponseMetadata, "OpenAI ChatResponseMetadata must not be null"); - return ChatResponseMetadata.builder() - .id(chatResponseMetadata.getId() != null ? chatResponseMetadata.getId() : "") - .usage(usage) - .model(chatResponseMetadata.getModel() != null ? chatResponseMetadata.getModel() : "") - .build(); - } - - /** - * Convert the ChatCompletionChunk into a ChatCompletion. The Usage is set to null. - * @param chunk the ChatCompletionChunk to convert - * @return the ChatCompletion - */ - private ChatCompletion chunkToChatCompletion(ChatCompletionChunk chunk) { - List choices = chunk.choices().stream().map(chunkChoice -> { - ChatCompletion.Choice.FinishReason finishReason = ChatCompletion.Choice.FinishReason.of(""); - if (chunkChoice.finishReason().isPresent()) { - finishReason = ChatCompletion.Choice.FinishReason - .of(chunkChoice.finishReason().get().value().name().toLowerCase()); - } - - ChatCompletion.Choice.Builder choiceBuilder = ChatCompletion.Choice.builder() - .finishReason(finishReason) - .index(chunkChoice.index()) - .message(ChatCompletionMessage.builder() - .content(chunkChoice.delta().content()) - .refusal(chunkChoice.delta().refusal()) - .build()); - - // Handle optional logprobs - if (chunkChoice.logprobs().isPresent()) { - var logprobs = chunkChoice.logprobs().get(); - choiceBuilder.logprobs(ChatCompletion.Choice.Logprobs.builder() - .content(logprobs.content()) - .refusal(logprobs.refusal()) - .build()); - } - else { - // Provide empty logprobs when not present - choiceBuilder - .logprobs(ChatCompletion.Choice.Logprobs.builder().content(List.of()).refusal(List.of()).build()); - } - - return choiceBuilder.build(); - }).toList(); - - return ChatCompletion.builder() - .id(chunk.id()) - .choices(choices) - .created(chunk.created()) - .model(chunk.model()) - .usage(chunk.usage() - .orElse(CompletionUsage.builder().promptTokens(0).completionTokens(0).totalTokens(0).build())) - .build(); - } - - private DefaultUsage getDefaultUsage(CompletionUsage usage) { - return new DefaultUsage(Math.toIntExact(usage.promptTokens()), Math.toIntExact(usage.completionTokens()), - Math.toIntExact(usage.totalTokens()), usage); - } - - /** - * Builds the request prompt by merging runtime options with default options. - * @param prompt the original prompt - * @return the prompt with merged options - */ - Prompt buildRequestPrompt(Prompt prompt) { - // Process runtime options - OpenAiOfficialChatOptions runtimeOptions = null; - if (prompt.getOptions() != null) { - if (prompt.getOptions() instanceof ToolCallingChatOptions toolCallingChatOptions) { - runtimeOptions = ModelOptionsUtils.copyToTarget(toolCallingChatOptions, ToolCallingChatOptions.class, - OpenAiOfficialChatOptions.class); - } - else { - runtimeOptions = ModelOptionsUtils.copyToTarget(prompt.getOptions(), ChatOptions.class, - OpenAiOfficialChatOptions.class); - } - } - - // Define request options by merging runtime options and default options - OpenAiOfficialChatOptions requestOptions = OpenAiOfficialChatOptions.builder() - .from(this.options) - .merge(runtimeOptions != null ? runtimeOptions : OpenAiOfficialChatOptions.builder().build()) - .build(); - - // Merge @JsonIgnore-annotated options explicitly since they are ignored by - // Jackson, used by ModelOptionsUtils. - if (runtimeOptions != null) { - if (runtimeOptions.getTopK() != null) { - logger.warn("The topK option is not supported by OpenAI chat models. Ignoring."); - } - - Map mergedHttpHeaders = new HashMap<>(this.options.getHttpHeaders()); - mergedHttpHeaders.putAll(runtimeOptions.getHttpHeaders()); - requestOptions.setHttpHeaders(mergedHttpHeaders); - - requestOptions.setInternalToolExecutionEnabled(runtimeOptions.getInternalToolExecutionEnabled() != null - ? runtimeOptions.getInternalToolExecutionEnabled() - : this.options.getInternalToolExecutionEnabled()); - requestOptions.setToolNames( - ToolCallingChatOptions.mergeToolNames(runtimeOptions.getToolNames(), this.options.getToolNames())); - requestOptions.setToolCallbacks(ToolCallingChatOptions.mergeToolCallbacks(runtimeOptions.getToolCallbacks(), - this.options.getToolCallbacks())); - requestOptions.setToolContext(ToolCallingChatOptions.mergeToolContext(runtimeOptions.getToolContext(), - this.options.getToolContext())); - } - else { - requestOptions.setHttpHeaders(this.options.getHttpHeaders()); - requestOptions.setInternalToolExecutionEnabled(this.options.getInternalToolExecutionEnabled()); - requestOptions.setToolNames(this.options.getToolNames()); - requestOptions.setToolCallbacks(this.options.getToolCallbacks()); - requestOptions.setToolContext(this.options.getToolContext()); - } - - ToolCallingChatOptions.validateToolCallbacks(requestOptions.getToolCallbacks()); - - return new Prompt(prompt.getInstructions(), requestOptions); - } - - /** - * Creates a chat completion request from the given prompt. - * @param prompt the prompt containing messages and options - * @param stream whether this is a streaming request - * @return the chat completion create parameters - */ - ChatCompletionCreateParams createRequest(Prompt prompt, boolean stream) { - - List chatCompletionMessageParams = prompt.getInstructions() - .stream() - .map(message -> { - if (message.getMessageType() == MessageType.USER || message.getMessageType() == MessageType.SYSTEM) { - // Handle simple text content for user and system messages - ChatCompletionUserMessageParam.Builder builder = ChatCompletionUserMessageParam.builder(); - - if (message instanceof UserMessage userMessage - && !CollectionUtils.isEmpty(userMessage.getMedia())) { - // Handle media content (images, audio, files) - List parts = new ArrayList<>(); - - if (!message.getText().isEmpty()) { - parts.add(ChatCompletionContentPart - .ofText(ChatCompletionContentPartText.builder().text(message.getText()).build())); - } - - // Add media content parts - userMessage.getMedia().forEach(media -> { - String mimeType = media.getMimeType().toString(); - if (mimeType.startsWith("image/")) { - if (media.getData() instanceof java.net.URI uri) { - parts.add(ChatCompletionContentPart - .ofImageUrl(ChatCompletionContentPartImage.builder() - .imageUrl(ChatCompletionContentPartImage.ImageUrl.builder() - .url(uri.toString()) - .build()) - .build())); - } - else if (media.getData() instanceof String text) { - // The org.springframework.ai.content.Media object - // should store the URL as a java.net.URI but it - // transforms it to String somewhere along the way, - // for example in its Builder class. So, we accept - // String as well here for image URLs. - parts.add(ChatCompletionContentPart - .ofImageUrl(ChatCompletionContentPartImage.builder() - .imageUrl( - ChatCompletionContentPartImage.ImageUrl.builder().url(text).build()) - .build())); - } - else if (media.getData() instanceof byte[] bytes) { - // Assume the bytes are an image. So, convert the - // bytes to a base64 encoded - ChatCompletionContentPartImage.ImageUrl.Builder imageUrlBuilder = ChatCompletionContentPartImage.ImageUrl - .builder(); - - imageUrlBuilder.url("data:" + mimeType + ";base64," - + Base64.getEncoder().encodeToString(bytes)); - parts.add(ChatCompletionContentPart - .ofImageUrl(ChatCompletionContentPartImage.builder() - .imageUrl(imageUrlBuilder.build()) - .build())); - } - else { - logger.info( - "Could not process image media with data of type: {}. Only java.net.URI is supported for image URLs.", - media.getData().getClass().getSimpleName()); - } - } - else if (mimeType.startsWith("audio/")) { - parts.add(ChatCompletionContentPart - .ofInputAudio(ChatCompletionContentPartInputAudio.builder() - .inputAudio(ChatCompletionContentPartInputAudio.builder() - .inputAudio(ChatCompletionContentPartInputAudio.InputAudio.builder() - .data(fromAudioData(media.getData())) - .format(mimeType.contains("mp3") - ? ChatCompletionContentPartInputAudio.InputAudio.Format.MP3 - : ChatCompletionContentPartInputAudio.InputAudio.Format.WAV) - .build()) - .build() - .inputAudio()) - .build())); - } - else { - // Assume it's a file or other media type represented as a - // data URL - parts.add(ChatCompletionContentPart.ofText(ChatCompletionContentPartText.builder() - .text(fromMediaData(media.getMimeType(), media.getData())) - .build())); - } - }); - builder.contentOfArrayOfContentParts(parts); - } - else { - // Simple text message - builder.content(ChatCompletionContentPartText.builder().text(message.getText()).build().text()); - } - - if (message.getMessageType() == MessageType.USER) { - builder.role(JsonValue.from(MessageType.USER.getValue())); - } - else { - builder.role(JsonValue.from(MessageType.SYSTEM.getValue())); - } - - return List.of(ChatCompletionMessageParam.ofUser(builder.build())); - } - else if (message.getMessageType() == MessageType.ASSISTANT) { - var assistantMessage = (AssistantMessage) message; - ChatCompletionAssistantMessageParam.Builder builder = ChatCompletionAssistantMessageParam.builder() - .role(JsonValue.from(MessageType.ASSISTANT.getValue())); - - if (assistantMessage.getText() != null) { - builder.content(ChatCompletionAssistantMessageParam.builder() - .content(assistantMessage.getText()) - .build() - .content()); - } - - if (!CollectionUtils.isEmpty(assistantMessage.getToolCalls())) { - List toolCalls = assistantMessage.getToolCalls() - .stream() - .map(toolCall -> ChatCompletionMessageToolCall - .ofFunction(ChatCompletionMessageFunctionToolCall.builder() - .id(toolCall.id()) - .function(ChatCompletionMessageFunctionToolCall.Function.builder() - .name(toolCall.name()) - .arguments(toolCall.arguments()) - .build()) - .build())) - .toList(); - - builder.toolCalls(toolCalls); - } - - return List.of(ChatCompletionMessageParam.ofAssistant(builder.build())); - } - else if (message.getMessageType() == MessageType.TOOL) { - ToolResponseMessage toolMessage = (ToolResponseMessage) message; - - ChatCompletionToolMessageParam.Builder builder = ChatCompletionToolMessageParam.builder(); - builder.content(toolMessage.getText() != null ? toolMessage.getText() : ""); - builder.role(JsonValue.from(MessageType.TOOL.getValue())); - - if (toolMessage.getResponses().isEmpty()) { - return List.of(ChatCompletionMessageParam.ofTool(builder.build())); - } - return toolMessage.getResponses().stream().map(response -> { - String callId = response.id(); - String callResponse = response.responseData(); - - return ChatCompletionMessageParam - .ofTool(builder.toolCallId(callId).content(callResponse).build()); - }).toList(); - } - else { - throw new IllegalArgumentException("Unsupported message type: " + message.getMessageType()); - } - }) - .flatMap(List::stream) - .toList(); - - ChatCompletionCreateParams.Builder builder = ChatCompletionCreateParams.builder(); - - chatCompletionMessageParams.forEach(builder::addMessage); - - OpenAiOfficialChatOptions requestOptions = (OpenAiOfficialChatOptions) prompt.getOptions(); - - // Use deployment name if available (for Azure AI Foundry), otherwise use model - // name - if (requestOptions.getDeploymentName() != null) { - builder.model(requestOptions.getDeploymentName()); - } - else if (requestOptions.getModel() != null) { - builder.model(requestOptions.getModel()); - } - - if (requestOptions.getFrequencyPenalty() != null) { - builder.frequencyPenalty(requestOptions.getFrequencyPenalty()); - } - if (requestOptions.getLogitBias() != null) { - builder.logitBias(ChatCompletionCreateParams.LogitBias.builder() - .putAllAdditionalProperties(requestOptions.getLogitBias() - .entrySet() - .stream() - .collect(Collectors.toMap(Map.Entry::getKey, entry -> JsonValue.from(entry.getValue())))) - .build()); - } - if (requestOptions.getLogprobs() != null) { - builder.logprobs(requestOptions.getLogprobs()); - } - if (requestOptions.getTopLogprobs() != null) { - builder.topLogprobs(requestOptions.getTopLogprobs()); - } - if (requestOptions.getMaxTokens() != null) { - builder.maxTokens(requestOptions.getMaxTokens()); - } - if (requestOptions.getMaxCompletionTokens() != null) { - builder.maxCompletionTokens(requestOptions.getMaxCompletionTokens()); - } - if (requestOptions.getN() != null) { - builder.n(requestOptions.getN()); - } - if (requestOptions.getOutputAudio() != null) { - builder.audio(requestOptions.getOutputAudio()); - } - if (requestOptions.getPresencePenalty() != null) { - builder.presencePenalty(requestOptions.getPresencePenalty()); - } - if (requestOptions.getResponseFormat() != null) { - ResponseFormat responseFormat = requestOptions.getResponseFormat(); - if (responseFormat.getType().equals(ResponseFormat.Type.TEXT)) { - builder.responseFormat(ResponseFormatText.builder().build()); - } - else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_OBJECT)) { - builder.responseFormat(ResponseFormatJsonObject.builder().build()); - } - else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_SCHEMA)) { - String jsonSchemaString = responseFormat.getJsonSchema() != null ? responseFormat.getJsonSchema() : ""; - try { - com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); - ResponseFormatJsonSchema.JsonSchema.Builder jsonSchemaBuilder = ResponseFormatJsonSchema.JsonSchema - .builder(); - jsonSchemaBuilder.name("json_schema"); - jsonSchemaBuilder.strict(true); - - ResponseFormatJsonSchema.JsonSchema.Schema schema = mapper.readValue(jsonSchemaString, - ResponseFormatJsonSchema.JsonSchema.Schema.class); - - jsonSchemaBuilder.schema(schema); - - builder.responseFormat( - ResponseFormatJsonSchema.builder().jsonSchema(jsonSchemaBuilder.build()).build()); - } - catch (Exception e) { - throw new IllegalArgumentException("Failed to parse JSON schema: " + jsonSchemaString, e); - } - } - else { - throw new IllegalArgumentException("Unsupported response format type: " + responseFormat.getType()); - } - } - if (requestOptions.getSeed() != null) { - builder.seed(requestOptions.getSeed()); - } - if (requestOptions.getStop() != null && !requestOptions.getStop().isEmpty()) { - if (requestOptions.getStop().size() == 1) { - builder.stop(ChatCompletionCreateParams.Stop.ofString(requestOptions.getStop().get(0))); - } - else { - builder.stop(ChatCompletionCreateParams.Stop.ofStrings(requestOptions.getStop())); - } - } - if (requestOptions.getTemperature() != null) { - builder.temperature(requestOptions.getTemperature()); - } - if (requestOptions.getTopP() != null) { - builder.topP(requestOptions.getTopP()); - } - if (requestOptions.getUser() != null) { - builder.user(requestOptions.getUser()); - } - if (requestOptions.getParallelToolCalls() != null) { - builder.parallelToolCalls(requestOptions.getParallelToolCalls()); - } - if (requestOptions.getReasoningEffort() != null) { - builder.reasoningEffort(ReasoningEffort.of(requestOptions.getReasoningEffort().toLowerCase())); - } - if (requestOptions.getVerbosity() != null) { - builder.verbosity(ChatCompletionCreateParams.Verbosity.of(requestOptions.getVerbosity())); - } - - if (requestOptions.getStore() != null) { - builder.store(requestOptions.getStore()); - } - if (requestOptions.getMetadata() != null && !requestOptions.getMetadata().isEmpty()) { - builder.metadata(ChatCompletionCreateParams.Metadata.builder() - .putAllAdditionalProperties(requestOptions.getMetadata() - .entrySet() - .stream() - .collect(Collectors.toMap(Map.Entry::getKey, entry -> JsonValue.from(entry.getValue())))) - .build()); - } - if (requestOptions.getServiceTier() != null) { - builder.serviceTier(ChatCompletionCreateParams.ServiceTier.of(requestOptions.getServiceTier())); - } - - if (stream) { - if (requestOptions.getStreamOptions() != null) { - ChatCompletionStreamOptions.Builder streamOptionsBuilder = ChatCompletionStreamOptions.builder(); - - if (requestOptions.getStreamOptions().includeObfuscation().isPresent()) { - streamOptionsBuilder - .includeObfuscation(requestOptions.getStreamOptions().includeObfuscation().get()); - } - streamOptionsBuilder.additionalProperties(requestOptions.getStreamOptions()._additionalProperties()); - streamOptionsBuilder.includeUsage(requestOptions.getStreamUsage()); - builder.streamOptions(streamOptionsBuilder.build()); - } - else { - builder.streamOptions(ChatCompletionStreamOptions.builder() - .includeUsage(true) // Include usage by default for streaming - .build()); - } - } - - // Add the tool definitions to the request's tools parameter. - List toolDefinitions = this.toolCallingManager.resolveToolDefinitions(requestOptions); - if (!CollectionUtils.isEmpty(toolDefinitions)) { - builder.tools(getChatCompletionTools(toolDefinitions)); - } - - if (requestOptions.getToolChoice() != null) { - builder.toolChoice(requestOptions.getToolChoice()); - } - - return builder.build(); - } - - private String fromAudioData(Object audioData) { - if (audioData instanceof byte[] bytes) { - return Base64.getEncoder().encodeToString(bytes); - } - throw new IllegalArgumentException("Unsupported audio data type: " + audioData.getClass().getSimpleName()); - } - - private String fromMediaData(org.springframework.util.MimeType mimeType, Object mediaContentData) { - if (mediaContentData instanceof byte[] bytes) { - // Assume the bytes are an image. So, convert the bytes to a base64 encoded - // following the prefix pattern. - return String.format("data:%s;base64,%s", mimeType.toString(), Base64.getEncoder().encodeToString(bytes)); - } - else if (mediaContentData instanceof String text) { - // Assume the text is a URLs or a base64 encoded image prefixed by the user. - return text; - } - else { - throw new IllegalArgumentException( - "Unsupported media data type: " + mediaContentData.getClass().getSimpleName()); - } - } - - private List getChatCompletionTools(List toolDefinitions) { - return toolDefinitions.stream().map(toolDefinition -> { - FunctionParameters.Builder parametersBuilder = FunctionParameters.builder(); - - if (!toolDefinition.inputSchema().isEmpty()) { - // Parse the schema and add its properties directly - try { - com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); - @SuppressWarnings("unchecked") - Map schemaMap = mapper.readValue(toolDefinition.inputSchema(), Map.class); - - // Add each property from the schema to the parameters - schemaMap - .forEach((key, value) -> parametersBuilder.putAdditionalProperty(key, JsonValue.from(value))); - - // Add strict mode - parametersBuilder.putAdditionalProperty("strict", JsonValue.from(true)); // TODO - // allow - // non-strict - // mode - } - catch (Exception e) { - logger.error("Failed to parse tool schema", e); - } - } - - FunctionDefinition functionDefinition = FunctionDefinition.builder() - .name(toolDefinition.name()) - .description(toolDefinition.description()) - .parameters(parametersBuilder.build()) - .build(); - - return ChatCompletionTool - .ofFunction(ChatCompletionFunctionTool.builder().function(functionDefinition).build()); - }).toList(); - } - - @Override - public ChatOptions getDefaultOptions() { - return this.options.copy(); - } - - /** - * Use the provided convention for reporting observation data - * @param observationConvention The provided convention - */ - public void setObservationConvention(ChatModelObservationConvention observationConvention) { - Assert.notNull(observationConvention, "observationConvention cannot be null"); - this.observationConvention = observationConvention; - } - - /** - * Response format (text, json_object, json_schema) for OpenAiOfficialChatModel - * responses. - * - * @author Julien Dubois - */ - public static class ResponseFormat { - - private Type type = Type.TEXT; - - private String jsonSchema; - - public Type getType() { - return type; - } - - public void setType(Type type) { - this.type = type; - } - - public String getJsonSchema() { - return jsonSchema; - } - - public void setJsonSchema(String jsonSchema) { - this.jsonSchema = jsonSchema; - } - - public static Builder builder() { - return new Builder(); - } - - public static final class Builder { - - private final ResponseFormat responseFormat = new ResponseFormat(); - - private Builder() { - } - - public Builder type(Type type) { - this.responseFormat.setType(type); - return this; - } - - public Builder jsonSchema(String jsonSchema) { - this.responseFormat.setType(Type.JSON_SCHEMA); - this.responseFormat.setJsonSchema(jsonSchema); - return this; - } - - public ResponseFormat build() { - return this.responseFormat; - } - - } - - public enum Type { - - /** - * Generates a text response. (default) - */ - TEXT, - - /** - * Enables JSON mode, which guarantees the message the model generates is - * valid JSON. - */ - JSON_OBJECT, - - /** - * Enables Structured Outputs which guarantees the model will match your - * supplied JSON schema. - */ - JSON_SCHEMA - - } - - } - - /** - * Helper class to merge streaming tool calls that arrive in pieces across multiple - * chunks. In OpenAI streaming, a tool call's ID, name, and arguments can arrive in - * separate chunks. - */ - private static class ToolCallBuilder { - - private String id = ""; - - private String type = "function"; - - private String name = ""; - - private StringBuilder arguments = new StringBuilder(); - - void merge(AssistantMessage.ToolCall toolCall) { - if (toolCall.id() != null && !toolCall.id().isEmpty()) { - this.id = toolCall.id(); - } - if (toolCall.type() != null && !toolCall.type().isEmpty()) { - this.type = toolCall.type(); - } - if (toolCall.name() != null && !toolCall.name().isEmpty()) { - this.name = toolCall.name(); - } - if (toolCall.arguments() != null && !toolCall.arguments().isEmpty()) { - this.arguments.append(toolCall.arguments()); - } - } + private static final String DEFAULT_MODEL_NAME = OpenAiOfficialChatOptions.DEFAULT_CHAT_MODEL; + + private static final ChatModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultChatModelObservationConvention(); + + private static final ToolCallingManager DEFAULT_TOOL_CALLING_MANAGER = ToolCallingManager.builder().build(); + + private final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatModel.class); + + private final OpenAIClient openAiClient; + + private final OpenAIClientAsync openAiClientAsync; + + private final OpenAiOfficialChatOptions options; + + private final ObservationRegistry observationRegistry; + + private final ToolCallingManager toolCallingManager; + + private final ToolExecutionEligibilityPredicate toolExecutionEligibilityPredicate; + + private ChatModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; + + /** + * Creates a new OpenAiOfficialChatModel with default options. + */ + public OpenAiOfficialChatModel() { + this(null, null, null, null, null, null); + } + + /** + * Creates a new OpenAiOfficialChatModel with the given options. + * @param options the chat options + */ + public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options) { + this(null, null, options, null, null, null); + } + + /** + * Creates a new OpenAiOfficialChatModel with the given options and observation + * registry. + * @param options the chat options + * @param observationRegistry the observation registry + */ + public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options, ObservationRegistry observationRegistry) { + this(null, null, options, null, observationRegistry, null); + } + + /** + * Creates a new OpenAiOfficialChatModel with the given options, tool calling manager, + * and observation registry. + * @param options the chat options + * @param toolCallingManager the tool calling manager + * @param observationRegistry the observation registry + */ + public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options, ToolCallingManager toolCallingManager, + ObservationRegistry observationRegistry) { + this(null, null, options, toolCallingManager, observationRegistry, null); + } + + /** + * Creates a new OpenAiOfficialChatModel with the given OpenAI clients. + * @param openAIClient the synchronous OpenAI client + * @param openAiClientAsync the asynchronous OpenAI client + */ + public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync) { + this(openAIClient, openAiClientAsync, null, null, null, null); + } + + /** + * Creates a new OpenAiOfficialChatModel with the given OpenAI clients and options. + * @param openAIClient the synchronous OpenAI client + * @param openAiClientAsync the asynchronous OpenAI client + * @param options the chat options + */ + public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync, + OpenAiOfficialChatOptions options) { + this(openAIClient, openAiClientAsync, options, null, null, null); + } + + /** + * Creates a new OpenAiOfficialChatModel with the given OpenAI clients, options, and + * observation registry. + * @param openAIClient the synchronous OpenAI client + * @param openAiClientAsync the asynchronous OpenAI client + * @param options the chat options + * @param observationRegistry the observation registry + */ + public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync, + OpenAiOfficialChatOptions options, ObservationRegistry observationRegistry) { + this(openAIClient, openAiClientAsync, options, null, observationRegistry, null); + } + + /** + * Creates a new OpenAiOfficialChatModel with all configuration options. + * @param openAiClient the synchronous OpenAI client + * @param openAiClientAsync the asynchronous OpenAI client + * @param options the chat options + * @param toolCallingManager the tool calling manager + * @param observationRegistry the observation registry + * @param toolExecutionEligibilityPredicate the predicate to determine tool execution + * eligibility + */ + public OpenAiOfficialChatModel(OpenAIClient openAiClient, OpenAIClientAsync openAiClientAsync, + OpenAiOfficialChatOptions options, ToolCallingManager toolCallingManager, + ObservationRegistry observationRegistry, + ToolExecutionEligibilityPredicate toolExecutionEligibilityPredicate) { + + if (options == null) { + this.options = OpenAiOfficialChatOptions.builder().model(DEFAULT_MODEL_NAME).build(); + } + else { + this.options = options; + } + this.openAiClient = Objects.requireNonNullElseGet(openAiClient, + () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), + this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), + this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), + this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), + this.options.getProxy(), this.options.getCustomHeaders())); + + this.openAiClientAsync = Objects.requireNonNullElseGet(openAiClientAsync, + () -> setupAsyncClient(this.options.getBaseUrl(), this.options.getApiKey(), + this.options.getCredential(), this.options.getAzureDeploymentName(), + this.options.getAzureOpenAIServiceVersion(), this.options.getOrganizationId(), + this.options.isAzure(), this.options.isGitHubModels(), this.options.getModel(), + this.options.getTimeout(), this.options.getMaxRetries(), this.options.getProxy(), + this.options.getCustomHeaders())); + + this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); + this.toolCallingManager = Objects.requireNonNullElse(toolCallingManager, DEFAULT_TOOL_CALLING_MANAGER); + this.toolExecutionEligibilityPredicate = Objects.requireNonNullElse(toolExecutionEligibilityPredicate, + new DefaultToolExecutionEligibilityPredicate()); + } + + /** + * Gets the chat options for this model. + * @return the chat options + */ + public OpenAiOfficialChatOptions getOptions() { + return this.options; + } + + @Override + public ChatResponse call(Prompt prompt) { + if (this.openAiClient == null) { + throw new IllegalStateException( + "OpenAI sync client is not configured. Have you set the 'streamUsage' option to false?"); + } + Prompt requestPrompt = buildRequestPrompt(prompt); + return this.internalCall(requestPrompt, null); + } + + /** + * Internal method to handle chat completion calls with tool execution support. + * @param prompt the prompt for the chat completion + * @param previousChatResponse the previous chat response for accumulating usage + * @return the chat response + */ + public ChatResponse internalCall(Prompt prompt, ChatResponse previousChatResponse) { + + ChatCompletionCreateParams request = createRequest(prompt, false); + + ChatModelObservationContext observationContext = ChatModelObservationContext.builder() + .prompt(prompt) + .provider(AiProvider.OPENAI_OFFICIAL.value()) + .build(); + + ChatResponse response = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION + .observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, + this.observationRegistry) + .observe(() -> { + + ChatCompletion chatCompletion = this.openAiClient.chat().completions().create(request); + + List choices = chatCompletion.choices(); + if (choices.isEmpty()) { + logger.warn("No choices returned for prompt: {}", prompt); + return new ChatResponse(List.of()); + } + + List generations = choices.stream().map(choice -> { + chatCompletion.id(); + choice.finishReason(); + Map metadata = Map.of("id", chatCompletion.id(), "role", + choice.message()._role().asString().isPresent() ? choice.message()._role().asStringOrThrow() + : "", + "index", choice.index(), "finishReason", choice.finishReason().value().toString(), + "refusal", choice.message().refusal().isPresent() ? choice.message().refusal() : "", + "annotations", choice.message().annotations().isPresent() ? choice.message().annotations() + : List.of(Map.of())); + return buildGeneration(choice, metadata); + }).toList(); + + // Current usage + CompletionUsage usage = chatCompletion.usage().orElse(null); + Usage currentChatResponseUsage = usage != null ? getDefaultUsage(usage) : new EmptyUsage(); + Usage accumulatedUsage = UsageCalculator.getCumulativeUsage(currentChatResponseUsage, + previousChatResponse); + ChatResponse chatResponse = new ChatResponse(generations, from(chatCompletion, accumulatedUsage)); + + observationContext.setResponse(chatResponse); + + return chatResponse; + + }); + + if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(prompt.getOptions(), response)) { + var toolExecutionResult = this.toolCallingManager.executeToolCalls(prompt, response); + if (toolExecutionResult.returnDirect()) { + // Return tool execution result directly to the client. + return ChatResponse.builder() + .from(response) + .generations(ToolExecutionResult.buildGenerations(toolExecutionResult)) + .build(); + } + else { + // Send the tool execution result back to the model. + return this.internalCall(new Prompt(toolExecutionResult.conversationHistory(), prompt.getOptions()), + response); + } + } + + return response; + } + + @Override + public Flux stream(Prompt prompt) { + if (this.openAiClientAsync == null) { + throw new IllegalStateException( + "OpenAI async client is not configured. Streaming is not supported with the current configuration. Have you set the 'streamUsage' option to true?"); + } + Prompt requestPrompt = buildRequestPrompt(prompt); + return internalStream(requestPrompt, null); + } + + /** + * Safely extracts the assistant message from a chat response. + * @param response the chat response + * @return the assistant message, or null if not available + */ + public AssistantMessage safeAssistantMessage(ChatResponse response) { + if (response == null) + return null; + Generation gen = response.getResult(); + if (gen == null) + return null; + return gen.getOutput(); + } + + /** + * Internal method to handle streaming chat completion calls with tool execution + * support. + * @param prompt the prompt for the chat completion + * @param previousChatResponse the previous chat response for accumulating usage + * @return a Flux of chat responses + */ + public Flux internalStream(Prompt prompt, ChatResponse previousChatResponse) { + return Flux.deferContextual(contextView -> { + ChatCompletionCreateParams request = createRequest(prompt, true); + ConcurrentHashMap roleMap = new ConcurrentHashMap<>(); + final ChatModelObservationContext observationContext = ChatModelObservationContext.builder() + .prompt(prompt) + .provider(AiProvider.OPENAI_OFFICIAL.value()) + .build(); + Observation observation = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION.observation( + this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, + this.observationRegistry); + observation.parentObservation(contextView.getOrDefault(ObservationThreadLocalAccessor.KEY, null)).start(); + + Flux chatResponses = Flux.create(sink -> { + this.openAiClientAsync.chat().completions().createStreaming(request).subscribe(chunk -> { + try { + ChatCompletion chatCompletion = chunkToChatCompletion(chunk); + String id = chatCompletion.id(); + List generations = chatCompletion.choices().stream().map(choice -> { + roleMap.putIfAbsent(id, choice.message()._role().asString().isPresent() + ? choice.message()._role().asStringOrThrow() : ""); + + Map metadata = Map.of("id", id, "role", roleMap.getOrDefault(id, ""), + "index", choice.index(), "finishReason", choice.finishReason().value(), "refusal", + choice.message().refusal().isPresent() ? choice.message().refusal() : "", + "annotations", choice.message().annotations().isPresent() + ? choice.message().annotations() : List.of(), + "chunkChoice", chunk.choices().get((int) choice.index())); + + return buildGeneration(choice, metadata); + }).toList(); + Optional usage = chatCompletion.usage(); + CompletionUsage usageVal = usage.orElse(null); + Usage currentUsage = usageVal != null ? getDefaultUsage(usageVal) : new EmptyUsage(); + Usage accumulated = UsageCalculator.getCumulativeUsage(currentUsage, previousChatResponse); + sink.next(new ChatResponse(generations, from(chatCompletion, accumulated))); + } + catch (Exception e) { + logger.error("Error processing chat completion", e); + sink.error(e); + } + }).onCompleteFuture().whenComplete((unused, throwable) -> { + if (throwable != null) + sink.error(throwable); + else + sink.complete(); + }); + }).buffer(2, 1).map(buffer -> { + ChatResponse first = buffer.get(0); + if (request.streamOptions().isPresent() && buffer.size() == 2) { + ChatResponse second = buffer.get(1); + if (second != null) { + Usage usage = second.getMetadata().getUsage(); + if (!UsageCalculator.isEmpty(usage)) { + return new ChatResponse(first.getResults(), from(first.getMetadata(), usage)); + } + } + } + return first; + }); + + Flux flux = chatResponses + .contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation)); + + return flux.collectList().flatMapMany(list -> { + if (list.isEmpty()) + return Flux.empty(); + boolean hasToolCalls = list.stream() + .map(this::safeAssistantMessage) + .filter(Objects::nonNull) + .anyMatch(am -> !CollectionUtils.isEmpty(am.getToolCalls())); + if (!hasToolCalls) { + if (list.size() > 2) { + ChatResponse penultimateResponse = list.get(list.size() - 2); // Get + // the + // finish + // reason + ChatResponse lastResponse = list.get(list.size() - 1); // Get the + // usage + Usage usage = lastResponse.getMetadata().getUsage(); + observationContext.setResponse(new ChatResponse(penultimateResponse.getResults(), + from(penultimateResponse.getMetadata(), usage))); + } + return Flux.fromIterable(list); + } + Map builders = new HashMap<>(); + StringBuilder text = new StringBuilder(); + ChatResponseMetadata finalMetadata = null; + ChatGenerationMetadata finalGenMetadata = null; + Map props = new HashMap<>(); + for (ChatResponse chatResponse : list) { + AssistantMessage am = safeAssistantMessage(chatResponse); + if (am == null) + continue; + if (am.getText() != null) + text.append(am.getText()); + if (am.getMetadata() != null) + props.putAll(am.getMetadata()); + if (!CollectionUtils.isEmpty(am.getToolCalls())) { + Object ccObj = am.getMetadata().get("chunkChoice"); + if (ccObj instanceof ChatCompletionChunk.Choice chunkChoice + && chunkChoice.delta().toolCalls().isPresent()) { + List deltaCalls = chunkChoice.delta() + .toolCalls() + .get(); + for (int i = 0; i < am.getToolCalls().size() && i < deltaCalls.size(); i++) { + AssistantMessage.ToolCall tc = am.getToolCalls().get(i); + ChatCompletionChunk.Choice.Delta.ToolCall dtc = deltaCalls.get(i); + String key = chunkChoice.index() + "-" + dtc.index(); + ToolCallBuilder toolCallBuilder = builders.computeIfAbsent(key, + k -> new ToolCallBuilder()); + toolCallBuilder.merge(tc); + } + } + else { + for (AssistantMessage.ToolCall tc : am.getToolCalls()) { + ToolCallBuilder toolCallBuilder = builders.computeIfAbsent(tc.id(), + k -> new ToolCallBuilder()); + toolCallBuilder.merge(tc); + } + } + } + Generation generation = chatResponse.getResult(); + if (generation != null && generation.getMetadata() != null + && generation.getMetadata() != ChatGenerationMetadata.NULL) { + finalGenMetadata = generation.getMetadata(); + } + if (chatResponse.getMetadata() != null) + finalMetadata = chatResponse.getMetadata(); + } + List merged = builders.values() + .stream() + .map(ToolCallBuilder::build) + .filter(tc -> StringUtils.hasText(tc.name())) + .toList(); + AssistantMessage.Builder assistantMessageBuilder = AssistantMessage.builder() + .content(text.toString()) + .properties(props); + if (!merged.isEmpty()) { + assistantMessageBuilder.toolCalls(merged); + } + AssistantMessage assistantMessage = assistantMessageBuilder.build(); + Generation finalGen = new Generation(assistantMessage, + finalGenMetadata != null ? finalGenMetadata : ChatGenerationMetadata.NULL); + ChatResponse aggregated = new ChatResponse(List.of(finalGen), finalMetadata); + observationContext.setResponse(aggregated); + if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(prompt.getOptions(), aggregated)) { + return Flux.deferContextual(ctx -> { + ToolExecutionResult tetoolExecutionResult; + try { + ToolCallReactiveContextHolder.setContext(ctx); + tetoolExecutionResult = this.toolCallingManager.executeToolCalls(prompt, aggregated); + } + finally { + ToolCallReactiveContextHolder.clearContext(); + } + if (tetoolExecutionResult.returnDirect()) + return Flux.just(ChatResponse.builder() + .from(aggregated) + .generations(ToolExecutionResult.buildGenerations(tetoolExecutionResult)) + .build()); + return this.internalStream( + new Prompt(tetoolExecutionResult.conversationHistory(), prompt.getOptions()), + aggregated); + }).subscribeOn(Schedulers.boundedElastic()); + } + return Flux.just(aggregated); + }).doOnError(observation::error).doFinally(s -> observation.stop()); + }); + } + + private Generation buildGeneration(ChatCompletion.Choice choice, Map metadata) { + ChatCompletionMessage message = choice.message(); + List toolCalls = new ArrayList<>(); + + if (metadata.containsKey("chunkChoice")) { + Object chunkChoiceObj = metadata.get("chunkChoice"); + if (chunkChoiceObj instanceof ChatCompletionChunk.Choice chunkChoice) { + if (chunkChoice.delta().toolCalls().isPresent()) { + toolCalls = chunkChoice.delta() + .toolCalls() + .get() + .stream() + .filter(tc -> tc.function().isPresent()) + .map(tc -> { + var funcOpt = tc.function(); + if (funcOpt.isEmpty()) + return null; + var func = funcOpt.get(); + String id = tc.id().orElse(""); + String name = func.name().orElse(""); + String arguments = func.arguments().orElse(""); + return new AssistantMessage.ToolCall(id, "function", name, arguments); + }) + .filter(Objects::nonNull) + .toList(); + } + } + } + else { + toolCalls = message.toolCalls() + .map(list -> list.stream().filter(tc -> tc.function().isPresent()).map(tc -> { + var opt = tc.function(); + if (opt.isEmpty()) + return null; + var funcCall = opt.get(); + var functionDef = funcCall.function(); + String id = funcCall.id(); + String name = functionDef.name(); + String arguments = functionDef.arguments(); + return new AssistantMessage.ToolCall(id, "function", name, arguments); + }).filter(Objects::nonNull).toList()) + .orElse(List.of()); + } + + var generationMetadataBuilder = ChatGenerationMetadata.builder() + .finishReason(choice.finishReason().value().name()); + String textContent = message.content().orElse(""); + var assistantMessage = AssistantMessage.builder() + .content(textContent) + .properties(metadata) + .toolCalls(toolCalls) + .build(); + return new Generation(assistantMessage, generationMetadataBuilder.build()); + } + + private ChatResponseMetadata from(ChatCompletion result, Usage usage) { + Assert.notNull(result, "OpenAI ChatCompletion must not be null"); + result.model(); + result.id(); + return ChatResponseMetadata.builder() + .id(result.id()) + .usage(usage) + .model(result.model()) + .keyValue("created", result.created()) + .build(); + } + + private ChatResponseMetadata from(ChatResponseMetadata chatResponseMetadata, Usage usage) { + Assert.notNull(chatResponseMetadata, "OpenAI ChatResponseMetadata must not be null"); + return ChatResponseMetadata.builder() + .id(chatResponseMetadata.getId() != null ? chatResponseMetadata.getId() : "") + .usage(usage) + .model(chatResponseMetadata.getModel() != null ? chatResponseMetadata.getModel() : "") + .build(); + } + + /** + * Convert the ChatCompletionChunk into a ChatCompletion. The Usage is set to null. + * @param chunk the ChatCompletionChunk to convert + * @return the ChatCompletion + */ + private ChatCompletion chunkToChatCompletion(ChatCompletionChunk chunk) { + List choices = chunk.choices().stream().map(chunkChoice -> { + ChatCompletion.Choice.FinishReason finishReason = ChatCompletion.Choice.FinishReason.of(""); + if (chunkChoice.finishReason().isPresent()) { + finishReason = ChatCompletion.Choice.FinishReason + .of(chunkChoice.finishReason().get().value().name().toLowerCase()); + } + + ChatCompletion.Choice.Builder choiceBuilder = ChatCompletion.Choice.builder() + .finishReason(finishReason) + .index(chunkChoice.index()) + .message(ChatCompletionMessage.builder() + .content(chunkChoice.delta().content()) + .refusal(chunkChoice.delta().refusal()) + .build()); + + // Handle optional logprobs + if (chunkChoice.logprobs().isPresent()) { + var logprobs = chunkChoice.logprobs().get(); + choiceBuilder.logprobs(ChatCompletion.Choice.Logprobs.builder() + .content(logprobs.content()) + .refusal(logprobs.refusal()) + .build()); + } + else { + // Provide empty logprobs when not present + choiceBuilder + .logprobs(ChatCompletion.Choice.Logprobs.builder().content(List.of()).refusal(List.of()).build()); + } + + return choiceBuilder.build(); + }).toList(); + + return ChatCompletion.builder() + .id(chunk.id()) + .choices(choices) + .created(chunk.created()) + .model(chunk.model()) + .usage(chunk.usage() + .orElse(CompletionUsage.builder().promptTokens(0).completionTokens(0).totalTokens(0).build())) + .build(); + } + + private DefaultUsage getDefaultUsage(CompletionUsage usage) { + return new DefaultUsage(Math.toIntExact(usage.promptTokens()), Math.toIntExact(usage.completionTokens()), + Math.toIntExact(usage.totalTokens()), usage); + } + + /** + * Builds the request prompt by merging runtime options with default options. + * @param prompt the original prompt + * @return the prompt with merged options + */ + Prompt buildRequestPrompt(Prompt prompt) { + // Process runtime options + OpenAiOfficialChatOptions runtimeOptions = null; + if (prompt.getOptions() != null) { + if (prompt.getOptions() instanceof ToolCallingChatOptions toolCallingChatOptions) { + runtimeOptions = ModelOptionsUtils.copyToTarget(toolCallingChatOptions, ToolCallingChatOptions.class, + OpenAiOfficialChatOptions.class); + } + else { + runtimeOptions = ModelOptionsUtils.copyToTarget(prompt.getOptions(), ChatOptions.class, + OpenAiOfficialChatOptions.class); + } + } + + // Define request options by merging runtime options and default options + OpenAiOfficialChatOptions requestOptions = OpenAiOfficialChatOptions.builder() + .from(this.options) + .merge(runtimeOptions != null ? runtimeOptions : OpenAiOfficialChatOptions.builder().build()) + .build(); + + // Merge @JsonIgnore-annotated options explicitly since they are ignored by + // Jackson, used by ModelOptionsUtils. + if (runtimeOptions != null) { + if (runtimeOptions.getTopK() != null) { + logger.warn("The topK option is not supported by OpenAI chat models. Ignoring."); + } + + Map mergedHttpHeaders = new HashMap<>(this.options.getHttpHeaders()); + mergedHttpHeaders.putAll(runtimeOptions.getHttpHeaders()); + requestOptions.setHttpHeaders(mergedHttpHeaders); + + requestOptions.setInternalToolExecutionEnabled(runtimeOptions.getInternalToolExecutionEnabled() != null + ? runtimeOptions.getInternalToolExecutionEnabled() + : this.options.getInternalToolExecutionEnabled()); + requestOptions.setToolNames( + ToolCallingChatOptions.mergeToolNames(runtimeOptions.getToolNames(), this.options.getToolNames())); + requestOptions.setToolCallbacks(ToolCallingChatOptions.mergeToolCallbacks(runtimeOptions.getToolCallbacks(), + this.options.getToolCallbacks())); + requestOptions.setToolContext(ToolCallingChatOptions.mergeToolContext(runtimeOptions.getToolContext(), + this.options.getToolContext())); + } + else { + requestOptions.setHttpHeaders(this.options.getHttpHeaders()); + requestOptions.setInternalToolExecutionEnabled(this.options.getInternalToolExecutionEnabled()); + requestOptions.setToolNames(this.options.getToolNames()); + requestOptions.setToolCallbacks(this.options.getToolCallbacks()); + requestOptions.setToolContext(this.options.getToolContext()); + } + + ToolCallingChatOptions.validateToolCallbacks(requestOptions.getToolCallbacks()); + + return new Prompt(prompt.getInstructions(), requestOptions); + } + + /** + * Creates a chat completion request from the given prompt. + * @param prompt the prompt containing messages and options + * @param stream whether this is a streaming request + * @return the chat completion create parameters + */ + ChatCompletionCreateParams createRequest(Prompt prompt, boolean stream) { + + List chatCompletionMessageParams = prompt.getInstructions() + .stream() + .map(message -> { + if (message.getMessageType() == MessageType.USER || message.getMessageType() == MessageType.SYSTEM) { + // Handle simple text content for user and system messages + ChatCompletionUserMessageParam.Builder builder = ChatCompletionUserMessageParam.builder(); + + if (message instanceof UserMessage userMessage + && !CollectionUtils.isEmpty(userMessage.getMedia())) { + // Handle media content (images, audio, files) + List parts = new ArrayList<>(); + + if (!message.getText().isEmpty()) { + parts.add(ChatCompletionContentPart + .ofText(ChatCompletionContentPartText.builder().text(message.getText()).build())); + } + + // Add media content parts + userMessage.getMedia().forEach(media -> { + String mimeType = media.getMimeType().toString(); + if (mimeType.startsWith("image/")) { + if (media.getData() instanceof java.net.URI uri) { + parts.add(ChatCompletionContentPart + .ofImageUrl(ChatCompletionContentPartImage.builder() + .imageUrl(ChatCompletionContentPartImage.ImageUrl.builder() + .url(uri.toString()) + .build()) + .build())); + } + else if (media.getData() instanceof String text) { + // The org.springframework.ai.content.Media object + // should store the URL as a java.net.URI but it + // transforms it to String somewhere along the way, + // for example in its Builder class. So, we accept + // String as well here for image URLs. + parts.add(ChatCompletionContentPart + .ofImageUrl(ChatCompletionContentPartImage.builder() + .imageUrl( + ChatCompletionContentPartImage.ImageUrl.builder().url(text).build()) + .build())); + } + else if (media.getData() instanceof byte[] bytes) { + // Assume the bytes are an image. So, convert the + // bytes to a base64 encoded + ChatCompletionContentPartImage.ImageUrl.Builder imageUrlBuilder = ChatCompletionContentPartImage.ImageUrl + .builder(); + + imageUrlBuilder.url("data:" + mimeType + ";base64," + + Base64.getEncoder().encodeToString(bytes)); + parts.add(ChatCompletionContentPart + .ofImageUrl(ChatCompletionContentPartImage.builder() + .imageUrl(imageUrlBuilder.build()) + .build())); + } + else { + logger.info( + "Could not process image media with data of type: {}. Only java.net.URI is supported for image URLs.", + media.getData().getClass().getSimpleName()); + } + } + else if (mimeType.startsWith("audio/")) { + parts.add(ChatCompletionContentPart + .ofInputAudio(ChatCompletionContentPartInputAudio.builder() + .inputAudio(ChatCompletionContentPartInputAudio.builder() + .inputAudio(ChatCompletionContentPartInputAudio.InputAudio.builder() + .data(fromAudioData(media.getData())) + .format(mimeType.contains("mp3") + ? ChatCompletionContentPartInputAudio.InputAudio.Format.MP3 + : ChatCompletionContentPartInputAudio.InputAudio.Format.WAV) + .build()) + .build() + .inputAudio()) + .build())); + } + else { + // Assume it's a file or other media type represented as a + // data URL + parts.add(ChatCompletionContentPart.ofText(ChatCompletionContentPartText.builder() + .text(fromMediaData(media.getMimeType(), media.getData())) + .build())); + } + }); + builder.contentOfArrayOfContentParts(parts); + } + else { + // Simple text message + builder.content(ChatCompletionContentPartText.builder().text(message.getText()).build().text()); + } + + if (message.getMessageType() == MessageType.USER) { + builder.role(JsonValue.from(MessageType.USER.getValue())); + } + else { + builder.role(JsonValue.from(MessageType.SYSTEM.getValue())); + } + + return List.of(ChatCompletionMessageParam.ofUser(builder.build())); + } + else if (message.getMessageType() == MessageType.ASSISTANT) { + var assistantMessage = (AssistantMessage) message; + ChatCompletionAssistantMessageParam.Builder builder = ChatCompletionAssistantMessageParam.builder() + .role(JsonValue.from(MessageType.ASSISTANT.getValue())); + + if (assistantMessage.getText() != null) { + builder.content(ChatCompletionAssistantMessageParam.builder() + .content(assistantMessage.getText()) + .build() + .content()); + } + + if (!CollectionUtils.isEmpty(assistantMessage.getToolCalls())) { + List toolCalls = assistantMessage.getToolCalls() + .stream() + .map(toolCall -> ChatCompletionMessageToolCall + .ofFunction(ChatCompletionMessageFunctionToolCall.builder() + .id(toolCall.id()) + .function(ChatCompletionMessageFunctionToolCall.Function.builder() + .name(toolCall.name()) + .arguments(toolCall.arguments()) + .build()) + .build())) + .toList(); + + builder.toolCalls(toolCalls); + } + + return List.of(ChatCompletionMessageParam.ofAssistant(builder.build())); + } + else if (message.getMessageType() == MessageType.TOOL) { + ToolResponseMessage toolMessage = (ToolResponseMessage) message; + + ChatCompletionToolMessageParam.Builder builder = ChatCompletionToolMessageParam.builder(); + builder.content(toolMessage.getText() != null ? toolMessage.getText() : ""); + builder.role(JsonValue.from(MessageType.TOOL.getValue())); + + if (toolMessage.getResponses().isEmpty()) { + return List.of(ChatCompletionMessageParam.ofTool(builder.build())); + } + return toolMessage.getResponses().stream().map(response -> { + String callId = response.id(); + String callResponse = response.responseData(); + + return ChatCompletionMessageParam + .ofTool(builder.toolCallId(callId).content(callResponse).build()); + }).toList(); + } + else { + throw new IllegalArgumentException("Unsupported message type: " + message.getMessageType()); + } + }) + .flatMap(List::stream) + .toList(); + + ChatCompletionCreateParams.Builder builder = ChatCompletionCreateParams.builder(); + + chatCompletionMessageParams.forEach(builder::addMessage); + + OpenAiOfficialChatOptions requestOptions = (OpenAiOfficialChatOptions) prompt.getOptions(); + + // Use deployment name if available (for Azure AI Foundry), otherwise use model + // name + if (requestOptions.getDeploymentName() != null) { + builder.model(requestOptions.getDeploymentName()); + } + else if (requestOptions.getModel() != null) { + builder.model(requestOptions.getModel()); + } + + if (requestOptions.getFrequencyPenalty() != null) { + builder.frequencyPenalty(requestOptions.getFrequencyPenalty()); + } + if (requestOptions.getLogitBias() != null) { + builder.logitBias(ChatCompletionCreateParams.LogitBias.builder() + .putAllAdditionalProperties(requestOptions.getLogitBias() + .entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> JsonValue.from(entry.getValue())))) + .build()); + } + if (requestOptions.getLogprobs() != null) { + builder.logprobs(requestOptions.getLogprobs()); + } + if (requestOptions.getTopLogprobs() != null) { + builder.topLogprobs(requestOptions.getTopLogprobs()); + } + if (requestOptions.getMaxTokens() != null) { + builder.maxTokens(requestOptions.getMaxTokens()); + } + if (requestOptions.getMaxCompletionTokens() != null) { + builder.maxCompletionTokens(requestOptions.getMaxCompletionTokens()); + } + if (requestOptions.getN() != null) { + builder.n(requestOptions.getN()); + } + if (requestOptions.getOutputAudio() != null) { + builder.audio(requestOptions.getOutputAudio()); + } + if (requestOptions.getPresencePenalty() != null) { + builder.presencePenalty(requestOptions.getPresencePenalty()); + } + if (requestOptions.getResponseFormat() != null) { + ResponseFormat responseFormat = requestOptions.getResponseFormat(); + if (responseFormat.getType().equals(ResponseFormat.Type.TEXT)) { + builder.responseFormat(ResponseFormatText.builder().build()); + } + else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_OBJECT)) { + builder.responseFormat(ResponseFormatJsonObject.builder().build()); + } + else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_SCHEMA)) { + String jsonSchemaString = responseFormat.getJsonSchema() != null ? responseFormat.getJsonSchema() : ""; + try { + com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); + ResponseFormatJsonSchema.JsonSchema.Builder jsonSchemaBuilder = ResponseFormatJsonSchema.JsonSchema + .builder(); + jsonSchemaBuilder.name("json_schema"); + jsonSchemaBuilder.strict(true); + + ResponseFormatJsonSchema.JsonSchema.Schema schema = mapper.readValue(jsonSchemaString, + ResponseFormatJsonSchema.JsonSchema.Schema.class); + + jsonSchemaBuilder.schema(schema); + + builder.responseFormat( + ResponseFormatJsonSchema.builder().jsonSchema(jsonSchemaBuilder.build()).build()); + } + catch (Exception e) { + throw new IllegalArgumentException("Failed to parse JSON schema: " + jsonSchemaString, e); + } + } + else { + throw new IllegalArgumentException("Unsupported response format type: " + responseFormat.getType()); + } + } + if (requestOptions.getSeed() != null) { + builder.seed(requestOptions.getSeed()); + } + if (requestOptions.getStop() != null && !requestOptions.getStop().isEmpty()) { + if (requestOptions.getStop().size() == 1) { + builder.stop(ChatCompletionCreateParams.Stop.ofString(requestOptions.getStop().get(0))); + } + else { + builder.stop(ChatCompletionCreateParams.Stop.ofStrings(requestOptions.getStop())); + } + } + if (requestOptions.getTemperature() != null) { + builder.temperature(requestOptions.getTemperature()); + } + if (requestOptions.getTopP() != null) { + builder.topP(requestOptions.getTopP()); + } + if (requestOptions.getUser() != null) { + builder.user(requestOptions.getUser()); + } + if (requestOptions.getParallelToolCalls() != null) { + builder.parallelToolCalls(requestOptions.getParallelToolCalls()); + } + if (requestOptions.getReasoningEffort() != null) { + builder.reasoningEffort(ReasoningEffort.of(requestOptions.getReasoningEffort().toLowerCase())); + } + if (requestOptions.getVerbosity() != null) { + builder.verbosity(ChatCompletionCreateParams.Verbosity.of(requestOptions.getVerbosity())); + } + + if (requestOptions.getStore() != null) { + builder.store(requestOptions.getStore()); + } + if (requestOptions.getMetadata() != null && !requestOptions.getMetadata().isEmpty()) { + builder.metadata(ChatCompletionCreateParams.Metadata.builder() + .putAllAdditionalProperties(requestOptions.getMetadata() + .entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> JsonValue.from(entry.getValue())))) + .build()); + } + if (requestOptions.getServiceTier() != null) { + builder.serviceTier(ChatCompletionCreateParams.ServiceTier.of(requestOptions.getServiceTier())); + } + + if (stream) { + if (requestOptions.getStreamOptions() != null) { + ChatCompletionStreamOptions.Builder streamOptionsBuilder = ChatCompletionStreamOptions.builder(); + + if (requestOptions.getStreamOptions().includeObfuscation().isPresent()) { + streamOptionsBuilder + .includeObfuscation(requestOptions.getStreamOptions().includeObfuscation().get()); + } + streamOptionsBuilder.additionalProperties(requestOptions.getStreamOptions()._additionalProperties()); + streamOptionsBuilder.includeUsage(requestOptions.getStreamUsage()); + builder.streamOptions(streamOptionsBuilder.build()); + } + else { + builder.streamOptions(ChatCompletionStreamOptions.builder() + .includeUsage(true) // Include usage by default for streaming + .build()); + } + } + + // Add the tool definitions to the request's tools parameter. + List toolDefinitions = this.toolCallingManager.resolveToolDefinitions(requestOptions); + if (!CollectionUtils.isEmpty(toolDefinitions)) { + builder.tools(getChatCompletionTools(toolDefinitions)); + } + + if (requestOptions.getToolChoice() != null) { + builder.toolChoice(requestOptions.getToolChoice()); + } + + return builder.build(); + } + + private String fromAudioData(Object audioData) { + if (audioData instanceof byte[] bytes) { + return Base64.getEncoder().encodeToString(bytes); + } + throw new IllegalArgumentException("Unsupported audio data type: " + audioData.getClass().getSimpleName()); + } + + private String fromMediaData(org.springframework.util.MimeType mimeType, Object mediaContentData) { + if (mediaContentData instanceof byte[] bytes) { + // Assume the bytes are an image. So, convert the bytes to a base64 encoded + // following the prefix pattern. + return String.format("data:%s;base64,%s", mimeType.toString(), Base64.getEncoder().encodeToString(bytes)); + } + else if (mediaContentData instanceof String text) { + // Assume the text is a URLs or a base64 encoded image prefixed by the user. + return text; + } + else { + throw new IllegalArgumentException( + "Unsupported media data type: " + mediaContentData.getClass().getSimpleName()); + } + } + + private List getChatCompletionTools(List toolDefinitions) { + return toolDefinitions.stream().map(toolDefinition -> { + FunctionParameters.Builder parametersBuilder = FunctionParameters.builder(); + + if (!toolDefinition.inputSchema().isEmpty()) { + // Parse the schema and add its properties directly + try { + com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); + @SuppressWarnings("unchecked") + Map schemaMap = mapper.readValue(toolDefinition.inputSchema(), Map.class); + + // Add each property from the schema to the parameters + schemaMap + .forEach((key, value) -> parametersBuilder.putAdditionalProperty(key, JsonValue.from(value))); + + // Add strict mode + parametersBuilder.putAdditionalProperty("strict", JsonValue.from(true)); // TODO + // allow + // non-strict + // mode + } + catch (Exception e) { + logger.error("Failed to parse tool schema", e); + } + } + + FunctionDefinition functionDefinition = FunctionDefinition.builder() + .name(toolDefinition.name()) + .description(toolDefinition.description()) + .parameters(parametersBuilder.build()) + .build(); + + return ChatCompletionTool + .ofFunction(ChatCompletionFunctionTool.builder().function(functionDefinition).build()); + }).toList(); + } + + @Override + public ChatOptions getDefaultOptions() { + return this.options.copy(); + } + + /** + * Use the provided convention for reporting observation data + * @param observationConvention The provided convention + */ + public void setObservationConvention(ChatModelObservationConvention observationConvention) { + Assert.notNull(observationConvention, "observationConvention cannot be null"); + this.observationConvention = observationConvention; + } + + /** + * Response format (text, json_object, json_schema) for OpenAiOfficialChatModel + * responses. + * + * @author Julien Dubois + */ + public static class ResponseFormat { + + private Type type = Type.TEXT; + + private String jsonSchema; + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } + + public String getJsonSchema() { + return jsonSchema; + } + + public void setJsonSchema(String jsonSchema) { + this.jsonSchema = jsonSchema; + } + + public static Builder builder() { + return new Builder(); + } + + public static final class Builder { + + private final ResponseFormat responseFormat = new ResponseFormat(); + + private Builder() { + } + + public Builder type(Type type) { + this.responseFormat.setType(type); + return this; + } + + public Builder jsonSchema(String jsonSchema) { + this.responseFormat.setType(Type.JSON_SCHEMA); + this.responseFormat.setJsonSchema(jsonSchema); + return this; + } + + public ResponseFormat build() { + return this.responseFormat; + } + + } + + public enum Type { + + /** + * Generates a text response. (default) + */ + TEXT, + + /** + * Enables JSON mode, which guarantees the message the model generates is + * valid JSON. + */ + JSON_OBJECT, + + /** + * Enables Structured Outputs which guarantees the model will match your + * supplied JSON schema. + */ + JSON_SCHEMA + + } + + } + + /** + * Helper class to merge streaming tool calls that arrive in pieces across multiple + * chunks. In OpenAI streaming, a tool call's ID, name, and arguments can arrive in + * separate chunks. + */ + private static class ToolCallBuilder { + + private String id = ""; + + private String type = "function"; + + private String name = ""; + + private StringBuilder arguments = new StringBuilder(); + + void merge(AssistantMessage.ToolCall toolCall) { + if (toolCall.id() != null && !toolCall.id().isEmpty()) { + this.id = toolCall.id(); + } + if (toolCall.type() != null && !toolCall.type().isEmpty()) { + this.type = toolCall.type(); + } + if (toolCall.name() != null && !toolCall.name().isEmpty()) { + this.name = toolCall.name(); + } + if (toolCall.arguments() != null && !toolCall.arguments().isEmpty()) { + this.arguments.append(toolCall.arguments()); + } + } - AssistantMessage.ToolCall build() { - return new AssistantMessage.ToolCall(id, type, name, arguments.toString()); - } + AssistantMessage.ToolCall build() { + return new AssistantMessage.ToolCall(id, type, name, arguments.toString()); + } - } + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java index b051df91c90..f22218ffc6c 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java @@ -45,932 +45,932 @@ */ public class OpenAiOfficialChatOptions extends AbstractOpenAiOfficialOptions implements ToolCallingChatOptions { - public static final String DEFAULT_CHAT_MODEL = GPT_5_MINI.asString(); + public static final String DEFAULT_CHAT_MODEL = GPT_5_MINI.asString(); - private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatOptions.class); + private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatOptions.class); - private Double frequencyPenalty; + private Double frequencyPenalty; - private Map logitBias; + private Map logitBias; - private Boolean logprobs; + private Boolean logprobs; - private Integer topLogprobs; + private Integer topLogprobs; - private Integer maxTokens; + private Integer maxTokens; - private Integer maxCompletionTokens; + private Integer maxCompletionTokens; - private Integer n; + private Integer n; - private ChatCompletionAudioParam outputAudio; + private ChatCompletionAudioParam outputAudio; - private Double presencePenalty; + private Double presencePenalty; - private OpenAiOfficialChatModel.ResponseFormat responseFormat; + private OpenAiOfficialChatModel.ResponseFormat responseFormat; - private ResponseCreateParams.StreamOptions streamOptions; + private ResponseCreateParams.StreamOptions streamOptions; - private Boolean streamUsage; + private Boolean streamUsage; - private Integer seed; + private Integer seed; - private List stop; + private List stop; - private Double temperature; + private Double temperature; - private Double topP; + private Double topP; - private List tools; + private List tools; - private ChatCompletionToolChoiceOption toolChoice; + private ChatCompletionToolChoiceOption toolChoice; - private String user; + private String user; - private Boolean parallelToolCalls; + private Boolean parallelToolCalls; - private Boolean store; + private Boolean store; - private Map metadata; + private Map metadata; - private String reasoningEffort; + private String reasoningEffort; - private String verbosity; + private String verbosity; - private String serviceTier; + private String serviceTier; - private List toolCallbacks = new ArrayList<>(); + private List toolCallbacks = new ArrayList<>(); - private Set toolNames = new HashSet<>(); + private Set toolNames = new HashSet<>(); - private Boolean internalToolExecutionEnabled; + private Boolean internalToolExecutionEnabled; - private Map httpHeaders = new HashMap<>(); + private Map httpHeaders = new HashMap<>(); - private Map toolContext = new HashMap<>(); + private Map toolContext = new HashMap<>(); - /** - * Gets the frequency penalty parameter. - * @return the frequency penalty - */ - @Override - public Double getFrequencyPenalty() { - return this.frequencyPenalty; - } + /** + * Gets the frequency penalty parameter. + * @return the frequency penalty + */ + @Override + public Double getFrequencyPenalty() { + return this.frequencyPenalty; + } - /** - * Sets the frequency penalty parameter. - * @param frequencyPenalty the frequency penalty to set - */ - public void setFrequencyPenalty(Double frequencyPenalty) { - this.frequencyPenalty = frequencyPenalty; - } + /** + * Sets the frequency penalty parameter. + * @param frequencyPenalty the frequency penalty to set + */ + public void setFrequencyPenalty(Double frequencyPenalty) { + this.frequencyPenalty = frequencyPenalty; + } - /** - * Gets the logit bias map. - * @return the logit bias map - */ - public Map getLogitBias() { - return this.logitBias; - } + /** + * Gets the logit bias map. + * @return the logit bias map + */ + public Map getLogitBias() { + return this.logitBias; + } - /** - * Sets the logit bias map. - * @param logitBias the logit bias map to set - */ - public void setLogitBias(Map logitBias) { - this.logitBias = logitBias; - } + /** + * Sets the logit bias map. + * @param logitBias the logit bias map to set + */ + public void setLogitBias(Map logitBias) { + this.logitBias = logitBias; + } - /** - * Gets whether to return log probabilities. - * @return true if log probabilities should be returned - */ - public Boolean getLogprobs() { - return this.logprobs; - } - - /** - * Sets whether to return log probabilities. - * @param logprobs whether to return log probabilities - */ - public void setLogprobs(Boolean logprobs) { - this.logprobs = logprobs; - } - - /** - * Gets the number of top log probabilities to return. - * @return the number of top log probabilities - */ - public Integer getTopLogprobs() { - return this.topLogprobs; - } - - /** - * Sets the number of top log probabilities to return. - * @param topLogprobs the number of top log probabilities - */ - public void setTopLogprobs(Integer topLogprobs) { - this.topLogprobs = topLogprobs; - } - - @Override - public Integer getMaxTokens() { - return this.maxTokens; - } - - /** - * Sets the maximum number of tokens to generate. - * @param maxTokens the maximum number of tokens - */ - public void setMaxTokens(Integer maxTokens) { - this.maxTokens = maxTokens; - } - - /** - * Gets the maximum number of completion tokens. - * @return the maximum number of completion tokens - */ - public Integer getMaxCompletionTokens() { - return this.maxCompletionTokens; - } - - /** - * Sets the maximum number of completion tokens. - * @param maxCompletionTokens the maximum number of completion tokens - */ - public void setMaxCompletionTokens(Integer maxCompletionTokens) { - this.maxCompletionTokens = maxCompletionTokens; - } - - /** - * Gets the number of completions to generate. - * @return the number of completions - */ - public Integer getN() { - return this.n; - } - - /** - * Sets the number of completions to generate. - * @param n the number of completions - */ - public void setN(Integer n) { - this.n = n; - } - - /** - * Gets the output audio parameters. - * @return the output audio parameters - */ - public ChatCompletionAudioParam getOutputAudio() { - return this.outputAudio; - } - - /** - * Sets the output audio parameters. - * @param outputAudio the output audio parameters - */ - public void setOutputAudio(ChatCompletionAudioParam outputAudio) { - this.outputAudio = outputAudio; - } - - @Override - public Double getPresencePenalty() { - return this.presencePenalty; - } - - /** - * Sets the presence penalty parameter. - * @param presencePenalty the presence penalty to set - */ - public void setPresencePenalty(Double presencePenalty) { - this.presencePenalty = presencePenalty; - } - - /** - * Gets the response format configuration. - * @return the response format - */ - public OpenAiOfficialChatModel.ResponseFormat getResponseFormat() { - return this.responseFormat; - } - - /** - * Sets the response format configuration. - * @param responseFormat the response format to set - */ - public void setResponseFormat(OpenAiOfficialChatModel.ResponseFormat responseFormat) { - this.responseFormat = responseFormat; - } - - /** - * Gets the stream options. - * @return the stream options - */ - public ResponseCreateParams.StreamOptions getStreamOptions() { - return this.streamOptions; - } - - /** - * Sets the stream options. - * @param streamOptions the stream options to set - */ - public void setStreamOptions(ResponseCreateParams.StreamOptions streamOptions) { - this.streamOptions = streamOptions; - } - - /** - * Gets whether to include usage information in streaming responses. - * @return true if usage should be included in streams - */ - public Boolean getStreamUsage() { - return this.streamUsage; - } - - /** - * Sets whether to include usage information in streaming responses. - * @param streamUsage whether to include usage in streams - */ - public void setStreamUsage(Boolean streamUsage) { - this.streamUsage = streamUsage; - } - - /** - * Gets the random seed for deterministic generation. - * @return the random seed - */ - public Integer getSeed() { - return this.seed; - } - - /** - * Sets the random seed for deterministic generation. - * @param seed the random seed - */ - public void setSeed(Integer seed) { - this.seed = seed; - } - - /** - * Gets the stop sequences. - * @return the list of stop sequences - */ - public List getStop() { - return this.stop; - } - - /** - * Sets the stop sequences. - * @param stop the list of stop sequences - */ - public void setStop(List stop) { - this.stop = stop; - } - - @Override - public List getStopSequences() { - return getStop(); - } - - /** - * Sets the stop sequences. - * @param stopSequences the list of stop sequences - */ - public void setStopSequences(List stopSequences) { - setStop(stopSequences); - } - - @Override - public Double getTemperature() { - return this.temperature; - } - - /** - * Sets the temperature for sampling. - * @param temperature the temperature value - */ - public void setTemperature(Double temperature) { - this.temperature = temperature; - } - - @Override - public Double getTopP() { - return this.topP; - } - - /** - * Sets the top-p nucleus sampling parameter. - * @param topP the top-p value - */ - public void setTopP(Double topP) { - this.topP = topP; - } - - /** - * Gets the list of tool definitions. - * @return the list of tools - */ - public List getTools() { - return this.tools; - } - - /** - * Sets the list of tool definitions. - * @param tools the list of tools - */ - public void setTools(List tools) { - this.tools = tools; - } - - /** - * Gets the tool choice configuration. - * @return the tool choice option - */ - public ChatCompletionToolChoiceOption getToolChoice() { - return this.toolChoice; - } - - /** - * Sets the tool choice configuration. - * @param toolChoice the tool choice option - */ - public void setToolChoice(ChatCompletionToolChoiceOption toolChoice) { - this.toolChoice = toolChoice; - } - - /** - * Gets the user identifier. - * @return the user identifier - */ - public String getUser() { - return this.user; - } - - /** - * Sets the user identifier. - * @param user the user identifier - */ - public void setUser(String user) { - this.user = user; - } - - /** - * Gets whether to enable parallel tool calls. - * @return true if parallel tool calls are enabled - */ - public Boolean getParallelToolCalls() { - return this.parallelToolCalls; - } - - /** - * Sets whether to enable parallel tool calls. - * @param parallelToolCalls whether to enable parallel tool calls - */ - public void setParallelToolCalls(Boolean parallelToolCalls) { - this.parallelToolCalls = parallelToolCalls; - } - - /** - * Gets whether to store the conversation. - * @return true if the conversation should be stored - */ - public Boolean getStore() { - return this.store; - } - - /** - * Sets whether to store the conversation. - * @param store whether to store the conversation - */ - public void setStore(Boolean store) { - this.store = store; - } - - /** - * Gets the metadata map. - * @return the metadata map - */ - public Map getMetadata() { - return this.metadata; - } - - /** - * Sets the metadata map. - * @param metadata the metadata map - */ - public void setMetadata(Map metadata) { - this.metadata = metadata; - } - - /** - * Gets the reasoning effort level. - * @return the reasoning effort level - */ - public String getReasoningEffort() { - return this.reasoningEffort; - } - - /** - * Sets the reasoning effort level. - * @param reasoningEffort the reasoning effort level - */ - public void setReasoningEffort(String reasoningEffort) { - this.reasoningEffort = reasoningEffort; - } - - /** - * Gets the verbosity level. - * @return the verbosity level - */ - public String getVerbosity() { - return this.verbosity; - } - - /** - * Sets the verbosity level. - * @param verbosity the verbosity level - */ - public void setVerbosity(String verbosity) { - this.verbosity = verbosity; - } - - /** - * Gets the service tier. - * @return the service tier - */ - public String getServiceTier() { - return this.serviceTier; - } - - /** - * Sets the service tier. - * @param serviceTier the service tier - */ - public void setServiceTier(String serviceTier) { - this.serviceTier = serviceTier; - } - - @Override - public List getToolCallbacks() { - return this.toolCallbacks; - } - - @Override - public void setToolCallbacks(List toolCallbacks) { - Assert.notNull(toolCallbacks, "toolCallbacks cannot be null"); - Assert.noNullElements(toolCallbacks, "toolCallbacks cannot contain null elements"); - this.toolCallbacks = toolCallbacks; - } - - @Override - public Set getToolNames() { - return this.toolNames; - } - - @Override - public void setToolNames(Set toolNames) { - Assert.notNull(toolNames, "toolNames cannot be null"); - Assert.noNullElements(toolNames, "toolNames cannot contain null elements"); - toolNames.forEach(tool -> Assert.hasText(tool, "toolNames cannot contain empty elements")); - this.toolNames = toolNames; - } - - @Override - @Nullable - public Boolean getInternalToolExecutionEnabled() { - return this.internalToolExecutionEnabled; - } - - @Override - public void setInternalToolExecutionEnabled(@Nullable Boolean internalToolExecutionEnabled) { - this.internalToolExecutionEnabled = internalToolExecutionEnabled; - } - - /** - * Gets the HTTP headers to include in requests. - * @return the HTTP headers map - */ - public Map getHttpHeaders() { - return this.httpHeaders; - } - - /** - * Sets the HTTP headers to include in requests. - * @param httpHeaders the HTTP headers map - */ - public void setHttpHeaders(Map httpHeaders) { - this.httpHeaders = httpHeaders; - } - - @Override - public Map getToolContext() { - return this.toolContext; - } - - @Override - public void setToolContext(Map toolContext) { - this.toolContext = toolContext; - } - - @Override - public Integer getTopK() { - return null; - } - - public static Builder builder() { - return new Builder(); - } - - @Override - public OpenAiOfficialChatOptions copy() { - return builder().from(this).build(); - } - - @Override - public boolean equals(Object o) { - if (o == null || getClass() != o.getClass()) - return false; - OpenAiOfficialChatOptions options = (OpenAiOfficialChatOptions) o; - return Objects.equals(getModel(), options.getModel()) - && Objects.equals(frequencyPenalty, options.frequencyPenalty) - && Objects.equals(logitBias, options.logitBias) && Objects.equals(logprobs, options.logprobs) - && Objects.equals(topLogprobs, options.topLogprobs) && Objects.equals(maxTokens, options.maxTokens) - && Objects.equals(maxCompletionTokens, options.maxCompletionTokens) && Objects.equals(n, options.n) - && Objects.equals(outputAudio, options.outputAudio) - && Objects.equals(presencePenalty, options.presencePenalty) - && Objects.equals(responseFormat, options.responseFormat) - && Objects.equals(streamOptions, options.streamOptions) - && Objects.equals(streamUsage, options.streamUsage) && Objects.equals(seed, options.seed) - && Objects.equals(stop, options.stop) && Objects.equals(temperature, options.temperature) - && Objects.equals(topP, options.topP) && Objects.equals(tools, options.tools) - && Objects.equals(toolChoice, options.toolChoice) && Objects.equals(user, options.user) - && Objects.equals(parallelToolCalls, options.parallelToolCalls) && Objects.equals(store, options.store) - && Objects.equals(metadata, options.metadata) - && Objects.equals(reasoningEffort, options.reasoningEffort) - && Objects.equals(verbosity, options.verbosity) && Objects.equals(serviceTier, options.serviceTier) - && Objects.equals(toolCallbacks, options.toolCallbacks) && Objects.equals(toolNames, options.toolNames) - && Objects.equals(internalToolExecutionEnabled, options.internalToolExecutionEnabled) - && Objects.equals(httpHeaders, options.httpHeaders) && Objects.equals(toolContext, options.toolContext); - } - - @Override - public int hashCode() { - return Objects.hash(getModel(), frequencyPenalty, logitBias, logprobs, topLogprobs, maxTokens, - maxCompletionTokens, n, outputAudio, presencePenalty, responseFormat, streamOptions, streamUsage, seed, - stop, temperature, topP, tools, toolChoice, user, parallelToolCalls, store, metadata, reasoningEffort, - verbosity, serviceTier, toolCallbacks, toolNames, internalToolExecutionEnabled, httpHeaders, - toolContext); - } - - @Override - public String toString() { - return "OpenAiOfficialChatOptions{" + "model='" + getModel() + ", frequencyPenalty=" + frequencyPenalty - + ", logitBias=" + logitBias + ", logprobs=" + logprobs + ", topLogprobs=" + topLogprobs - + ", maxTokens=" + maxTokens + ", maxCompletionTokens=" + maxCompletionTokens + ", n=" + n - + ", outputAudio=" + outputAudio + ", presencePenalty=" + presencePenalty + ", responseFormat=" - + responseFormat + ", streamOptions=" + streamOptions + ", streamUsage=" + streamUsage + ", seed=" - + seed + ", stop=" + stop + ", temperature=" + temperature + ", topP=" + topP + ", tools=" + tools - + ", toolChoice=" + toolChoice + ", user='" + user + '\'' + ", parallelToolCalls=" + parallelToolCalls - + ", store=" + store + ", metadata=" + metadata + ", reasoningEffort='" + reasoningEffort + '\'' - + ", verbosity='" + verbosity + '\'' + ", serviceTier='" + serviceTier + '\'' + ", toolCallbacks=" - + toolCallbacks + ", toolNames=" + toolNames + ", internalToolExecutionEnabled=" - + internalToolExecutionEnabled + ", httpHeaders=" + httpHeaders + ", toolContext=" + toolContext + '}'; - } - - public static final class Builder { - - private final OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); - - public Builder from(OpenAiOfficialChatOptions fromOptions) { - this.options.setModel(fromOptions.getModel()); - this.options.setDeploymentName(fromOptions.getDeploymentName()); - this.options.setFrequencyPenalty(fromOptions.getFrequencyPenalty()); - this.options.setLogitBias(fromOptions.getLogitBias()); - this.options.setLogprobs(fromOptions.getLogprobs()); - this.options.setTopLogprobs(fromOptions.getTopLogprobs()); - this.options.setMaxTokens(fromOptions.getMaxTokens()); - this.options.setMaxCompletionTokens(fromOptions.getMaxCompletionTokens()); - this.options.setN(fromOptions.getN()); - this.options.setOutputAudio(fromOptions.getOutputAudio()); - this.options.setPresencePenalty(fromOptions.getPresencePenalty()); - this.options.setResponseFormat(fromOptions.getResponseFormat()); - this.options.setStreamOptions(fromOptions.getStreamOptions()); - this.options.setStreamUsage(fromOptions.getStreamUsage()); - this.options.setSeed(fromOptions.getSeed()); - this.options.setStop(fromOptions.getStop() != null ? new ArrayList<>(fromOptions.getStop()) : null); - this.options.setTemperature(fromOptions.getTemperature()); - this.options.setTopP(fromOptions.getTopP()); - this.options.setTools(fromOptions.getTools()); - this.options.setToolChoice(fromOptions.getToolChoice()); - this.options.setUser(fromOptions.getUser()); - this.options.setParallelToolCalls(fromOptions.getParallelToolCalls()); - this.options.setToolCallbacks(new ArrayList<>(fromOptions.getToolCallbacks())); - this.options.setToolNames(new HashSet<>(fromOptions.getToolNames())); - this.options.setHttpHeaders( - fromOptions.getHttpHeaders() != null ? new HashMap<>(fromOptions.getHttpHeaders()) : null); - this.options.setInternalToolExecutionEnabled(fromOptions.getInternalToolExecutionEnabled()); - this.options.setToolContext(new HashMap<>(fromOptions.getToolContext())); - this.options.setStore(fromOptions.getStore()); - this.options.setMetadata(fromOptions.getMetadata()); - this.options.setReasoningEffort(fromOptions.getReasoningEffort()); - this.options.setVerbosity(fromOptions.getVerbosity()); - this.options.setServiceTier(fromOptions.getServiceTier()); - return this; - } - - public Builder merge(OpenAiOfficialChatOptions from) { - if (from.getModel() != null) { - this.options.setModel(from.getModel()); - } - if (from.getDeploymentName() != null) { - this.options.setDeploymentName(from.getDeploymentName()); - } - if (from.getFrequencyPenalty() != null) { - this.options.setFrequencyPenalty(from.getFrequencyPenalty()); - } - if (from.getLogitBias() != null) { - this.options.setLogitBias(from.getLogitBias()); - } - if (from.getLogprobs() != null) { - this.options.setLogprobs(from.getLogprobs()); - } - if (from.getTopLogprobs() != null) { - this.options.setTopLogprobs(from.getTopLogprobs()); - } - if (from.getMaxTokens() != null) { - this.options.setMaxTokens(from.getMaxTokens()); - } - if (from.getMaxCompletionTokens() != null) { - this.options.setMaxCompletionTokens(from.getMaxCompletionTokens()); - } - if (from.getN() != null) { - this.options.setN(from.getN()); - } - if (from.getOutputAudio() != null) { - this.options.setOutputAudio(from.getOutputAudio()); - } - if (from.getPresencePenalty() != null) { - this.options.setPresencePenalty(from.getPresencePenalty()); - } - if (from.getResponseFormat() != null) { - this.options.setResponseFormat(from.getResponseFormat()); - } - if (from.getStreamOptions() != null) { - this.options.setStreamOptions(from.getStreamOptions()); - } - if (from.getStreamUsage() != null) { - this.options.setStreamUsage(from.getStreamUsage()); - } - if (from.getSeed() != null) { - this.options.setSeed(from.getSeed()); - } - if (from.getStop() != null) { - this.options.setStop(new ArrayList<>(from.getStop())); - } - if (from.getTemperature() != null) { - this.options.setTemperature(from.getTemperature()); - } - if (from.getTopP() != null) { - this.options.setTopP(from.getTopP()); - } - if (from.getTools() != null) { - this.options.setTools(from.getTools()); - } - if (from.getToolChoice() != null) { - this.options.setToolChoice(from.getToolChoice()); - } - if (from.getUser() != null) { - this.options.setUser(from.getUser()); - } - if (from.getParallelToolCalls() != null) { - this.options.setParallelToolCalls(from.getParallelToolCalls()); - } - if (!from.getToolCallbacks().isEmpty()) { - this.options.setToolCallbacks(new ArrayList<>(from.getToolCallbacks())); - } - if (!from.getToolNames().isEmpty()) { - this.options.setToolNames(new HashSet<>(from.getToolNames())); - } - if (from.getHttpHeaders() != null) { - this.options.setHttpHeaders(new HashMap<>(from.getHttpHeaders())); - } - if (from.getInternalToolExecutionEnabled() != null) { - this.options.setInternalToolExecutionEnabled(from.getInternalToolExecutionEnabled()); - } - if (!from.getToolContext().isEmpty()) { - this.options.setToolContext(new HashMap<>(from.getToolContext())); - } - if (from.getStore() != null) { - this.options.setStore(from.getStore()); - } - if (from.getMetadata() != null) { - this.options.setMetadata(from.getMetadata()); - } - if (from.getReasoningEffort() != null) { - this.options.setReasoningEffort(from.getReasoningEffort()); - } - if (from.getVerbosity() != null) { - this.options.setVerbosity(from.getVerbosity()); - } - if (from.getServiceTier() != null) { - this.options.setServiceTier(from.getServiceTier()); - } - return this; - } - - public Builder model(String model) { - this.options.setModel(model); - return this; - } - - public Builder deploymentName(String deploymentName) { - this.options.setDeploymentName(deploymentName); - return this; - } - - public Builder frequencyPenalty(Double frequencyPenalty) { - this.options.setFrequencyPenalty(frequencyPenalty); - return this; - } - - public Builder logitBias(Map logitBias) { - this.options.setLogitBias(logitBias); - return this; - } - - public Builder logprobs(Boolean logprobs) { - this.options.setLogprobs(logprobs); - return this; - } - - public Builder topLogprobs(Integer topLogprobs) { - this.options.setTopLogprobs(topLogprobs); - return this; - } - - public Builder maxTokens(Integer maxTokens) { - if (maxTokens != null && this.options.getMaxCompletionTokens() != null) { - logger.warn( - "Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " - + "As maxToken is deprecated, we will ignore it and use maxCompletionToken ({}).", - this.options.getMaxCompletionTokens()); - } - else { - this.options.setMaxTokens(maxTokens); - } - return this; - } - - public Builder maxCompletionTokens(Integer maxCompletionTokens) { - if (maxCompletionTokens != null && this.options.getMaxTokens() != null) { - logger.warn( - "Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " - + "As maxToken is deprecated, we will use maxCompletionToken ({}).", - maxCompletionTokens); - - this.options.setMaxTokens(null); - } - this.options.setMaxCompletionTokens(maxCompletionTokens); - return this; - } - - public Builder N(Integer n) { - this.options.setN(n); - return this; - } - - public Builder outputAudio(ChatCompletionAudioParam audio) { - this.options.setOutputAudio(audio); - return this; - } - - public Builder presencePenalty(Double presencePenalty) { - this.options.setPresencePenalty(presencePenalty); - return this; - } - - public Builder responseFormat(OpenAiOfficialChatModel.ResponseFormat responseFormat) { - this.options.setResponseFormat(responseFormat); - return this; - } - - public Builder streamOptions(ResponseCreateParams.StreamOptions streamOptions) { - this.options.setStreamOptions(streamOptions); - return this; - } - - public Builder streamUsage(Boolean streamUsage) { - this.options.setStreamUsage(streamUsage); - return this; - } - - public Builder seed(Integer seed) { - this.options.setSeed(seed); - return this; - } - - public Builder stop(List stop) { - this.options.setStop(stop); - return this; - } - - public Builder temperature(Double temperature) { - this.options.setTemperature(temperature); - return this; - } - - public Builder topP(Double topP) { - this.options.setTopP(topP); - return this; - } - - public Builder tools(List tools) { - this.options.setTools(tools); - return this; - } - - public Builder toolChoice(ChatCompletionToolChoiceOption toolChoice) { - this.options.setToolChoice(toolChoice); - return this; - } - - public Builder user(String user) { - this.options.setUser(user); - return this; - } - - public Builder parallelToolCalls(Boolean parallelToolCalls) { - this.options.setParallelToolCalls(parallelToolCalls); - return this; - } - - public Builder toolCallbacks(List toolCallbacks) { - this.options.setToolCallbacks(toolCallbacks); - return this; - } - - public Builder toolCallbacks(ToolCallback... toolCallbacks) { - this.options.setToolCallbacks(Arrays.asList(toolCallbacks)); - return this; - } - - public Builder toolNames(Set toolNames) { - Assert.notNull(toolNames, "toolNames cannot be null"); - this.options.setToolNames(toolNames); - return this; - } - - public Builder toolNames(String... toolNames) { - Assert.notNull(toolNames, "toolNames cannot be null"); - this.options.setToolNames(new HashSet<>(Arrays.asList(toolNames))); - return this; - } - - public Builder httpHeaders(Map httpHeaders) { - this.options.setHttpHeaders(httpHeaders); - return this; - } - - public Builder internalToolExecutionEnabled(@Nullable Boolean internalToolExecutionEnabled) { - this.options.setInternalToolExecutionEnabled(internalToolExecutionEnabled); - return this; - } - - public Builder toolContext(Map toolContext) { - this.options.setToolContext(toolContext); - return this; - } - - public Builder store(Boolean store) { - this.options.setStore(store); - return this; - } - - public Builder metadata(Map metadata) { - this.options.setMetadata(metadata); - return this; - } - - public Builder reasoningEffort(String reasoningEffort) { - this.options.setReasoningEffort(reasoningEffort); - return this; - } - - public Builder verbosity(String verbosity) { - this.options.setVerbosity(verbosity); - return this; - } - - public Builder serviceTier(String serviceTier) { - this.options.setServiceTier(serviceTier); - return this; - } - - public OpenAiOfficialChatOptions build() { - return this.options; - } - - } + /** + * Gets whether to return log probabilities. + * @return true if log probabilities should be returned + */ + public Boolean getLogprobs() { + return this.logprobs; + } + + /** + * Sets whether to return log probabilities. + * @param logprobs whether to return log probabilities + */ + public void setLogprobs(Boolean logprobs) { + this.logprobs = logprobs; + } + + /** + * Gets the number of top log probabilities to return. + * @return the number of top log probabilities + */ + public Integer getTopLogprobs() { + return this.topLogprobs; + } + + /** + * Sets the number of top log probabilities to return. + * @param topLogprobs the number of top log probabilities + */ + public void setTopLogprobs(Integer topLogprobs) { + this.topLogprobs = topLogprobs; + } + + @Override + public Integer getMaxTokens() { + return this.maxTokens; + } + + /** + * Sets the maximum number of tokens to generate. + * @param maxTokens the maximum number of tokens + */ + public void setMaxTokens(Integer maxTokens) { + this.maxTokens = maxTokens; + } + + /** + * Gets the maximum number of completion tokens. + * @return the maximum number of completion tokens + */ + public Integer getMaxCompletionTokens() { + return this.maxCompletionTokens; + } + + /** + * Sets the maximum number of completion tokens. + * @param maxCompletionTokens the maximum number of completion tokens + */ + public void setMaxCompletionTokens(Integer maxCompletionTokens) { + this.maxCompletionTokens = maxCompletionTokens; + } + + /** + * Gets the number of completions to generate. + * @return the number of completions + */ + public Integer getN() { + return this.n; + } + + /** + * Sets the number of completions to generate. + * @param n the number of completions + */ + public void setN(Integer n) { + this.n = n; + } + + /** + * Gets the output audio parameters. + * @return the output audio parameters + */ + public ChatCompletionAudioParam getOutputAudio() { + return this.outputAudio; + } + + /** + * Sets the output audio parameters. + * @param outputAudio the output audio parameters + */ + public void setOutputAudio(ChatCompletionAudioParam outputAudio) { + this.outputAudio = outputAudio; + } + + @Override + public Double getPresencePenalty() { + return this.presencePenalty; + } + + /** + * Sets the presence penalty parameter. + * @param presencePenalty the presence penalty to set + */ + public void setPresencePenalty(Double presencePenalty) { + this.presencePenalty = presencePenalty; + } + + /** + * Gets the response format configuration. + * @return the response format + */ + public OpenAiOfficialChatModel.ResponseFormat getResponseFormat() { + return this.responseFormat; + } + + /** + * Sets the response format configuration. + * @param responseFormat the response format to set + */ + public void setResponseFormat(OpenAiOfficialChatModel.ResponseFormat responseFormat) { + this.responseFormat = responseFormat; + } + + /** + * Gets the stream options. + * @return the stream options + */ + public ResponseCreateParams.StreamOptions getStreamOptions() { + return this.streamOptions; + } + + /** + * Sets the stream options. + * @param streamOptions the stream options to set + */ + public void setStreamOptions(ResponseCreateParams.StreamOptions streamOptions) { + this.streamOptions = streamOptions; + } + + /** + * Gets whether to include usage information in streaming responses. + * @return true if usage should be included in streams + */ + public Boolean getStreamUsage() { + return this.streamUsage; + } + + /** + * Sets whether to include usage information in streaming responses. + * @param streamUsage whether to include usage in streams + */ + public void setStreamUsage(Boolean streamUsage) { + this.streamUsage = streamUsage; + } + + /** + * Gets the random seed for deterministic generation. + * @return the random seed + */ + public Integer getSeed() { + return this.seed; + } + + /** + * Sets the random seed for deterministic generation. + * @param seed the random seed + */ + public void setSeed(Integer seed) { + this.seed = seed; + } + + /** + * Gets the stop sequences. + * @return the list of stop sequences + */ + public List getStop() { + return this.stop; + } + + /** + * Sets the stop sequences. + * @param stop the list of stop sequences + */ + public void setStop(List stop) { + this.stop = stop; + } + + @Override + public List getStopSequences() { + return getStop(); + } + + /** + * Sets the stop sequences. + * @param stopSequences the list of stop sequences + */ + public void setStopSequences(List stopSequences) { + setStop(stopSequences); + } + + @Override + public Double getTemperature() { + return this.temperature; + } + + /** + * Sets the temperature for sampling. + * @param temperature the temperature value + */ + public void setTemperature(Double temperature) { + this.temperature = temperature; + } + + @Override + public Double getTopP() { + return this.topP; + } + + /** + * Sets the top-p nucleus sampling parameter. + * @param topP the top-p value + */ + public void setTopP(Double topP) { + this.topP = topP; + } + + /** + * Gets the list of tool definitions. + * @return the list of tools + */ + public List getTools() { + return this.tools; + } + + /** + * Sets the list of tool definitions. + * @param tools the list of tools + */ + public void setTools(List tools) { + this.tools = tools; + } + + /** + * Gets the tool choice configuration. + * @return the tool choice option + */ + public ChatCompletionToolChoiceOption getToolChoice() { + return this.toolChoice; + } + + /** + * Sets the tool choice configuration. + * @param toolChoice the tool choice option + */ + public void setToolChoice(ChatCompletionToolChoiceOption toolChoice) { + this.toolChoice = toolChoice; + } + + /** + * Gets the user identifier. + * @return the user identifier + */ + public String getUser() { + return this.user; + } + + /** + * Sets the user identifier. + * @param user the user identifier + */ + public void setUser(String user) { + this.user = user; + } + + /** + * Gets whether to enable parallel tool calls. + * @return true if parallel tool calls are enabled + */ + public Boolean getParallelToolCalls() { + return this.parallelToolCalls; + } + + /** + * Sets whether to enable parallel tool calls. + * @param parallelToolCalls whether to enable parallel tool calls + */ + public void setParallelToolCalls(Boolean parallelToolCalls) { + this.parallelToolCalls = parallelToolCalls; + } + + /** + * Gets whether to store the conversation. + * @return true if the conversation should be stored + */ + public Boolean getStore() { + return this.store; + } + + /** + * Sets whether to store the conversation. + * @param store whether to store the conversation + */ + public void setStore(Boolean store) { + this.store = store; + } + + /** + * Gets the metadata map. + * @return the metadata map + */ + public Map getMetadata() { + return this.metadata; + } + + /** + * Sets the metadata map. + * @param metadata the metadata map + */ + public void setMetadata(Map metadata) { + this.metadata = metadata; + } + + /** + * Gets the reasoning effort level. + * @return the reasoning effort level + */ + public String getReasoningEffort() { + return this.reasoningEffort; + } + + /** + * Sets the reasoning effort level. + * @param reasoningEffort the reasoning effort level + */ + public void setReasoningEffort(String reasoningEffort) { + this.reasoningEffort = reasoningEffort; + } + + /** + * Gets the verbosity level. + * @return the verbosity level + */ + public String getVerbosity() { + return this.verbosity; + } + + /** + * Sets the verbosity level. + * @param verbosity the verbosity level + */ + public void setVerbosity(String verbosity) { + this.verbosity = verbosity; + } + + /** + * Gets the service tier. + * @return the service tier + */ + public String getServiceTier() { + return this.serviceTier; + } + + /** + * Sets the service tier. + * @param serviceTier the service tier + */ + public void setServiceTier(String serviceTier) { + this.serviceTier = serviceTier; + } + + @Override + public List getToolCallbacks() { + return this.toolCallbacks; + } + + @Override + public void setToolCallbacks(List toolCallbacks) { + Assert.notNull(toolCallbacks, "toolCallbacks cannot be null"); + Assert.noNullElements(toolCallbacks, "toolCallbacks cannot contain null elements"); + this.toolCallbacks = toolCallbacks; + } + + @Override + public Set getToolNames() { + return this.toolNames; + } + + @Override + public void setToolNames(Set toolNames) { + Assert.notNull(toolNames, "toolNames cannot be null"); + Assert.noNullElements(toolNames, "toolNames cannot contain null elements"); + toolNames.forEach(tool -> Assert.hasText(tool, "toolNames cannot contain empty elements")); + this.toolNames = toolNames; + } + + @Override + @Nullable + public Boolean getInternalToolExecutionEnabled() { + return this.internalToolExecutionEnabled; + } + + @Override + public void setInternalToolExecutionEnabled(@Nullable Boolean internalToolExecutionEnabled) { + this.internalToolExecutionEnabled = internalToolExecutionEnabled; + } + + /** + * Gets the HTTP headers to include in requests. + * @return the HTTP headers map + */ + public Map getHttpHeaders() { + return this.httpHeaders; + } + + /** + * Sets the HTTP headers to include in requests. + * @param httpHeaders the HTTP headers map + */ + public void setHttpHeaders(Map httpHeaders) { + this.httpHeaders = httpHeaders; + } + + @Override + public Map getToolContext() { + return this.toolContext; + } + + @Override + public void setToolContext(Map toolContext) { + this.toolContext = toolContext; + } + + @Override + public Integer getTopK() { + return null; + } + + public static Builder builder() { + return new Builder(); + } + + @Override + public OpenAiOfficialChatOptions copy() { + return builder().from(this).build(); + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) + return false; + OpenAiOfficialChatOptions options = (OpenAiOfficialChatOptions) o; + return Objects.equals(getModel(), options.getModel()) + && Objects.equals(frequencyPenalty, options.frequencyPenalty) + && Objects.equals(logitBias, options.logitBias) && Objects.equals(logprobs, options.logprobs) + && Objects.equals(topLogprobs, options.topLogprobs) && Objects.equals(maxTokens, options.maxTokens) + && Objects.equals(maxCompletionTokens, options.maxCompletionTokens) && Objects.equals(n, options.n) + && Objects.equals(outputAudio, options.outputAudio) + && Objects.equals(presencePenalty, options.presencePenalty) + && Objects.equals(responseFormat, options.responseFormat) + && Objects.equals(streamOptions, options.streamOptions) + && Objects.equals(streamUsage, options.streamUsage) && Objects.equals(seed, options.seed) + && Objects.equals(stop, options.stop) && Objects.equals(temperature, options.temperature) + && Objects.equals(topP, options.topP) && Objects.equals(tools, options.tools) + && Objects.equals(toolChoice, options.toolChoice) && Objects.equals(user, options.user) + && Objects.equals(parallelToolCalls, options.parallelToolCalls) && Objects.equals(store, options.store) + && Objects.equals(metadata, options.metadata) + && Objects.equals(reasoningEffort, options.reasoningEffort) + && Objects.equals(verbosity, options.verbosity) && Objects.equals(serviceTier, options.serviceTier) + && Objects.equals(toolCallbacks, options.toolCallbacks) && Objects.equals(toolNames, options.toolNames) + && Objects.equals(internalToolExecutionEnabled, options.internalToolExecutionEnabled) + && Objects.equals(httpHeaders, options.httpHeaders) && Objects.equals(toolContext, options.toolContext); + } + + @Override + public int hashCode() { + return Objects.hash(getModel(), frequencyPenalty, logitBias, logprobs, topLogprobs, maxTokens, + maxCompletionTokens, n, outputAudio, presencePenalty, responseFormat, streamOptions, streamUsage, seed, + stop, temperature, topP, tools, toolChoice, user, parallelToolCalls, store, metadata, reasoningEffort, + verbosity, serviceTier, toolCallbacks, toolNames, internalToolExecutionEnabled, httpHeaders, + toolContext); + } + + @Override + public String toString() { + return "OpenAiOfficialChatOptions{" + "model='" + getModel() + ", frequencyPenalty=" + frequencyPenalty + + ", logitBias=" + logitBias + ", logprobs=" + logprobs + ", topLogprobs=" + topLogprobs + + ", maxTokens=" + maxTokens + ", maxCompletionTokens=" + maxCompletionTokens + ", n=" + n + + ", outputAudio=" + outputAudio + ", presencePenalty=" + presencePenalty + ", responseFormat=" + + responseFormat + ", streamOptions=" + streamOptions + ", streamUsage=" + streamUsage + ", seed=" + + seed + ", stop=" + stop + ", temperature=" + temperature + ", topP=" + topP + ", tools=" + tools + + ", toolChoice=" + toolChoice + ", user='" + user + '\'' + ", parallelToolCalls=" + parallelToolCalls + + ", store=" + store + ", metadata=" + metadata + ", reasoningEffort='" + reasoningEffort + '\'' + + ", verbosity='" + verbosity + '\'' + ", serviceTier='" + serviceTier + '\'' + ", toolCallbacks=" + + toolCallbacks + ", toolNames=" + toolNames + ", internalToolExecutionEnabled=" + + internalToolExecutionEnabled + ", httpHeaders=" + httpHeaders + ", toolContext=" + toolContext + '}'; + } + + public static final class Builder { + + private final OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + + public Builder from(OpenAiOfficialChatOptions fromOptions) { + this.options.setModel(fromOptions.getModel()); + this.options.setDeploymentName(fromOptions.getDeploymentName()); + this.options.setFrequencyPenalty(fromOptions.getFrequencyPenalty()); + this.options.setLogitBias(fromOptions.getLogitBias()); + this.options.setLogprobs(fromOptions.getLogprobs()); + this.options.setTopLogprobs(fromOptions.getTopLogprobs()); + this.options.setMaxTokens(fromOptions.getMaxTokens()); + this.options.setMaxCompletionTokens(fromOptions.getMaxCompletionTokens()); + this.options.setN(fromOptions.getN()); + this.options.setOutputAudio(fromOptions.getOutputAudio()); + this.options.setPresencePenalty(fromOptions.getPresencePenalty()); + this.options.setResponseFormat(fromOptions.getResponseFormat()); + this.options.setStreamOptions(fromOptions.getStreamOptions()); + this.options.setStreamUsage(fromOptions.getStreamUsage()); + this.options.setSeed(fromOptions.getSeed()); + this.options.setStop(fromOptions.getStop() != null ? new ArrayList<>(fromOptions.getStop()) : null); + this.options.setTemperature(fromOptions.getTemperature()); + this.options.setTopP(fromOptions.getTopP()); + this.options.setTools(fromOptions.getTools()); + this.options.setToolChoice(fromOptions.getToolChoice()); + this.options.setUser(fromOptions.getUser()); + this.options.setParallelToolCalls(fromOptions.getParallelToolCalls()); + this.options.setToolCallbacks(new ArrayList<>(fromOptions.getToolCallbacks())); + this.options.setToolNames(new HashSet<>(fromOptions.getToolNames())); + this.options.setHttpHeaders( + fromOptions.getHttpHeaders() != null ? new HashMap<>(fromOptions.getHttpHeaders()) : null); + this.options.setInternalToolExecutionEnabled(fromOptions.getInternalToolExecutionEnabled()); + this.options.setToolContext(new HashMap<>(fromOptions.getToolContext())); + this.options.setStore(fromOptions.getStore()); + this.options.setMetadata(fromOptions.getMetadata()); + this.options.setReasoningEffort(fromOptions.getReasoningEffort()); + this.options.setVerbosity(fromOptions.getVerbosity()); + this.options.setServiceTier(fromOptions.getServiceTier()); + return this; + } + + public Builder merge(OpenAiOfficialChatOptions from) { + if (from.getModel() != null) { + this.options.setModel(from.getModel()); + } + if (from.getDeploymentName() != null) { + this.options.setDeploymentName(from.getDeploymentName()); + } + if (from.getFrequencyPenalty() != null) { + this.options.setFrequencyPenalty(from.getFrequencyPenalty()); + } + if (from.getLogitBias() != null) { + this.options.setLogitBias(from.getLogitBias()); + } + if (from.getLogprobs() != null) { + this.options.setLogprobs(from.getLogprobs()); + } + if (from.getTopLogprobs() != null) { + this.options.setTopLogprobs(from.getTopLogprobs()); + } + if (from.getMaxTokens() != null) { + this.options.setMaxTokens(from.getMaxTokens()); + } + if (from.getMaxCompletionTokens() != null) { + this.options.setMaxCompletionTokens(from.getMaxCompletionTokens()); + } + if (from.getN() != null) { + this.options.setN(from.getN()); + } + if (from.getOutputAudio() != null) { + this.options.setOutputAudio(from.getOutputAudio()); + } + if (from.getPresencePenalty() != null) { + this.options.setPresencePenalty(from.getPresencePenalty()); + } + if (from.getResponseFormat() != null) { + this.options.setResponseFormat(from.getResponseFormat()); + } + if (from.getStreamOptions() != null) { + this.options.setStreamOptions(from.getStreamOptions()); + } + if (from.getStreamUsage() != null) { + this.options.setStreamUsage(from.getStreamUsage()); + } + if (from.getSeed() != null) { + this.options.setSeed(from.getSeed()); + } + if (from.getStop() != null) { + this.options.setStop(new ArrayList<>(from.getStop())); + } + if (from.getTemperature() != null) { + this.options.setTemperature(from.getTemperature()); + } + if (from.getTopP() != null) { + this.options.setTopP(from.getTopP()); + } + if (from.getTools() != null) { + this.options.setTools(from.getTools()); + } + if (from.getToolChoice() != null) { + this.options.setToolChoice(from.getToolChoice()); + } + if (from.getUser() != null) { + this.options.setUser(from.getUser()); + } + if (from.getParallelToolCalls() != null) { + this.options.setParallelToolCalls(from.getParallelToolCalls()); + } + if (!from.getToolCallbacks().isEmpty()) { + this.options.setToolCallbacks(new ArrayList<>(from.getToolCallbacks())); + } + if (!from.getToolNames().isEmpty()) { + this.options.setToolNames(new HashSet<>(from.getToolNames())); + } + if (from.getHttpHeaders() != null) { + this.options.setHttpHeaders(new HashMap<>(from.getHttpHeaders())); + } + if (from.getInternalToolExecutionEnabled() != null) { + this.options.setInternalToolExecutionEnabled(from.getInternalToolExecutionEnabled()); + } + if (!from.getToolContext().isEmpty()) { + this.options.setToolContext(new HashMap<>(from.getToolContext())); + } + if (from.getStore() != null) { + this.options.setStore(from.getStore()); + } + if (from.getMetadata() != null) { + this.options.setMetadata(from.getMetadata()); + } + if (from.getReasoningEffort() != null) { + this.options.setReasoningEffort(from.getReasoningEffort()); + } + if (from.getVerbosity() != null) { + this.options.setVerbosity(from.getVerbosity()); + } + if (from.getServiceTier() != null) { + this.options.setServiceTier(from.getServiceTier()); + } + return this; + } + + public Builder model(String model) { + this.options.setModel(model); + return this; + } + + public Builder deploymentName(String deploymentName) { + this.options.setDeploymentName(deploymentName); + return this; + } + + public Builder frequencyPenalty(Double frequencyPenalty) { + this.options.setFrequencyPenalty(frequencyPenalty); + return this; + } + + public Builder logitBias(Map logitBias) { + this.options.setLogitBias(logitBias); + return this; + } + + public Builder logprobs(Boolean logprobs) { + this.options.setLogprobs(logprobs); + return this; + } + + public Builder topLogprobs(Integer topLogprobs) { + this.options.setTopLogprobs(topLogprobs); + return this; + } + + public Builder maxTokens(Integer maxTokens) { + if (maxTokens != null && this.options.getMaxCompletionTokens() != null) { + logger.warn( + "Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " + + "As maxToken is deprecated, we will ignore it and use maxCompletionToken ({}).", + this.options.getMaxCompletionTokens()); + } + else { + this.options.setMaxTokens(maxTokens); + } + return this; + } + + public Builder maxCompletionTokens(Integer maxCompletionTokens) { + if (maxCompletionTokens != null && this.options.getMaxTokens() != null) { + logger.warn( + "Both maxTokens and maxCompletionTokens are set. OpenAI API does not support setting both parameters simultaneously. " + + "As maxToken is deprecated, we will use maxCompletionToken ({}).", + maxCompletionTokens); + + this.options.setMaxTokens(null); + } + this.options.setMaxCompletionTokens(maxCompletionTokens); + return this; + } + + public Builder N(Integer n) { + this.options.setN(n); + return this; + } + + public Builder outputAudio(ChatCompletionAudioParam audio) { + this.options.setOutputAudio(audio); + return this; + } + + public Builder presencePenalty(Double presencePenalty) { + this.options.setPresencePenalty(presencePenalty); + return this; + } + + public Builder responseFormat(OpenAiOfficialChatModel.ResponseFormat responseFormat) { + this.options.setResponseFormat(responseFormat); + return this; + } + + public Builder streamOptions(ResponseCreateParams.StreamOptions streamOptions) { + this.options.setStreamOptions(streamOptions); + return this; + } + + public Builder streamUsage(Boolean streamUsage) { + this.options.setStreamUsage(streamUsage); + return this; + } + + public Builder seed(Integer seed) { + this.options.setSeed(seed); + return this; + } + + public Builder stop(List stop) { + this.options.setStop(stop); + return this; + } + + public Builder temperature(Double temperature) { + this.options.setTemperature(temperature); + return this; + } + + public Builder topP(Double topP) { + this.options.setTopP(topP); + return this; + } + + public Builder tools(List tools) { + this.options.setTools(tools); + return this; + } + + public Builder toolChoice(ChatCompletionToolChoiceOption toolChoice) { + this.options.setToolChoice(toolChoice); + return this; + } + + public Builder user(String user) { + this.options.setUser(user); + return this; + } + + public Builder parallelToolCalls(Boolean parallelToolCalls) { + this.options.setParallelToolCalls(parallelToolCalls); + return this; + } + + public Builder toolCallbacks(List toolCallbacks) { + this.options.setToolCallbacks(toolCallbacks); + return this; + } + + public Builder toolCallbacks(ToolCallback... toolCallbacks) { + this.options.setToolCallbacks(Arrays.asList(toolCallbacks)); + return this; + } + + public Builder toolNames(Set toolNames) { + Assert.notNull(toolNames, "toolNames cannot be null"); + this.options.setToolNames(toolNames); + return this; + } + + public Builder toolNames(String... toolNames) { + Assert.notNull(toolNames, "toolNames cannot be null"); + this.options.setToolNames(new HashSet<>(Arrays.asList(toolNames))); + return this; + } + + public Builder httpHeaders(Map httpHeaders) { + this.options.setHttpHeaders(httpHeaders); + return this; + } + + public Builder internalToolExecutionEnabled(@Nullable Boolean internalToolExecutionEnabled) { + this.options.setInternalToolExecutionEnabled(internalToolExecutionEnabled); + return this; + } + + public Builder toolContext(Map toolContext) { + this.options.setToolContext(toolContext); + return this; + } + + public Builder store(Boolean store) { + this.options.setStore(store); + return this; + } + + public Builder metadata(Map metadata) { + this.options.setMetadata(metadata); + return this; + } + + public Builder reasoningEffort(String reasoningEffort) { + this.options.setReasoningEffort(reasoningEffort); + return this; + } + + public Builder verbosity(String verbosity) { + this.options.setVerbosity(verbosity); + return this; + } + + public Builder serviceTier(String serviceTier) { + this.options.setServiceTier(serviceTier); + return this; + } + + public OpenAiOfficialChatOptions build() { + return this.options; + } + + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java index 53ee176bc87..8e9388c88a9 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java @@ -52,209 +52,213 @@ */ public class OpenAiOfficialEmbeddingModel extends AbstractEmbeddingModel { - private static final String DEFAULT_MODEL_NAME = OpenAiOfficialEmbeddingOptions.DEFAULT_EMBEDDING_MODEL; - - private static final EmbeddingModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultEmbeddingModelObservationConvention(); - - private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialEmbeddingModel.class); - - private final OpenAIClient openAiClient; - - private final OpenAiOfficialEmbeddingOptions options; - - private final MetadataMode metadataMode; - - private final ObservationRegistry observationRegistry; - - private EmbeddingModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; - - /** - * Creates a new OpenAiOfficialEmbeddingModel with default options. - */ - public OpenAiOfficialEmbeddingModel() { - this(null, null, null, null); - } - - /** - * Creates a new OpenAiOfficialEmbeddingModel with the given options. - * @param options the embedding options - */ - public OpenAiOfficialEmbeddingModel(OpenAiOfficialEmbeddingOptions options) { - this(null, null, options, null); - } - - /** - * Creates a new OpenAiOfficialEmbeddingModel with the given metadata mode and options. - * @param metadataMode the metadata mode - * @param options the embedding options - */ - public OpenAiOfficialEmbeddingModel(MetadataMode metadataMode, OpenAiOfficialEmbeddingOptions options) { - this(null, metadataMode, options, null); - } - - /** - * Creates a new OpenAiOfficialEmbeddingModel with the given options and observation registry. - * @param options the embedding options - * @param observationRegistry the observation registry - */ - public OpenAiOfficialEmbeddingModel(OpenAiOfficialEmbeddingOptions options, - ObservationRegistry observationRegistry) { - this(null, null, options, observationRegistry); - } - - /** - * Creates a new OpenAiOfficialEmbeddingModel with the given metadata mode, options, and observation registry. - * @param metadataMode the metadata mode - * @param options the embedding options - * @param observationRegistry the observation registry - */ - public OpenAiOfficialEmbeddingModel(MetadataMode metadataMode, OpenAiOfficialEmbeddingOptions options, - ObservationRegistry observationRegistry) { - this(null, metadataMode, options, observationRegistry); - } - - /** - * Creates a new OpenAiOfficialEmbeddingModel with the given OpenAI client. - * @param openAiClient the OpenAI client - */ - public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient) { - this(openAiClient, null, null, null); - } - - /** - * Creates a new OpenAiOfficialEmbeddingModel with the given OpenAI client and metadata mode. - * @param openAiClient the OpenAI client - * @param metadataMode the metadata mode - */ - public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode) { - this(openAiClient, metadataMode, null, null); - } - - /** - * Creates a new OpenAiOfficialEmbeddingModel with all configuration options. - * @param openAiClient the OpenAI client - * @param metadataMode the metadata mode - * @param options the embedding options - */ - public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, - OpenAiOfficialEmbeddingOptions options) { - this(openAiClient, metadataMode, options, null); - } - - /** - * Creates a new OpenAiOfficialEmbeddingModel with all configuration options. - * @param openAiClient the OpenAI client - * @param metadataMode the metadata mode - * @param options the embedding options - * @param observationRegistry the observation registry - */ - public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, - OpenAiOfficialEmbeddingOptions options, ObservationRegistry observationRegistry) { - - if (options == null) { - this.options = OpenAiOfficialEmbeddingOptions.builder().model(DEFAULT_MODEL_NAME).build(); - } - else { - this.options = options; - } - this.openAiClient = Objects.requireNonNullElseGet(openAiClient, - () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), - this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), - this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), - this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), - this.options.getProxy(), this.options.getCustomHeaders())); - this.metadataMode = Objects.requireNonNullElse(metadataMode, MetadataMode.EMBED); - this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); - } - - @Override - public float[] embed(Document document) { - EmbeddingResponse response = this - .call(new EmbeddingRequest(List.of(document.getFormattedContent(this.metadataMode)), null)); - - if (CollectionUtils.isEmpty(response.getResults())) { - return new float[0]; - } - return response.getResults().get(0).getOutput(); - } - - @Override - public EmbeddingResponse call(EmbeddingRequest embeddingRequest) { - OpenAiOfficialEmbeddingOptions options = OpenAiOfficialEmbeddingOptions.builder() - .from(this.options) - .merge(embeddingRequest.getOptions()) - .build(); - - EmbeddingRequest embeddingRequestWithMergedOptions = new EmbeddingRequest(embeddingRequest.getInstructions(), - options); - - EmbeddingCreateParams embeddingCreateParams = options - .toOpenAiCreateParams(embeddingRequestWithMergedOptions.getInstructions()); - - if (logger.isTraceEnabled()) { - logger.trace("OpenAiOfficialEmbeddingModel call {} with the following options : {} ", options.getModel(), - embeddingCreateParams); - } - - var observationContext = EmbeddingModelObservationContext.builder() - .embeddingRequest(embeddingRequestWithMergedOptions) - .provider(AiProvider.OPENAI_OFFICIAL.value()) - .build(); - - return Objects.requireNonNull( - EmbeddingModelObservationDocumentation.EMBEDDING_MODEL_OPERATION - .observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, - this.observationRegistry) - .observe(() -> { - CreateEmbeddingResponse response = this.openAiClient.embeddings().create(embeddingCreateParams); - - var embeddingResponse = generateEmbeddingResponse(response); - observationContext.setResponse(embeddingResponse); - return embeddingResponse; - })); - } - - private EmbeddingResponse generateEmbeddingResponse(CreateEmbeddingResponse response) { - - List data = generateEmbeddingList(response.data()); - EmbeddingResponseMetadata metadata = new EmbeddingResponseMetadata(); - metadata.setModel(response.model()); - metadata.setUsage(getDefaultUsage(response.usage())); - return new EmbeddingResponse(data, metadata); - } - - private DefaultUsage getDefaultUsage(CreateEmbeddingResponse.Usage nativeUsage) { - return new DefaultUsage(Math.toIntExact(nativeUsage.promptTokens()), 0, - Math.toIntExact(nativeUsage.totalTokens()), nativeUsage); - } - - private List generateEmbeddingList(List nativeData) { - List data = new ArrayList<>(); - for (com.openai.models.embeddings.Embedding nativeDatum : nativeData) { - List nativeDatumEmbedding = nativeDatum.embedding(); - long nativeIndex = nativeDatum.index(); - Embedding embedding = new Embedding(EmbeddingUtils.toPrimitive(nativeDatumEmbedding), - Math.toIntExact(nativeIndex)); - data.add(embedding); - } - return data; - } - - /** - * Gets the embedding options for this model. - * @return the embedding options - */ - public OpenAiOfficialEmbeddingOptions getOptions() { - return this.options; - } - - /** - * Use the provided convention for reporting observation data - * @param observationConvention The provided convention - */ - public void setObservationConvention(EmbeddingModelObservationConvention observationConvention) { - Assert.notNull(observationConvention, "observationConvention cannot be null"); - this.observationConvention = observationConvention; - } + private static final String DEFAULT_MODEL_NAME = OpenAiOfficialEmbeddingOptions.DEFAULT_EMBEDDING_MODEL; + + private static final EmbeddingModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultEmbeddingModelObservationConvention(); + + private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialEmbeddingModel.class); + + private final OpenAIClient openAiClient; + + private final OpenAiOfficialEmbeddingOptions options; + + private final MetadataMode metadataMode; + + private final ObservationRegistry observationRegistry; + + private EmbeddingModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; + + /** + * Creates a new OpenAiOfficialEmbeddingModel with default options. + */ + public OpenAiOfficialEmbeddingModel() { + this(null, null, null, null); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with the given options. + * @param options the embedding options + */ + public OpenAiOfficialEmbeddingModel(OpenAiOfficialEmbeddingOptions options) { + this(null, null, options, null); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with the given metadata mode and + * options. + * @param metadataMode the metadata mode + * @param options the embedding options + */ + public OpenAiOfficialEmbeddingModel(MetadataMode metadataMode, OpenAiOfficialEmbeddingOptions options) { + this(null, metadataMode, options, null); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with the given options and observation + * registry. + * @param options the embedding options + * @param observationRegistry the observation registry + */ + public OpenAiOfficialEmbeddingModel(OpenAiOfficialEmbeddingOptions options, + ObservationRegistry observationRegistry) { + this(null, null, options, observationRegistry); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with the given metadata mode, options, + * and observation registry. + * @param metadataMode the metadata mode + * @param options the embedding options + * @param observationRegistry the observation registry + */ + public OpenAiOfficialEmbeddingModel(MetadataMode metadataMode, OpenAiOfficialEmbeddingOptions options, + ObservationRegistry observationRegistry) { + this(null, metadataMode, options, observationRegistry); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with the given OpenAI client. + * @param openAiClient the OpenAI client + */ + public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient) { + this(openAiClient, null, null, null); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with the given OpenAI client and + * metadata mode. + * @param openAiClient the OpenAI client + * @param metadataMode the metadata mode + */ + public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode) { + this(openAiClient, metadataMode, null, null); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with all configuration options. + * @param openAiClient the OpenAI client + * @param metadataMode the metadata mode + * @param options the embedding options + */ + public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, + OpenAiOfficialEmbeddingOptions options) { + this(openAiClient, metadataMode, options, null); + } + + /** + * Creates a new OpenAiOfficialEmbeddingModel with all configuration options. + * @param openAiClient the OpenAI client + * @param metadataMode the metadata mode + * @param options the embedding options + * @param observationRegistry the observation registry + */ + public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, + OpenAiOfficialEmbeddingOptions options, ObservationRegistry observationRegistry) { + + if (options == null) { + this.options = OpenAiOfficialEmbeddingOptions.builder().model(DEFAULT_MODEL_NAME).build(); + } + else { + this.options = options; + } + this.openAiClient = Objects.requireNonNullElseGet(openAiClient, + () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), + this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), + this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), + this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), + this.options.getProxy(), this.options.getCustomHeaders())); + this.metadataMode = Objects.requireNonNullElse(metadataMode, MetadataMode.EMBED); + this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); + } + + @Override + public float[] embed(Document document) { + EmbeddingResponse response = this + .call(new EmbeddingRequest(List.of(document.getFormattedContent(this.metadataMode)), null)); + + if (CollectionUtils.isEmpty(response.getResults())) { + return new float[0]; + } + return response.getResults().get(0).getOutput(); + } + + @Override + public EmbeddingResponse call(EmbeddingRequest embeddingRequest) { + OpenAiOfficialEmbeddingOptions options = OpenAiOfficialEmbeddingOptions.builder() + .from(this.options) + .merge(embeddingRequest.getOptions()) + .build(); + + EmbeddingRequest embeddingRequestWithMergedOptions = new EmbeddingRequest(embeddingRequest.getInstructions(), + options); + + EmbeddingCreateParams embeddingCreateParams = options + .toOpenAiCreateParams(embeddingRequestWithMergedOptions.getInstructions()); + + if (logger.isTraceEnabled()) { + logger.trace("OpenAiOfficialEmbeddingModel call {} with the following options : {} ", options.getModel(), + embeddingCreateParams); + } + + var observationContext = EmbeddingModelObservationContext.builder() + .embeddingRequest(embeddingRequestWithMergedOptions) + .provider(AiProvider.OPENAI_OFFICIAL.value()) + .build(); + + return Objects.requireNonNull( + EmbeddingModelObservationDocumentation.EMBEDDING_MODEL_OPERATION + .observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, + this.observationRegistry) + .observe(() -> { + CreateEmbeddingResponse response = this.openAiClient.embeddings().create(embeddingCreateParams); + + var embeddingResponse = generateEmbeddingResponse(response); + observationContext.setResponse(embeddingResponse); + return embeddingResponse; + })); + } + + private EmbeddingResponse generateEmbeddingResponse(CreateEmbeddingResponse response) { + + List data = generateEmbeddingList(response.data()); + EmbeddingResponseMetadata metadata = new EmbeddingResponseMetadata(); + metadata.setModel(response.model()); + metadata.setUsage(getDefaultUsage(response.usage())); + return new EmbeddingResponse(data, metadata); + } + + private DefaultUsage getDefaultUsage(CreateEmbeddingResponse.Usage nativeUsage) { + return new DefaultUsage(Math.toIntExact(nativeUsage.promptTokens()), 0, + Math.toIntExact(nativeUsage.totalTokens()), nativeUsage); + } + + private List generateEmbeddingList(List nativeData) { + List data = new ArrayList<>(); + for (com.openai.models.embeddings.Embedding nativeDatum : nativeData) { + List nativeDatumEmbedding = nativeDatum.embedding(); + long nativeIndex = nativeDatum.index(); + Embedding embedding = new Embedding(EmbeddingUtils.toPrimitive(nativeDatumEmbedding), + Math.toIntExact(nativeIndex)); + data.add(embedding); + } + return data; + } + + /** + * Gets the embedding options for this model. + * @return the embedding options + */ + public OpenAiOfficialEmbeddingOptions getOptions() { + return this.options; + } + + /** + * Use the provided convention for reporting observation data + * @param observationConvention The provided convention + */ + public void setObservationConvention(EmbeddingModelObservationConvention observationConvention) { + Assert.notNull(observationConvention, "observationConvention cannot be null"); + this.observationConvention = observationConvention; + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingOptions.java index bf6f583a073..403ed446869 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingOptions.java @@ -31,138 +31,138 @@ */ public class OpenAiOfficialEmbeddingOptions extends AbstractOpenAiOfficialOptions implements EmbeddingOptions { - public static final String DEFAULT_EMBEDDING_MODEL = TEXT_EMBEDDING_ADA_002.asString(); - - /** - * An identifier for the caller or end user of the operation. This may be used for - * tracking or rate-limiting purposes. - */ - private String user; - - /* - * The number of dimensions the resulting output embeddings should have. Only - * supported in `text-embedding-3` and later models. - */ - private Integer dimensions; - - public static Builder builder() { - return new Builder(); - } - - public String getUser() { - return this.user; - } - - public void setUser(String user) { - this.user = user; - } - - @Override - public Integer getDimensions() { - return this.dimensions; - } - - public void setDimensions(Integer dimensions) { - this.dimensions = dimensions; - } - - @Override - public String toString() { - return "OpenAiOfficialEmbeddingOptions{" + "user='" + user + '\'' + ", model='" + getModel() + '\'' - + ", deploymentName='" + getDeploymentName() + '\'' + ", dimensions=" + dimensions + '}'; - } - - public EmbeddingCreateParams toOpenAiCreateParams(List instructions) { - - EmbeddingCreateParams.Builder builder = EmbeddingCreateParams.builder(); - - // Use deployment name if available (for Azure AI Foundry), otherwise use model - // name - if (this.getDeploymentName() != null) { - builder.model(this.getDeploymentName()); - } - else if (this.getModel() != null) { - builder.model(this.getModel()); - } - - if (instructions != null && !instructions.isEmpty()) { - builder.input(EmbeddingCreateParams.Input.ofArrayOfStrings(instructions)); - } - if (this.getUser() != null) { - builder.user(this.getUser()); - } - if (this.getDimensions() != null) { - builder.dimensions(this.getDimensions()); - } - return builder.build(); - } - - public static final class Builder { - - private final OpenAiOfficialEmbeddingOptions options = new OpenAiOfficialEmbeddingOptions(); - - public Builder from(OpenAiOfficialEmbeddingOptions fromOptions) { - this.options.setUser(fromOptions.getUser()); - this.options.setModel(fromOptions.getModel()); - this.options.setDeploymentName(fromOptions.getDeploymentName()); - this.options.setDimensions(fromOptions.getDimensions()); - return this; - } - - public Builder merge(EmbeddingOptions from) { - if (from instanceof OpenAiOfficialEmbeddingOptions castFrom) { - - if (castFrom.getUser() != null) { - this.options.setUser(castFrom.getUser()); - } - if (castFrom.getModel() != null) { - this.options.setModel(castFrom.getModel()); - } - if (castFrom.getDeploymentName() != null) { - this.options.setDeploymentName(castFrom.getDeploymentName()); - } - if (castFrom.getDimensions() != null) { - this.options.setDimensions(castFrom.getDimensions()); - } - } - return this; - } - - public Builder from(EmbeddingCreateParams openAiCreateParams) { - - if (openAiCreateParams.user().isPresent()) { - this.options.setUser(openAiCreateParams.user().get()); - } - if (openAiCreateParams.dimensions().isPresent()) { - this.options.setDimensions(Math.toIntExact(openAiCreateParams.dimensions().get())); - } - return this; - } - - public Builder user(String user) { - this.options.setUser(user); - return this; - } - - public Builder deploymentName(String deploymentName) { - this.options.setDeploymentName(deploymentName); - return this; - } - - public Builder model(String model) { - this.options.setModel(model); - return this; - } - - public Builder dimensions(Integer dimensions) { - this.options.dimensions = dimensions; - return this; - } - - public OpenAiOfficialEmbeddingOptions build() { - return this.options; - } - - } + public static final String DEFAULT_EMBEDDING_MODEL = TEXT_EMBEDDING_ADA_002.asString(); + + /** + * An identifier for the caller or end user of the operation. This may be used for + * tracking or rate-limiting purposes. + */ + private String user; + + /* + * The number of dimensions the resulting output embeddings should have. Only + * supported in `text-embedding-3` and later models. + */ + private Integer dimensions; + + public static Builder builder() { + return new Builder(); + } + + public String getUser() { + return this.user; + } + + public void setUser(String user) { + this.user = user; + } + + @Override + public Integer getDimensions() { + return this.dimensions; + } + + public void setDimensions(Integer dimensions) { + this.dimensions = dimensions; + } + + @Override + public String toString() { + return "OpenAiOfficialEmbeddingOptions{" + "user='" + user + '\'' + ", model='" + getModel() + '\'' + + ", deploymentName='" + getDeploymentName() + '\'' + ", dimensions=" + dimensions + '}'; + } + + public EmbeddingCreateParams toOpenAiCreateParams(List instructions) { + + EmbeddingCreateParams.Builder builder = EmbeddingCreateParams.builder(); + + // Use deployment name if available (for Azure AI Foundry), otherwise use model + // name + if (this.getDeploymentName() != null) { + builder.model(this.getDeploymentName()); + } + else if (this.getModel() != null) { + builder.model(this.getModel()); + } + + if (instructions != null && !instructions.isEmpty()) { + builder.input(EmbeddingCreateParams.Input.ofArrayOfStrings(instructions)); + } + if (this.getUser() != null) { + builder.user(this.getUser()); + } + if (this.getDimensions() != null) { + builder.dimensions(this.getDimensions()); + } + return builder.build(); + } + + public static final class Builder { + + private final OpenAiOfficialEmbeddingOptions options = new OpenAiOfficialEmbeddingOptions(); + + public Builder from(OpenAiOfficialEmbeddingOptions fromOptions) { + this.options.setUser(fromOptions.getUser()); + this.options.setModel(fromOptions.getModel()); + this.options.setDeploymentName(fromOptions.getDeploymentName()); + this.options.setDimensions(fromOptions.getDimensions()); + return this; + } + + public Builder merge(EmbeddingOptions from) { + if (from instanceof OpenAiOfficialEmbeddingOptions castFrom) { + + if (castFrom.getUser() != null) { + this.options.setUser(castFrom.getUser()); + } + if (castFrom.getModel() != null) { + this.options.setModel(castFrom.getModel()); + } + if (castFrom.getDeploymentName() != null) { + this.options.setDeploymentName(castFrom.getDeploymentName()); + } + if (castFrom.getDimensions() != null) { + this.options.setDimensions(castFrom.getDimensions()); + } + } + return this; + } + + public Builder from(EmbeddingCreateParams openAiCreateParams) { + + if (openAiCreateParams.user().isPresent()) { + this.options.setUser(openAiCreateParams.user().get()); + } + if (openAiCreateParams.dimensions().isPresent()) { + this.options.setDimensions(Math.toIntExact(openAiCreateParams.dimensions().get())); + } + return this; + } + + public Builder user(String user) { + this.options.setUser(user); + return this; + } + + public Builder deploymentName(String deploymentName) { + this.options.setDeploymentName(deploymentName); + return this; + } + + public Builder model(String model) { + this.options.setModel(model); + return this; + } + + public Builder dimensions(Integer dimensions) { + this.options.dimensions = dimensions; + return this; + } + + public OpenAiOfficialEmbeddingOptions build() { + return this.options; + } + + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageModel.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageModel.java index 5296ff6c937..040bf1b0857 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageModel.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageModel.java @@ -48,170 +48,172 @@ */ public class OpenAiOfficialImageModel implements ImageModel { - private static final String DEFAULT_MODEL_NAME = OpenAiOfficialImageOptions.DEFAULT_IMAGE_MODEL; - - private static final ImageModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultImageModelObservationConvention(); - - private final Logger logger = LoggerFactory.getLogger(OpenAiOfficialImageModel.class); - - private final OpenAIClient openAiClient; - - private final OpenAiOfficialImageOptions options; - - private final ObservationRegistry observationRegistry; - - private ImageModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; - - /** - * Creates a new OpenAiOfficialImageModel with default options. - */ - public OpenAiOfficialImageModel() { - this(null, null, null); - } - - /** - * Creates a new OpenAiOfficialImageModel with the given options. - * @param options the image options - */ - public OpenAiOfficialImageModel(OpenAiOfficialImageOptions options) { - this(null, options, null); - } - - /** - * Creates a new OpenAiOfficialImageModel with the given observation registry. - * @param observationRegistry the observation registry - */ - public OpenAiOfficialImageModel(ObservationRegistry observationRegistry) { - this(null, null, observationRegistry); - } - - /** - * Creates a new OpenAiOfficialImageModel with the given options and observation registry. - * @param options the image options - * @param observationRegistry the observation registry - */ - public OpenAiOfficialImageModel(OpenAiOfficialImageOptions options, ObservationRegistry observationRegistry) { - this(null, options, observationRegistry); - } - - /** - * Creates a new OpenAiOfficialImageModel with the given OpenAI client. - * @param openAIClient the OpenAI client - */ - public OpenAiOfficialImageModel(OpenAIClient openAIClient) { - this(openAIClient, null, null); - } - - /** - * Creates a new OpenAiOfficialImageModel with the given OpenAI client and options. - * @param openAIClient the OpenAI client - * @param options the image options - */ - public OpenAiOfficialImageModel(OpenAIClient openAIClient, OpenAiOfficialImageOptions options) { - this(openAIClient, options, null); - } - - /** - * Creates a new OpenAiOfficialImageModel with the given OpenAI client and observation registry. - * @param openAIClient the OpenAI client - * @param observationRegistry the observation registry - */ - public OpenAiOfficialImageModel(OpenAIClient openAIClient, ObservationRegistry observationRegistry) { - this(openAIClient, null, observationRegistry); - } - - /** - * Creates a new OpenAiOfficialImageModel with all configuration options. - * @param openAiClient the OpenAI client - * @param options the image options - * @param observationRegistry the observation registry - */ - public OpenAiOfficialImageModel(OpenAIClient openAiClient, OpenAiOfficialImageOptions options, - ObservationRegistry observationRegistry) { - - if (options == null) { - this.options = OpenAiOfficialImageOptions.builder().model(DEFAULT_MODEL_NAME).build(); - } - else { - this.options = options; - } - this.openAiClient = Objects.requireNonNullElseGet(openAiClient, - () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), - this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), - this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), - this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), - this.options.getProxy(), this.options.getCustomHeaders())); - this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); - } - - /** - * Gets the image options for this model. - * @return the image options - */ - public OpenAiOfficialImageOptions getOptions() { - return this.options; - } - - @Override - public ImageResponse call(ImagePrompt imagePrompt) { - OpenAiOfficialImageOptions options = OpenAiOfficialImageOptions.builder() - .from(this.options) - .merge(imagePrompt.getOptions()) - .build(); - - ImageGenerateParams imageGenerateParams = options.toOpenAiImageGenerateParams(imagePrompt); - - if (logger.isTraceEnabled()) { - logger.trace("OpenAiOfficialImageOptions call {} with the following options : {} ", options.getModel(), - imageGenerateParams); - } - - var observationContext = ImageModelObservationContext.builder() - .imagePrompt(imagePrompt) - .provider(AiProvider.OPENAI_OFFICIAL.value()) - .build(); - - return Objects.requireNonNull( - ImageModelObservationDocumentation.IMAGE_MODEL_OPERATION - .observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, - this.observationRegistry) - .observe(() -> { - var images = this.openAiClient.images().generate(imageGenerateParams); - - if (images.data().isEmpty() && images.data().get().isEmpty()) { - throw new IllegalArgumentException("Image generation failed: no image returned"); - } - - List imageGenerations = images.data().get().stream().map(nativeImage -> { - Image image; - if (nativeImage.url().isPresent()) { - image = new Image(nativeImage.url().get(), null); - } - else if (nativeImage.b64Json().isPresent()) { - image = new Image(null, nativeImage.b64Json().get()); - } - else { - throw new IllegalArgumentException( - "Image generation failed: image entry missing url and b64_json"); - } - var metadata = new OpenAiOfficialImageGenerationMetadata(nativeImage.revisedPrompt()); - return new ImageGeneration(image, metadata); - }).toList(); - ImageResponseMetadata openAiImageResponseMetadata = OpenAiOfficialImageResponseMetadata - .from(images); - ImageResponse imageResponse = new ImageResponse(imageGenerations, openAiImageResponseMetadata); - observationContext.setResponse(imageResponse); - return imageResponse; - })); - } - - /** - * Use the provided convention for reporting observation data - * @param observationConvention The provided convention - */ - public void setObservationConvention(ImageModelObservationConvention observationConvention) { - Assert.notNull(observationConvention, "observationConvention cannot be null"); - this.observationConvention = observationConvention; - } + private static final String DEFAULT_MODEL_NAME = OpenAiOfficialImageOptions.DEFAULT_IMAGE_MODEL; + + private static final ImageModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultImageModelObservationConvention(); + + private final Logger logger = LoggerFactory.getLogger(OpenAiOfficialImageModel.class); + + private final OpenAIClient openAiClient; + + private final OpenAiOfficialImageOptions options; + + private final ObservationRegistry observationRegistry; + + private ImageModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; + + /** + * Creates a new OpenAiOfficialImageModel with default options. + */ + public OpenAiOfficialImageModel() { + this(null, null, null); + } + + /** + * Creates a new OpenAiOfficialImageModel with the given options. + * @param options the image options + */ + public OpenAiOfficialImageModel(OpenAiOfficialImageOptions options) { + this(null, options, null); + } + + /** + * Creates a new OpenAiOfficialImageModel with the given observation registry. + * @param observationRegistry the observation registry + */ + public OpenAiOfficialImageModel(ObservationRegistry observationRegistry) { + this(null, null, observationRegistry); + } + + /** + * Creates a new OpenAiOfficialImageModel with the given options and observation + * registry. + * @param options the image options + * @param observationRegistry the observation registry + */ + public OpenAiOfficialImageModel(OpenAiOfficialImageOptions options, ObservationRegistry observationRegistry) { + this(null, options, observationRegistry); + } + + /** + * Creates a new OpenAiOfficialImageModel with the given OpenAI client. + * @param openAIClient the OpenAI client + */ + public OpenAiOfficialImageModel(OpenAIClient openAIClient) { + this(openAIClient, null, null); + } + + /** + * Creates a new OpenAiOfficialImageModel with the given OpenAI client and options. + * @param openAIClient the OpenAI client + * @param options the image options + */ + public OpenAiOfficialImageModel(OpenAIClient openAIClient, OpenAiOfficialImageOptions options) { + this(openAIClient, options, null); + } + + /** + * Creates a new OpenAiOfficialImageModel with the given OpenAI client and observation + * registry. + * @param openAIClient the OpenAI client + * @param observationRegistry the observation registry + */ + public OpenAiOfficialImageModel(OpenAIClient openAIClient, ObservationRegistry observationRegistry) { + this(openAIClient, null, observationRegistry); + } + + /** + * Creates a new OpenAiOfficialImageModel with all configuration options. + * @param openAiClient the OpenAI client + * @param options the image options + * @param observationRegistry the observation registry + */ + public OpenAiOfficialImageModel(OpenAIClient openAiClient, OpenAiOfficialImageOptions options, + ObservationRegistry observationRegistry) { + + if (options == null) { + this.options = OpenAiOfficialImageOptions.builder().model(DEFAULT_MODEL_NAME).build(); + } + else { + this.options = options; + } + this.openAiClient = Objects.requireNonNullElseGet(openAiClient, + () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), + this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), + this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), + this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), + this.options.getProxy(), this.options.getCustomHeaders())); + this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); + } + + /** + * Gets the image options for this model. + * @return the image options + */ + public OpenAiOfficialImageOptions getOptions() { + return this.options; + } + + @Override + public ImageResponse call(ImagePrompt imagePrompt) { + OpenAiOfficialImageOptions options = OpenAiOfficialImageOptions.builder() + .from(this.options) + .merge(imagePrompt.getOptions()) + .build(); + + ImageGenerateParams imageGenerateParams = options.toOpenAiImageGenerateParams(imagePrompt); + + if (logger.isTraceEnabled()) { + logger.trace("OpenAiOfficialImageOptions call {} with the following options : {} ", options.getModel(), + imageGenerateParams); + } + + var observationContext = ImageModelObservationContext.builder() + .imagePrompt(imagePrompt) + .provider(AiProvider.OPENAI_OFFICIAL.value()) + .build(); + + return Objects.requireNonNull( + ImageModelObservationDocumentation.IMAGE_MODEL_OPERATION + .observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, + this.observationRegistry) + .observe(() -> { + var images = this.openAiClient.images().generate(imageGenerateParams); + + if (images.data().isEmpty() && images.data().get().isEmpty()) { + throw new IllegalArgumentException("Image generation failed: no image returned"); + } + + List imageGenerations = images.data().get().stream().map(nativeImage -> { + Image image; + if (nativeImage.url().isPresent()) { + image = new Image(nativeImage.url().get(), null); + } + else if (nativeImage.b64Json().isPresent()) { + image = new Image(null, nativeImage.b64Json().get()); + } + else { + throw new IllegalArgumentException( + "Image generation failed: image entry missing url and b64_json"); + } + var metadata = new OpenAiOfficialImageGenerationMetadata(nativeImage.revisedPrompt()); + return new ImageGeneration(image, metadata); + }).toList(); + ImageResponseMetadata openAiImageResponseMetadata = OpenAiOfficialImageResponseMetadata + .from(images); + ImageResponse imageResponse = new ImageResponse(imageGenerations, openAiImageResponseMetadata); + observationContext.setResponse(imageResponse); + return imageResponse; + })); + } + + /** + * Use the provided convention for reporting observation data + * @param observationConvention The provided convention + */ + public void setObservationConvention(ImageModelObservationConvention observationConvention) { + Assert.notNull(observationConvention, "observationConvention cannot be null"); + this.observationConvention = observationConvention; + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageOptions.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageOptions.java index bb36e4199dd..651de12cca0 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageOptions.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageOptions.java @@ -29,299 +29,299 @@ */ public class OpenAiOfficialImageOptions extends AbstractOpenAiOfficialOptions implements ImageOptions { - public static final String DEFAULT_IMAGE_MODEL = ImageModel.DALL_E_3.toString(); - - /** - * The number of images to generate. Must be between 1 and 10. For dall-e-3, only n=1 - * is supported. - */ - private Integer n; - - /** - * The width of the generated images. Must be one of 256, 512, or 1024 for dall-e-2. - */ - private Integer width; - - /** - * The height of the generated images. Must be one of 256, 512, or 1024 for dall-e-2. - */ - private Integer height; - - /** - * The quality of the image that will be generated. hd creates images with finer - * details and greater consistency across the image. This param is only supported for - * dall-e-3. standard or hd - */ - private String quality; - - /** - * The format in which the generated images are returned. Must be one of url or - * b64_json. - */ - private String responseFormat; - - /** - * The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024 for - * dall-e-2. Must be one of 1024x1024, 1792x1024, or 1024x1792 for dall-e-3 models. - */ - private String size; - - /** - * The style of the generated images. Must be one of vivid or natural. Vivid causes - * the model to lean towards generating hyper-real and dramatic images. Natural causes - * the model to produce more natural, less hyper-real looking images. This param is - * only supported for dall-e-3. natural or vivid - */ - private String style; - - /** - * A unique identifier representing your end-user, which can help OpenAI to monitor - * and detect abuse. - */ - private String user; - - public static Builder builder() { - return new Builder(); - } - - @Override - public Integer getN() { - return this.n; - } - - public void setN(Integer n) { - this.n = n; - } - - @Override - public Integer getWidth() { - return this.width; - } - - public void setWidth(Integer width) { - this.width = width; - this.size = this.width + "x" + this.height; - } - - @Override - public Integer getHeight() { - return this.height; - } - - public void setHeight(Integer height) { - this.height = height; - this.size = this.width + "x" + this.height; - } - - @Override - public String getResponseFormat() { - return this.responseFormat; - } - - public void setResponseFormat(String responseFormat) { - this.responseFormat = responseFormat; - } - - public String getSize() { - if (this.size != null) { - return this.size; - } - return (this.width != null && this.height != null) ? this.width + "x" + this.height : null; - } - - public void setSize(String size) { - this.size = size; - } - - public String getUser() { - return this.user; - } - - public void setUser(String user) { - this.user = user; - } - - public String getQuality() { - return this.quality; - } - - public void setQuality(String quality) { - this.quality = quality; - } - - @Override - public String getStyle() { - return this.style; - } - - public void setStyle(String style) { - this.style = style; - } - - @Override - public boolean equals(Object o) { - if (o == null || getClass() != o.getClass()) - return false; - OpenAiOfficialImageOptions that = (OpenAiOfficialImageOptions) o; - return Objects.equals(n, that.n) && Objects.equals(width, that.width) && Objects.equals(height, that.height) - && Objects.equals(quality, that.quality) && Objects.equals(responseFormat, that.responseFormat) - && Objects.equals(size, that.size) && Objects.equals(style, that.style) - && Objects.equals(user, that.user); - } - - @Override - public int hashCode() { - return Objects.hash(n, width, height, quality, responseFormat, size, style, user); - } - - @Override - public String toString() { - return "OpenAiOfficialImageOptions{" + "n=" + n + ", width=" + width + ", height=" + height + ", quality='" - + quality + '\'' + ", responseFormat='" + responseFormat + '\'' + ", size='" + size + '\'' + ", style='" - + style + '\'' + ", user='" + user + '\'' + '}'; - } - - public ImageGenerateParams toOpenAiImageGenerateParams(ImagePrompt imagePrompt) { - if (imagePrompt.getInstructions().isEmpty()) { - throw new IllegalArgumentException("Image prompt instructions cannot be empty"); - } - - String prompt = imagePrompt.getInstructions().get(0).getText(); - ImageGenerateParams.Builder builder = ImageGenerateParams.builder().prompt(prompt); - - // Use deployment name if available (for Azure AI Foundry), otherwise use model - // name - if (this.getDeploymentName() != null) { - builder.model(this.getDeploymentName()); - } - else if (this.getModel() != null) { - builder.model(this.getModel()); - } - - if (this.getN() != null) { - builder.n(this.getN().longValue()); - } - if (this.getQuality() != null) { - builder.quality(ImageGenerateParams.Quality.of(this.getQuality().toLowerCase())); - } - if (this.getResponseFormat() != null) { - builder.responseFormat(ImageGenerateParams.ResponseFormat.of(this.getResponseFormat().toLowerCase())); - } - if (this.getSize() != null) { - builder.size(ImageGenerateParams.Size.of(this.getSize())); - } - if (this.getStyle() != null) { - builder.style(ImageGenerateParams.Style.of(this.getStyle().toLowerCase())); - } - if (this.getUser() != null) { - builder.user(this.getUser()); - } - - return builder.build(); - } - - public static final class Builder { - - private final OpenAiOfficialImageOptions options; - - private Builder() { - this.options = new OpenAiOfficialImageOptions(); - } - - public Builder from(OpenAiOfficialImageOptions fromOptions) { - this.options.setN(fromOptions.getN()); - this.options.setModel(fromOptions.getModel()); - this.options.setDeploymentName(fromOptions.getDeploymentName()); - this.options.setWidth(fromOptions.getWidth()); - this.options.setHeight(fromOptions.getHeight()); - this.options.setQuality(fromOptions.getQuality()); - this.options.setResponseFormat(fromOptions.getResponseFormat()); - this.options.setSize(fromOptions.getSize()); - this.options.setStyle(fromOptions.getStyle()); - this.options.setUser(fromOptions.getUser()); - return this; - } - - public Builder merge(ImageOptions from) { - if (from instanceof OpenAiOfficialImageOptions castFrom) { - if (castFrom.getN() != null) { - this.options.setN(castFrom.getN()); - } - if (castFrom.getModel() != null) { - this.options.setModel(castFrom.getModel()); - } - if (castFrom.getDeploymentName() != null) { - this.options.setDeploymentName(castFrom.getDeploymentName()); - } - if (castFrom.getWidth() != null) { - this.options.setWidth(castFrom.getWidth()); - } - if (castFrom.getHeight() != null) { - this.options.setHeight(castFrom.getHeight()); - } - if (castFrom.getQuality() != null) { - this.options.setQuality(castFrom.getQuality()); - } - if (castFrom.getResponseFormat() != null) { - this.options.setResponseFormat(castFrom.getResponseFormat()); - } - if (castFrom.getSize() != null) { - this.options.setSize(castFrom.getSize()); - } - if (castFrom.getStyle() != null) { - this.options.setStyle(castFrom.getStyle()); - } - if (castFrom.getUser() != null) { - this.options.setUser(castFrom.getUser()); - } - } - return this; - } - - public Builder N(Integer n) { - this.options.setN(n); - return this; - } - - public Builder model(String model) { - this.options.setModel(model); - return this; - } - - public Builder deploymentName(String deploymentName) { - this.options.setDeploymentName(deploymentName); - return this; - } - - public Builder responseFormat(String responseFormat) { - this.options.setResponseFormat(responseFormat); - return this; - } - - public Builder width(Integer width) { - this.options.setWidth(width); - return this; - } - - public Builder height(Integer height) { - this.options.setHeight(height); - return this; - } - - public Builder user(String user) { - this.options.setUser(user); - return this; - } - - public Builder style(String style) { - this.options.setStyle(style); - return this; - } - - public OpenAiOfficialImageOptions build() { - return this.options; - } - - } + public static final String DEFAULT_IMAGE_MODEL = ImageModel.DALL_E_3.toString(); + + /** + * The number of images to generate. Must be between 1 and 10. For dall-e-3, only n=1 + * is supported. + */ + private Integer n; + + /** + * The width of the generated images. Must be one of 256, 512, or 1024 for dall-e-2. + */ + private Integer width; + + /** + * The height of the generated images. Must be one of 256, 512, or 1024 for dall-e-2. + */ + private Integer height; + + /** + * The quality of the image that will be generated. hd creates images with finer + * details and greater consistency across the image. This param is only supported for + * dall-e-3. standard or hd + */ + private String quality; + + /** + * The format in which the generated images are returned. Must be one of url or + * b64_json. + */ + private String responseFormat; + + /** + * The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024 for + * dall-e-2. Must be one of 1024x1024, 1792x1024, or 1024x1792 for dall-e-3 models. + */ + private String size; + + /** + * The style of the generated images. Must be one of vivid or natural. Vivid causes + * the model to lean towards generating hyper-real and dramatic images. Natural causes + * the model to produce more natural, less hyper-real looking images. This param is + * only supported for dall-e-3. natural or vivid + */ + private String style; + + /** + * A unique identifier representing your end-user, which can help OpenAI to monitor + * and detect abuse. + */ + private String user; + + public static Builder builder() { + return new Builder(); + } + + @Override + public Integer getN() { + return this.n; + } + + public void setN(Integer n) { + this.n = n; + } + + @Override + public Integer getWidth() { + return this.width; + } + + public void setWidth(Integer width) { + this.width = width; + this.size = this.width + "x" + this.height; + } + + @Override + public Integer getHeight() { + return this.height; + } + + public void setHeight(Integer height) { + this.height = height; + this.size = this.width + "x" + this.height; + } + + @Override + public String getResponseFormat() { + return this.responseFormat; + } + + public void setResponseFormat(String responseFormat) { + this.responseFormat = responseFormat; + } + + public String getSize() { + if (this.size != null) { + return this.size; + } + return (this.width != null && this.height != null) ? this.width + "x" + this.height : null; + } + + public void setSize(String size) { + this.size = size; + } + + public String getUser() { + return this.user; + } + + public void setUser(String user) { + this.user = user; + } + + public String getQuality() { + return this.quality; + } + + public void setQuality(String quality) { + this.quality = quality; + } + + @Override + public String getStyle() { + return this.style; + } + + public void setStyle(String style) { + this.style = style; + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) + return false; + OpenAiOfficialImageOptions that = (OpenAiOfficialImageOptions) o; + return Objects.equals(n, that.n) && Objects.equals(width, that.width) && Objects.equals(height, that.height) + && Objects.equals(quality, that.quality) && Objects.equals(responseFormat, that.responseFormat) + && Objects.equals(size, that.size) && Objects.equals(style, that.style) + && Objects.equals(user, that.user); + } + + @Override + public int hashCode() { + return Objects.hash(n, width, height, quality, responseFormat, size, style, user); + } + + @Override + public String toString() { + return "OpenAiOfficialImageOptions{" + "n=" + n + ", width=" + width + ", height=" + height + ", quality='" + + quality + '\'' + ", responseFormat='" + responseFormat + '\'' + ", size='" + size + '\'' + ", style='" + + style + '\'' + ", user='" + user + '\'' + '}'; + } + + public ImageGenerateParams toOpenAiImageGenerateParams(ImagePrompt imagePrompt) { + if (imagePrompt.getInstructions().isEmpty()) { + throw new IllegalArgumentException("Image prompt instructions cannot be empty"); + } + + String prompt = imagePrompt.getInstructions().get(0).getText(); + ImageGenerateParams.Builder builder = ImageGenerateParams.builder().prompt(prompt); + + // Use deployment name if available (for Azure AI Foundry), otherwise use model + // name + if (this.getDeploymentName() != null) { + builder.model(this.getDeploymentName()); + } + else if (this.getModel() != null) { + builder.model(this.getModel()); + } + + if (this.getN() != null) { + builder.n(this.getN().longValue()); + } + if (this.getQuality() != null) { + builder.quality(ImageGenerateParams.Quality.of(this.getQuality().toLowerCase())); + } + if (this.getResponseFormat() != null) { + builder.responseFormat(ImageGenerateParams.ResponseFormat.of(this.getResponseFormat().toLowerCase())); + } + if (this.getSize() != null) { + builder.size(ImageGenerateParams.Size.of(this.getSize())); + } + if (this.getStyle() != null) { + builder.style(ImageGenerateParams.Style.of(this.getStyle().toLowerCase())); + } + if (this.getUser() != null) { + builder.user(this.getUser()); + } + + return builder.build(); + } + + public static final class Builder { + + private final OpenAiOfficialImageOptions options; + + private Builder() { + this.options = new OpenAiOfficialImageOptions(); + } + + public Builder from(OpenAiOfficialImageOptions fromOptions) { + this.options.setN(fromOptions.getN()); + this.options.setModel(fromOptions.getModel()); + this.options.setDeploymentName(fromOptions.getDeploymentName()); + this.options.setWidth(fromOptions.getWidth()); + this.options.setHeight(fromOptions.getHeight()); + this.options.setQuality(fromOptions.getQuality()); + this.options.setResponseFormat(fromOptions.getResponseFormat()); + this.options.setSize(fromOptions.getSize()); + this.options.setStyle(fromOptions.getStyle()); + this.options.setUser(fromOptions.getUser()); + return this; + } + + public Builder merge(ImageOptions from) { + if (from instanceof OpenAiOfficialImageOptions castFrom) { + if (castFrom.getN() != null) { + this.options.setN(castFrom.getN()); + } + if (castFrom.getModel() != null) { + this.options.setModel(castFrom.getModel()); + } + if (castFrom.getDeploymentName() != null) { + this.options.setDeploymentName(castFrom.getDeploymentName()); + } + if (castFrom.getWidth() != null) { + this.options.setWidth(castFrom.getWidth()); + } + if (castFrom.getHeight() != null) { + this.options.setHeight(castFrom.getHeight()); + } + if (castFrom.getQuality() != null) { + this.options.setQuality(castFrom.getQuality()); + } + if (castFrom.getResponseFormat() != null) { + this.options.setResponseFormat(castFrom.getResponseFormat()); + } + if (castFrom.getSize() != null) { + this.options.setSize(castFrom.getSize()); + } + if (castFrom.getStyle() != null) { + this.options.setStyle(castFrom.getStyle()); + } + if (castFrom.getUser() != null) { + this.options.setUser(castFrom.getUser()); + } + } + return this; + } + + public Builder N(Integer n) { + this.options.setN(n); + return this; + } + + public Builder model(String model) { + this.options.setModel(model); + return this; + } + + public Builder deploymentName(String deploymentName) { + this.options.setDeploymentName(deploymentName); + return this; + } + + public Builder responseFormat(String responseFormat) { + this.options.setResponseFormat(responseFormat); + return this; + } + + public Builder width(Integer width) { + this.options.setWidth(width); + return this; + } + + public Builder height(Integer height) { + this.options.setHeight(height); + return this; + } + + public Builder user(String user) { + this.options.setUser(user); + return this; + } + + public Builder style(String style) { + this.options.setStyle(style); + return this; + } + + public OpenAiOfficialImageOptions build() { + return this.options; + } + + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageGenerationMetadata.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageGenerationMetadata.java index 7e446e3eac3..98d9ba1d130 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageGenerationMetadata.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageGenerationMetadata.java @@ -28,48 +28,48 @@ */ public class OpenAiOfficialImageGenerationMetadata implements ImageGenerationMetadata { - private final String revisedPrompt; + private final String revisedPrompt; - /** - * Creates a new OpenAiOfficialImageGenerationMetadata. - * @param revisedPrompt the revised prompt used for generation - */ - public OpenAiOfficialImageGenerationMetadata(Optional revisedPrompt) { - if (revisedPrompt.isPresent()) { - this.revisedPrompt = revisedPrompt.get(); - } - else { - this.revisedPrompt = null; - } - } + /** + * Creates a new OpenAiOfficialImageGenerationMetadata. + * @param revisedPrompt the revised prompt used for generation + */ + public OpenAiOfficialImageGenerationMetadata(Optional revisedPrompt) { + if (revisedPrompt.isPresent()) { + this.revisedPrompt = revisedPrompt.get(); + } + else { + this.revisedPrompt = null; + } + } - /** - * Gets the revised prompt that was used for image generation. - * @return the revised prompt, or null if not available - */ - public String getRevisedPrompt() { - return this.revisedPrompt; - } + /** + * Gets the revised prompt that was used for image generation. + * @return the revised prompt, or null if not available + */ + public String getRevisedPrompt() { + return this.revisedPrompt; + } - @Override - public String toString() { - return "OpenAiOfficialImageGenerationMetadata{" + "revisedPrompt='" + revisedPrompt + '\'' + '}'; - } + @Override + public String toString() { + return "OpenAiOfficialImageGenerationMetadata{" + "revisedPrompt='" + revisedPrompt + '\'' + '}'; + } - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof OpenAiOfficialImageGenerationMetadata that)) { - return false; - } - return Objects.equals(this.revisedPrompt, that.revisedPrompt); - } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof OpenAiOfficialImageGenerationMetadata that)) { + return false; + } + return Objects.equals(this.revisedPrompt, that.revisedPrompt); + } - @Override - public int hashCode() { - return Objects.hash(this.revisedPrompt); - } + @Override + public int hashCode() { + return Objects.hash(this.revisedPrompt); + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageResponseMetadata.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageResponseMetadata.java index 34567225799..b7f0be15fb8 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageResponseMetadata.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageResponseMetadata.java @@ -29,50 +29,50 @@ */ public class OpenAiOfficialImageResponseMetadata extends ImageResponseMetadata { - private final Long created; + private final Long created; - /** - * Creates a new OpenAiOfficialImageResponseMetadata. - * @param created the creation timestamp - */ - protected OpenAiOfficialImageResponseMetadata(Long created) { - this.created = created; - } + /** + * Creates a new OpenAiOfficialImageResponseMetadata. + * @param created the creation timestamp + */ + protected OpenAiOfficialImageResponseMetadata(Long created) { + this.created = created; + } - /** - * Creates metadata from an ImagesResponse. - * @param imagesResponse the OpenAI images response - * @return the metadata instance - */ - public static OpenAiOfficialImageResponseMetadata from(ImagesResponse imagesResponse) { - Assert.notNull(imagesResponse, "imagesResponse must not be null"); - return new OpenAiOfficialImageResponseMetadata(imagesResponse.created()); - } + /** + * Creates metadata from an ImagesResponse. + * @param imagesResponse the OpenAI images response + * @return the metadata instance + */ + public static OpenAiOfficialImageResponseMetadata from(ImagesResponse imagesResponse) { + Assert.notNull(imagesResponse, "imagesResponse must not be null"); + return new OpenAiOfficialImageResponseMetadata(imagesResponse.created()); + } - @Override - public Long getCreated() { - return this.created; - } + @Override + public Long getCreated() { + return this.created; + } - @Override - public String toString() { - return "OpenAiOfficialImageResponseMetadata{" + "created=" + created + '}'; - } + @Override + public String toString() { + return "OpenAiOfficialImageResponseMetadata{" + "created=" + created + '}'; + } - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof OpenAiOfficialImageResponseMetadata that)) { - return false; - } - return Objects.equals(this.created, that.created); - } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof OpenAiOfficialImageResponseMetadata that)) { + return false; + } + return Objects.equals(this.created, that.created); + } - @Override - public int hashCode() { - return Objects.hash(this.created); - } + @Override + public int hashCode() { + return Objects.hash(this.created); + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/package-info.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/package-info.java index dc9412f493b..2903ac2375d 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/package-info.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/package-info.java @@ -17,10 +17,9 @@ /** * Metadata classes for OpenAI Official model responses. *

- * This package contains metadata implementations for chat, embedding, - * and image model responses. + * This package contains metadata implementations for chat, embedding, and image model + * responses. * * @author Julien Dubois */ package org.springframework.ai.openaiofficial.metadata; - diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/package-info.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/package-info.java index e3277b6d0a8..322b5a57ccf 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/package-info.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/package-info.java @@ -17,10 +17,9 @@ /** * Spring AI integration with the official OpenAI Java SDK. *

- * This package provides chat, embedding, and image model implementations - * using the official OpenAI Java client library. + * This package provides chat, embedding, and image model implementations using the + * official OpenAI Java client library. * * @author Julien Dubois */ package org.springframework.ai.openaiofficial; - diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java index cd335b45c5b..12a8c01e842 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java @@ -22,9 +22,8 @@ import com.openai.credential.Credential; /** - * Specific configuration for authenticating on Azure. - * This is in a separate class to avoid needing the Azure SDK dependencies - * when not using Azure as a platform. + * Specific configuration for authenticating on Azure. This is in a separate class to + * avoid needing the Azure SDK dependencies when not using Azure as a platform. * * This code is inspired by LangChain4j's * `dev.langchain4j.model.openaiofficial.AzureInternalOpenAiOfficialHelper` class, which @@ -34,9 +33,9 @@ */ class AzureInternalOpenAiOfficialHelper { - static Credential getAzureCredential() { - return BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier( - new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default")); - } + static Credential getAzureCredential() { + return BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier( + new DefaultAzureCredentialBuilder().build(), "https://cognitiveservices.azure.com/.default")); + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java index ee46439eb14..216a82dc695 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java @@ -43,233 +43,230 @@ */ public class OpenAiOfficialSetup { - static final String OPENAI_URL = "https://api.openai.com/v1"; - static final String OPENAI_API_KEY = "OPENAI_API_KEY"; - static final String AZURE_OPENAI_KEY = "AZURE_OPENAI_KEY"; - static final String GITHUB_MODELS_URL = "https://models.inference.ai.azure.com"; - static final String GITHUB_TOKEN = "GITHUB_TOKEN"; - static final String DEFAULT_USER_AGENT = "spring-ai-openai-official"; - - private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialSetup.class); - - private static final Duration DEFAULT_DURATION = ofSeconds(60); - - private static final int DEFAULT_MAX_RETRIES = 3; - - public enum ModelProvider { - - OPENAI, AZURE_OPENAI, GITHUB_MODELS - - } - - public static OpenAIClient setupSyncClient(String baseUrl, String apiKey, Credential credential, - String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAiServiceVersion, String organizationId, - boolean isAzure, boolean isGitHubModels, String modelName, Duration timeout, Integer maxRetries, - Proxy proxy, Map customHeaders) { - - baseUrl = detectBaseUrlFromEnv(baseUrl); - var modelProvider = detectModelProvider(isAzure, isGitHubModels, baseUrl, azureDeploymentName, - azureOpenAiServiceVersion); - if (timeout == null) { - timeout = DEFAULT_DURATION; - } - if (maxRetries == null) { - maxRetries = DEFAULT_MAX_RETRIES; - } - OpenAIOkHttpClient.Builder builder = OpenAIOkHttpClient.builder(); - builder - .baseUrl(calculateBaseUrl(baseUrl, modelProvider, modelName, azureDeploymentName, azureOpenAiServiceVersion)); - - String calculatedApiKey = apiKey != null ? apiKey : detectApiKey(modelProvider); - if (calculatedApiKey != null) { - builder.apiKey(calculatedApiKey); - } - else { - if (credential != null) { - builder.credential(credential); - } - else if (modelProvider == ModelProvider.AZURE_OPENAI) { - // If no API key is provided for Azure OpenAI, we try to use passwordless - // authentication - builder.credential(azureAuthentication()); - } - } - builder.organization(organizationId); - - if (azureOpenAiServiceVersion != null) { - builder.azureServiceVersion(azureOpenAiServiceVersion); - } - - if (proxy != null) { - builder.proxy(proxy); - } - - builder.putHeader("User-Agent", DEFAULT_USER_AGENT); - if (customHeaders != null) { - builder.putAllHeaders(customHeaders.entrySet() - .stream() - .collect(Collectors.toMap(Map.Entry::getKey, entry -> Collections.singletonList(entry.getValue())))); - } - - builder.timeout(timeout); - builder.maxRetries(maxRetries); - return builder.build(); - } - - /** - * The asynchronous client setup is the same as the synchronous one in the OpenAI Java - * SDK, but uses a different client implementation. - */ - public static OpenAIClientAsync setupAsyncClient(String baseUrl, String apiKey, Credential credential, - String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAiServiceVersion, String organizationId, - boolean isAzure, boolean isGitHubModels, String modelName, Duration timeout, Integer maxRetries, - Proxy proxy, Map customHeaders) { - - baseUrl = detectBaseUrlFromEnv(baseUrl); - var modelProvider = detectModelProvider(isAzure, isGitHubModels, baseUrl, azureDeploymentName, - azureOpenAiServiceVersion); - if (timeout == null) { - timeout = DEFAULT_DURATION; - } - if (maxRetries == null) { - maxRetries = DEFAULT_MAX_RETRIES; - } - OpenAIOkHttpClientAsync.Builder builder = OpenAIOkHttpClientAsync.builder(); - builder - .baseUrl(calculateBaseUrl(baseUrl, modelProvider, modelName, azureDeploymentName, azureOpenAiServiceVersion)); - - String calculatedApiKey = apiKey != null ? apiKey : detectApiKey(modelProvider); - if (calculatedApiKey != null) { - builder.apiKey(calculatedApiKey); - } - else { - if (credential != null) { - builder.credential(credential); - } - else if (modelProvider == ModelProvider.AZURE_OPENAI) { - // If no API key is provided for Azure OpenAI, we try to use passwordless - // authentication - builder.credential(azureAuthentication()); - } - } - builder.organization(organizationId); - - if (azureOpenAiServiceVersion != null) { - builder.azureServiceVersion(azureOpenAiServiceVersion); - } - - if (proxy != null) { - builder.proxy(proxy); - } - - builder.putHeader("User-Agent", DEFAULT_USER_AGENT); - if (customHeaders != null) { - builder.putAllHeaders(customHeaders.entrySet() - .stream() - .collect(Collectors.toMap(Map.Entry::getKey, entry -> Collections.singletonList(entry.getValue())))); - } - - builder.timeout(timeout); - builder.maxRetries(maxRetries); - return builder.build(); - } - - static String detectBaseUrlFromEnv(String baseUrl) { - if (baseUrl == null) { - var openAiBaseUrl = System.getenv("OPENAI_BASE_URL"); - if (openAiBaseUrl != null) { - baseUrl = openAiBaseUrl; - logger.debug("OpenAI Base URL detected from environment variable OPENAI_BASE_URL."); - } - var azureOpenAiBaseUrl = System.getenv("AZURE_OPENAI_BASE_URL"); - if (azureOpenAiBaseUrl != null) { - baseUrl = azureOpenAiBaseUrl; - logger.debug("Azure OpenAI Base URL detected from environment variable AZURE_OPENAI_BASE_URL."); - } - } - return baseUrl; - } - - static ModelProvider detectModelProvider(boolean isAzure, boolean isGitHubModels, String baseUrl, - String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAIServiceVersion) { - - if (isAzure) { - return ModelProvider.AZURE_OPENAI; // Forced by the user - } - if (isGitHubModels) { - return ModelProvider.GITHUB_MODELS; // Forced by the user - } - if (baseUrl != null) { - if (baseUrl.endsWith("openai.azure.com") || baseUrl.endsWith("openai.azure.com/") - || baseUrl.endsWith("cognitiveservices.azure.com") - || baseUrl.endsWith("cognitiveservices.azure.com/")) { - return ModelProvider.AZURE_OPENAI; - } - else if (baseUrl.startsWith(GITHUB_MODELS_URL)) { - return ModelProvider.GITHUB_MODELS; - } - } - if (azureDeploymentName != null || azureOpenAIServiceVersion != null) { - return ModelProvider.AZURE_OPENAI; - } - return ModelProvider.OPENAI; - } - - static String calculateBaseUrl(String baseUrl, ModelProvider modelProvider, String modelName, String azureDeploymentName, - AzureOpenAIServiceVersion azureOpenAiServiceVersion) { - - if (modelProvider == ModelProvider.OPENAI) { - if (baseUrl == null || baseUrl.isBlank()) { - return OPENAI_URL; - } - return baseUrl; - } - else if (modelProvider == ModelProvider.GITHUB_MODELS) { - return GITHUB_MODELS_URL; - } - else if (modelProvider == ModelProvider.AZURE_OPENAI) { - // Using Azure OpenAI - String tmpUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl; - // If the Azure deployment name is not configured, the model name will be used - // by default by the OpenAI Java - // SDK - if (azureDeploymentName != null && !azureDeploymentName.equals(modelName)) { - tmpUrl += "/openai/deployments/" + azureDeploymentName; - } - if (azureOpenAiServiceVersion != null) { - tmpUrl += "?api-version=" + azureOpenAiServiceVersion.value(); - } - return tmpUrl; - } - else { - throw new IllegalArgumentException("Unknown model host: " + modelProvider); - } - } - - static Credential azureAuthentication() { - try { - return AzureInternalOpenAiOfficialHelper.getAzureCredential(); - } - catch (NoClassDefFoundError e) { - throw new IllegalArgumentException("Azure OpenAI was detected, but no credential was provided. " - + "If you want to use passwordless authentication, you need to add the Azure Identity library (groupId=`com.azure`, artifactId=`azure-identity`) to your classpath."); - } - } - - static String detectApiKey(ModelProvider modelProvider) { - if (modelProvider == ModelProvider.OPENAI && System.getenv(OPENAI_API_KEY) != null) { - return System.getenv(OPENAI_API_KEY); - } - else if (modelProvider == ModelProvider.AZURE_OPENAI && System.getenv(AZURE_OPENAI_KEY) != null) { - return System.getenv(AZURE_OPENAI_KEY); - } - else if (modelProvider == ModelProvider.AZURE_OPENAI && System.getenv(OPENAI_API_KEY) != null) { - return System.getenv(OPENAI_API_KEY); - } - else if (modelProvider == ModelProvider.GITHUB_MODELS && System.getenv(GITHUB_TOKEN) != null) { - return System.getenv(GITHUB_TOKEN); - } - return null; - } + static final String OPENAI_URL = "https://api.openai.com/v1"; + static final String OPENAI_API_KEY = "OPENAI_API_KEY"; + static final String AZURE_OPENAI_KEY = "AZURE_OPENAI_KEY"; + static final String GITHUB_MODELS_URL = "https://models.inference.ai.azure.com"; + static final String GITHUB_TOKEN = "GITHUB_TOKEN"; + static final String DEFAULT_USER_AGENT = "spring-ai-openai-official"; + + private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialSetup.class); + + private static final Duration DEFAULT_DURATION = ofSeconds(60); + + private static final int DEFAULT_MAX_RETRIES = 3; + + public enum ModelProvider { + + OPEN_AI, AZURE_OPEN_AI, GITHUB_MODELS + + } + + public static OpenAIClient setupSyncClient(String baseUrl, String apiKey, Credential credential, + String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAiServiceVersion, String organizationId, + boolean isAzure, boolean isGitHubModels, String modelName, Duration timeout, Integer maxRetries, + Proxy proxy, Map customHeaders) { + + baseUrl = detectBaseUrlFromEnv(baseUrl); + var modelProvider = detectModelProvider(isAzure, isGitHubModels, baseUrl, azureDeploymentName, + azureOpenAiServiceVersion); + if (timeout == null) { + timeout = DEFAULT_DURATION; + } + if (maxRetries == null) { + maxRetries = DEFAULT_MAX_RETRIES; + } + OpenAIOkHttpClient.Builder builder = OpenAIOkHttpClient.builder(); + builder.baseUrl(calculateBaseUrl(baseUrl, modelProvider, modelName, azureDeploymentName)); + + String calculatedApiKey = apiKey != null ? apiKey : detectApiKey(modelProvider); + if (calculatedApiKey != null) { + builder.apiKey(calculatedApiKey); + } + else { + if (credential != null) { + builder.credential(credential); + } + else if (modelProvider == ModelProvider.AZURE_OPEN_AI) { + // If no API key is provided for Azure OpenAI, we try to use passwordless + // authentication + builder.credential(azureAuthentication()); + } + } + builder.organization(organizationId); + + if (azureOpenAiServiceVersion != null) { + builder.azureServiceVersion(azureOpenAiServiceVersion); + } + + if (proxy != null) { + builder.proxy(proxy); + } + + builder.putHeader("User-Agent", DEFAULT_USER_AGENT); + if (customHeaders != null) { + builder.putAllHeaders(customHeaders.entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> Collections.singletonList(entry.getValue())))); + } + + builder.timeout(timeout); + builder.maxRetries(maxRetries); + return builder.build(); + } + + /** + * The asynchronous client setup is the same as the synchronous one in the OpenAI Java + * SDK, but uses a different client implementation. + */ + public static OpenAIClientAsync setupAsyncClient(String baseUrl, String apiKey, Credential credential, + String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAiServiceVersion, String organizationId, + boolean isAzure, boolean isGitHubModels, String modelName, Duration timeout, Integer maxRetries, + Proxy proxy, Map customHeaders) { + + baseUrl = detectBaseUrlFromEnv(baseUrl); + var modelProvider = detectModelProvider(isAzure, isGitHubModels, baseUrl, azureDeploymentName, + azureOpenAiServiceVersion); + if (timeout == null) { + timeout = DEFAULT_DURATION; + } + if (maxRetries == null) { + maxRetries = DEFAULT_MAX_RETRIES; + } + OpenAIOkHttpClientAsync.Builder builder = OpenAIOkHttpClientAsync.builder(); + builder.baseUrl(calculateBaseUrl(baseUrl, modelProvider, modelName, azureDeploymentName)); + + String calculatedApiKey = apiKey != null ? apiKey : detectApiKey(modelProvider); + if (calculatedApiKey != null) { + builder.apiKey(calculatedApiKey); + } + else { + if (credential != null) { + builder.credential(credential); + } + else if (modelProvider == ModelProvider.AZURE_OPEN_AI) { + // If no API key is provided for Azure OpenAI, we try to use passwordless + // authentication + builder.credential(azureAuthentication()); + } + } + builder.organization(organizationId); + + if (azureOpenAiServiceVersion != null) { + builder.azureServiceVersion(azureOpenAiServiceVersion); + } + + if (proxy != null) { + builder.proxy(proxy); + } + + builder.putHeader("User-Agent", DEFAULT_USER_AGENT); + if (customHeaders != null) { + builder.putAllHeaders(customHeaders.entrySet() + .stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> Collections.singletonList(entry.getValue())))); + } + + builder.timeout(timeout); + builder.maxRetries(maxRetries); + return builder.build(); + } + + static String detectBaseUrlFromEnv(String baseUrl) { + if (baseUrl == null) { + var openAiBaseUrl = System.getenv("OPENAI_BASE_URL"); + if (openAiBaseUrl != null) { + baseUrl = openAiBaseUrl; + logger.debug("OpenAI Base URL detected from environment variable OPENAI_BASE_URL."); + } + var azureOpenAiBaseUrl = System.getenv("AZURE_OPENAI_BASE_URL"); + if (azureOpenAiBaseUrl != null) { + baseUrl = azureOpenAiBaseUrl; + logger.debug("Azure OpenAI Base URL detected from environment variable AZURE_OPENAI_BASE_URL."); + } + } + return baseUrl; + } + + public static ModelProvider detectModelProvider(boolean isAzure, boolean isGitHubModels, String baseUrl, + String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAIServiceVersion) { + + if (isAzure) { + return ModelProvider.AZURE_OPEN_AI; // Forced by the user + } + if (isGitHubModels) { + return ModelProvider.GITHUB_MODELS; // Forced by the user + } + if (baseUrl != null) { + if (baseUrl.endsWith("openai.azure.com") || baseUrl.endsWith("openai.azure.com/") + || baseUrl.endsWith("cognitiveservices.azure.com") + || baseUrl.endsWith("cognitiveservices.azure.com/")) { + return ModelProvider.AZURE_OPEN_AI; + } + else if (baseUrl.startsWith(GITHUB_MODELS_URL)) { + return ModelProvider.GITHUB_MODELS; + } + } + if (azureDeploymentName != null || azureOpenAIServiceVersion != null) { + return ModelProvider.AZURE_OPEN_AI; + } + return ModelProvider.OPEN_AI; + } + + static String calculateBaseUrl(String baseUrl, ModelProvider modelProvider, String modelName, + String azureDeploymentName) { + + if (modelProvider == ModelProvider.OPEN_AI) { + if (baseUrl == null || baseUrl.isBlank()) { + return OPENAI_URL; + } + return baseUrl; + } + else if (modelProvider == ModelProvider.GITHUB_MODELS) { + return GITHUB_MODELS_URL; + } + else if (modelProvider == ModelProvider.AZURE_OPEN_AI) { + String tmpUrl = baseUrl; + if (baseUrl.endsWith("/") || baseUrl.endsWith("?")) { + tmpUrl = baseUrl.substring(0, baseUrl.length() - 1); + } + // If the Azure deployment name is not configured, the model name will be used + // by default by the OpenAI Java + // SDK + if (azureDeploymentName != null && !azureDeploymentName.equals(modelName)) { + tmpUrl += "/openai/deployments/" + azureDeploymentName; + } + return tmpUrl; + } + else { + throw new IllegalArgumentException("Unknown model provider: " + modelProvider); + } + } + + static Credential azureAuthentication() { + try { + return AzureInternalOpenAiOfficialHelper.getAzureCredential(); + } + catch (NoClassDefFoundError e) { + throw new IllegalArgumentException("Azure OpenAI was detected, but no credential was provided. " + + "If you want to use passwordless authentication, you need to add the Azure Identity library (groupId=`com.azure`, artifactId=`azure-identity`) to your classpath."); + } + } + + static String detectApiKey(ModelProvider modelProvider) { + if (modelProvider == ModelProvider.OPEN_AI && System.getenv(OPENAI_API_KEY) != null) { + return System.getenv(OPENAI_API_KEY); + } + else if (modelProvider == ModelProvider.AZURE_OPEN_AI && System.getenv(AZURE_OPENAI_KEY) != null) { + return System.getenv(AZURE_OPENAI_KEY); + } + else if (modelProvider == ModelProvider.AZURE_OPEN_AI && System.getenv(OPENAI_API_KEY) != null) { + return System.getenv(OPENAI_API_KEY); + } + else if (modelProvider == ModelProvider.GITHUB_MODELS && System.getenv(GITHUB_TOKEN) != null) { + return System.getenv(GITHUB_TOKEN); + } + return null; + } } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/package-info.java b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/package-info.java index 202da167925..dd699ffee52 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/package-info.java +++ b/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/package-info.java @@ -17,11 +17,9 @@ /** * Setup and configuration utilities for OpenAI Official clients. *

- * This package contains helper classes for configuring and setting up - * OpenAI clients for different environments including OpenAI, Azure OpenAI, - * and GitHub Models. + * This package contains helper classes for configuring and setting up OpenAI clients for + * different environments including OpenAI, Azure OpenAI, and GitHub Models. * * @author Julien Dubois */ package org.springframework.ai.openaiofficial.setup; - diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetupTests.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetupTests.java index c66b3d3ae97..2259dd52fed 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetupTests.java +++ b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetupTests.java @@ -14,14 +14,16 @@ public class OpenAiOfficialSetupTests { @Test void detectModelProvider_returnsAzureOpenAI_whenAzureFlagIsTrue() { - OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(true, false, null, null, null); + OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(true, false, null, null, + null); - assertEquals(OpenAiOfficialSetup.ModelProvider.AZURE_OPENAI, result); + assertEquals(OpenAiOfficialSetup.ModelProvider.AZURE_OPEN_AI, result); } @Test void detectModelProvider_returnsGitHubModels_whenGitHubFlagIsTrue() { - OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(false, true, null, null, null); + OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(false, true, null, null, + null); assertEquals(OpenAiOfficialSetup.ModelProvider.GITHUB_MODELS, result); } @@ -31,7 +33,7 @@ void detectModelProvider_returnsAzureOpenAI_whenBaseUrlMatchesAzure() { OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(false, false, "https://example.openai.azure.com", null, null); - assertEquals(OpenAiOfficialSetup.ModelProvider.AZURE_OPENAI, result); + assertEquals(OpenAiOfficialSetup.ModelProvider.AZURE_OPEN_AI, result); } @Test @@ -44,9 +46,10 @@ void detectModelProvider_returnsGitHubModels_whenBaseUrlMatchesGitHub() { @Test void detectModelProvider_returnsOpenAI_whenNoConditionsMatch() { - OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(false, false, null, null, null); + OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(false, false, null, null, + null); - assertEquals(OpenAiOfficialSetup.ModelProvider.OPENAI, result); + assertEquals(OpenAiOfficialSetup.ModelProvider.OPEN_AI, result); } @Test @@ -69,7 +72,7 @@ void setupSyncClient_appliesCustomHeaders_whenProvided() { @Test void calculateBaseUrl_returnsDefaultOpenAIUrl_whenBaseUrlIsNull() { - String result = OpenAiOfficialSetup.calculateBaseUrl(null, OpenAiOfficialSetup.ModelProvider.OPENAI, null, null, + String result = OpenAiOfficialSetup.calculateBaseUrl(null, OpenAiOfficialSetup.ModelProvider.OPEN_AI, null, null); assertEquals(OpenAiOfficialSetup.OPENAI_URL, result); @@ -77,7 +80,7 @@ void calculateBaseUrl_returnsDefaultOpenAIUrl_whenBaseUrlIsNull() { @Test void calculateBaseUrl_returnsGitHubUrl_whenModelHostIsGitHub() { - String result = OpenAiOfficialSetup.calculateBaseUrl(null, OpenAiOfficialSetup.ModelProvider.GITHUB_MODELS, null, + String result = OpenAiOfficialSetup.calculateBaseUrl(null, OpenAiOfficialSetup.ModelProvider.GITHUB_MODELS, null, null); assertEquals(OpenAiOfficialSetup.GITHUB_MODELS_URL, result); From fd7de3326e506af8c99f33afcfa2a4ba12042512 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Thu, 20 Nov 2025 18:40:20 +0100 Subject: [PATCH 27/49] Implementation of the OpenAI Java SDK - Refactor "official" to "sdk" Signed-off-by: Julien Dubois --- ...ialTestConfigurationWithObservability.java | 59 --------- .../setup/OpenAiOfficialSetupTests.java | 89 -------------- .../README.md | 0 .../pom.xml | 8 +- .../openaisdk/AbstractOpenAiSdkOptions.java} | 4 +- .../ai/openaisdk/OpenAiSdkChatModel.java} | 78 ++++++------ .../ai/openaisdk/OpenAiSdkChatOptions.java} | 28 ++--- .../openaisdk/OpenAiSdkEmbeddingModel.java} | 68 +++++------ .../openaisdk/OpenAiSdkEmbeddingOptions.java} | 14 +-- .../ai/openaisdk/OpenAiSdkImageModel.java} | 64 +++++----- .../ai/openaisdk/OpenAiSdkImageOptions.java} | 22 ++-- .../OpenAiSdkImageGenerationMetadata.java} | 12 +- .../OpenAiSdkImageResponseMetadata.java} | 16 +-- .../ai/openaisdk}/metadata/package-info.java | 4 +- .../ai/openaisdk}/package-info.java | 6 +- .../setup/AzureInternalOpenAiSdkHelper.java} | 4 +- .../ai/openaisdk/setup/OpenAiSdkSetup.java} | 10 +- .../ai/openaisdk}/setup/package-info.java | 4 +- .../OpenAiSdkTestConfiguration.java} | 18 +-- ...SdkTestConfigurationWithObservability.java | 59 +++++++++ .../ai/openaisdk}/chat/ActorsFilms.java | 2 +- .../openaisdk}/chat/MockWeatherService.java | 2 +- .../openaisdk/chat/OpenAiSdkChatModelIT.java} | 41 ++++--- .../OpenAiSdkChatModelObservationIT.java} | 25 ++-- .../OpenAiSdkChatModelResponseFormatIT.java} | 38 +++--- .../chat/OpenAiSdkChatOptionsTests.java} | 113 +++++++----------- .../embedding/OpenAiSdkEmbeddingIT.java} | 59 ++++----- ...OpenAiSdkEmbeddingModelObservationIT.java} | 22 ++-- .../image/OpenAiSdkImageModelIT.java} | 26 ++-- .../OpenAiSdkImageModelObservationIT.java} | 21 ++-- .../openaisdk/setup/OpenAiSdkSetupTests.java | 84 +++++++++++++ .../test/resources/prompts/system-message.st | 0 .../src/test/resources/test.png | Bin .../src/test/resources/text_source.txt | 0 .../test/script/deploy-azure-openai-models.sh | 2 +- pom.xml | 6 +- .../observation/conventions/AiProvider.java | 2 +- src/checkstyle/checkstyle-suppressions.xml | 2 +- 38 files changed, 483 insertions(+), 529 deletions(-) delete mode 100644 models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java delete mode 100644 models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetupTests.java rename models/{spring-ai-openai-official => spring-ai-openai-sdk}/README.md (100%) rename models/{spring-ai-openai-official => spring-ai-openai-sdk}/pom.xml (92%) rename models/{spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java => spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java} (97%) rename models/{spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java => spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java} (94%) rename models/{spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java => spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java} (96%) rename models/{spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java => spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingModel.java} (75%) rename models/{spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingOptions.java => spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingOptions.java} (88%) rename models/{spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageModel.java => spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageModel.java} (70%) rename models/{spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialImageOptions.java => spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java} (91%) rename models/{spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageGenerationMetadata.java => spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/OpenAiSdkImageGenerationMetadata.java} (78%) rename models/{spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageResponseMetadata.java => spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/OpenAiSdkImageResponseMetadata.java} (73%) rename models/{spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial => spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk}/metadata/package-info.java (87%) rename models/{spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial => spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk}/package-info.java (88%) rename models/{spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java => spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/AzureInternalOpenAiSdkHelper.java} (93%) rename models/{spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java => spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java} (97%) rename models/{spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial => spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk}/setup/package-info.java (87%) rename models/{spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java => spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfiguration.java} (65%) create mode 100644 models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfigurationWithObservability.java rename models/{spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial => spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk}/chat/ActorsFilms.java (95%) rename models/{spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial => spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk}/chat/MockWeatherService.java (98%) rename models/{spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java => spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java} (94%) rename models/{spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java => spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java} (85%) rename models/{spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java => spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelResponseFormatIT.java} (84%) rename models/{spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatOptionsTests.java => spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java} (83%) rename models/{spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/OpenAiOfficialEmbeddingIT.java => spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java} (66%) rename models/{spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/OpenAiOfficialEmbeddingModelObservationIT.java => spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java} (84%) rename models/{spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelIT.java => spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelIT.java} (71%) rename models/{spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java => spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelObservationIT.java} (84%) create mode 100644 models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java rename models/{spring-ai-openai-official => spring-ai-openai-sdk}/src/test/resources/prompts/system-message.st (100%) rename models/{spring-ai-openai-official => spring-ai-openai-sdk}/src/test/resources/test.png (100%) rename models/{spring-ai-openai-official => spring-ai-openai-sdk}/src/test/resources/text_source.txt (100%) rename models/{spring-ai-openai-official => spring-ai-openai-sdk}/src/test/script/deploy-azure-openai-models.sh (98%) diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java deleted file mode 100644 index 3261873aa43..00000000000 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfigurationWithObservability.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2023-2025 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.ai.openaiofficial; - -import io.micrometer.observation.tck.TestObservationRegistry; -import org.springframework.ai.document.MetadataMode; -import org.springframework.boot.SpringBootConfiguration; -import org.springframework.context.annotation.Bean; - -import static org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions.DEFAULT_CHAT_MODEL; -import static org.springframework.ai.openaiofficial.OpenAiOfficialEmbeddingOptions.DEFAULT_EMBEDDING_MODEL; -import static org.springframework.ai.openaiofficial.OpenAiOfficialImageOptions.DEFAULT_IMAGE_MODEL; - -/** - * Context configuration for OpenAI official SDK tests. - * - * @author Julien Dubois - */ -@SpringBootConfiguration -public class OpenAiOfficialTestConfigurationWithObservability { - - @Bean - public TestObservationRegistry testObservationRegistry() { - return TestObservationRegistry.create(); - } - - @Bean - public OpenAiOfficialEmbeddingModel openAiEmbeddingModel(TestObservationRegistry observationRegistry) { - return new OpenAiOfficialEmbeddingModel(MetadataMode.EMBED, - OpenAiOfficialEmbeddingOptions.builder().model(DEFAULT_EMBEDDING_MODEL).build(), observationRegistry); - } - - @Bean - public OpenAiOfficialImageModel openAiImageModel(TestObservationRegistry observationRegistry) { - return new OpenAiOfficialImageModel(OpenAiOfficialImageOptions.builder().model(DEFAULT_IMAGE_MODEL).build(), - observationRegistry); - } - - @Bean - public OpenAiOfficialChatModel openAiChatModel(TestObservationRegistry observationRegistry) { - return new OpenAiOfficialChatModel(OpenAiOfficialChatOptions.builder().model(DEFAULT_CHAT_MODEL).build(), - observationRegistry); - } - -} diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetupTests.java b/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetupTests.java deleted file mode 100644 index 2259dd52fed..00000000000 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetupTests.java +++ /dev/null @@ -1,89 +0,0 @@ -package org.springframework.ai.openaiofficial.setup; - -import com.openai.client.OpenAIClient; -import org.junit.jupiter.api.Test; - -import java.time.Duration; -import java.util.Collections; -import java.util.Map; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -public class OpenAiOfficialSetupTests { - - @Test - void detectModelProvider_returnsAzureOpenAI_whenAzureFlagIsTrue() { - OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(true, false, null, null, - null); - - assertEquals(OpenAiOfficialSetup.ModelProvider.AZURE_OPEN_AI, result); - } - - @Test - void detectModelProvider_returnsGitHubModels_whenGitHubFlagIsTrue() { - OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(false, true, null, null, - null); - - assertEquals(OpenAiOfficialSetup.ModelProvider.GITHUB_MODELS, result); - } - - @Test - void detectModelProvider_returnsAzureOpenAI_whenBaseUrlMatchesAzure() { - OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(false, false, - "https://example.openai.azure.com", null, null); - - assertEquals(OpenAiOfficialSetup.ModelProvider.AZURE_OPEN_AI, result); - } - - @Test - void detectModelProvider_returnsGitHubModels_whenBaseUrlMatchesGitHub() { - OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(false, false, - "https://models.inference.ai.azure.com", null, null); - - assertEquals(OpenAiOfficialSetup.ModelProvider.GITHUB_MODELS, result); - } - - @Test - void detectModelProvider_returnsOpenAI_whenNoConditionsMatch() { - OpenAiOfficialSetup.ModelProvider result = OpenAiOfficialSetup.detectModelProvider(false, false, null, null, - null); - - assertEquals(OpenAiOfficialSetup.ModelProvider.OPEN_AI, result); - } - - @Test - void setupSyncClient_returnsClient_whenValidApiKeyProvided() { - OpenAIClient client = OpenAiOfficialSetup.setupSyncClient(null, "valid-api-key", null, null, null, null, false, - false, null, Duration.ofSeconds(30), 2, null, null); - - assertNotNull(client); - } - - @Test - void setupSyncClient_appliesCustomHeaders_whenProvided() { - Map customHeaders = Collections.singletonMap("X-Custom-Header", "value"); - - OpenAIClient client = OpenAiOfficialSetup.setupSyncClient(null, "valid-api-key", null, null, null, null, false, - false, null, Duration.ofSeconds(30), 2, null, customHeaders); - - assertNotNull(client); - } - - @Test - void calculateBaseUrl_returnsDefaultOpenAIUrl_whenBaseUrlIsNull() { - String result = OpenAiOfficialSetup.calculateBaseUrl(null, OpenAiOfficialSetup.ModelProvider.OPEN_AI, null, - null); - - assertEquals(OpenAiOfficialSetup.OPENAI_URL, result); - } - - @Test - void calculateBaseUrl_returnsGitHubUrl_whenModelHostIsGitHub() { - String result = OpenAiOfficialSetup.calculateBaseUrl(null, OpenAiOfficialSetup.ModelProvider.GITHUB_MODELS, - null, null); - - assertEquals(OpenAiOfficialSetup.GITHUB_MODELS_URL, result); - } - -} diff --git a/models/spring-ai-openai-official/README.md b/models/spring-ai-openai-sdk/README.md similarity index 100% rename from models/spring-ai-openai-official/README.md rename to models/spring-ai-openai-sdk/README.md diff --git a/models/spring-ai-openai-official/pom.xml b/models/spring-ai-openai-sdk/pom.xml similarity index 92% rename from models/spring-ai-openai-official/pom.xml rename to models/spring-ai-openai-sdk/pom.xml index 20feab8233f..09d37bcc339 100644 --- a/models/spring-ai-openai-official/pom.xml +++ b/models/spring-ai-openai-sdk/pom.xml @@ -23,10 +23,10 @@ 2.0.0-SNAPSHOT ../../pom.xml - spring-ai-openai-official + spring-ai-openai-sdk jar - Spring AI Model - OpenAI Official - OpenAI Java API Library support + Spring AI Model - OpenAI SDK + OpenAI Java SDK support https://github.com/spring-projects/spring-ai @@ -50,7 +50,7 @@ com.openai openai-java - ${openai-official.version} + ${openai-sdk.version} diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java similarity index 97% rename from models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java rename to models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java index 0044fd27645..8ba8c8baaed 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/AbstractOpenAiOfficialOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial; +package org.springframework.ai.openaisdk; import com.openai.azure.AzureOpenAIServiceVersion; import com.openai.credential.Credential; @@ -23,7 +23,7 @@ import java.time.Duration; import java.util.Map; -public class AbstractOpenAiOfficialOptions { +public class AbstractOpenAiSdkOptions { /** * The deployment URL to connect to OpenAI. diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java similarity index 94% rename from models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java rename to models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java index 01777ec2e87..1f005aec820 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatModel.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial; +package org.springframework.ai.openaisdk; import com.openai.client.OpenAIClient; import com.openai.client.OpenAIClientAsync; @@ -76,29 +76,29 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; -import static org.springframework.ai.openaiofficial.setup.OpenAiOfficialSetup.setupAsyncClient; -import static org.springframework.ai.openaiofficial.setup.OpenAiOfficialSetup.setupSyncClient; +import static org.springframework.ai.openaisdk.setup.OpenAiSdkSetup.setupAsyncClient; +import static org.springframework.ai.openaisdk.setup.OpenAiSdkSetup.setupSyncClient; /** * Chat Model implementation using the OpenAI Java SDK. * * @author Julien Dubois */ -public class OpenAiOfficialChatModel implements ChatModel { +public class OpenAiSdkChatModel implements ChatModel { - private static final String DEFAULT_MODEL_NAME = OpenAiOfficialChatOptions.DEFAULT_CHAT_MODEL; + private static final String DEFAULT_MODEL_NAME = OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL; private static final ChatModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultChatModelObservationConvention(); private static final ToolCallingManager DEFAULT_TOOL_CALLING_MANAGER = ToolCallingManager.builder().build(); - private final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatModel.class); + private final Logger logger = LoggerFactory.getLogger(OpenAiSdkChatModel.class); private final OpenAIClient openAiClient; private final OpenAIClientAsync openAiClientAsync; - private final OpenAiOfficialChatOptions options; + private final OpenAiSdkChatOptions options; private final ObservationRegistry observationRegistry; @@ -109,77 +109,76 @@ public class OpenAiOfficialChatModel implements ChatModel { private ChatModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; /** - * Creates a new OpenAiOfficialChatModel with default options. + * Creates a new OpenAiSdkChatModel with default options. */ - public OpenAiOfficialChatModel() { + public OpenAiSdkChatModel() { this(null, null, null, null, null, null); } /** - * Creates a new OpenAiOfficialChatModel with the given options. + * Creates a new OpenAiSdkChatModel with the given options. * @param options the chat options */ - public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options) { + public OpenAiSdkChatModel(OpenAiSdkChatOptions options) { this(null, null, options, null, null, null); } /** - * Creates a new OpenAiOfficialChatModel with the given options and observation - * registry. + * Creates a new OpenAiSdkChatModel with the given options and observation registry. * @param options the chat options * @param observationRegistry the observation registry */ - public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options, ObservationRegistry observationRegistry) { + public OpenAiSdkChatModel(OpenAiSdkChatOptions options, ObservationRegistry observationRegistry) { this(null, null, options, null, observationRegistry, null); } /** - * Creates a new OpenAiOfficialChatModel with the given options, tool calling manager, - * and observation registry. + * Creates a new OpenAiSdkChatModel with the given options, tool calling manager, and + * observation registry. * @param options the chat options * @param toolCallingManager the tool calling manager * @param observationRegistry the observation registry */ - public OpenAiOfficialChatModel(OpenAiOfficialChatOptions options, ToolCallingManager toolCallingManager, + public OpenAiSdkChatModel(OpenAiSdkChatOptions options, ToolCallingManager toolCallingManager, ObservationRegistry observationRegistry) { this(null, null, options, toolCallingManager, observationRegistry, null); } /** - * Creates a new OpenAiOfficialChatModel with the given OpenAI clients. + * Creates a new OpenAiSdkChatModel with the given OpenAI clients. * @param openAIClient the synchronous OpenAI client * @param openAiClientAsync the asynchronous OpenAI client */ - public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync) { + public OpenAiSdkChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync) { this(openAIClient, openAiClientAsync, null, null, null, null); } /** - * Creates a new OpenAiOfficialChatModel with the given OpenAI clients and options. + * Creates a new OpenAiSdkChatModel with the given OpenAI clients and options. * @param openAIClient the synchronous OpenAI client * @param openAiClientAsync the asynchronous OpenAI client * @param options the chat options */ - public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync, - OpenAiOfficialChatOptions options) { + public OpenAiSdkChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync, + OpenAiSdkChatOptions options) { this(openAIClient, openAiClientAsync, options, null, null, null); } /** - * Creates a new OpenAiOfficialChatModel with the given OpenAI clients, options, and + * Creates a new OpenAiSdkChatModel with the given OpenAI clients, options, and * observation registry. * @param openAIClient the synchronous OpenAI client * @param openAiClientAsync the asynchronous OpenAI client * @param options the chat options * @param observationRegistry the observation registry */ - public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync, - OpenAiOfficialChatOptions options, ObservationRegistry observationRegistry) { + public OpenAiSdkChatModel(OpenAIClient openAIClient, OpenAIClientAsync openAiClientAsync, + OpenAiSdkChatOptions options, ObservationRegistry observationRegistry) { this(openAIClient, openAiClientAsync, options, null, observationRegistry, null); } /** - * Creates a new OpenAiOfficialChatModel with all configuration options. + * Creates a new OpenAiSdkChatModel with all configuration options. * @param openAiClient the synchronous OpenAI client * @param openAiClientAsync the asynchronous OpenAI client * @param options the chat options @@ -188,13 +187,13 @@ public OpenAiOfficialChatModel(OpenAIClient openAIClient, OpenAIClientAsync open * @param toolExecutionEligibilityPredicate the predicate to determine tool execution * eligibility */ - public OpenAiOfficialChatModel(OpenAIClient openAiClient, OpenAIClientAsync openAiClientAsync, - OpenAiOfficialChatOptions options, ToolCallingManager toolCallingManager, + public OpenAiSdkChatModel(OpenAIClient openAiClient, OpenAIClientAsync openAiClientAsync, + OpenAiSdkChatOptions options, ToolCallingManager toolCallingManager, ObservationRegistry observationRegistry, ToolExecutionEligibilityPredicate toolExecutionEligibilityPredicate) { if (options == null) { - this.options = OpenAiOfficialChatOptions.builder().model(DEFAULT_MODEL_NAME).build(); + this.options = OpenAiSdkChatOptions.builder().model(DEFAULT_MODEL_NAME).build(); } else { this.options = options; @@ -224,7 +223,7 @@ public OpenAiOfficialChatModel(OpenAIClient openAiClient, OpenAIClientAsync open * Gets the chat options for this model. * @return the chat options */ - public OpenAiOfficialChatOptions getOptions() { + public OpenAiSdkChatOptions getOptions() { return this.options; } @@ -250,7 +249,7 @@ public ChatResponse internalCall(Prompt prompt, ChatResponse previousChatRespons ChatModelObservationContext observationContext = ChatModelObservationContext.builder() .prompt(prompt) - .provider(AiProvider.OPENAI_OFFICIAL.value()) + .provider(AiProvider.OPENAI_SDK.value()) .build(); ChatResponse response = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION @@ -348,7 +347,7 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha ConcurrentHashMap roleMap = new ConcurrentHashMap<>(); final ChatModelObservationContext observationContext = ChatModelObservationContext.builder() .prompt(prompt) - .provider(AiProvider.OPENAI_OFFICIAL.value()) + .provider(AiProvider.OPENAI_SDK.value()) .build(); Observation observation = ChatModelObservationDocumentation.CHAT_MODEL_OPERATION.observation( this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, @@ -649,22 +648,22 @@ private DefaultUsage getDefaultUsage(CompletionUsage usage) { */ Prompt buildRequestPrompt(Prompt prompt) { // Process runtime options - OpenAiOfficialChatOptions runtimeOptions = null; + OpenAiSdkChatOptions runtimeOptions = null; if (prompt.getOptions() != null) { if (prompt.getOptions() instanceof ToolCallingChatOptions toolCallingChatOptions) { runtimeOptions = ModelOptionsUtils.copyToTarget(toolCallingChatOptions, ToolCallingChatOptions.class, - OpenAiOfficialChatOptions.class); + OpenAiSdkChatOptions.class); } else { runtimeOptions = ModelOptionsUtils.copyToTarget(prompt.getOptions(), ChatOptions.class, - OpenAiOfficialChatOptions.class); + OpenAiSdkChatOptions.class); } } // Define request options by merging runtime options and default options - OpenAiOfficialChatOptions requestOptions = OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions requestOptions = OpenAiSdkChatOptions.builder() .from(this.options) - .merge(runtimeOptions != null ? runtimeOptions : OpenAiOfficialChatOptions.builder().build()) + .merge(runtimeOptions != null ? runtimeOptions : OpenAiSdkChatOptions.builder().build()) .build(); // Merge @JsonIgnore-annotated options explicitly since they are ignored by @@ -866,7 +865,7 @@ else if (message.getMessageType() == MessageType.TOOL) { chatCompletionMessageParams.forEach(builder::addMessage); - OpenAiOfficialChatOptions requestOptions = (OpenAiOfficialChatOptions) prompt.getOptions(); + OpenAiSdkChatOptions requestOptions = (OpenAiSdkChatOptions) prompt.getOptions(); // Use deployment name if available (for Azure AI Foundry), otherwise use model // name @@ -1094,8 +1093,7 @@ public void setObservationConvention(ChatModelObservationConvention observationC } /** - * Response format (text, json_object, json_schema) for OpenAiOfficialChatModel - * responses. + * Response format (text, json_object, json_schema) for OpenAiSdkChatModel responses. * * @author Julien Dubois */ diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java similarity index 96% rename from models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java rename to models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java index f22218ffc6c..fb648caff5a 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialChatOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial; +package org.springframework.ai.openaisdk; import com.openai.models.FunctionDefinition; import com.openai.models.chat.completions.ChatCompletionAudioParam; @@ -43,11 +43,11 @@ * * @author Julien Dubois */ -public class OpenAiOfficialChatOptions extends AbstractOpenAiOfficialOptions implements ToolCallingChatOptions { +public class OpenAiSdkChatOptions extends AbstractOpenAiSdkOptions implements ToolCallingChatOptions { public static final String DEFAULT_CHAT_MODEL = GPT_5_MINI.asString(); - private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatOptions.class); + private static final Logger logger = LoggerFactory.getLogger(OpenAiSdkChatOptions.class); private Double frequencyPenalty; @@ -67,7 +67,7 @@ public class OpenAiOfficialChatOptions extends AbstractOpenAiOfficialOptions imp private Double presencePenalty; - private OpenAiOfficialChatModel.ResponseFormat responseFormat; + private OpenAiSdkChatModel.ResponseFormat responseFormat; private ResponseCreateParams.StreamOptions streamOptions; @@ -252,7 +252,7 @@ public void setPresencePenalty(Double presencePenalty) { * Gets the response format configuration. * @return the response format */ - public OpenAiOfficialChatModel.ResponseFormat getResponseFormat() { + public OpenAiSdkChatModel.ResponseFormat getResponseFormat() { return this.responseFormat; } @@ -260,7 +260,7 @@ public OpenAiOfficialChatModel.ResponseFormat getResponseFormat() { * Sets the response format configuration. * @param responseFormat the response format to set */ - public void setResponseFormat(OpenAiOfficialChatModel.ResponseFormat responseFormat) { + public void setResponseFormat(OpenAiSdkChatModel.ResponseFormat responseFormat) { this.responseFormat = responseFormat; } @@ -583,7 +583,7 @@ public static Builder builder() { } @Override - public OpenAiOfficialChatOptions copy() { + public OpenAiSdkChatOptions copy() { return builder().from(this).build(); } @@ -591,7 +591,7 @@ public OpenAiOfficialChatOptions copy() { public boolean equals(Object o) { if (o == null || getClass() != o.getClass()) return false; - OpenAiOfficialChatOptions options = (OpenAiOfficialChatOptions) o; + OpenAiSdkChatOptions options = (OpenAiSdkChatOptions) o; return Objects.equals(getModel(), options.getModel()) && Objects.equals(frequencyPenalty, options.frequencyPenalty) && Objects.equals(logitBias, options.logitBias) && Objects.equals(logprobs, options.logprobs) @@ -625,7 +625,7 @@ public int hashCode() { @Override public String toString() { - return "OpenAiOfficialChatOptions{" + "model='" + getModel() + ", frequencyPenalty=" + frequencyPenalty + return "OpenAiSdkChatOptions{" + "model='" + getModel() + ", frequencyPenalty=" + frequencyPenalty + ", logitBias=" + logitBias + ", logprobs=" + logprobs + ", topLogprobs=" + topLogprobs + ", maxTokens=" + maxTokens + ", maxCompletionTokens=" + maxCompletionTokens + ", n=" + n + ", outputAudio=" + outputAudio + ", presencePenalty=" + presencePenalty + ", responseFormat=" @@ -640,9 +640,9 @@ public String toString() { public static final class Builder { - private final OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + private final OpenAiSdkChatOptions options = new OpenAiSdkChatOptions(); - public Builder from(OpenAiOfficialChatOptions fromOptions) { + public Builder from(OpenAiSdkChatOptions fromOptions) { this.options.setModel(fromOptions.getModel()); this.options.setDeploymentName(fromOptions.getDeploymentName()); this.options.setFrequencyPenalty(fromOptions.getFrequencyPenalty()); @@ -679,7 +679,7 @@ public Builder from(OpenAiOfficialChatOptions fromOptions) { return this; } - public Builder merge(OpenAiOfficialChatOptions from) { + public Builder merge(OpenAiSdkChatOptions from) { if (from.getModel() != null) { this.options.setModel(from.getModel()); } @@ -850,7 +850,7 @@ public Builder presencePenalty(Double presencePenalty) { return this; } - public Builder responseFormat(OpenAiOfficialChatModel.ResponseFormat responseFormat) { + public Builder responseFormat(OpenAiSdkChatModel.ResponseFormat responseFormat) { this.options.setResponseFormat(responseFormat); return this; } @@ -967,7 +967,7 @@ public Builder serviceTier(String serviceTier) { return this; } - public OpenAiOfficialChatOptions build() { + public OpenAiSdkChatOptions build() { return this.options; } diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingModel.java similarity index 75% rename from models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java rename to models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingModel.java index 8e9388c88a9..3900e4adade 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/OpenAiOfficialEmbeddingModel.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingModel.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial; +package org.springframework.ai.openaisdk; import com.openai.client.OpenAIClient; import com.openai.models.embeddings.CreateEmbeddingResponse; @@ -43,24 +43,24 @@ import java.util.List; import java.util.Objects; -import static org.springframework.ai.openaiofficial.setup.OpenAiOfficialSetup.setupSyncClient; +import static org.springframework.ai.openaisdk.setup.OpenAiSdkSetup.setupSyncClient; /** * Embedding Model implementation using the OpenAI Java SDK. * * @author Julien Dubois */ -public class OpenAiOfficialEmbeddingModel extends AbstractEmbeddingModel { +public class OpenAiSdkEmbeddingModel extends AbstractEmbeddingModel { - private static final String DEFAULT_MODEL_NAME = OpenAiOfficialEmbeddingOptions.DEFAULT_EMBEDDING_MODEL; + private static final String DEFAULT_MODEL_NAME = OpenAiSdkEmbeddingOptions.DEFAULT_EMBEDDING_MODEL; private static final EmbeddingModelObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultEmbeddingModelObservationConvention(); - private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialEmbeddingModel.class); + private static final Logger logger = LoggerFactory.getLogger(OpenAiSdkEmbeddingModel.class); private final OpenAIClient openAiClient; - private final OpenAiOfficialEmbeddingOptions options; + private final OpenAiSdkEmbeddingOptions options; private final MetadataMode metadataMode; @@ -69,94 +69,92 @@ public class OpenAiOfficialEmbeddingModel extends AbstractEmbeddingModel { private EmbeddingModelObservationConvention observationConvention = DEFAULT_OBSERVATION_CONVENTION; /** - * Creates a new OpenAiOfficialEmbeddingModel with default options. + * Creates a new OpenAiSdkEmbeddingModel with default options. */ - public OpenAiOfficialEmbeddingModel() { + public OpenAiSdkEmbeddingModel() { this(null, null, null, null); } /** - * Creates a new OpenAiOfficialEmbeddingModel with the given options. + * Creates a new OpenAiSdkEmbeddingModel with the given options. * @param options the embedding options */ - public OpenAiOfficialEmbeddingModel(OpenAiOfficialEmbeddingOptions options) { + public OpenAiSdkEmbeddingModel(OpenAiSdkEmbeddingOptions options) { this(null, null, options, null); } /** - * Creates a new OpenAiOfficialEmbeddingModel with the given metadata mode and - * options. + * Creates a new OpenAiSdkEmbeddingModel with the given metadata mode and options. * @param metadataMode the metadata mode * @param options the embedding options */ - public OpenAiOfficialEmbeddingModel(MetadataMode metadataMode, OpenAiOfficialEmbeddingOptions options) { + public OpenAiSdkEmbeddingModel(MetadataMode metadataMode, OpenAiSdkEmbeddingOptions options) { this(null, metadataMode, options, null); } /** - * Creates a new OpenAiOfficialEmbeddingModel with the given options and observation + * Creates a new OpenAiSdkEmbeddingModel with the given options and observation * registry. * @param options the embedding options * @param observationRegistry the observation registry */ - public OpenAiOfficialEmbeddingModel(OpenAiOfficialEmbeddingOptions options, - ObservationRegistry observationRegistry) { + public OpenAiSdkEmbeddingModel(OpenAiSdkEmbeddingOptions options, ObservationRegistry observationRegistry) { this(null, null, options, observationRegistry); } /** - * Creates a new OpenAiOfficialEmbeddingModel with the given metadata mode, options, - * and observation registry. + * Creates a new OpenAiSdkEmbeddingModel with the given metadata mode, options, and + * observation registry. * @param metadataMode the metadata mode * @param options the embedding options * @param observationRegistry the observation registry */ - public OpenAiOfficialEmbeddingModel(MetadataMode metadataMode, OpenAiOfficialEmbeddingOptions options, + public OpenAiSdkEmbeddingModel(MetadataMode metadataMode, OpenAiSdkEmbeddingOptions options, ObservationRegistry observationRegistry) { this(null, metadataMode, options, observationRegistry); } /** - * Creates a new OpenAiOfficialEmbeddingModel with the given OpenAI client. + * Creates a new OpenAiSdkEmbeddingModel with the given OpenAI client. * @param openAiClient the OpenAI client */ - public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient) { + public OpenAiSdkEmbeddingModel(OpenAIClient openAiClient) { this(openAiClient, null, null, null); } /** - * Creates a new OpenAiOfficialEmbeddingModel with the given OpenAI client and - * metadata mode. + * Creates a new OpenAiSdkEmbeddingModel with the given OpenAI client and metadata + * mode. * @param openAiClient the OpenAI client * @param metadataMode the metadata mode */ - public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode) { + public OpenAiSdkEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode) { this(openAiClient, metadataMode, null, null); } /** - * Creates a new OpenAiOfficialEmbeddingModel with all configuration options. + * Creates a new OpenAiSdkEmbeddingModel with all configuration options. * @param openAiClient the OpenAI client * @param metadataMode the metadata mode * @param options the embedding options */ - public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, - OpenAiOfficialEmbeddingOptions options) { + public OpenAiSdkEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, + OpenAiSdkEmbeddingOptions options) { this(openAiClient, metadataMode, options, null); } /** - * Creates a new OpenAiOfficialEmbeddingModel with all configuration options. + * Creates a new OpenAiSdkEmbeddingModel with all configuration options. * @param openAiClient the OpenAI client * @param metadataMode the metadata mode * @param options the embedding options * @param observationRegistry the observation registry */ - public OpenAiOfficialEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, - OpenAiOfficialEmbeddingOptions options, ObservationRegistry observationRegistry) { + public OpenAiSdkEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataMode, + OpenAiSdkEmbeddingOptions options, ObservationRegistry observationRegistry) { if (options == null) { - this.options = OpenAiOfficialEmbeddingOptions.builder().model(DEFAULT_MODEL_NAME).build(); + this.options = OpenAiSdkEmbeddingOptions.builder().model(DEFAULT_MODEL_NAME).build(); } else { this.options = options; @@ -184,7 +182,7 @@ public float[] embed(Document document) { @Override public EmbeddingResponse call(EmbeddingRequest embeddingRequest) { - OpenAiOfficialEmbeddingOptions options = OpenAiOfficialEmbeddingOptions.builder() + OpenAiSdkEmbeddingOptions options = OpenAiSdkEmbeddingOptions.builder() .from(this.options) .merge(embeddingRequest.getOptions()) .build(); @@ -196,13 +194,13 @@ public EmbeddingResponse call(EmbeddingRequest embeddingRequest) { .toOpenAiCreateParams(embeddingRequestWithMergedOptions.getInstructions()); if (logger.isTraceEnabled()) { - logger.trace("OpenAiOfficialEmbeddingModel call {} with the following options : {} ", options.getModel(), + logger.trace("OpenAiSdkEmbeddingModel call {} with the following options : {} ", options.getModel(), embeddingCreateParams); } var observationContext = EmbeddingModelObservationContext.builder() .embeddingRequest(embeddingRequestWithMergedOptions) - .provider(AiProvider.OPENAI_OFFICIAL.value()) + .provider(AiProvider.OPENAI_SDK.value()) .build(); return Objects.requireNonNull( @@ -248,7 +246,7 @@ private List generateEmbeddingList(List revisedPrompt) { + public OpenAiSdkImageGenerationMetadata(Optional revisedPrompt) { if (revisedPrompt.isPresent()) { this.revisedPrompt = revisedPrompt.get(); } @@ -53,7 +53,7 @@ public String getRevisedPrompt() { @Override public String toString() { - return "OpenAiOfficialImageGenerationMetadata{" + "revisedPrompt='" + revisedPrompt + '\'' + '}'; + return "OpenAiSdkImageGenerationMetadata{" + "revisedPrompt='" + revisedPrompt + '\'' + '}'; } @Override @@ -61,7 +61,7 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof OpenAiOfficialImageGenerationMetadata that)) { + if (!(o instanceof OpenAiSdkImageGenerationMetadata that)) { return false; } return Objects.equals(this.revisedPrompt, that.revisedPrompt); diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageResponseMetadata.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/OpenAiSdkImageResponseMetadata.java similarity index 73% rename from models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageResponseMetadata.java rename to models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/OpenAiSdkImageResponseMetadata.java index b7f0be15fb8..d28d973fdea 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/OpenAiOfficialImageResponseMetadata.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/OpenAiSdkImageResponseMetadata.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial.metadata; +package org.springframework.ai.openaisdk.metadata; import com.openai.models.images.ImagesResponse; import org.springframework.ai.image.ImageResponseMetadata; @@ -27,15 +27,15 @@ * * @author Julien Dubois */ -public class OpenAiOfficialImageResponseMetadata extends ImageResponseMetadata { +public class OpenAiSdkImageResponseMetadata extends ImageResponseMetadata { private final Long created; /** - * Creates a new OpenAiOfficialImageResponseMetadata. + * Creates a new OpenAiSdkImageResponseMetadata. * @param created the creation timestamp */ - protected OpenAiOfficialImageResponseMetadata(Long created) { + protected OpenAiSdkImageResponseMetadata(Long created) { this.created = created; } @@ -44,9 +44,9 @@ protected OpenAiOfficialImageResponseMetadata(Long created) { * @param imagesResponse the OpenAI images response * @return the metadata instance */ - public static OpenAiOfficialImageResponseMetadata from(ImagesResponse imagesResponse) { + public static OpenAiSdkImageResponseMetadata from(ImagesResponse imagesResponse) { Assert.notNull(imagesResponse, "imagesResponse must not be null"); - return new OpenAiOfficialImageResponseMetadata(imagesResponse.created()); + return new OpenAiSdkImageResponseMetadata(imagesResponse.created()); } @Override @@ -56,7 +56,7 @@ public Long getCreated() { @Override public String toString() { - return "OpenAiOfficialImageResponseMetadata{" + "created=" + created + '}'; + return "OpenAiSdkImageResponseMetadata{" + "created=" + created + '}'; } @Override @@ -64,7 +64,7 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (!(o instanceof OpenAiOfficialImageResponseMetadata that)) { + if (!(o instanceof OpenAiSdkImageResponseMetadata that)) { return false; } return Objects.equals(this.created, that.created); diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/package-info.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/package-info.java similarity index 87% rename from models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/package-info.java rename to models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/package-info.java index 2903ac2375d..96a728de702 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/metadata/package-info.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/package-info.java @@ -15,11 +15,11 @@ */ /** - * Metadata classes for OpenAI Official model responses. + * Metadata classes for OpenAI SDK model responses. *

* This package contains metadata implementations for chat, embedding, and image model * responses. * * @author Julien Dubois */ -package org.springframework.ai.openaiofficial.metadata; +package org.springframework.ai.openaisdk.metadata; diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/package-info.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/package-info.java similarity index 88% rename from models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/package-info.java rename to models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/package-info.java index 322b5a57ccf..9a3295a5f9d 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/package-info.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/package-info.java @@ -17,9 +17,9 @@ /** * Spring AI integration with the official OpenAI Java SDK. *

- * This package provides chat, embedding, and image model implementations using the - * official OpenAI Java client library. + * This package provides chat, embedding, and image model implementations using the OpenAI + * Java SDK client library. * * @author Julien Dubois */ -package org.springframework.ai.openaiofficial; +package org.springframework.ai.openaisdk; diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/AzureInternalOpenAiSdkHelper.java similarity index 93% rename from models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java rename to models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/AzureInternalOpenAiSdkHelper.java index 12a8c01e842..814250b22e7 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/AzureInternalOpenAiOfficialHelper.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/AzureInternalOpenAiSdkHelper.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial.setup; +package org.springframework.ai.openaisdk.setup; import com.azure.identity.AuthenticationUtil; import com.azure.identity.DefaultAzureCredentialBuilder; @@ -31,7 +31,7 @@ * * @author Julien Dubois */ -class AzureInternalOpenAiOfficialHelper { +class AzureInternalOpenAiSdkHelper { static Credential getAzureCredential() { return BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier( diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java similarity index 97% rename from models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java rename to models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java index 216a82dc695..95c9ac47f7e 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/OpenAiOfficialSetup.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial.setup; +package org.springframework.ai.openaisdk.setup; import com.openai.azure.AzureOpenAIServiceVersion; import com.openai.client.OpenAIClient; @@ -41,16 +41,16 @@ * * @author Julien Dubois */ -public class OpenAiOfficialSetup { +public class OpenAiSdkSetup { static final String OPENAI_URL = "https://api.openai.com/v1"; static final String OPENAI_API_KEY = "OPENAI_API_KEY"; static final String AZURE_OPENAI_KEY = "AZURE_OPENAI_KEY"; static final String GITHUB_MODELS_URL = "https://models.inference.ai.azure.com"; static final String GITHUB_TOKEN = "GITHUB_TOKEN"; - static final String DEFAULT_USER_AGENT = "spring-ai-openai-official"; + static final String DEFAULT_USER_AGENT = "spring-ai-openai-sdk"; - private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialSetup.class); + private static final Logger logger = LoggerFactory.getLogger(OpenAiSdkSetup.class); private static final Duration DEFAULT_DURATION = ofSeconds(60); @@ -245,7 +245,7 @@ else if (modelProvider == ModelProvider.AZURE_OPEN_AI) { static Credential azureAuthentication() { try { - return AzureInternalOpenAiOfficialHelper.getAzureCredential(); + return AzureInternalOpenAiSdkHelper.getAzureCredential(); } catch (NoClassDefFoundError e) { throw new IllegalArgumentException("Azure OpenAI was detected, but no credential was provided. " diff --git a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/package-info.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/package-info.java similarity index 87% rename from models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/package-info.java rename to models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/package-info.java index dd699ffee52..5c5744c5140 100644 --- a/models/spring-ai-openai-official/src/main/java/org/springframework/ai/openaiofficial/setup/package-info.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/package-info.java @@ -15,11 +15,11 @@ */ /** - * Setup and configuration utilities for OpenAI Official clients. + * Setup and configuration utilities for OpenAI Sdk clients. *

* This package contains helper classes for configuring and setting up OpenAI clients for * different environments including OpenAI, Azure OpenAI, and GitHub Models. * * @author Julien Dubois */ -package org.springframework.ai.openaiofficial.setup; +package org.springframework.ai.openaisdk.setup; diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfiguration.java similarity index 65% rename from models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java rename to models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfiguration.java index 203ab59d458..8e83f0b8949 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/OpenAiOfficialTestConfiguration.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfiguration.java @@ -14,32 +14,32 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial; +package org.springframework.ai.openaisdk; import org.springframework.boot.SpringBootConfiguration; import org.springframework.context.annotation.Bean; /** - * Context configuration for OpenAI official SDK tests. + * Context configuration for OpenAI Java SDK tests. * * @author Julien Dubois */ @SpringBootConfiguration -public class OpenAiOfficialTestConfiguration { +public class OpenAiSdkTestConfiguration { @Bean - public OpenAiOfficialEmbeddingModel openAiEmbeddingModel() { - return new OpenAiOfficialEmbeddingModel(); + public OpenAiSdkEmbeddingModel openAiEmbeddingModel() { + return new OpenAiSdkEmbeddingModel(); } @Bean - public OpenAiOfficialImageModel openAiImageModel() { - return new OpenAiOfficialImageModel(); + public OpenAiSdkImageModel openAiImageModel() { + return new OpenAiSdkImageModel(); } @Bean - public OpenAiOfficialChatModel openAiChatModel() { - return new OpenAiOfficialChatModel(); + public OpenAiSdkChatModel openAiChatModel() { + return new OpenAiSdkChatModel(); } } diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfigurationWithObservability.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfigurationWithObservability.java new file mode 100644 index 00000000000..aa00e96220b --- /dev/null +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfigurationWithObservability.java @@ -0,0 +1,59 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaisdk; + +import io.micrometer.observation.tck.TestObservationRegistry; +import org.springframework.ai.document.MetadataMode; +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.context.annotation.Bean; + +import static org.springframework.ai.openaisdk.OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL; +import static org.springframework.ai.openaisdk.OpenAiSdkEmbeddingOptions.DEFAULT_EMBEDDING_MODEL; +import static org.springframework.ai.openaisdk.OpenAiSdkImageOptions.DEFAULT_IMAGE_MODEL; + +/** + * Context configuration for OpenAI Java SDK tests. + * + * @author Julien Dubois + */ +@SpringBootConfiguration +public class OpenAiSdkTestConfigurationWithObservability { + + @Bean + public TestObservationRegistry testObservationRegistry() { + return TestObservationRegistry.create(); + } + + @Bean + public OpenAiSdkEmbeddingModel openAiEmbeddingModel(TestObservationRegistry observationRegistry) { + return new OpenAiSdkEmbeddingModel(MetadataMode.EMBED, + OpenAiSdkEmbeddingOptions.builder().model(DEFAULT_EMBEDDING_MODEL).build(), observationRegistry); + } + + @Bean + public OpenAiSdkImageModel openAiImageModel(TestObservationRegistry observationRegistry) { + return new OpenAiSdkImageModel(OpenAiSdkImageOptions.builder().model(DEFAULT_IMAGE_MODEL).build(), + observationRegistry); + } + + @Bean + public OpenAiSdkChatModel openAiChatModel(TestObservationRegistry observationRegistry) { + return new OpenAiSdkChatModel(OpenAiSdkChatOptions.builder().model(DEFAULT_CHAT_MODEL).build(), + observationRegistry); + } + +} diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/ActorsFilms.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/ActorsFilms.java similarity index 95% rename from models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/ActorsFilms.java rename to models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/ActorsFilms.java index 0271bb43af3..d3b2d28b8ae 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/ActorsFilms.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/ActorsFilms.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial.chat; +package org.springframework.ai.openaisdk.chat; import java.util.List; diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/MockWeatherService.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/MockWeatherService.java similarity index 98% rename from models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/MockWeatherService.java rename to models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/MockWeatherService.java index 50f8ab11067..ab76e796a5d 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/MockWeatherService.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/MockWeatherService.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial.chat; +package org.springframework.ai.openaisdk.chat; import com.fasterxml.jackson.annotation.JsonClassDescription; import com.fasterxml.jackson.annotation.JsonInclude; diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java similarity index 94% rename from models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java rename to models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java index d214f62125f..14b98569071 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial.chat; +package org.springframework.ai.openaisdk.chat; import com.openai.models.ReasoningEffort; import org.assertj.core.data.Percentage; @@ -46,9 +46,9 @@ import org.springframework.ai.model.tool.ToolCallingChatOptions; import org.springframework.ai.model.tool.ToolCallingManager; import org.springframework.ai.model.tool.ToolExecutionResult; -import org.springframework.ai.openaiofficial.OpenAiOfficialChatModel; -import org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions; -import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfiguration; +import org.springframework.ai.openaisdk.OpenAiSdkChatModel; +import org.springframework.ai.openaisdk.OpenAiSdkChatOptions; +import org.springframework.ai.openaisdk.OpenAiSdkTestConfiguration; import org.springframework.ai.support.ToolCallbacks; import org.springframework.ai.tool.annotation.Tool; import org.springframework.ai.tool.function.FunctionToolCallback; @@ -76,21 +76,21 @@ import static org.assertj.core.api.Assertions.assertThat; /** - * Integration tests for {@link OpenAiOfficialChatModel}. + * Integration tests for {@link OpenAiSdkChatModel}. * * @author Julien Dubois */ -@SpringBootTest(classes = OpenAiOfficialTestConfiguration.class) +@SpringBootTest(classes = OpenAiSdkTestConfiguration.class) @EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") -public class OpenAiOfficialChatModelIT { +public class OpenAiSdkChatModelIT { - private static final Logger logger = LoggerFactory.getLogger(OpenAiOfficialChatModelIT.class); + private static final Logger logger = LoggerFactory.getLogger(OpenAiSdkChatModelIT.class); @Value("classpath:/prompts/system-message.st") private Resource systemResource; @Autowired - private OpenAiOfficialChatModel chatModel; + private OpenAiSdkChatModel chatModel; @Test void roleTest() { @@ -222,7 +222,7 @@ void streamRoleTest() { @Test void streamingWithTokenUsage() { - var promptOptions = OpenAiOfficialChatOptions.builder() + var promptOptions = OpenAiSdkChatOptions.builder() .streamUsage(true) .reasoningEffort(ReasoningEffort.MINIMAL.toString()) .seed(1) @@ -371,7 +371,7 @@ void functionCallTest() { List messages = new ArrayList<>(List.of(userMessage)); - var promptOptions = OpenAiOfficialChatOptions.builder() + var promptOptions = OpenAiSdkChatOptions.builder() .toolCallbacks(List.of(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService()) .description("Get the weather in location") .inputType(MockWeatherService.Request.class) @@ -393,7 +393,7 @@ void streamFunctionCallTest() { List messages = new ArrayList<>(List.of(userMessage)); - var promptOptions = OpenAiOfficialChatOptions.builder() + var promptOptions = OpenAiSdkChatOptions.builder() .toolCallbacks(List.of(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService()) .description("Get the weather in location") .inputType(MockWeatherService.Request.class) @@ -426,7 +426,7 @@ void functionCallUsageTest() { List messages = new ArrayList<>(List.of(userMessage)); - var promptOptions = OpenAiOfficialChatOptions.builder() + var promptOptions = OpenAiSdkChatOptions.builder() .toolCallbacks(List.of(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService()) .description("Get the weather in location") .inputType(MockWeatherService.Request.class) @@ -454,7 +454,7 @@ void streamFunctionCallUsageTest() { List messages = new ArrayList<>(List.of(userMessage)); - var promptOptions = OpenAiOfficialChatOptions.builder() + var promptOptions = OpenAiSdkChatOptions.builder() .toolCallbacks(List.of(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService()) .description("Get the weather in location") .inputType(MockWeatherService.Request.class) @@ -485,8 +485,7 @@ void multiModalityEmbeddedImage() throws IOException { .media(List.of(new Media(MimeTypeUtils.IMAGE_PNG, imageData))) .build(); - var response = this.chatModel - .call(new Prompt(List.of(userMessage), OpenAiOfficialChatOptions.builder().build())); + var response = this.chatModel.call(new Prompt(List.of(userMessage), OpenAiSdkChatOptions.builder().build())); logger.info(response.getResult().getOutput().getText()); assertThat(response.getResult().getOutput().getText()).containsAnyOf("bananas", "apple", "bowl", "basket", @@ -505,7 +504,7 @@ void multiModalityImageUrl() throws IOException { .build(); ChatResponse response = this.chatModel - .call(new Prompt(List.of(userMessage), OpenAiOfficialChatOptions.builder().build())); + .call(new Prompt(List.of(userMessage), OpenAiSdkChatOptions.builder().build())); logger.info(response.getResult().getOutput().getText()); assertThat(response.getResult().getOutput().getText()).containsAnyOf("bananas", "apple", "bowl", "basket", @@ -524,7 +523,7 @@ void streamingMultiModalityImageUrl() throws IOException { .build(); Flux response = this.chatModel - .stream(new Prompt(List.of(userMessage), OpenAiOfficialChatOptions.builder().build())); + .stream(new Prompt(List.of(userMessage), OpenAiSdkChatOptions.builder().build())); String content = response.collectList() .block() @@ -540,10 +539,10 @@ void streamingMultiModalityImageUrl() throws IOException { @Test void validateCallResponseMetadata() { - String model = OpenAiOfficialChatOptions.DEFAULT_CHAT_MODEL; + String model = OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL; // @formatter:off ChatResponse response = ChatClient.create(this.chatModel).prompt() - .options(OpenAiOfficialChatOptions.builder().model(model).build()) + .options(OpenAiSdkChatOptions.builder().model(model).build()) .user("Tell me about 3 famous pirates from the Golden Age of Piracy and what they did") .call() .chatResponse(); @@ -559,7 +558,7 @@ void validateCallResponseMetadata() { @Test void validateStoreAndMetadata() { - OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder() .store(true) .metadata(Map.of("type", "dev")) .build(); diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java similarity index 85% rename from models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java rename to models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java index 5060d416c28..73bc31c35fe 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelObservationIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial.chat; +package org.springframework.ai.openaisdk.chat; import io.micrometer.observation.tck.TestObservationRegistry; import io.micrometer.observation.tck.TestObservationRegistryAssert; @@ -27,9 +27,9 @@ import org.springframework.ai.chat.prompt.Prompt; import org.springframework.ai.observation.conventions.AiOperationType; import org.springframework.ai.observation.conventions.AiProvider; -import org.springframework.ai.openaiofficial.OpenAiOfficialChatModel; -import org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions; -import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfigurationWithObservability; +import org.springframework.ai.openaisdk.OpenAiSdkChatModel; +import org.springframework.ai.openaisdk.OpenAiSdkChatOptions; +import org.springframework.ai.openaisdk.OpenAiSdkTestConfigurationWithObservability; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import reactor.core.publisher.Flux; @@ -40,22 +40,22 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.ai.chat.observation.ChatModelObservationDocumentation.HighCardinalityKeyNames; import static org.springframework.ai.chat.observation.ChatModelObservationDocumentation.LowCardinalityKeyNames; -import static org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions.DEFAULT_CHAT_MODEL; +import static org.springframework.ai.openaisdk.OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL; /** - * Integration tests for observation instrumentation in {@link OpenAiOfficialChatModel}. + * Integration tests for observation instrumentation in {@link OpenAiSdkChatModel}. * * @author Julien Dubois */ -@SpringBootTest(classes = OpenAiOfficialTestConfigurationWithObservability.class) +@SpringBootTest(classes = OpenAiSdkTestConfigurationWithObservability.class) @EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") -public class OpenAiOfficialChatModelObservationIT { +public class OpenAiSdkChatModelObservationIT { @Autowired TestObservationRegistry observationRegistry; @Autowired - private OpenAiOfficialChatModel chatModel; + private OpenAiSdkChatModel chatModel; @BeforeEach void setUp() { @@ -65,7 +65,7 @@ void setUp() { @Test void observationForChatOperation() throws InterruptedException { - var options = OpenAiOfficialChatOptions.builder().model(DEFAULT_CHAT_MODEL).build(); + var options = OpenAiSdkChatOptions.builder().model(DEFAULT_CHAT_MODEL).build(); Prompt prompt = new Prompt("Why does a raven look like a desk?", options); @@ -80,7 +80,7 @@ void observationForChatOperation() throws InterruptedException { @Test void observationForStreamingChatOperation() throws InterruptedException { - var options = OpenAiOfficialChatOptions.builder().model(DEFAULT_CHAT_MODEL).streamUsage(true).build(); + var options = OpenAiSdkChatOptions.builder().model(DEFAULT_CHAT_MODEL).streamUsage(true).build(); Prompt prompt = new Prompt("Why does a raven look like a desk?", options); @@ -113,8 +113,7 @@ private void validate(ChatResponseMetadata responseMetadata) throws InterruptedE .that() .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_OPERATION_TYPE.asString(), AiOperationType.CHAT.value()) - .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_PROVIDER.asString(), - AiProvider.OPENAI_OFFICIAL.value()) + .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_PROVIDER.asString(), AiProvider.OPENAI_SDK.value()) .hasLowCardinalityKeyValue(LowCardinalityKeyNames.REQUEST_MODEL.asString(), DEFAULT_CHAT_MODEL) .hasLowCardinalityKeyValue(LowCardinalityKeyNames.RESPONSE_MODEL.asString(), responseMetadata.getModel()) .hasHighCardinalityKeyValue(HighCardinalityKeyNames.RESPONSE_ID.asString(), responseMetadata.getId()) diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelResponseFormatIT.java similarity index 84% rename from models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java rename to models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelResponseFormatIT.java index 1a3ce76c7af..9b32fb16994 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatModelResponseFormatIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelResponseFormatIT.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial.chat; +package org.springframework.ai.openaisdk.chat; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonPropertyOrder; @@ -29,23 +29,23 @@ import org.springframework.ai.chat.model.ChatResponse; import org.springframework.ai.chat.prompt.Prompt; import org.springframework.ai.converter.BeanOutputConverter; -import org.springframework.ai.openaiofficial.OpenAiOfficialChatModel; -import org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions; -import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfiguration; +import org.springframework.ai.openaisdk.OpenAiSdkChatModel; +import org.springframework.ai.openaisdk.OpenAiSdkChatOptions; +import org.springframework.ai.openaisdk.OpenAiSdkTestConfiguration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions.DEFAULT_CHAT_MODEL; +import static org.springframework.ai.openaisdk.OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL; /** - * Integration tests for the response format in {@link OpenAiOfficialChatModel}. + * Integration tests for the response format in {@link OpenAiSdkChatModel}. * * @author Julien Dubois */ -@SpringBootTest(classes = OpenAiOfficialTestConfiguration.class) +@SpringBootTest(classes = OpenAiSdkTestConfiguration.class) @EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") -public class OpenAiOfficialChatModelResponseFormatIT { +public class OpenAiSdkChatModelResponseFormatIT { private static final ObjectMapper MAPPER = new ObjectMapper() .enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS); @@ -53,7 +53,7 @@ public class OpenAiOfficialChatModelResponseFormatIT { private final Logger logger = LoggerFactory.getLogger(getClass()); @Autowired - private OpenAiOfficialChatModel chatModel; + private OpenAiSdkChatModel chatModel; public static boolean isValidJson(String json) { try { @@ -69,9 +69,9 @@ public static boolean isValidJson(String json) { void jsonObject() { Prompt prompt = new Prompt("List 8 planets. Use JSON response", - OpenAiOfficialChatOptions.builder() - .responseFormat(OpenAiOfficialChatModel.ResponseFormat.builder() - .type(OpenAiOfficialChatModel.ResponseFormat.Type.JSON_OBJECT) + OpenAiSdkChatOptions.builder() + .responseFormat(OpenAiSdkChatModel.ResponseFormat.builder() + .type(OpenAiSdkChatModel.ResponseFormat.Type.JSON_OBJECT) .build()) .build()); @@ -113,10 +113,10 @@ void jsonSchema() { """; Prompt prompt = new Prompt("how can I solve 8x + 7 = -23", - OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions.builder() .model(DEFAULT_CHAT_MODEL) - .responseFormat(OpenAiOfficialChatModel.ResponseFormat.builder() - .type(OpenAiOfficialChatModel.ResponseFormat.Type.JSON_SCHEMA) + .responseFormat(OpenAiSdkChatModel.ResponseFormat.builder() + .type(OpenAiSdkChatModel.ResponseFormat.Type.JSON_SCHEMA) .jsonSchema(jsonSchema) .build()) .build()); @@ -159,9 +159,9 @@ void jsonSchemaThroughIndividualSetters() throws JsonProcessingException { """; Prompt prompt = new Prompt("how can I solve 8x + 7 = -23", - OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions.builder() .model(DEFAULT_CHAT_MODEL) - .responseFormat(OpenAiOfficialChatModel.ResponseFormat.builder().jsonSchema(jsonSchema).build()) + .responseFormat(OpenAiSdkChatModel.ResponseFormat.builder().jsonSchema(jsonSchema).build()) .build()); ChatResponse response = this.chatModel.call(prompt); @@ -240,9 +240,9 @@ record Items(@JsonProperty(required = true, value = "explanation") String explan assertThat(jsonSchema1).isEqualTo(expectedJsonSchema); Prompt prompt = new Prompt("how can I solve 8x + 7 = -23", - OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions.builder() .model(DEFAULT_CHAT_MODEL) - .responseFormat(OpenAiOfficialChatModel.ResponseFormat.builder().jsonSchema(jsonSchema1).build()) + .responseFormat(OpenAiSdkChatModel.ResponseFormat.builder().jsonSchema(jsonSchema1).build()) .build()); ChatResponse response = this.chatModel.call(prompt); diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatOptionsTests.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java similarity index 83% rename from models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatOptionsTests.java rename to models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java index 311361d314d..622b105a205 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/chat/OpenAiOfficialChatOptionsTests.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java @@ -14,11 +14,11 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial.chat; +package org.springframework.ai.openaisdk.chat; import com.openai.models.FunctionDefinition; import org.junit.jupiter.api.Test; -import org.springframework.ai.openaiofficial.OpenAiOfficialChatOptions; +import org.springframework.ai.openaisdk.OpenAiSdkChatOptions; import org.springframework.ai.tool.ToolCallback; import java.util.ArrayList; @@ -33,11 +33,11 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; /** - * Tests for {@link OpenAiOfficialChatOptions}. + * Tests for {@link OpenAiSdkChatOptions}. * * @author Julien Dubois */ -public class OpenAiOfficialChatOptionsTests { +public class OpenAiSdkChatOptionsTests { @Test void testBuilderWithAllFields() { @@ -51,7 +51,7 @@ void testBuilderWithAllFields() { Map toolContext = Map.of("keyA", "valueA"); Map httpHeaders = Map.of("header1", "value1"); - OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder() .model("test-model") .deploymentName("test-deployment") .frequencyPenalty(0.5) @@ -118,7 +118,7 @@ void testCopy() { List tools = new ArrayList<>(); Map metadata = Map.of("key1", "value1"); - OpenAiOfficialChatOptions originalOptions = OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions originalOptions = OpenAiSdkChatOptions.builder() .model("test-model") .deploymentName("test-deployment") .frequencyPenalty(0.5) @@ -145,7 +145,7 @@ void testCopy() { .httpHeaders(Map.of("header1", "value1")) .build(); - OpenAiOfficialChatOptions copiedOptions = originalOptions.copy(); + OpenAiSdkChatOptions copiedOptions = originalOptions.copy(); assertThat(copiedOptions).isNotSameAs(originalOptions).isEqualTo(originalOptions); // Verify collections are copied @@ -165,7 +165,7 @@ void testSetters() { List tools = new ArrayList<>(); Map metadata = Map.of("key2", "value2"); - OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + OpenAiSdkChatOptions options = new OpenAiSdkChatOptions(); options.setModel("test-model"); options.setDeploymentName("test-deployment"); options.setFrequencyPenalty(0.5); @@ -221,7 +221,7 @@ void testSetters() { @Test void testDefaultValues() { - OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + OpenAiSdkChatOptions options = new OpenAiSdkChatOptions(); assertThat(options.getModel()).isNull(); assertThat(options.getDeploymentName()).isNull(); @@ -261,19 +261,19 @@ void testDefaultValues() { @Test void testEqualsAndHashCode() { - OpenAiOfficialChatOptions options1 = OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions options1 = OpenAiSdkChatOptions.builder() .model("test-model") .temperature(0.7) .maxTokens(100) .build(); - OpenAiOfficialChatOptions options2 = OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions options2 = OpenAiSdkChatOptions.builder() .model("test-model") .temperature(0.7) .maxTokens(100) .build(); - OpenAiOfficialChatOptions options3 = OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions options3 = OpenAiSdkChatOptions.builder() .model("different-model") .temperature(0.7) .maxTokens(100) @@ -290,7 +290,7 @@ void testEqualsAndHashCode() { @Test void testBuilderWithNullValues() { - OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder() .temperature(null) .logitBias(null) .stop(null) @@ -310,13 +310,13 @@ void testBuilderWithNullValues() { @Test void testBuilderChaining() { - OpenAiOfficialChatOptions.Builder builder = OpenAiOfficialChatOptions.builder(); + OpenAiSdkChatOptions.Builder builder = OpenAiSdkChatOptions.builder(); - OpenAiOfficialChatOptions.Builder result = builder.model("test-model").temperature(0.7).maxTokens(100); + OpenAiSdkChatOptions.Builder result = builder.model("test-model").temperature(0.7).maxTokens(100); assertThat(result).isSameAs(builder); - OpenAiOfficialChatOptions options = result.build(); + OpenAiSdkChatOptions options = result.build(); assertThat(options.getModel()).isEqualTo("test-model"); assertThat(options.getTemperature()).isEqualTo(0.7); assertThat(options.getMaxTokens()).isEqualTo(100); @@ -324,7 +324,7 @@ void testBuilderChaining() { @Test void testNullAndEmptyCollections() { - OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + OpenAiSdkChatOptions options = new OpenAiSdkChatOptions(); // Test setting null collections options.setLogitBias(null); @@ -355,7 +355,7 @@ void testNullAndEmptyCollections() { @Test void testStopSequencesAlias() { - OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + OpenAiSdkChatOptions options = new OpenAiSdkChatOptions(); List stopSequences = List.of("stop1", "stop2"); // Setting stopSequences should also set stop @@ -372,12 +372,9 @@ void testStopSequencesAlias() { @Test void testCopyChangeIndependence() { - OpenAiOfficialChatOptions original = OpenAiOfficialChatOptions.builder() - .model("original-model") - .temperature(0.5) - .build(); + OpenAiSdkChatOptions original = OpenAiSdkChatOptions.builder().model("original-model").temperature(0.5).build(); - OpenAiOfficialChatOptions copied = original.copy(); + OpenAiSdkChatOptions copied = original.copy(); // Modify original original.setModel("modified-model"); @@ -392,10 +389,7 @@ void testCopyChangeIndependence() { void testMaxTokensIsDeprectaed() { // Test that setting maxCompletionTokens takes precedence over maxTokens in // builder - OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() - .maxCompletionTokens(100) - .maxTokens(50) - .build(); + OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder().maxCompletionTokens(100).maxTokens(50).build(); assertThat(options.getMaxTokens()).isNull(); assertThat(options.getMaxCompletionTokens()).isEqualTo(100); @@ -404,10 +398,7 @@ void testMaxTokensIsDeprectaed() { @Test void testMaxCompletionTokensMutualExclusivityValidation() { // Test that setting maxCompletionTokens clears maxTokens in builder - OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() - .maxTokens(50) - .maxCompletionTokens(100) - .build(); + OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder().maxTokens(50).maxCompletionTokens(100).build(); assertThat(options.getMaxTokens()).isNull(); assertThat(options.getMaxCompletionTokens()).isEqualTo(100); @@ -416,10 +407,7 @@ void testMaxCompletionTokensMutualExclusivityValidation() { @Test void testMaxTokensWithNullDoesNotClearMaxCompletionTokens() { // Test that setting maxTokens to null doesn't trigger validation - OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() - .maxCompletionTokens(100) - .maxTokens(null) - .build(); + OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder().maxCompletionTokens(100).maxTokens(null).build(); assertThat(options.getMaxTokens()).isNull(); assertThat(options.getMaxCompletionTokens()).isEqualTo(100); @@ -428,10 +416,7 @@ void testMaxTokensWithNullDoesNotClearMaxCompletionTokens() { @Test void testMaxCompletionTokensWithNullDoesNotClearMaxTokens() { // Test that setting maxCompletionTokens to null doesn't trigger validation - OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() - .maxTokens(50) - .maxCompletionTokens(null) - .build(); + OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder().maxTokens(50).maxCompletionTokens(null).build(); assertThat(options.getMaxTokens()).isEqualTo(50); assertThat(options.getMaxCompletionTokens()).isNull(); @@ -439,7 +424,7 @@ void testMaxCompletionTokensWithNullDoesNotClearMaxTokens() { @Test void testBuilderCanSetOnlyMaxTokens() { - OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder().maxTokens(100).build(); + OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder().maxTokens(100).build(); assertThat(options.getMaxTokens()).isEqualTo(100); assertThat(options.getMaxCompletionTokens()).isNull(); @@ -447,7 +432,7 @@ void testBuilderCanSetOnlyMaxTokens() { @Test void testBuilderCanSetOnlyMaxCompletionTokens() { - OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder().maxCompletionTokens(150).build(); + OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder().maxCompletionTokens(150).build(); assertThat(options.getMaxTokens()).isNull(); assertThat(options.getMaxCompletionTokens()).isEqualTo(150); @@ -456,7 +441,7 @@ void testBuilderCanSetOnlyMaxCompletionTokens() { @Test void testSettersMutualExclusivityNotEnforced() { // Test that direct setters do NOT enforce mutual exclusivity (only builder does) - OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + OpenAiSdkChatOptions options = new OpenAiSdkChatOptions(); options.setMaxTokens(50); options.setMaxCompletionTokens(100); @@ -499,7 +484,7 @@ public String call(String toolInput) { } }; - OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder() .toolCallbacks(callback1, callback2) .toolNames("tool1", "tool2") .build(); @@ -527,7 +512,7 @@ public String call(String toolInput) { }; List callbacks = List.of(callback); - OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder().toolCallbacks(callbacks).build(); + OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder().toolCallbacks(callbacks).build(); assertThat(options.getToolCallbacks()).hasSize(1).containsExactly(callback); } @@ -536,7 +521,7 @@ public String call(String toolInput) { void testToolNamesSet() { Set toolNames = new HashSet<>(Arrays.asList("tool1", "tool2", "tool3")); - OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder().toolNames(toolNames).build(); + OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder().toolNames(toolNames).build(); assertThat(options.getToolNames()).hasSize(3).containsExactlyInAnyOrder("tool1", "tool2", "tool3"); } @@ -544,7 +529,7 @@ void testToolNamesSet() { @Test @SuppressWarnings("DataFlowIssue") void testSetToolCallbacksValidation() { - OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + OpenAiSdkChatOptions options = new OpenAiSdkChatOptions(); // Test null validation assertThatThrownBy(() -> options.setToolCallbacks(null)).isInstanceOf(IllegalArgumentException.class) @@ -561,7 +546,7 @@ void testSetToolCallbacksValidation() { @Test @SuppressWarnings("DataFlowIssue") void testSetToolNamesValidation() { - OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + OpenAiSdkChatOptions options = new OpenAiSdkChatOptions(); // Test null validation assertThatThrownBy(() -> options.setToolNames(null)).isInstanceOf(IllegalArgumentException.class) @@ -589,18 +574,15 @@ void testSetToolNamesValidation() { @Test void testBuilderMerge() { - OpenAiOfficialChatOptions base = OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions base = OpenAiSdkChatOptions.builder() .model("base-model") .temperature(0.5) .maxTokens(100) .build(); - OpenAiOfficialChatOptions override = OpenAiOfficialChatOptions.builder() - .model("override-model") - .topP(0.9) - .build(); + OpenAiSdkChatOptions override = OpenAiSdkChatOptions.builder().model("override-model").topP(0.9).build(); - OpenAiOfficialChatOptions merged = OpenAiOfficialChatOptions.builder().from(base).merge(override).build(); + OpenAiSdkChatOptions merged = OpenAiSdkChatOptions.builder().from(base).merge(override).build(); // Model should be overridden assertThat(merged.getModel()).isEqualTo("override-model"); @@ -618,7 +600,7 @@ void testBuilderFrom() { List stop = List.of("stop"); Map metadata = Map.of("key", "value"); - OpenAiOfficialChatOptions source = OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions source = OpenAiSdkChatOptions.builder() .model("source-model") .temperature(0.7) .maxTokens(100) @@ -627,7 +609,7 @@ void testBuilderFrom() { .metadata(metadata) .build(); - OpenAiOfficialChatOptions copy = OpenAiOfficialChatOptions.builder().from(source).build(); + OpenAiSdkChatOptions copy = OpenAiSdkChatOptions.builder().from(source).build(); assertThat(copy.getModel()).isEqualTo("source-model"); assertThat(copy.getTemperature()).isEqualTo(0.7); @@ -641,15 +623,15 @@ void testBuilderFrom() { @Test void testMergeDoesNotOverrideWithNull() { - OpenAiOfficialChatOptions base = OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions base = OpenAiSdkChatOptions.builder() .model("base-model") .temperature(0.5) .maxTokens(100) .build(); - OpenAiOfficialChatOptions override = OpenAiOfficialChatOptions.builder().model(null).temperature(null).build(); + OpenAiSdkChatOptions override = OpenAiSdkChatOptions.builder().model(null).temperature(null).build(); - OpenAiOfficialChatOptions merged = OpenAiOfficialChatOptions.builder().from(base).merge(override).build(); + OpenAiSdkChatOptions merged = OpenAiSdkChatOptions.builder().from(base).merge(override).build(); // Null values should not override assertThat(merged.getModel()).isEqualTo("base-model"); @@ -675,15 +657,15 @@ public String call(String toolInput) { } }; - OpenAiOfficialChatOptions base = OpenAiOfficialChatOptions.builder() + OpenAiSdkChatOptions base = OpenAiSdkChatOptions.builder() .toolCallbacks(callback) .toolNames("tool1") .toolContext(Map.of("key", "value")) .build(); - OpenAiOfficialChatOptions override = new OpenAiOfficialChatOptions(); + OpenAiSdkChatOptions override = new OpenAiSdkChatOptions(); - OpenAiOfficialChatOptions merged = OpenAiOfficialChatOptions.builder().from(base).merge(override).build(); + OpenAiSdkChatOptions merged = OpenAiSdkChatOptions.builder().from(base).merge(override).build(); // Empty collections should not override assertThat(merged.getToolCallbacks()).hasSize(1); @@ -693,20 +675,17 @@ public String call(String toolInput) { @Test void testToString() { - OpenAiOfficialChatOptions options = OpenAiOfficialChatOptions.builder() - .model("test-model") - .temperature(0.7) - .build(); + OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder().model("test-model").temperature(0.7).build(); String toString = options.toString(); - assertThat(toString).contains("OpenAiOfficialChatOptions"); + assertThat(toString).contains("OpenAiSdkChatOptions"); assertThat(toString).contains("test-model"); assertThat(toString).contains("0.7"); } @Test void testTopKReturnsNull() { - OpenAiOfficialChatOptions options = new OpenAiOfficialChatOptions(); + OpenAiSdkChatOptions options = new OpenAiSdkChatOptions(); // TopK is not supported by OpenAI, should always return null assertThat(options.getTopK()).isNull(); } diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/OpenAiOfficialEmbeddingIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java similarity index 66% rename from models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/OpenAiOfficialEmbeddingIT.java rename to models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java index ee058bd293d..c68c824e47b 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/OpenAiOfficialEmbeddingIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial.embedding; +package org.springframework.ai.openaisdk.embedding; import com.openai.models.embeddings.EmbeddingModel; import org.junit.jupiter.api.Test; @@ -23,9 +23,9 @@ import org.springframework.ai.embedding.EmbeddingRequest; import org.springframework.ai.embedding.EmbeddingResponse; import org.springframework.ai.embedding.TokenCountBatchingStrategy; -import org.springframework.ai.openaiofficial.OpenAiOfficialEmbeddingModel; -import org.springframework.ai.openaiofficial.OpenAiOfficialEmbeddingOptions; -import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfiguration; +import org.springframework.ai.openaisdk.OpenAiSdkEmbeddingModel; +import org.springframework.ai.openaisdk.OpenAiSdkEmbeddingOptions; +import org.springframework.ai.openaisdk.OpenAiSdkTestConfiguration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.core.io.DefaultResourceLoader; @@ -38,59 +38,54 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; /** - * Integration tests for {@link OpenAiOfficialEmbeddingModel}. + * Integration tests for {@link OpenAiSdkEmbeddingModel}. * * @author Julien Dubois */ -@SpringBootTest(classes = OpenAiOfficialTestConfiguration.class) +@SpringBootTest(classes = OpenAiSdkTestConfiguration.class) @EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") -class OpenAiOfficialEmbeddingIT { +class OpenAiSdkEmbeddingIT { private final Resource resource = new DefaultResourceLoader().getResource("classpath:text_source.txt"); @Autowired - private OpenAiOfficialEmbeddingModel openAiOfficialEmbeddingModel; + private OpenAiSdkEmbeddingModel openAiSdkEmbeddingModel; @Test void defaultEmbedding() { - assertThat(this.openAiOfficialEmbeddingModel).isNotNull(); + assertThat(this.openAiSdkEmbeddingModel).isNotNull(); - EmbeddingResponse embeddingResponse = this.openAiOfficialEmbeddingModel - .embedForResponse(List.of("Hello World")); + EmbeddingResponse embeddingResponse = this.openAiSdkEmbeddingModel.embedForResponse(List.of("Hello World")); assertThat(embeddingResponse.getResults()).hasSize(1); assertThat(embeddingResponse.getResults().get(0)).isNotNull(); assertThat(embeddingResponse.getResults().get(0).getOutput()).hasSize(1536); assertThat(embeddingResponse.getMetadata().getUsage().getTotalTokens()).isEqualTo(2); assertThat(embeddingResponse.getMetadata().getUsage().getPromptTokens()).isEqualTo(2); - assertThat(this.openAiOfficialEmbeddingModel.dimensions()).isEqualTo(1536); + assertThat(this.openAiSdkEmbeddingModel.dimensions()).isEqualTo(1536); assertThat(embeddingResponse.getMetadata().getModel()) .isEqualTo(EmbeddingModel.TEXT_EMBEDDING_ADA_002.toString()); } @Test void embeddingBatchDocuments() throws Exception { - assertThat(this.openAiOfficialEmbeddingModel).isNotNull(); - List embeddings = this.openAiOfficialEmbeddingModel.embed( + assertThat(this.openAiSdkEmbeddingModel).isNotNull(); + List embeddings = this.openAiSdkEmbeddingModel.embed( List.of(new Document("Hello world"), new Document("Hello Spring"), new Document("Hello Spring AI!")), - OpenAiOfficialEmbeddingOptions.builder() - .model(EmbeddingModel.TEXT_EMBEDDING_ADA_002.toString()) - .build(), + OpenAiSdkEmbeddingOptions.builder().model(EmbeddingModel.TEXT_EMBEDDING_ADA_002.toString()).build(), new TokenCountBatchingStrategy()); assertThat(embeddings.size()).isEqualTo(3); - embeddings.forEach( - embedding -> assertThat(embedding.length).isEqualTo(this.openAiOfficialEmbeddingModel.dimensions())); + embeddings + .forEach(embedding -> assertThat(embedding.length).isEqualTo(this.openAiSdkEmbeddingModel.dimensions())); } @Test void embeddingBatchDocumentsThatExceedTheLimit() throws Exception { - assertThat(this.openAiOfficialEmbeddingModel).isNotNull(); + assertThat(this.openAiSdkEmbeddingModel).isNotNull(); String contentAsString = this.resource.getContentAsString(StandardCharsets.UTF_8); - assertThatThrownBy(() -> this.openAiOfficialEmbeddingModel.embed( + assertThatThrownBy(() -> this.openAiSdkEmbeddingModel.embed( List.of(new Document("Hello World"), new Document(contentAsString)), - OpenAiOfficialEmbeddingOptions.builder() - .model(EmbeddingModel.TEXT_EMBEDDING_ADA_002.toString()) - .build(), + OpenAiSdkEmbeddingOptions.builder().model(EmbeddingModel.TEXT_EMBEDDING_ADA_002.toString()).build(), new TokenCountBatchingStrategy())) .isInstanceOf(IllegalArgumentException.class); } @@ -98,11 +93,9 @@ void embeddingBatchDocumentsThatExceedTheLimit() throws Exception { @Test void embedding3Large() { - EmbeddingResponse embeddingResponse = this.openAiOfficialEmbeddingModel - .call(new EmbeddingRequest(List.of("Hello World"), - OpenAiOfficialEmbeddingOptions.builder() - .model(EmbeddingModel.TEXT_EMBEDDING_3_LARGE.toString()) - .build())); + EmbeddingResponse embeddingResponse = this.openAiSdkEmbeddingModel.call(new EmbeddingRequest( + List.of("Hello World"), + OpenAiSdkEmbeddingOptions.builder().model(EmbeddingModel.TEXT_EMBEDDING_3_LARGE.toString()).build())); assertThat(embeddingResponse.getResults()).hasSize(1); assertThat(embeddingResponse.getResults().get(0)).isNotNull(); assertThat(embeddingResponse.getResults().get(0).getOutput()).hasSize(3072); @@ -115,11 +108,9 @@ void embedding3Large() { @Test void textEmbeddingAda002() { - EmbeddingResponse embeddingResponse = this.openAiOfficialEmbeddingModel - .call(new EmbeddingRequest(List.of("Hello World"), - OpenAiOfficialEmbeddingOptions.builder() - .model(EmbeddingModel.TEXT_EMBEDDING_3_SMALL.toString()) - .build())); + EmbeddingResponse embeddingResponse = this.openAiSdkEmbeddingModel.call(new EmbeddingRequest( + List.of("Hello World"), + OpenAiSdkEmbeddingOptions.builder().model(EmbeddingModel.TEXT_EMBEDDING_3_SMALL.toString()).build())); assertThat(embeddingResponse.getResults()).hasSize(1); assertThat(embeddingResponse.getResults().get(0)).isNotNull(); assertThat(embeddingResponse.getResults().get(0).getOutput()).hasSize(1536); diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/OpenAiOfficialEmbeddingModelObservationIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java similarity index 84% rename from models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/OpenAiOfficialEmbeddingModelObservationIT.java rename to models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java index a1a7e054b39..969d3200dad 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/embedding/OpenAiOfficialEmbeddingModelObservationIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial.embedding; +package org.springframework.ai.openaisdk.embedding; import io.micrometer.observation.tck.TestObservationRegistry; import io.micrometer.observation.tck.TestObservationRegistryAssert; @@ -27,9 +27,9 @@ import org.springframework.ai.embedding.observation.DefaultEmbeddingModelObservationConvention; import org.springframework.ai.observation.conventions.AiOperationType; import org.springframework.ai.observation.conventions.AiProvider; -import org.springframework.ai.openaiofficial.OpenAiOfficialEmbeddingModel; -import org.springframework.ai.openaiofficial.OpenAiOfficialEmbeddingOptions; -import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfigurationWithObservability; +import org.springframework.ai.openaisdk.OpenAiSdkEmbeddingModel; +import org.springframework.ai.openaisdk.OpenAiSdkEmbeddingOptions; +import org.springframework.ai.openaisdk.OpenAiSdkTestConfigurationWithObservability; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -41,20 +41,19 @@ import static org.springframework.ai.embedding.observation.EmbeddingModelObservationDocumentation.LowCardinalityKeyNames; /** - * Integration tests for observation instrumentation in - * {@link OpenAiOfficialEmbeddingModel}. + * Integration tests for observation instrumentation in {@link OpenAiSdkEmbeddingModel}. * * @author Julien Dubois */ -@SpringBootTest(classes = OpenAiOfficialTestConfigurationWithObservability.class) +@SpringBootTest(classes = OpenAiSdkTestConfigurationWithObservability.class) @EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") -public class OpenAiOfficialEmbeddingModelObservationIT { +public class OpenAiSdkEmbeddingModelObservationIT { @Autowired TestObservationRegistry observationRegistry; @Autowired - OpenAiOfficialEmbeddingModel embeddingModel; + OpenAiSdkEmbeddingModel embeddingModel; @BeforeEach void setUp() { @@ -63,7 +62,7 @@ void setUp() { @Test void observationForEmbeddingOperation() { - var options = OpenAiOfficialEmbeddingOptions.builder() + var options = OpenAiSdkEmbeddingOptions.builder() .model(TEXT_EMBEDDING_3_SMALL.toString()) .dimensions(1536) .build(); @@ -83,8 +82,7 @@ void observationForEmbeddingOperation() { .hasContextualNameEqualTo("embedding " + TEXT_EMBEDDING_3_SMALL) .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_OPERATION_TYPE.asString(), AiOperationType.EMBEDDING.value()) - .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_PROVIDER.asString(), - AiProvider.OPENAI_OFFICIAL.value()) + .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_PROVIDER.asString(), AiProvider.OPENAI_SDK.value()) .hasLowCardinalityKeyValue(LowCardinalityKeyNames.REQUEST_MODEL.asString(), TEXT_EMBEDDING_3_SMALL.toString()) .hasLowCardinalityKeyValue(LowCardinalityKeyNames.RESPONSE_MODEL.asString(), responseMetadata.getModel()) diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelIT.java similarity index 71% rename from models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelIT.java rename to models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelIT.java index 6523a29c82a..c5482e9eb74 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelIT.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial.image; +package org.springframework.ai.openaisdk.image; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; @@ -26,27 +26,27 @@ import org.springframework.ai.image.ImagePrompt; import org.springframework.ai.image.ImageResponse; import org.springframework.ai.image.ImageResponseMetadata; -import org.springframework.ai.openaiofficial.OpenAiOfficialImageModel; -import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfiguration; -import org.springframework.ai.openaiofficial.metadata.OpenAiOfficialImageGenerationMetadata; +import org.springframework.ai.openaisdk.OpenAiSdkImageModel; +import org.springframework.ai.openaisdk.OpenAiSdkTestConfiguration; +import org.springframework.ai.openaisdk.metadata.OpenAiSdkImageGenerationMetadata; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import static org.assertj.core.api.Assertions.assertThat; /** - * Integration tests for {@link OpenAiOfficialImageModel}. + * Integration tests for {@link OpenAiSdkImageModel}. * * @author Julien Dubois */ -@SpringBootTest(classes = OpenAiOfficialTestConfiguration.class) +@SpringBootTest(classes = OpenAiSdkTestConfiguration.class) @EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") -public class OpenAiOfficialImageModelIT { +public class OpenAiSdkImageModelIT { - private final Logger logger = LoggerFactory.getLogger(OpenAiOfficialImageModelIT.class); + private final Logger logger = LoggerFactory.getLogger(OpenAiSdkImageModelIT.class); @Autowired - private OpenAiOfficialImageModel imageModel; + private OpenAiSdkImageModel imageModel; @Test void imageAsUrlTest() { @@ -72,12 +72,12 @@ void imageAsUrlTest() { assertThat(image.getB64Json()).isNull(); var imageGenerationMetadata = generation.getMetadata(); - Assertions.assertThat(imageGenerationMetadata).isInstanceOf(OpenAiOfficialImageGenerationMetadata.class); + Assertions.assertThat(imageGenerationMetadata).isInstanceOf(OpenAiSdkImageGenerationMetadata.class); - OpenAiOfficialImageGenerationMetadata openAiOfficialImageGenerationMetadata = (OpenAiOfficialImageGenerationMetadata) imageGenerationMetadata; + OpenAiSdkImageGenerationMetadata openAiSdkImageGenerationMetadata = (OpenAiSdkImageGenerationMetadata) imageGenerationMetadata; - assertThat(openAiOfficialImageGenerationMetadata).isNotNull(); - assertThat(openAiOfficialImageGenerationMetadata.getRevisedPrompt()).isNotBlank(); + assertThat(openAiSdkImageGenerationMetadata).isNotNull(); + assertThat(openAiSdkImageGenerationMetadata.getRevisedPrompt()).isNotBlank(); } diff --git a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelObservationIT.java similarity index 84% rename from models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java rename to models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelObservationIT.java index 1fbfeabbff5..6312f375061 100644 --- a/models/spring-ai-openai-official/src/test/java/org/springframework/ai/openaiofficial/image/OpenAiOfficialImageModelObservationIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelObservationIT.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.ai.openaiofficial.image; +package org.springframework.ai.openaisdk.image; import io.micrometer.observation.tck.TestObservationRegistry; import io.micrometer.observation.tck.TestObservationRegistryAssert; @@ -26,9 +26,9 @@ import org.springframework.ai.image.observation.DefaultImageModelObservationConvention; import org.springframework.ai.observation.conventions.AiOperationType; import org.springframework.ai.observation.conventions.AiProvider; -import org.springframework.ai.openaiofficial.OpenAiOfficialImageModel; -import org.springframework.ai.openaiofficial.OpenAiOfficialImageOptions; -import org.springframework.ai.openaiofficial.OpenAiOfficialTestConfigurationWithObservability; +import org.springframework.ai.openaisdk.OpenAiSdkImageModel; +import org.springframework.ai.openaisdk.OpenAiSdkImageOptions; +import org.springframework.ai.openaisdk.OpenAiSdkTestConfigurationWithObservability; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -38,19 +38,19 @@ import static org.springframework.ai.image.observation.ImageModelObservationDocumentation.LowCardinalityKeyNames; /** - * Integration tests for observation instrumentation in {@link OpenAiOfficialImageModel}. + * Integration tests for observation instrumentation in {@link OpenAiSdkImageModel}. * * @author Julien Dubois */ -@SpringBootTest(classes = OpenAiOfficialTestConfigurationWithObservability.class) +@SpringBootTest(classes = OpenAiSdkTestConfigurationWithObservability.class) @EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") -public class OpenAiOfficialImageModelObservationIT { +public class OpenAiSdkImageModelObservationIT { @Autowired TestObservationRegistry observationRegistry; @Autowired - private OpenAiOfficialImageModel imageModel; + private OpenAiSdkImageModel imageModel; @BeforeEach void setUp() { @@ -59,7 +59,7 @@ void setUp() { @Test void observationForImageOperation() throws InterruptedException { - var options = OpenAiOfficialImageOptions.builder() + var options = OpenAiSdkImageOptions.builder() .model(DALL_E_3.asString()) .height(1024) .width(1024) @@ -85,8 +85,7 @@ void observationForImageOperation() throws InterruptedException { .hasContextualNameEqualTo("image " + DALL_E_3.asString()) .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_OPERATION_TYPE.asString(), AiOperationType.IMAGE.value()) - .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_PROVIDER.asString(), - AiProvider.OPENAI_OFFICIAL.value()) + .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_PROVIDER.asString(), AiProvider.OPENAI_SDK.value()) .hasLowCardinalityKeyValue(LowCardinalityKeyNames.REQUEST_MODEL.asString(), DALL_E_3.asString()) .hasHighCardinalityKeyValue(HighCardinalityKeyNames.REQUEST_IMAGE_SIZE.asString(), "1024x1024") .hasHighCardinalityKeyValue(HighCardinalityKeyNames.REQUEST_IMAGE_RESPONSE_FORMAT.asString(), "url") diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java new file mode 100644 index 00000000000..f2057d6931d --- /dev/null +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java @@ -0,0 +1,84 @@ +package org.springframework.ai.openaisdk.setup; + +import com.openai.client.OpenAIClient; +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.util.Collections; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class OpenAiSdkSetupTests { + + @Test + void detectModelProvider_returnsAzureOpenAI_whenAzureFlagIsTrue() { + OpenAiSdkSetup.ModelProvider result = OpenAiSdkSetup.detectModelProvider(true, false, null, null, null); + + assertEquals(OpenAiSdkSetup.ModelProvider.AZURE_OPEN_AI, result); + } + + @Test + void detectModelProvider_returnsGitHubModels_whenGitHubFlagIsTrue() { + OpenAiSdkSetup.ModelProvider result = OpenAiSdkSetup.detectModelProvider(false, true, null, null, null); + + assertEquals(OpenAiSdkSetup.ModelProvider.GITHUB_MODELS, result); + } + + @Test + void detectModelProvider_returnsAzureOpenAI_whenBaseUrlMatchesAzure() { + OpenAiSdkSetup.ModelProvider result = OpenAiSdkSetup.detectModelProvider(false, false, + "https://example.openai.azure.com", null, null); + + assertEquals(OpenAiSdkSetup.ModelProvider.AZURE_OPEN_AI, result); + } + + @Test + void detectModelProvider_returnsGitHubModels_whenBaseUrlMatchesGitHub() { + OpenAiSdkSetup.ModelProvider result = OpenAiSdkSetup.detectModelProvider(false, false, + "https://models.inference.ai.azure.com", null, null); + + assertEquals(OpenAiSdkSetup.ModelProvider.GITHUB_MODELS, result); + } + + @Test + void detectModelProvider_returnsOpenAI_whenNoConditionsMatch() { + OpenAiSdkSetup.ModelProvider result = OpenAiSdkSetup.detectModelProvider(false, false, null, null, null); + + assertEquals(OpenAiSdkSetup.ModelProvider.OPEN_AI, result); + } + + @Test + void setupSyncClient_returnsClient_whenValidApiKeyProvided() { + OpenAIClient client = OpenAiSdkSetup.setupSyncClient(null, "valid-api-key", null, null, null, null, false, + false, null, Duration.ofSeconds(30), 2, null, null); + + assertNotNull(client); + } + + @Test + void setupSyncClient_appliesCustomHeaders_whenProvided() { + Map customHeaders = Collections.singletonMap("X-Custom-Header", "value"); + + OpenAIClient client = OpenAiSdkSetup.setupSyncClient(null, "valid-api-key", null, null, null, null, false, + false, null, Duration.ofSeconds(30), 2, null, customHeaders); + + assertNotNull(client); + } + + @Test + void calculateBaseUrl_returnsDefaultOpenAIUrl_whenBaseUrlIsNull() { + String result = OpenAiSdkSetup.calculateBaseUrl(null, OpenAiSdkSetup.ModelProvider.OPEN_AI, null, null); + + assertEquals(OpenAiSdkSetup.OPENAI_URL, result); + } + + @Test + void calculateBaseUrl_returnsGitHubUrl_whenModelHostIsGitHub() { + String result = OpenAiSdkSetup.calculateBaseUrl(null, OpenAiSdkSetup.ModelProvider.GITHUB_MODELS, null, null); + + assertEquals(OpenAiSdkSetup.GITHUB_MODELS_URL, result); + } + +} diff --git a/models/spring-ai-openai-official/src/test/resources/prompts/system-message.st b/models/spring-ai-openai-sdk/src/test/resources/prompts/system-message.st similarity index 100% rename from models/spring-ai-openai-official/src/test/resources/prompts/system-message.st rename to models/spring-ai-openai-sdk/src/test/resources/prompts/system-message.st diff --git a/models/spring-ai-openai-official/src/test/resources/test.png b/models/spring-ai-openai-sdk/src/test/resources/test.png similarity index 100% rename from models/spring-ai-openai-official/src/test/resources/test.png rename to models/spring-ai-openai-sdk/src/test/resources/test.png diff --git a/models/spring-ai-openai-official/src/test/resources/text_source.txt b/models/spring-ai-openai-sdk/src/test/resources/text_source.txt similarity index 100% rename from models/spring-ai-openai-official/src/test/resources/text_source.txt rename to models/spring-ai-openai-sdk/src/test/resources/text_source.txt diff --git a/models/spring-ai-openai-official/src/test/script/deploy-azure-openai-models.sh b/models/spring-ai-openai-sdk/src/test/script/deploy-azure-openai-models.sh similarity index 98% rename from models/spring-ai-openai-official/src/test/script/deploy-azure-openai-models.sh rename to models/spring-ai-openai-sdk/src/test/script/deploy-azure-openai-models.sh index 3bd74b4710d..a402a097e3a 100755 --- a/models/spring-ai-openai-official/src/test/script/deploy-azure-openai-models.sh +++ b/models/spring-ai-openai-sdk/src/test/script/deploy-azure-openai-models.sh @@ -16,7 +16,7 @@ echo "Setting up environment variables..." echo "----------------------------------" -PROJECT="spring-ai-open-ai-official-$RANDOM-$RANDOM-$RANDOM" +PROJECT="spring-ai-open-ai-sdk-$RANDOM-$RANDOM-$RANDOM" RESOURCE_GROUP="rg-$PROJECT" LOCATION="eastus" AI_SERVICE="ai-$PROJECT" diff --git a/pom.xml b/pom.xml index 4d90bbcabcf..fadca598645 100644 --- a/pom.xml +++ b/pom.xml @@ -180,7 +180,7 @@ models/spring-ai-oci-genai models/spring-ai-ollama models/spring-ai-openai - models/spring-ai-openai-official + models/spring-ai-openai-sdk models/spring-ai-postgresml models/spring-ai-stability-ai models/spring-ai-transformers @@ -278,7 +278,7 @@ 4.0.0 4.3.4 1.0.0-beta.16 - 4.8.0 + 4.8.0 1.15.4 1.1.0 2.2.21 @@ -839,7 +839,7 @@ org.springframework.ai.mistralai/**/*IT.java org.springframework.ai.oci/**/*IT.java org.springframework.ai.ollama/**/*IT.java - org.springframework.ai.openaiofficial/**/*IT.java + org.springframework.ai.openaisdk/**/*IT.java org.springframework.ai.postgresml/**/*IT.java org.springframework.ai.stabilityai/**/*IT.java org.springframework.ai.transformers/**/*IT.java diff --git a/spring-ai-commons/src/main/java/org/springframework/ai/observation/conventions/AiProvider.java b/spring-ai-commons/src/main/java/org/springframework/ai/observation/conventions/AiProvider.java index d78522580dc..6a585339a7f 100644 --- a/spring-ai-commons/src/main/java/org/springframework/ai/observation/conventions/AiProvider.java +++ b/spring-ai-commons/src/main/java/org/springframework/ai/observation/conventions/AiProvider.java @@ -88,7 +88,7 @@ public enum AiProvider { /** * AI system provided by the official OpenAI SDK. */ - OPENAI_OFFICIAL("openai_official"), + OPENAI_SDK("openai_sdk"), /** * AI system provided by Spring AI. diff --git a/src/checkstyle/checkstyle-suppressions.xml b/src/checkstyle/checkstyle-suppressions.xml index 112ef964c87..38a8d472778 100644 --- a/src/checkstyle/checkstyle-suppressions.xml +++ b/src/checkstyle/checkstyle-suppressions.xml @@ -41,7 +41,7 @@ - + From 94d13f05f3ce32f36f6e1ae6cae940f086c23531 Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Fri, 21 Nov 2025 12:56:07 +0100 Subject: [PATCH 28/49] refactor(openai-sdk): fix checkstyle issues add docs - Standardize copyright years to 2025-2025 - Reorganize imports with Java standard library before third-party - Add 'this.' prefix to instance variables for clarity - Add braces to single-line if statements - Make utility classes final with private constructors - Clean up static imports using fully qualified names - Improve equals/hashCode/toString method consistency - Add initial OpenAI SDK documentation Signed-off-by: Christian Tzolov --- .../openaisdk/AbstractOpenAiSdkOptions.java | 40 +- .../ai/openaisdk/OpenAiSdkChatModel.java | 104 +-- .../ai/openaisdk/OpenAiSdkChatOptions.java | 108 +-- .../ai/openaisdk/OpenAiSdkEmbeddingModel.java | 25 +- .../openaisdk/OpenAiSdkEmbeddingOptions.java | 16 +- .../ai/openaisdk/OpenAiSdkImageModel.java | 23 +- .../ai/openaisdk/OpenAiSdkImageOptions.java | 28 +- .../OpenAiSdkImageGenerationMetadata.java | 8 +- .../OpenAiSdkImageResponseMetadata.java | 9 +- .../ai/openaisdk/metadata/package-info.java | 2 +- .../ai/openaisdk/package-info.java | 2 +- .../setup/AzureInternalOpenAiSdkHelper.java | 7 +- .../ai/openaisdk/setup/OpenAiSdkSetup.java | 23 +- .../ai/openaisdk/setup/package-info.java | 2 +- .../openaisdk/OpenAiSdkTestConfiguration.java | 2 +- ...SdkTestConfigurationWithObservability.java | 16 +- .../ai/openaisdk/chat/ActorsFilms.java | 2 +- .../ai/openaisdk/chat/MockWeatherService.java | 6 +- .../openaisdk/chat/OpenAiSdkChatModelIT.java | 29 +- .../chat/OpenAiSdkChatModelObservationIT.java | 26 +- .../OpenAiSdkChatModelResponseFormatIT.java | 10 +- .../chat/OpenAiSdkChatOptionsTests.java | 13 +- .../embedding/OpenAiSdkEmbeddingIT.java | 9 +- .../OpenAiSdkEmbeddingModelObservationIT.java | 19 +- .../image/OpenAiSdkImageModelIT.java | 3 +- .../OpenAiSdkImageModelObservationIT.java | 30 +- .../openaisdk/setup/OpenAiSdkSetupTests.java | 22 +- .../src/main/antora/modules/ROOT/nav.adoc | 1 + .../ROOT/pages/api/chat/comparison.adoc | 2 + .../ROOT/pages/api/chat/openai-sdk-chat.adoc | 613 ++++++++++++++++++ 30 files changed, 948 insertions(+), 252 deletions(-) create mode 100644 spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java index 8ba8c8baaed..e1d08e79302 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,13 +16,13 @@ package org.springframework.ai.openaisdk; -import com.openai.azure.AzureOpenAIServiceVersion; -import com.openai.credential.Credential; - import java.net.Proxy; import java.time.Duration; import java.util.Map; +import com.openai.azure.AzureOpenAIServiceVersion; +import com.openai.credential.Credential; + public class AbstractOpenAiSdkOptions { /** @@ -94,7 +94,7 @@ public class AbstractOpenAiSdkOptions { private Map customHeaders; public String getBaseUrl() { - return baseUrl; + return this.baseUrl; } public void setBaseUrl(String baseUrl) { @@ -102,7 +102,7 @@ public void setBaseUrl(String baseUrl) { } public String getApiKey() { - return apiKey; + return this.apiKey; } public void setApiKey(String apiKey) { @@ -110,7 +110,7 @@ public void setApiKey(String apiKey) { } public Credential getCredential() { - return credential; + return this.credential; } public void setCredential(Credential credential) { @@ -118,7 +118,7 @@ public void setCredential(Credential credential) { } public String getModel() { - return model; + return this.model; } public void setModel(String model) { @@ -126,7 +126,7 @@ public void setModel(String model) { } public String getAzureDeploymentName() { - return azureDeploymentName; + return this.azureDeploymentName; } public void setAzureDeploymentName(String azureDeploymentName) { @@ -137,7 +137,7 @@ public void setAzureDeploymentName(String azureDeploymentName) { * Alias for getAzureDeploymentName() */ public String getDeploymentName() { - return azureDeploymentName; + return this.azureDeploymentName; } /** @@ -148,7 +148,7 @@ public void setDeploymentName(String azureDeploymentName) { } public AzureOpenAIServiceVersion getAzureOpenAIServiceVersion() { - return azureOpenAIServiceVersion; + return this.azureOpenAIServiceVersion; } public void setAzureOpenAIServiceVersion(AzureOpenAIServiceVersion azureOpenAIServiceVersion) { @@ -156,7 +156,7 @@ public void setAzureOpenAIServiceVersion(AzureOpenAIServiceVersion azureOpenAISe } public String getOrganizationId() { - return organizationId; + return this.organizationId; } public void setOrganizationId(String organizationId) { @@ -164,23 +164,23 @@ public void setOrganizationId(String organizationId) { } public boolean isAzure() { - return isAzure; + return this.isAzure; } public void setAzure(boolean azure) { - isAzure = azure; + this.isAzure = azure; } public boolean isGitHubModels() { - return isGitHubModels; + return this.isGitHubModels; } public void setGitHubModels(boolean gitHubModels) { - isGitHubModels = gitHubModels; + this.isGitHubModels = gitHubModels; } public Duration getTimeout() { - return timeout; + return this.timeout; } public void setTimeout(Duration timeout) { @@ -188,7 +188,7 @@ public void setTimeout(Duration timeout) { } public Integer getMaxRetries() { - return maxRetries; + return this.maxRetries; } public void setMaxRetries(Integer maxRetries) { @@ -196,7 +196,7 @@ public void setMaxRetries(Integer maxRetries) { } public Proxy getProxy() { - return proxy; + return this.proxy; } public void setProxy(Proxy proxy) { @@ -204,7 +204,7 @@ public void setProxy(Proxy proxy) { } public Map getCustomHeaders() { - return customHeaders; + return this.customHeaders; } public void setCustomHeaders(Map customHeaders) { diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java index 1f005aec820..bab050885fe 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,16 @@ package org.springframework.ai.openaisdk; +import java.util.ArrayList; +import java.util.Base64; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + import com.openai.client.OpenAIClient; import com.openai.client.OpenAIClientAsync; import com.openai.core.JsonValue; @@ -25,13 +35,32 @@ import com.openai.models.ResponseFormatJsonObject; import com.openai.models.ResponseFormatJsonSchema; import com.openai.models.ResponseFormatText; -import com.openai.models.chat.completions.*; +import com.openai.models.chat.completions.ChatCompletion; +import com.openai.models.chat.completions.ChatCompletionAssistantMessageParam; +import com.openai.models.chat.completions.ChatCompletionChunk; +import com.openai.models.chat.completions.ChatCompletionContentPart; +import com.openai.models.chat.completions.ChatCompletionContentPartImage; +import com.openai.models.chat.completions.ChatCompletionContentPartInputAudio; +import com.openai.models.chat.completions.ChatCompletionContentPartText; +import com.openai.models.chat.completions.ChatCompletionCreateParams; +import com.openai.models.chat.completions.ChatCompletionFunctionTool; +import com.openai.models.chat.completions.ChatCompletionMessage; +import com.openai.models.chat.completions.ChatCompletionMessageFunctionToolCall; +import com.openai.models.chat.completions.ChatCompletionMessageParam; +import com.openai.models.chat.completions.ChatCompletionMessageToolCall; +import com.openai.models.chat.completions.ChatCompletionStreamOptions; +import com.openai.models.chat.completions.ChatCompletionTool; +import com.openai.models.chat.completions.ChatCompletionToolMessageParam; +import com.openai.models.chat.completions.ChatCompletionUserMessageParam; import com.openai.models.completions.CompletionUsage; import io.micrometer.observation.Observation; import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import reactor.core.publisher.Flux; +import reactor.core.scheduler.Schedulers; + import org.springframework.ai.chat.messages.AssistantMessage; import org.springframework.ai.chat.messages.MessageType; import org.springframework.ai.chat.messages.ToolResponseMessage; @@ -58,26 +87,12 @@ import org.springframework.ai.model.tool.ToolExecutionResult; import org.springframework.ai.model.tool.internal.ToolCallReactiveContextHolder; import org.springframework.ai.observation.conventions.AiProvider; +import org.springframework.ai.openaisdk.setup.OpenAiSdkSetup; import org.springframework.ai.support.UsageCalculator; import org.springframework.ai.tool.definition.ToolDefinition; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; -import reactor.core.publisher.Flux; -import reactor.core.scheduler.Schedulers; - -import java.util.ArrayList; -import java.util.Base64; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; -import java.util.stream.Collectors; - -import static org.springframework.ai.openaisdk.setup.OpenAiSdkSetup.setupAsyncClient; -import static org.springframework.ai.openaisdk.setup.OpenAiSdkSetup.setupSyncClient; /** * Chat Model implementation using the OpenAI Java SDK. @@ -199,14 +214,15 @@ public OpenAiSdkChatModel(OpenAIClient openAiClient, OpenAIClientAsync openAiCli this.options = options; } this.openAiClient = Objects.requireNonNullElseGet(openAiClient, - () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), - this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), - this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), - this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), - this.options.getProxy(), this.options.getCustomHeaders())); + () -> OpenAiSdkSetup.setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), + this.options.getCredential(), this.options.getAzureDeploymentName(), + this.options.getAzureOpenAIServiceVersion(), this.options.getOrganizationId(), + this.options.isAzure(), this.options.isGitHubModels(), this.options.getModel(), + this.options.getTimeout(), this.options.getMaxRetries(), this.options.getProxy(), + this.options.getCustomHeaders())); this.openAiClientAsync = Objects.requireNonNullElseGet(openAiClientAsync, - () -> setupAsyncClient(this.options.getBaseUrl(), this.options.getApiKey(), + () -> OpenAiSdkSetup.setupAsyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), this.options.getModel(), @@ -326,11 +342,13 @@ public Flux stream(Prompt prompt) { * @return the assistant message, or null if not available */ public AssistantMessage safeAssistantMessage(ChatResponse response) { - if (response == null) + if (response == null) { return null; + } Generation gen = response.getResult(); - if (gen == null) + if (gen == null) { return null; + } return gen.getOutput(); } @@ -383,10 +401,12 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha sink.error(e); } }).onCompleteFuture().whenComplete((unused, throwable) -> { - if (throwable != null) + if (throwable != null) { sink.error(throwable); - else + } + else { sink.complete(); + } }); }).buffer(2, 1).map(buffer -> { ChatResponse first = buffer.get(0); @@ -406,8 +426,9 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha .contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation)); return flux.collectList().flatMapMany(list -> { - if (list.isEmpty()) + if (list.isEmpty()) { return Flux.empty(); + } boolean hasToolCalls = list.stream() .map(this::safeAssistantMessage) .filter(Objects::nonNull) @@ -433,12 +454,15 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha Map props = new HashMap<>(); for (ChatResponse chatResponse : list) { AssistantMessage am = safeAssistantMessage(chatResponse); - if (am == null) + if (am == null) { continue; - if (am.getText() != null) + } + if (am.getText() != null) { text.append(am.getText()); - if (am.getMetadata() != null) + } + if (am.getMetadata() != null) { props.putAll(am.getMetadata()); + } if (!CollectionUtils.isEmpty(am.getToolCalls())) { Object ccObj = am.getMetadata().get("chunkChoice"); if (ccObj instanceof ChatCompletionChunk.Choice chunkChoice @@ -468,8 +492,9 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha && generation.getMetadata() != ChatGenerationMetadata.NULL) { finalGenMetadata = generation.getMetadata(); } - if (chatResponse.getMetadata() != null) + if (chatResponse.getMetadata() != null) { finalMetadata = chatResponse.getMetadata(); + } } List merged = builders.values() .stream() @@ -497,11 +522,12 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha finally { ToolCallReactiveContextHolder.clearContext(); } - if (tetoolExecutionResult.returnDirect()) + if (tetoolExecutionResult.returnDirect()) { return Flux.just(ChatResponse.builder() .from(aggregated) .generations(ToolExecutionResult.buildGenerations(tetoolExecutionResult)) .build()); + } return this.internalStream( new Prompt(tetoolExecutionResult.conversationHistory(), prompt.getOptions()), aggregated); @@ -527,8 +553,9 @@ private Generation buildGeneration(ChatCompletion.Choice choice, Map tc.function().isPresent()) .map(tc -> { var funcOpt = tc.function(); - if (funcOpt.isEmpty()) + if (funcOpt.isEmpty()) { return null; + } var func = funcOpt.get(); String id = tc.id().orElse(""); String name = func.name().orElse(""); @@ -544,8 +571,9 @@ private Generation buildGeneration(ChatCompletion.Choice choice, Map list.stream().filter(tc -> tc.function().isPresent()).map(tc -> { var opt = tc.function(); - if (opt.isEmpty()) + if (opt.isEmpty()) { return null; + } var funcCall = opt.get(); var functionDef = funcCall.function(); String id = funcCall.id(); @@ -1104,7 +1132,7 @@ public static class ResponseFormat { private String jsonSchema; public Type getType() { - return type; + return this.type; } public void setType(Type type) { @@ -1112,7 +1140,7 @@ public void setType(Type type) { } public String getJsonSchema() { - return jsonSchema; + return this.jsonSchema; } public void setJsonSchema(String jsonSchema) { @@ -1201,7 +1229,7 @@ void merge(AssistantMessage.ToolCall toolCall) { } AssistantMessage.ToolCall build() { - return new AssistantMessage.ToolCall(id, type, name, arguments.toString()); + return new AssistantMessage.ToolCall(this.id, this.type, this.name, this.arguments.toString()); } } diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java index fb648caff5a..927262bc67c 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,28 +16,28 @@ package org.springframework.ai.openaisdk; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +import com.openai.models.ChatModel; import com.openai.models.FunctionDefinition; import com.openai.models.chat.completions.ChatCompletionAudioParam; import com.openai.models.chat.completions.ChatCompletionToolChoiceOption; import com.openai.models.responses.ResponseCreateParams; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.springframework.ai.model.tool.ToolCallingChatOptions; import org.springframework.ai.tool.ToolCallback; import org.springframework.lang.Nullable; import org.springframework.util.Assert; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; - -import static com.openai.models.ChatModel.GPT_5_MINI; - /** * Configuration information for the Chat Model implementation using the OpenAI Java SDK. * @@ -45,7 +45,7 @@ */ public class OpenAiSdkChatOptions extends AbstractOpenAiSdkOptions implements ToolCallingChatOptions { - public static final String DEFAULT_CHAT_MODEL = GPT_5_MINI.asString(); + public static final String DEFAULT_CHAT_MODEL = ChatModel.GPT_5_MINI.asString(); private static final Logger logger = LoggerFactory.getLogger(OpenAiSdkChatOptions.class); @@ -589,53 +589,61 @@ public OpenAiSdkChatOptions copy() { @Override public boolean equals(Object o) { - if (o == null || getClass() != o.getClass()) + if (o == null || getClass() != o.getClass()) { return false; + } OpenAiSdkChatOptions options = (OpenAiSdkChatOptions) o; - return Objects.equals(getModel(), options.getModel()) - && Objects.equals(frequencyPenalty, options.frequencyPenalty) - && Objects.equals(logitBias, options.logitBias) && Objects.equals(logprobs, options.logprobs) - && Objects.equals(topLogprobs, options.topLogprobs) && Objects.equals(maxTokens, options.maxTokens) - && Objects.equals(maxCompletionTokens, options.maxCompletionTokens) && Objects.equals(n, options.n) - && Objects.equals(outputAudio, options.outputAudio) - && Objects.equals(presencePenalty, options.presencePenalty) - && Objects.equals(responseFormat, options.responseFormat) - && Objects.equals(streamOptions, options.streamOptions) - && Objects.equals(streamUsage, options.streamUsage) && Objects.equals(seed, options.seed) - && Objects.equals(stop, options.stop) && Objects.equals(temperature, options.temperature) - && Objects.equals(topP, options.topP) && Objects.equals(tools, options.tools) - && Objects.equals(toolChoice, options.toolChoice) && Objects.equals(user, options.user) - && Objects.equals(parallelToolCalls, options.parallelToolCalls) && Objects.equals(store, options.store) - && Objects.equals(metadata, options.metadata) - && Objects.equals(reasoningEffort, options.reasoningEffort) - && Objects.equals(verbosity, options.verbosity) && Objects.equals(serviceTier, options.serviceTier) - && Objects.equals(toolCallbacks, options.toolCallbacks) && Objects.equals(toolNames, options.toolNames) - && Objects.equals(internalToolExecutionEnabled, options.internalToolExecutionEnabled) - && Objects.equals(httpHeaders, options.httpHeaders) && Objects.equals(toolContext, options.toolContext); + return Objects.equals(this.getModel(), options.getModel()) + && Objects.equals(this.frequencyPenalty, options.frequencyPenalty) + && Objects.equals(this.logitBias, options.logitBias) && Objects.equals(this.logprobs, options.logprobs) + && Objects.equals(this.temperature, options.temperature) + && Objects.equals(this.maxTokens, options.maxTokens) + && Objects.equals(this.maxCompletionTokens, options.maxCompletionTokens) + && Objects.equals(this.n, options.n) && Objects.equals(this.outputAudio, options.outputAudio) + && Objects.equals(this.presencePenalty, options.presencePenalty) + && Objects.equals(this.responseFormat, options.responseFormat) + && Objects.equals(this.streamOptions, options.streamOptions) + && Objects.equals(this.streamUsage, options.streamUsage) && Objects.equals(this.seed, options.seed) + && Objects.equals(this.stop, options.stop) && Objects.equals(this.temperature, options.temperature) + && Objects.equals(this.topP, options.topP) && Objects.equals(this.tools, options.tools) + && Objects.equals(this.toolChoice, options.toolChoice) && Objects.equals(this.user, options.user) + && Objects.equals(this.parallelToolCalls, options.parallelToolCalls) + && Objects.equals(this.store, options.store) && Objects.equals(this.metadata, options.metadata) + && Objects.equals(this.reasoningEffort, options.reasoningEffort) + && Objects.equals(this.verbosity, options.verbosity) + && Objects.equals(this.serviceTier, options.serviceTier) + && Objects.equals(this.toolCallbacks, options.toolCallbacks) + && Objects.equals(this.toolNames, options.toolNames) + && Objects.equals(this.internalToolExecutionEnabled, options.internalToolExecutionEnabled) + && Objects.equals(this.httpHeaders, options.httpHeaders) + && Objects.equals(this.toolContext, options.toolContext); } @Override public int hashCode() { - return Objects.hash(getModel(), frequencyPenalty, logitBias, logprobs, topLogprobs, maxTokens, - maxCompletionTokens, n, outputAudio, presencePenalty, responseFormat, streamOptions, streamUsage, seed, - stop, temperature, topP, tools, toolChoice, user, parallelToolCalls, store, metadata, reasoningEffort, - verbosity, serviceTier, toolCallbacks, toolNames, internalToolExecutionEnabled, httpHeaders, - toolContext); + return Objects.hash(this.getModel(), this.frequencyPenalty, this.logitBias, this.logprobs, this.topLogprobs, + this.maxTokens, this.maxCompletionTokens, this.n, this.outputAudio, this.presencePenalty, + this.responseFormat, this.streamOptions, this.streamUsage, this.seed, this.stop, this.temperature, + this.topP, this.tools, this.toolChoice, this.user, this.parallelToolCalls, this.store, this.metadata, + this.reasoningEffort, this.verbosity, this.serviceTier, this.toolCallbacks, this.toolNames, + this.internalToolExecutionEnabled, this.httpHeaders, this.toolContext); } @Override public String toString() { - return "OpenAiSdkChatOptions{" + "model='" + getModel() + ", frequencyPenalty=" + frequencyPenalty - + ", logitBias=" + logitBias + ", logprobs=" + logprobs + ", topLogprobs=" + topLogprobs - + ", maxTokens=" + maxTokens + ", maxCompletionTokens=" + maxCompletionTokens + ", n=" + n - + ", outputAudio=" + outputAudio + ", presencePenalty=" + presencePenalty + ", responseFormat=" - + responseFormat + ", streamOptions=" + streamOptions + ", streamUsage=" + streamUsage + ", seed=" - + seed + ", stop=" + stop + ", temperature=" + temperature + ", topP=" + topP + ", tools=" + tools - + ", toolChoice=" + toolChoice + ", user='" + user + '\'' + ", parallelToolCalls=" + parallelToolCalls - + ", store=" + store + ", metadata=" + metadata + ", reasoningEffort='" + reasoningEffort + '\'' - + ", verbosity='" + verbosity + '\'' + ", serviceTier='" + serviceTier + '\'' + ", toolCallbacks=" - + toolCallbacks + ", toolNames=" + toolNames + ", internalToolExecutionEnabled=" - + internalToolExecutionEnabled + ", httpHeaders=" + httpHeaders + ", toolContext=" + toolContext + '}'; + return "OpenAiSdkChatOptions{" + "model='" + this.getModel() + ", frequencyPenalty=" + this.frequencyPenalty + + ", logitBias=" + this.logitBias + ", logprobs=" + this.logprobs + ", topLogprobs=" + this.topLogprobs + + ", maxTokens=" + this.maxTokens + ", maxCompletionTokens=" + this.maxCompletionTokens + ", n=" + + this.n + ", outputAudio=" + this.outputAudio + ", presencePenalty=" + this.presencePenalty + + ", responseFormat=" + this.responseFormat + ", streamOptions=" + this.streamOptions + ", streamUsage=" + + this.streamUsage + ", seed=" + this.seed + ", stop=" + this.stop + ", temperature=" + this.temperature + + ", topP=" + this.topP + ", tools=" + this.tools + ", toolChoice=" + this.toolChoice + ", user='" + + this.user + '\'' + ", parallelToolCalls=" + this.parallelToolCalls + ", store=" + this.store + + ", metadata=" + this.metadata + ", reasoningEffort='" + this.reasoningEffort + '\'' + ", verbosity='" + + this.verbosity + '\'' + ", serviceTier='" + this.serviceTier + '\'' + ", toolCallbacks=" + + this.toolCallbacks + ", toolNames=" + this.toolNames + ", internalToolExecutionEnabled=" + + this.internalToolExecutionEnabled + ", httpHeaders=" + this.httpHeaders + ", toolContext=" + + this.toolContext + '}'; } public static final class Builder { diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingModel.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingModel.java index 3900e4adade..ff23e63d962 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingModel.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,12 +16,17 @@ package org.springframework.ai.openaisdk; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + import com.openai.client.OpenAIClient; import com.openai.models.embeddings.CreateEmbeddingResponse; import com.openai.models.embeddings.EmbeddingCreateParams; import io.micrometer.observation.ObservationRegistry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.springframework.ai.chat.metadata.DefaultUsage; import org.springframework.ai.document.Document; import org.springframework.ai.document.MetadataMode; @@ -36,15 +41,10 @@ import org.springframework.ai.embedding.observation.EmbeddingModelObservationDocumentation; import org.springframework.ai.model.EmbeddingUtils; import org.springframework.ai.observation.conventions.AiProvider; +import org.springframework.ai.openaisdk.setup.OpenAiSdkSetup; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -import static org.springframework.ai.openaisdk.setup.OpenAiSdkSetup.setupSyncClient; - /** * Embedding Model implementation using the OpenAI Java SDK. * @@ -160,11 +160,12 @@ public OpenAiSdkEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataM this.options = options; } this.openAiClient = Objects.requireNonNullElseGet(openAiClient, - () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), - this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), - this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), - this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), - this.options.getProxy(), this.options.getCustomHeaders())); + () -> OpenAiSdkSetup.setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), + this.options.getCredential(), this.options.getAzureDeploymentName(), + this.options.getAzureOpenAIServiceVersion(), this.options.getOrganizationId(), + this.options.isAzure(), this.options.isGitHubModels(), this.options.getModel(), + this.options.getTimeout(), this.options.getMaxRetries(), this.options.getProxy(), + this.options.getCustomHeaders())); this.metadataMode = Objects.requireNonNullElse(metadataMode, MetadataMode.EMBED); this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); } diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingOptions.java index 5bdcfc3d1b1..aca75d11634 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingOptions.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,12 +16,12 @@ package org.springframework.ai.openaisdk; -import com.openai.models.embeddings.EmbeddingCreateParams; -import org.springframework.ai.embedding.EmbeddingOptions; - import java.util.List; -import static com.openai.models.embeddings.EmbeddingModel.TEXT_EMBEDDING_ADA_002; +import com.openai.models.embeddings.EmbeddingCreateParams; +import com.openai.models.embeddings.EmbeddingModel; + +import org.springframework.ai.embedding.EmbeddingOptions; /** * Configuration information for the Embedding Model implementation using the OpenAI Java @@ -31,7 +31,7 @@ */ public class OpenAiSdkEmbeddingOptions extends AbstractOpenAiSdkOptions implements EmbeddingOptions { - public static final String DEFAULT_EMBEDDING_MODEL = TEXT_EMBEDDING_ADA_002.asString(); + public static final String DEFAULT_EMBEDDING_MODEL = EmbeddingModel.TEXT_EMBEDDING_ADA_002.asString(); /** * An identifier for the caller or end user of the operation. This may be used for @@ -68,8 +68,8 @@ public void setDimensions(Integer dimensions) { @Override public String toString() { - return "OpenAiSdkEmbeddingOptions{" + "user='" + user + '\'' + ", model='" + getModel() + '\'' - + ", deploymentName='" + getDeploymentName() + '\'' + ", dimensions=" + dimensions + '}'; + return "OpenAiSdkEmbeddingOptions{" + "user='" + this.user + '\'' + ", model='" + this.getModel() + '\'' + + ", deploymentName='" + this.getDeploymentName() + '\'' + ", dimensions=" + this.dimensions + '}'; } public EmbeddingCreateParams toOpenAiCreateParams(List instructions) { diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageModel.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageModel.java index 02964388fe2..871339bf2cd 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageModel.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +16,15 @@ package org.springframework.ai.openaisdk; +import java.util.List; +import java.util.Objects; + import com.openai.client.OpenAIClient; import com.openai.models.images.ImageGenerateParams; import io.micrometer.observation.ObservationRegistry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.springframework.ai.image.Image; import org.springframework.ai.image.ImageGeneration; import org.springframework.ai.image.ImageModel; @@ -34,13 +38,9 @@ import org.springframework.ai.observation.conventions.AiProvider; import org.springframework.ai.openaisdk.metadata.OpenAiSdkImageGenerationMetadata; import org.springframework.ai.openaisdk.metadata.OpenAiSdkImageResponseMetadata; +import org.springframework.ai.openaisdk.setup.OpenAiSdkSetup; import org.springframework.util.Assert; -import java.util.List; -import java.util.Objects; - -import static org.springframework.ai.openaisdk.setup.OpenAiSdkSetup.setupSyncClient; - /** * Image Model implementation using the OpenAI Java SDK. * @@ -137,11 +137,12 @@ public OpenAiSdkImageModel(OpenAIClient openAiClient, OpenAiSdkImageOptions opti this.options = options; } this.openAiClient = Objects.requireNonNullElseGet(openAiClient, - () -> setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), this.options.getCredential(), - this.options.getAzureDeploymentName(), this.options.getAzureOpenAIServiceVersion(), - this.options.getOrganizationId(), this.options.isAzure(), this.options.isGitHubModels(), - this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), - this.options.getProxy(), this.options.getCustomHeaders())); + () -> OpenAiSdkSetup.setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), + this.options.getCredential(), this.options.getAzureDeploymentName(), + this.options.getAzureOpenAIServiceVersion(), this.options.getOrganizationId(), + this.options.isAzure(), this.options.isGitHubModels(), this.options.getModel(), + this.options.getTimeout(), this.options.getMaxRetries(), this.options.getProxy(), + this.options.getCustomHeaders())); this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); } diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java index ca1c17a21c1..27d3ed7af9d 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java @@ -1,5 +1,5 @@ /* - * Copyright 2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,15 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.ai.openaisdk; +import java.util.Objects; + import com.openai.models.images.ImageGenerateParams; import com.openai.models.images.ImageModel; + import org.springframework.ai.image.ImageOptions; import org.springframework.ai.image.ImagePrompt; -import java.util.Objects; - /** * Configuration information for the Image Model implementation using the OpenAI Java SDK. * @@ -160,25 +162,27 @@ public void setStyle(String style) { @Override public boolean equals(Object o) { - if (o == null || getClass() != o.getClass()) + if (o == null || getClass() != o.getClass()) { return false; + } OpenAiSdkImageOptions that = (OpenAiSdkImageOptions) o; - return Objects.equals(n, that.n) && Objects.equals(width, that.width) && Objects.equals(height, that.height) - && Objects.equals(quality, that.quality) && Objects.equals(responseFormat, that.responseFormat) - && Objects.equals(size, that.size) && Objects.equals(style, that.style) - && Objects.equals(user, that.user); + return Objects.equals(this.n, that.n) && Objects.equals(this.width, that.width) + && Objects.equals(this.height, that.height) && Objects.equals(this.quality, that.quality) + && Objects.equals(this.responseFormat, that.responseFormat) && Objects.equals(this.size, that.size) + && Objects.equals(this.style, that.style) && Objects.equals(this.user, that.user); } @Override public int hashCode() { - return Objects.hash(n, width, height, quality, responseFormat, size, style, user); + return Objects.hash(this.n, this.width, this.height, this.quality, this.responseFormat, this.size, this.style, + this.user); } @Override public String toString() { - return "OpenAiSdkImageOptions{" + "n=" + n + ", width=" + width + ", height=" + height + ", quality='" + quality - + '\'' + ", responseFormat='" + responseFormat + '\'' + ", size='" + size + '\'' + ", style='" + style - + '\'' + ", user='" + user + '\'' + '}'; + return "OpenAiSdkImageOptions{" + "n=" + this.n + ", width=" + this.width + ", height=" + this.height + + ", quality='" + this.quality + '\'' + ", responseFormat='" + this.responseFormat + '\'' + ", size='" + + this.size + '\'' + ", style='" + this.style + '\'' + ", user='" + this.user + '\'' + '}'; } public ImageGenerateParams toOpenAiImageGenerateParams(ImagePrompt imagePrompt) { diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/OpenAiSdkImageGenerationMetadata.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/OpenAiSdkImageGenerationMetadata.java index d38424f3064..2cce8755992 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/OpenAiSdkImageGenerationMetadata.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/OpenAiSdkImageGenerationMetadata.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +16,11 @@ package org.springframework.ai.openaisdk.metadata; -import org.springframework.ai.image.ImageGenerationMetadata; - import java.util.Objects; import java.util.Optional; +import org.springframework.ai.image.ImageGenerationMetadata; + /** * Represents the metadata for image generation using the OpenAI Java SDK. * @@ -53,7 +53,7 @@ public String getRevisedPrompt() { @Override public String toString() { - return "OpenAiSdkImageGenerationMetadata{" + "revisedPrompt='" + revisedPrompt + '\'' + '}'; + return "OpenAiSdkImageGenerationMetadata{" + "revisedPrompt='" + this.revisedPrompt + '\'' + '}'; } @Override diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/OpenAiSdkImageResponseMetadata.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/OpenAiSdkImageResponseMetadata.java index d28d973fdea..1ad635d0a47 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/OpenAiSdkImageResponseMetadata.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/OpenAiSdkImageResponseMetadata.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,12 +16,13 @@ package org.springframework.ai.openaisdk.metadata; +import java.util.Objects; + import com.openai.models.images.ImagesResponse; + import org.springframework.ai.image.ImageResponseMetadata; import org.springframework.util.Assert; -import java.util.Objects; - /** * Represents the metadata for image response using the OpenAI Java SDK. * @@ -56,7 +57,7 @@ public Long getCreated() { @Override public String toString() { - return "OpenAiSdkImageResponseMetadata{" + "created=" + created + '}'; + return "OpenAiSdkImageResponseMetadata{" + "created=" + this.created + '}'; } @Override diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/package-info.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/package-info.java index 96a728de702..e33867ac25e 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/package-info.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/metadata/package-info.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/package-info.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/package-info.java index 9a3295a5f9d..b52b885c4c0 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/package-info.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/package-info.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/AzureInternalOpenAiSdkHelper.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/AzureInternalOpenAiSdkHelper.java index 814250b22e7..ab772a5ffaf 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/AzureInternalOpenAiSdkHelper.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/AzureInternalOpenAiSdkHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,7 +31,10 @@ * * @author Julien Dubois */ -class AzureInternalOpenAiSdkHelper { +final class AzureInternalOpenAiSdkHelper { + + private AzureInternalOpenAiSdkHelper() { + } static Credential getAzureCredential() { return BearerTokenCredential.create(AuthenticationUtil.getBearerTokenSupplier( diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java index 95c9ac47f7e..b4d32af8c90 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,12 @@ package org.springframework.ai.openaisdk.setup; +import java.net.Proxy; +import java.time.Duration; +import java.util.Collections; +import java.util.Map; +import java.util.stream.Collectors; + import com.openai.azure.AzureOpenAIServiceVersion; import com.openai.client.OpenAIClient; import com.openai.client.OpenAIClientAsync; @@ -25,14 +31,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.net.Proxy; -import java.time.Duration; -import java.util.Collections; -import java.util.Map; -import java.util.stream.Collectors; - -import static java.time.Duration.ofSeconds; - /** * Helps configure the OpenAI Java SDK, depending on the platform used. This code is * inspired by LangChain4j's @@ -41,7 +39,7 @@ * * @author Julien Dubois */ -public class OpenAiSdkSetup { +public final class OpenAiSdkSetup { static final String OPENAI_URL = "https://api.openai.com/v1"; static final String OPENAI_API_KEY = "OPENAI_API_KEY"; @@ -52,10 +50,13 @@ public class OpenAiSdkSetup { private static final Logger logger = LoggerFactory.getLogger(OpenAiSdkSetup.class); - private static final Duration DEFAULT_DURATION = ofSeconds(60); + private static final Duration DEFAULT_DURATION = Duration.ofSeconds(60); private static final int DEFAULT_MAX_RETRIES = 3; + private OpenAiSdkSetup() { + } + public enum ModelProvider { OPEN_AI, AZURE_OPEN_AI, GITHUB_MODELS diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/package-info.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/package-info.java index 5c5744c5140..b0714e1de5b 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/package-info.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/package-info.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfiguration.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfiguration.java index 8e83f0b8949..239650c969a 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfiguration.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfigurationWithObservability.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfigurationWithObservability.java index aa00e96220b..0e4817bf195 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfigurationWithObservability.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfigurationWithObservability.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,14 +17,11 @@ package org.springframework.ai.openaisdk; import io.micrometer.observation.tck.TestObservationRegistry; + import org.springframework.ai.document.MetadataMode; import org.springframework.boot.SpringBootConfiguration; import org.springframework.context.annotation.Bean; -import static org.springframework.ai.openaisdk.OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL; -import static org.springframework.ai.openaisdk.OpenAiSdkEmbeddingOptions.DEFAULT_EMBEDDING_MODEL; -import static org.springframework.ai.openaisdk.OpenAiSdkImageOptions.DEFAULT_IMAGE_MODEL; - /** * Context configuration for OpenAI Java SDK tests. * @@ -41,18 +38,21 @@ public TestObservationRegistry testObservationRegistry() { @Bean public OpenAiSdkEmbeddingModel openAiEmbeddingModel(TestObservationRegistry observationRegistry) { return new OpenAiSdkEmbeddingModel(MetadataMode.EMBED, - OpenAiSdkEmbeddingOptions.builder().model(DEFAULT_EMBEDDING_MODEL).build(), observationRegistry); + OpenAiSdkEmbeddingOptions.builder().model(OpenAiSdkEmbeddingOptions.DEFAULT_EMBEDDING_MODEL).build(), + observationRegistry); } @Bean public OpenAiSdkImageModel openAiImageModel(TestObservationRegistry observationRegistry) { - return new OpenAiSdkImageModel(OpenAiSdkImageOptions.builder().model(DEFAULT_IMAGE_MODEL).build(), + return new OpenAiSdkImageModel( + OpenAiSdkImageOptions.builder().model(OpenAiSdkImageOptions.DEFAULT_IMAGE_MODEL).build(), observationRegistry); } @Bean public OpenAiSdkChatModel openAiChatModel(TestObservationRegistry observationRegistry) { - return new OpenAiSdkChatModel(OpenAiSdkChatOptions.builder().model(DEFAULT_CHAT_MODEL).build(), + return new OpenAiSdkChatModel( + OpenAiSdkChatOptions.builder().model(OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL).build(), observationRegistry); } diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/ActorsFilms.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/ActorsFilms.java index d3b2d28b8ae..94edde06931 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/ActorsFilms.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/ActorsFilms.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/MockWeatherService.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/MockWeatherService.java index ab76e796a5d..ffdb9995eae 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/MockWeatherService.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/MockWeatherService.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package org.springframework.ai.openaisdk.chat; +import java.util.function.Function; + import com.fasterxml.jackson.annotation.JsonClassDescription; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; @@ -24,8 +26,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.function.Function; - public class MockWeatherService implements Function { private final Logger logger = LoggerFactory.getLogger(MockWeatherService.class); diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java index 14b98569071..7622e26bd94 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,12 +16,26 @@ package org.springframework.ai.openaisdk.chat; +import java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + import com.openai.models.ReasoningEffort; import org.assertj.core.data.Percentage; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import reactor.core.publisher.Flux; + import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.memory.ChatMemory; import org.springframework.ai.chat.memory.MessageWindowChatMemory; @@ -59,19 +73,6 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.util.MimeTypeUtils; -import reactor.core.publisher.Flux; - -import java.io.IOException; -import java.net.URI; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; -import java.util.stream.IntStream; import static org.assertj.core.api.Assertions.assertThat; diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java index 73bc31c35fe..a7322727d9f 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,13 +16,20 @@ package org.springframework.ai.openaisdk.chat; +import java.util.List; +import java.util.stream.Collectors; + import io.micrometer.observation.tck.TestObservationRegistry; import io.micrometer.observation.tck.TestObservationRegistryAssert; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import reactor.core.publisher.Flux; + import org.springframework.ai.chat.metadata.ChatResponseMetadata; import org.springframework.ai.chat.model.ChatResponse; +import org.springframework.ai.chat.observation.ChatModelObservationDocumentation.HighCardinalityKeyNames; +import org.springframework.ai.chat.observation.ChatModelObservationDocumentation.LowCardinalityKeyNames; import org.springframework.ai.chat.observation.DefaultChatModelObservationConvention; import org.springframework.ai.chat.prompt.Prompt; import org.springframework.ai.observation.conventions.AiOperationType; @@ -32,15 +39,8 @@ import org.springframework.ai.openaisdk.OpenAiSdkTestConfigurationWithObservability; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import reactor.core.publisher.Flux; - -import java.util.List; -import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.ai.chat.observation.ChatModelObservationDocumentation.HighCardinalityKeyNames; -import static org.springframework.ai.chat.observation.ChatModelObservationDocumentation.LowCardinalityKeyNames; -import static org.springframework.ai.openaisdk.OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL; /** * Integration tests for observation instrumentation in {@link OpenAiSdkChatModel}. @@ -65,7 +65,7 @@ void setUp() { @Test void observationForChatOperation() throws InterruptedException { - var options = OpenAiSdkChatOptions.builder().model(DEFAULT_CHAT_MODEL).build(); + var options = OpenAiSdkChatOptions.builder().model(OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL).build(); Prompt prompt = new Prompt("Why does a raven look like a desk?", options); @@ -80,7 +80,10 @@ void observationForChatOperation() throws InterruptedException { @Test void observationForStreamingChatOperation() throws InterruptedException { - var options = OpenAiSdkChatOptions.builder().model(DEFAULT_CHAT_MODEL).streamUsage(true).build(); + var options = OpenAiSdkChatOptions.builder() + .model(OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL) + .streamUsage(true) + .build(); Prompt prompt = new Prompt("Why does a raven look like a desk?", options); @@ -114,7 +117,8 @@ private void validate(ChatResponseMetadata responseMetadata) throws InterruptedE .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_OPERATION_TYPE.asString(), AiOperationType.CHAT.value()) .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_PROVIDER.asString(), AiProvider.OPENAI_SDK.value()) - .hasLowCardinalityKeyValue(LowCardinalityKeyNames.REQUEST_MODEL.asString(), DEFAULT_CHAT_MODEL) + .hasLowCardinalityKeyValue(LowCardinalityKeyNames.REQUEST_MODEL.asString(), + OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL) .hasLowCardinalityKeyValue(LowCardinalityKeyNames.RESPONSE_MODEL.asString(), responseMetadata.getModel()) .hasHighCardinalityKeyValue(HighCardinalityKeyNames.RESPONSE_ID.asString(), responseMetadata.getId()) .hasHighCardinalityKeyValue(HighCardinalityKeyNames.RESPONSE_FINISH_REASONS.asString(), "[\"STOP\"]") diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelResponseFormatIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelResponseFormatIT.java index 9b32fb16994..a273dfdde5f 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelResponseFormatIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelResponseFormatIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.springframework.ai.chat.model.ChatResponse; import org.springframework.ai.chat.prompt.Prompt; import org.springframework.ai.converter.BeanOutputConverter; @@ -36,7 +37,6 @@ import org.springframework.boot.test.context.SpringBootTest; import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.ai.openaisdk.OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL; /** * Integration tests for the response format in {@link OpenAiSdkChatModel}. @@ -114,7 +114,7 @@ void jsonSchema() { Prompt prompt = new Prompt("how can I solve 8x + 7 = -23", OpenAiSdkChatOptions.builder() - .model(DEFAULT_CHAT_MODEL) + .model(OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL) .responseFormat(OpenAiSdkChatModel.ResponseFormat.builder() .type(OpenAiSdkChatModel.ResponseFormat.Type.JSON_SCHEMA) .jsonSchema(jsonSchema) @@ -160,7 +160,7 @@ void jsonSchemaThroughIndividualSetters() throws JsonProcessingException { Prompt prompt = new Prompt("how can I solve 8x + 7 = -23", OpenAiSdkChatOptions.builder() - .model(DEFAULT_CHAT_MODEL) + .model(OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL) .responseFormat(OpenAiSdkChatModel.ResponseFormat.builder().jsonSchema(jsonSchema).build()) .build()); @@ -241,7 +241,7 @@ record Items(@JsonProperty(required = true, value = "explanation") String explan Prompt prompt = new Prompt("how can I solve 8x + 7 = -23", OpenAiSdkChatOptions.builder() - .model(DEFAULT_CHAT_MODEL) + .model(OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL) .responseFormat(OpenAiSdkChatModel.ResponseFormat.builder().jsonSchema(jsonSchema1).build()) .build()); diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java index 622b105a205..e4c8e4ea128 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +16,6 @@ package org.springframework.ai.openaisdk.chat; -import com.openai.models.FunctionDefinition; -import org.junit.jupiter.api.Test; -import org.springframework.ai.openaisdk.OpenAiSdkChatOptions; -import org.springframework.ai.tool.ToolCallback; - import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -29,6 +24,12 @@ import java.util.Map; import java.util.Set; +import com.openai.models.FunctionDefinition; +import org.junit.jupiter.api.Test; + +import org.springframework.ai.openaisdk.OpenAiSdkChatOptions; +import org.springframework.ai.tool.ToolCallback; + import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java index c68c824e47b..b386acf859e 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,9 +16,13 @@ package org.springframework.ai.openaisdk.embedding; +import java.nio.charset.StandardCharsets; +import java.util.List; + import com.openai.models.embeddings.EmbeddingModel; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; + import org.springframework.ai.document.Document; import org.springframework.ai.embedding.EmbeddingRequest; import org.springframework.ai.embedding.EmbeddingResponse; @@ -31,9 +35,6 @@ import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; -import java.nio.charset.StandardCharsets; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java index 969d3200dad..2dbe8626d0e 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,15 +16,21 @@ package org.springframework.ai.openaisdk.embedding; +import java.util.List; + +import com.openai.models.embeddings.EmbeddingModel; import io.micrometer.observation.tck.TestObservationRegistry; import io.micrometer.observation.tck.TestObservationRegistryAssert; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; + import org.springframework.ai.embedding.EmbeddingRequest; import org.springframework.ai.embedding.EmbeddingResponse; import org.springframework.ai.embedding.EmbeddingResponseMetadata; import org.springframework.ai.embedding.observation.DefaultEmbeddingModelObservationConvention; +import org.springframework.ai.embedding.observation.EmbeddingModelObservationDocumentation.HighCardinalityKeyNames; +import org.springframework.ai.embedding.observation.EmbeddingModelObservationDocumentation.LowCardinalityKeyNames; import org.springframework.ai.observation.conventions.AiOperationType; import org.springframework.ai.observation.conventions.AiProvider; import org.springframework.ai.openaisdk.OpenAiSdkEmbeddingModel; @@ -33,12 +39,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import java.util.List; - -import static com.openai.models.embeddings.EmbeddingModel.TEXT_EMBEDDING_3_SMALL; import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.ai.embedding.observation.EmbeddingModelObservationDocumentation.HighCardinalityKeyNames; -import static org.springframework.ai.embedding.observation.EmbeddingModelObservationDocumentation.LowCardinalityKeyNames; /** * Integration tests for observation instrumentation in {@link OpenAiSdkEmbeddingModel}. @@ -63,7 +64,7 @@ void setUp() { @Test void observationForEmbeddingOperation() { var options = OpenAiSdkEmbeddingOptions.builder() - .model(TEXT_EMBEDDING_3_SMALL.toString()) + .model(EmbeddingModel.TEXT_EMBEDDING_3_SMALL.toString()) .dimensions(1536) .build(); @@ -79,12 +80,12 @@ void observationForEmbeddingOperation() { .doesNotHaveAnyRemainingCurrentObservation() .hasObservationWithNameEqualTo(DefaultEmbeddingModelObservationConvention.DEFAULT_NAME) .that() - .hasContextualNameEqualTo("embedding " + TEXT_EMBEDDING_3_SMALL) + .hasContextualNameEqualTo("embedding " + EmbeddingModel.TEXT_EMBEDDING_3_SMALL) .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_OPERATION_TYPE.asString(), AiOperationType.EMBEDDING.value()) .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_PROVIDER.asString(), AiProvider.OPENAI_SDK.value()) .hasLowCardinalityKeyValue(LowCardinalityKeyNames.REQUEST_MODEL.asString(), - TEXT_EMBEDDING_3_SMALL.toString()) + EmbeddingModel.TEXT_EMBEDDING_3_SMALL.toString()) .hasLowCardinalityKeyValue(LowCardinalityKeyNames.RESPONSE_MODEL.asString(), responseMetadata.getModel()) .hasHighCardinalityKeyValue(HighCardinalityKeyNames.REQUEST_EMBEDDING_DIMENSIONS.asString(), "1536") .hasHighCardinalityKeyValue(HighCardinalityKeyNames.USAGE_INPUT_TOKENS.asString(), diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelIT.java index c5482e9eb74..46c5dbfe8b3 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +21,7 @@ import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.springframework.ai.image.Image; import org.springframework.ai.image.ImageOptionsBuilder; import org.springframework.ai.image.ImagePrompt; diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelObservationIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelObservationIT.java index 6312f375061..a6c911e0af3 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelObservationIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelObservationIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2025 the original author or authors. + * Copyright 2025-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,17 @@ package org.springframework.ai.openaisdk.image; +import com.openai.models.images.ImageModel; import io.micrometer.observation.tck.TestObservationRegistry; import io.micrometer.observation.tck.TestObservationRegistryAssert; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; + import org.springframework.ai.image.ImagePrompt; import org.springframework.ai.image.ImageResponse; import org.springframework.ai.image.observation.DefaultImageModelObservationConvention; +import org.springframework.ai.image.observation.ImageModelObservationDocumentation; import org.springframework.ai.observation.conventions.AiOperationType; import org.springframework.ai.observation.conventions.AiProvider; import org.springframework.ai.openaisdk.OpenAiSdkImageModel; @@ -32,10 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import static com.openai.models.images.ImageModel.DALL_E_3; import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.ai.image.observation.ImageModelObservationDocumentation.HighCardinalityKeyNames; -import static org.springframework.ai.image.observation.ImageModelObservationDocumentation.LowCardinalityKeyNames; /** * Integration tests for observation instrumentation in {@link OpenAiSdkImageModel}. @@ -60,7 +60,7 @@ void setUp() { @Test void observationForImageOperation() throws InterruptedException { var options = OpenAiSdkImageOptions.builder() - .model(DALL_E_3.asString()) + .model(ImageModel.DALL_E_3.asString()) .height(1024) .width(1024) .responseFormat("url") @@ -82,13 +82,21 @@ void observationForImageOperation() throws InterruptedException { .doesNotHaveAnyRemainingCurrentObservation() .hasObservationWithNameEqualTo(DefaultImageModelObservationConvention.DEFAULT_NAME) .that() - .hasContextualNameEqualTo("image " + DALL_E_3.asString()) - .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_OPERATION_TYPE.asString(), + .hasContextualNameEqualTo("image " + ImageModel.DALL_E_3.asString()) + .hasLowCardinalityKeyValue( + ImageModelObservationDocumentation.LowCardinalityKeyNames.AI_OPERATION_TYPE.asString(), AiOperationType.IMAGE.value()) - .hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_PROVIDER.asString(), AiProvider.OPENAI_SDK.value()) - .hasLowCardinalityKeyValue(LowCardinalityKeyNames.REQUEST_MODEL.asString(), DALL_E_3.asString()) - .hasHighCardinalityKeyValue(HighCardinalityKeyNames.REQUEST_IMAGE_SIZE.asString(), "1024x1024") - .hasHighCardinalityKeyValue(HighCardinalityKeyNames.REQUEST_IMAGE_RESPONSE_FORMAT.asString(), "url") + .hasLowCardinalityKeyValue(ImageModelObservationDocumentation.LowCardinalityKeyNames.AI_PROVIDER.asString(), + AiProvider.OPENAI_SDK.value()) + .hasLowCardinalityKeyValue( + ImageModelObservationDocumentation.LowCardinalityKeyNames.REQUEST_MODEL.asString(), + ImageModel.DALL_E_3.asString()) + .hasHighCardinalityKeyValue( + ImageModelObservationDocumentation.HighCardinalityKeyNames.REQUEST_IMAGE_SIZE.asString(), + "1024x1024") + .hasHighCardinalityKeyValue( + ImageModelObservationDocumentation.HighCardinalityKeyNames.REQUEST_IMAGE_RESPONSE_FORMAT.asString(), + "url") .hasBeenStarted() .hasBeenStopped(); } diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java index f2057d6931d..c5ce7e67850 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java @@ -1,12 +1,28 @@ -package org.springframework.ai.openaisdk.setup; +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import com.openai.client.OpenAIClient; -import org.junit.jupiter.api.Test; +package org.springframework.ai.openaisdk.setup; import java.time.Duration; import java.util.Collections; import java.util.Map; +import com.openai.client.OpenAIClient; +import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc index d4b40d8dd3f..99e67080903 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc @@ -33,6 +33,7 @@ **** xref:api/chat/perplexity-chat.adoc[Perplexity AI] **** OCI Generative AI ***** xref:api/chat/oci-genai/cohere-chat.adoc[Cohere] +**** xref:api/chat/openai-sdk-chat.adoc[OpenAI SDK (Official)] **** xref:api/chat/openai-chat.adoc[OpenAI] **** xref:api/chat/qianfan-chat.adoc[QianFan] **** xref:api/chat/zhipuai-chat.adoc[ZhiPu AI] diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/comparison.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/comparison.adoc index 09ac7f30ed5..8cff67dd866 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/comparison.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/comparison.adoc @@ -32,6 +32,8 @@ This table compares various Chat Models supported by Spring AI, detailing their | xref::api/chat/nvidia-chat.adoc[NVIDIA (OpenAI-proxy)] | text, image ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::no.svg[width=12] ^a| image::no.svg[width=12] ^a| image::yes.svg[width=16] | xref::api/chat/oci-genai/cohere-chat.adoc[OCI GenAI/Cohere] | text ^a| image::no.svg[width=12] ^a| image::no.svg[width=12] ^a| image::no.svg[width=12] ^a| image::yes.svg[width=12] ^a| image::no.svg[width=12] ^a| image::no.svg[width=12] ^a| image::no.svg[width=12] | xref::api/chat/ollama-chat.adoc[Ollama] | text, image ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] +| xref::api/chat/openai-sdk-chat.adoc[OpenAI SDK (Official)] a| In: text, image, audio +Out: text, audio ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::no.svg[width=12] ^a| image::yes.svg[width=16] | xref::api/chat/openai-chat.adoc[OpenAI] a| In: text, image, audio Out: text, audio ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::no.svg[width=12] ^a| image::yes.svg[width=16] | xref::api/chat/perplexity-chat.adoc[Perplexity (OpenAI-proxy)] | text ^a| image::no.svg[width=12] ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::yes.svg[width=16] ^a| image::no.svg[width=12] ^a| image::no.svg[width=12] ^a| image::yes.svg[width=16] diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc new file mode 100644 index 00000000000..807d604fe78 --- /dev/null +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc @@ -0,0 +1,613 @@ += OpenAI SDK Chat + +Spring AI supports OpenAI's language models through the OpenAI Java SDK, providing a robust and officially-maintained integration with OpenAI's services including Azure OpenAI and GitHub Models. + +NOTE: This implementation uses the official link:https://github.com/openai/openai-java[OpenAI Java SDK] from OpenAI. For the alternative Spring AI implementation, see xref:api/chat/openai-chat.adoc[OpenAI Chat]. + +== Prerequisites + +You will need to create an API key with OpenAI to access ChatGPT models. + +Create an account at https://platform.openai.com/signup[OpenAI signup page] and generate the token on the https://platform.openai.com/account/api-keys[API Keys page]. + +The Spring AI project defines a configuration property named `spring.ai.openai-sdk.api-key` that you should set to the value of the `API Key` obtained from openai.com. + +You can set this configuration property in your `application.properties` file: + +[source,properties] +---- +spring.ai.openai-sdk.api-key= +---- + +For enhanced security when handling sensitive information like API keys, you can use Spring Expression Language (SpEL) to reference a custom environment variable: + +[source,yaml] +---- +# In application.yml +spring: + ai: + openai-sdk: + api-key: ${OPENAI_API_KEY} +---- + +[source,bash] +---- +# In your environment or .env file +export OPENAI_API_KEY= +---- + +The implementation automatically detects the API key from the `OPENAI_API_KEY` environment variable if not explicitly configured. + +=== Add Repositories and BOM + +Spring AI artifacts are published in Maven Central and Spring Snapshot repositories. +Refer to the xref:getting-started.adoc#artifact-repositories[Artifact Repositories] section to add these repositories to your build system. + +To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build system. + +== Auto-configuration + +Spring AI provides Spring Boot auto-configuration for the OpenAI SDK Chat Client. +To enable it add the following dependency to your project's Maven `pom.xml` or Gradle `build.gradle` build files: + +[tabs] +====== +Maven:: ++ +[source, xml] +---- + + org.springframework.ai + spring-ai-openai-sdk + +---- + +Gradle:: ++ +[source,groovy] +---- +dependencies { + implementation 'org.springframework.ai:spring-ai-openai-sdk' +} +---- +====== + +TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file. + +=== Configuration Properties + +==== Connection Properties + +The prefix `spring.ai.openai-sdk` is used as the property prefix that lets you configure the OpenAI SDK client. + +[cols="3,5,1", stripes=even] +|==== +| Property | Description | Default + +| spring.ai.openai-sdk.base-url | The URL to connect to. Auto-detects from `OPENAI_BASE_URL` or `AZURE_OPENAI_BASE_URL` environment variables if not set. | https://api.openai.com/v1 +| spring.ai.openai-sdk.api-key | The API Key. Auto-detects from `OPENAI_API_KEY`, `AZURE_OPENAI_KEY`, or `GITHUB_TOKEN` environment variables based on the base URL. | - +| spring.ai.openai-sdk.organization-id | Optionally specify which organization to use for API requests. | - +| spring.ai.openai-sdk.timeout | Request timeout duration. | 60 seconds +| spring.ai.openai-sdk.max-retries | Maximum number of retry attempts for failed requests. | 3 +| spring.ai.openai-sdk.proxy.host | Proxy host for HTTP requests. | - +| spring.ai.openai-sdk.proxy.port | Proxy port for HTTP requests. | - +| spring.ai.openai-sdk.custom-headers | Custom HTTP headers to include in requests. | - +|==== + +==== Azure OpenAI Properties + +The OpenAI SDK implementation provides native support for Azure OpenAI with automatic configuration: + +[cols="3,5,1", stripes=even] +|==== +| Property | Description | Default + +| spring.ai.openai-sdk.azure | Enable Azure OpenAI mode. Auto-detected if base URL contains `openai.azure.com` or `cognitiveservices.azure.com`. | false +| spring.ai.openai-sdk.azure-deployment-name | Azure deployment name. If not specified, the model name will be used. | - +| spring.ai.openai-sdk.azure-openai-service-version | Azure OpenAI API version. | - +| spring.ai.openai-sdk.credential | Azure credential for passwordless authentication. | - +|==== + +TIP: Azure OpenAI supports passwordless authentication. If no API key is provided, the implementation automatically attempts to use Azure credentials from the environment. + +==== GitHub Models Properties + +Native support for GitHub Models is available: + +[cols="3,5,1", stripes=even] +|==== +| Property | Description | Default + +| spring.ai.openai-sdk.github-models | Enable GitHub Models mode. Auto-detected if base URL is `https://models.inference.ai.azure.com`. | false +|==== + +TIP: GitHub Models authentication uses the `GITHUB_TOKEN` environment variable when detected. + +==== Chat Model Properties + +The prefix `spring.ai.openai-sdk.chat` is the property prefix for configuring the chat model implementation: + +[cols="3,5,1", stripes=even] +|==== +| Property | Description | Default + +| spring.ai.openai-sdk.chat.options.model | Name of the OpenAI chat model to use. You can select between models such as: `gpt-5-mini`, `gpt-4o`, `gpt-4o-mini`, `gpt-4-turbo`, `o1`, `o3-mini`, and more. See the https://platform.openai.com/docs/models[models] page for more information. | `gpt-5-mini` +| spring.ai.openai-sdk.chat.options.temperature | The sampling temperature to use that controls the apparent creativity of generated completions. Higher values will make output more random while lower values will make results more focused and deterministic. It is not recommended to modify `temperature` and `top_p` for the same completions request as the interaction of these two settings is difficult to predict. | 0.7 +| spring.ai.openai-sdk.chat.options.frequency-penalty | Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. | 0.0 +| spring.ai.openai-sdk.chat.options.logit-bias | Modify the likelihood of specified tokens appearing in the completion. | - +| spring.ai.openai-sdk.chat.options.logprobs | Whether to return log probabilities of the output tokens. | false +| spring.ai.openai-sdk.chat.options.top-logprobs | An integer between 0 and 5 specifying the number of most likely tokens to return at each token position. Requires `logprobs` to be true. | - +| spring.ai.openai-sdk.chat.options.max-tokens | The maximum number of tokens to generate. *Use for non-reasoning models* (e.g., gpt-4o, gpt-3.5-turbo). *Cannot be used with reasoning models* (e.g., o1, o3, o4-mini series). *Mutually exclusive with maxCompletionTokens*. | - +| spring.ai.openai-sdk.chat.options.max-completion-tokens | An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and reasoning tokens. *Required for reasoning models* (e.g., o1, o3, o4-mini series). *Cannot be used with non-reasoning models*. *Mutually exclusive with maxTokens*. | - +| spring.ai.openai-sdk.chat.options.n | How many chat completion choices to generate for each input message. | 1 +| spring.ai.openai-sdk.chat.options.output-audio | Parameters for audio output. Required when audio output is requested. | - +| spring.ai.openai-sdk.chat.options.presence-penalty | Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far. | 0.0 +| spring.ai.openai-sdk.chat.options.response-format.type | Response format type: `TEXT`, `JSON_OBJECT`, or `JSON_SCHEMA`. | TEXT +| spring.ai.openai-sdk.chat.options.response-format.json-schema | JSON schema for structured outputs when type is `JSON_SCHEMA`. | - +| spring.ai.openai-sdk.chat.options.seed | If specified, the system will make a best effort to sample deterministically for reproducible results. | - +| spring.ai.openai-sdk.chat.options.stop | Up to 4 sequences where the API will stop generating further tokens. | - +| spring.ai.openai-sdk.chat.options.top-p | An alternative to sampling with temperature, called nucleus sampling. | - +| spring.ai.openai-sdk.chat.options.user | A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. | - +| spring.ai.openai-sdk.chat.options.parallel-tool-calls | Whether to enable parallel function calling during tool use. | true +| spring.ai.openai-sdk.chat.options.reasoning-effort | Constrains effort on reasoning for reasoning models: `low`, `medium`, or `high`. | - +| spring.ai.openai-sdk.chat.options.verbosity | Controls the verbosity of the model's response. | - +| spring.ai.openai-sdk.chat.options.store | Whether to store the output of this chat completion request for use in OpenAI's model distillation or evals products. | false +| spring.ai.openai-sdk.chat.options.metadata | Developer-defined tags and values used for filtering completions in the dashboard. | - +| spring.ai.openai-sdk.chat.options.service-tier | Specifies the latency tier to use: `auto`, `default`, `flex`, or `priority`. | - +| spring.ai.openai-sdk.chat.options.stream-usage | Whether to include usage statistics in streaming responses. | false +| spring.ai.openai-sdk.chat.options.tool-choice | Controls which (if any) function is called by the model. | - +| spring.ai.openai-sdk.chat.options.internal-tool-execution-enabled | If false, Spring AI will proxy tool calls to the client for manual handling. If true (default), Spring AI handles function calls internally. | true +|==== + +[NOTE] +==== +When using GPT-5 models such as `gpt-5`, `gpt-5-mini`, and `gpt-5-nano`, the `temperature` parameter is not supported. +These models are optimized for reasoning and do not use temperature. +Specifying a temperature value will result in an error. +In contrast, conversational models like `gpt-5-chat` do support the `temperature` parameter. +==== + +TIP: All properties prefixed with `spring.ai.openai-sdk.chat.options` can be overridden at runtime by adding request-specific <> to the `Prompt` call. + +=== Token Limit Parameters: Model-Specific Usage + +OpenAI provides two mutually exclusive parameters for controlling token generation limits: + +[cols="2,3,3", stripes=even] +|==== +| Parameter | Use Case | Compatible Models + +| `maxTokens` | Non-reasoning models | gpt-4o, gpt-4o-mini, gpt-4-turbo, gpt-3.5-turbo +| `maxCompletionTokens` | Reasoning models | o1, o1-mini, o1-preview, o3, o4-mini series +|==== + +IMPORTANT: These parameters are **mutually exclusive**. Setting both will result in an API error from OpenAI. + +==== Usage Examples + +**For non-reasoning models (gpt-4o, gpt-3.5-turbo):** +[source,java] +---- +ChatResponse response = chatModel.call( + new Prompt( + "Explain quantum computing in simple terms.", + OpenAiSdkChatOptions.builder() + .model("gpt-4o") + .maxTokens(150) // Use maxTokens for non-reasoning models + .build() + )); +---- + +**For reasoning models (o1, o3 series):** +[source,java] +---- +ChatResponse response = chatModel.call( + new Prompt( + "Solve this complex math problem step by step: ...", + OpenAiSdkChatOptions.builder() + .model("o1-preview") + .maxCompletionTokens(1000) // Use maxCompletionTokens for reasoning models + .build() + )); +---- + +== Runtime Options [[chat-options]] + +The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java[OpenAiSdkChatOptions.java] class provides model configurations such as the model to use, the temperature, the frequency penalty, etc. + +On start-up, the default options can be configured with the `OpenAiSdkChatModel(options)` constructor or the `spring.ai.openai-sdk.chat.options.*` properties. + +At run-time, you can override the default options by adding new, request-specific options to the `Prompt` call. +For example, to override the default model and temperature for a specific request: + +[source,java] +---- +ChatResponse response = chatModel.call( + new Prompt( + "Generate the names of 5 famous pirates.", + OpenAiSdkChatOptions.builder() + .model("gpt-4o") + .temperature(0.4) + .build() + )); +---- + +TIP: In addition to the model specific https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java[OpenAiSdkChatOptions] you can use a portable link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-model/src/main/java/org/springframework/ai/chat/prompt/ChatOptions.java[ChatOptions] instance, created with link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-model/src/main/java/org/springframework/ai/chat/prompt/DefaultChatOptionsBuilder.java[ChatOptions#builder()]. + +== Tool Calling + +You can register custom Java functions or methods with the `OpenAiSdkChatModel` and have the OpenAI model intelligently choose to output a JSON object containing arguments to call one or many of the registered functions/tools. +This is a powerful technique to connect the LLM capabilities with external tools and APIs. +Read more about xref:api/tools.adoc[Tool Calling]. + +Example usage: + +[source,java] +---- +var chatOptions = OpenAiSdkChatOptions.builder() + .toolCallbacks(List.of( + FunctionToolCallback.builder("getCurrentWeather", new WeatherService()) + .description("Get the weather in location") + .inputType(WeatherService.Request.class) + .build())) + .build(); + +ChatResponse response = chatModel.call( + new Prompt("What's the weather like in San Francisco?", chatOptions)); +---- + +== Multimodal + +Multimodality refers to a model's ability to simultaneously understand and process information from various sources, including text, images, audio, and other data formats. + +=== Vision + +OpenAI models that offer vision multimodal support include `gpt-4`, `gpt-4o`, and `gpt-4o-mini`. +Refer to the link:https://platform.openai.com/docs/guides/vision[Vision] guide for more information. + +Spring AI's link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-model/src/main/java/org/springframework/ai/chat/messages/Message.java[Message] interface facilitates multimodal AI models by introducing the link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-commons/src/main/java/org/springframework/ai/content/Media.java[Media] type. + +Below is a code example illustrating the fusion of user text with an image: + +[source,java] +---- +var imageResource = new ClassPathResource("/multimodal.test.png"); + +var userMessage = new UserMessage( + "Explain what do you see on this picture?", + List.of(new Media(MimeTypeUtils.IMAGE_PNG, imageResource))); + +ChatResponse response = chatModel.call( + new Prompt(userMessage, + OpenAiSdkChatOptions.builder() + .model("gpt-4o") + .build())); +---- + +Or using an image URL: + +[source,java] +---- +var userMessage = new UserMessage( + "Explain what do you see on this picture?", + List.of(Media.builder() + .mimeType(MimeTypeUtils.IMAGE_PNG) + .data(URI.create("https://docs.spring.io/spring-ai/reference/_images/multimodal.test.png")) + .build())); + +ChatResponse response = chatModel.call(new Prompt(userMessage)); +---- + +TIP: You can pass multiple images as well. + +=== Audio + +OpenAI models that offer audio input support include `gpt-4o-audio-preview`. +Refer to the link:https://platform.openai.com/docs/guides/audio[Audio] guide for more information. + +Spring AI supports base64-encoded audio files with the message. +Currently, OpenAI supports the following media types: `audio/mp3` and `audio/wav`. + +Example of audio input: + +[source,java] +---- +var audioResource = new ClassPathResource("speech1.mp3"); + +var userMessage = new UserMessage( + "What is this recording about?", + List.of(new Media(MimeTypeUtils.parseMimeType("audio/mp3"), audioResource))); + +ChatResponse response = chatModel.call( + new Prompt(userMessage, + OpenAiSdkChatOptions.builder() + .model("gpt-4o-audio-preview") + .build())); +---- + +=== Output Audio + +The `gpt-4o-audio-preview` model can generate audio responses. + +Example of generating audio output: + +[source,java] +---- +var userMessage = new UserMessage("Tell me a joke about Spring Framework"); + +ChatResponse response = chatModel.call( + new Prompt(userMessage, + OpenAiSdkChatOptions.builder() + .model("gpt-4o-audio-preview") + .outputAudio(new OutputAudio(Voice.ALLOY, AudioResponseFormat.WAV)) + .build())); + +String text = response.getResult().getOutput().getContent(); // audio transcript +byte[] waveAudio = response.getResult().getOutput().getMedia().get(0).getDataAsByteArray(); // audio data +---- + +== Structured Outputs + +OpenAI provides custom https://platform.openai.com/docs/guides/structured-outputs[Structured Outputs] APIs that ensure your model generates responses conforming strictly to your provided `JSON Schema`. + +=== Configuration + +You can set the response format programmatically with the `OpenAiSdkChatOptions` builder: + +[source,java] +---- +String jsonSchema = """ + { + "type": "object", + "properties": { + "steps": { + "type": "array", + "items": { + "type": "object", + "properties": { + "explanation": { "type": "string" }, + "output": { "type": "string" } + }, + "required": ["explanation", "output"], + "additionalProperties": false + } + }, + "final_answer": { "type": "string" } + }, + "required": ["steps", "final_answer"], + "additionalProperties": false + } + """; + +Prompt prompt = new Prompt( + "how can I solve 8x + 7 = -23", + OpenAiSdkChatOptions.builder() + .model("gpt-4o-mini") + .responseFormat(ResponseFormat.builder() + .type(ResponseFormat.Type.JSON_SCHEMA) + .jsonSchema(jsonSchema) + .build()) + .build()); + +ChatResponse response = chatModel.call(prompt); +---- + +=== Integrating with BeanOutputConverter + +You can leverage existing xref::api/structured-output-converter.adoc#_bean_output_converter[BeanOutputConverter] utilities: + +[source,java] +---- +record MathReasoning( + @JsonProperty(required = true, value = "steps") Steps steps, + @JsonProperty(required = true, value = "final_answer") String finalAnswer) { + + record Steps( + @JsonProperty(required = true, value = "items") Items[] items) { + + record Items( + @JsonProperty(required = true, value = "explanation") String explanation, + @JsonProperty(required = true, value = "output") String output) { + } + } +} + +var outputConverter = new BeanOutputConverter<>(MathReasoning.class); +String jsonSchema = outputConverter.getJsonSchema(); + +Prompt prompt = new Prompt( + "how can I solve 8x + 7 = -23", + OpenAiSdkChatOptions.builder() + .model("gpt-4o-mini") + .responseFormat(ResponseFormat.builder() + .type(ResponseFormat.Type.JSON_SCHEMA) + .jsonSchema(jsonSchema) + .build()) + .build()); + +ChatResponse response = chatModel.call(prompt); +MathReasoning mathReasoning = outputConverter.convert( + response.getResult().getOutput().getContent()); +---- + +== Sample Controller + +https://start.spring.io/[Create] a new Spring Boot project and add the `spring-ai-openai-sdk` to your pom (or gradle) dependencies. + +Add an `application.properties` file under the `src/main/resources` directory to configure the OpenAI SDK chat model: + +[source,application.properties] +---- +spring.ai.openai-sdk.api-key=YOUR_API_KEY +spring.ai.openai-sdk.chat.options.model=gpt-5-mini +spring.ai.openai-sdk.chat.options.temperature=0.7 +---- + +TIP: Replace the `api-key` with your OpenAI credentials. + +This will create an `OpenAiSdkChatModel` implementation that you can inject into your classes. +Here is an example of a simple `@RestController` class that uses the chat model for text generations. + +[source,java] +---- +@RestController +public class ChatController { + + private final OpenAiSdkChatModel chatModel; + + @Autowired + public ChatController(OpenAiSdkChatModel chatModel) { + this.chatModel = chatModel; + } + + @GetMapping("/ai/generate") + public Map generate( + @RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { + return Map.of("generation", chatModel.call(message)); + } + + @GetMapping("/ai/generateStream") + public Flux generateStream( + @RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { + Prompt prompt = new Prompt(new UserMessage(message)); + return chatModel.stream(prompt); + } +} +---- + +== Manual Configuration + +The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java[OpenAiSdkChatModel] implements the `ChatModel` and uses the official OpenAI Java SDK to connect to the OpenAI service. + +Add the `spring-ai-openai-sdk` dependency to your project's Maven `pom.xml` file: + +[source, xml] +---- + + org.springframework.ai + spring-ai-openai-sdk + +---- + +or to your Gradle `build.gradle` build file: + +[source,groovy] +---- +dependencies { + implementation 'org.springframework.ai:spring-ai-openai-sdk' +} +---- + +TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file. + +Next, create an `OpenAiSdkChatModel` and use it for text generations: + +[source,java] +---- +var chatOptions = OpenAiSdkChatOptions.builder() + .model("gpt-4o") + .temperature(0.7) + .apiKey(System.getenv("OPENAI_API_KEY")) + .build(); + +var chatModel = new OpenAiSdkChatModel(chatOptions); + +ChatResponse response = chatModel.call( + new Prompt("Generate the names of 5 famous pirates.")); + +// Or with streaming responses +Flux response = chatModel.stream( + new Prompt("Generate the names of 5 famous pirates.")); +---- + +=== Azure OpenAI Configuration + +For Azure OpenAI: + +[source,java] +---- +var chatOptions = OpenAiSdkChatOptions.builder() + .baseUrl("https://your-resource.openai.azure.com") + .apiKey(System.getenv("AZURE_OPENAI_KEY")) + .azureDeploymentName("gpt-4") + .azureOpenAIServiceVersion(AzureOpenAIServiceVersion.V2024_10_01_PREVIEW) + .azure(true) + .build(); + +var chatModel = new OpenAiSdkChatModel(chatOptions); +---- + +TIP: Azure OpenAI supports passwordless authentication. If you don't provide an API key, the implementation will automatically attempt to use Azure credentials from your environment. + +=== GitHub Models Configuration + +For GitHub Models: + +[source,java] +---- +var chatOptions = OpenAiSdkChatOptions.builder() + .baseUrl("https://models.inference.ai.azure.com") + .apiKey(System.getenv("GITHUB_TOKEN")) + .model("gpt-4o") + .githubModels(true) + .build(); + +var chatModel = new OpenAiSdkChatModel(chatOptions); +---- + +== Key Differences from Spring AI OpenAI + +This implementation differs from the xref:api/chat/openai-chat.adoc[Spring AI OpenAI] implementation in several ways: + +[cols="2,3,3", stripes=even] +|==== +| Aspect | Official OpenAI SDK | Existing OpenAI + +| **HTTP Client** | OkHttp (via official SDK) | Spring RestClient/WebClient +| **API Updates** | Automatic via SDK updates | Manual maintenance +| **Azure Support** | Native with passwordless auth | Manual URL construction +| **GitHub Models** | Native support | Not supported +| **Audio/Moderation** | Not yet supported | Fully supported +| **Retry Logic** | SDK-managed (exponential backoff) | Spring Retry (customizable) +| **Dependencies** | Official OpenAI SDK | Spring WebFlux +|==== + +**When to use OpenAI SDK:** + +* You're starting a new project +* You primarily use Azure OpenAI or GitHub Models +* You want automatic API updates from OpenAI +* You don't need audio transcription or moderation features +* You prefer official SDK support + +**When to use Spring AI OpenAI:** + +* You have an existing project using it +* You need audio transcription or moderation features +* You require fine-grained HTTP control +* You want native Spring reactive support +* You need custom retry strategies + +== Observability + +The OpenAI SDK implementation supports Spring AI's observability features through Micrometer. +All chat model operations are instrumented for monitoring and tracing. + +== Limitations + +The following features are not yet supported in the OpenAI SDK implementation: + +* Audio speech generation (TTS) +* Audio transcription +* Moderation API +* File API operations + +These features are available in the xref:api/chat/openai-chat.adoc[Spring AI OpenAI] implementation. + +== Additional Resources + +* link:https://github.com/openai/openai-java[Official OpenAI Java SDK] +* link:https://platform.openai.com/docs/api-reference/chat[OpenAI Chat API Documentation] +* link:https://platform.openai.com/docs/models[OpenAI Models] +* link:https://learn.microsoft.com/en-us/azure/ai-services/openai/[Azure OpenAI Documentation] +* link:https://github.com/marketplace/models[GitHub Models] From 1a095fe8322f28ece07ad069e1d6595c99d4df52 Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Sat, 22 Nov 2025 19:02:13 +0100 Subject: [PATCH 29/49] feat: add openai-sdk auto-configuration and decouples the Spring AI API from OpenAI SDK types Add openai-sdk auto-configurations with tests: - Add spring-ai-autoconfigure-model-openai-sdk module - Add spring-ai-starter-model-openai-sdk starter - Update BOM to include new modules - Add OPENAI_SDK constant to SpringAIModels Improve OpenAiSdkChatOptions by removing 3rd party code from API: - Replace ResponseCreateParams.StreamOptions with custom StreamOptions record - Change toolChoice from ChatCompletionToolChoiceOption to Object to support both typed options and JSON string representations - Remove tools field (List) from API - Add parseToolChoice() method for dynamic JSON parsing - Improve streamOptions null safety with explicit boolean checks - Update all tests to use new StreamOptions.builder() API Co-authored-by: Christian Tzolov Signed-off-by: Christian Tzolov --- .../tool/FunctionCallWithFunctionBeanIT.java | 2 +- .../pom.xml | 116 +++++++++++++ .../OpenAiSdkAutoConfigurationUtil.java | 80 +++++++++ .../OpenAiSdkChatAutoConfiguration.java | 91 ++++++++++ .../OpenAiSdkChatProperties.java | 48 ++++++ .../OpenAiSdkConnectionProperties.java | 27 +++ .../OpenAiSdkEmbeddingAutoConfiguration.java | 72 ++++++++ .../OpenAiSdkEmbeddingProperties.java | 51 ++++++ .../OpenAiSdkImageAutoConfiguration.java | 71 ++++++++ .../OpenAiSdkImageProperties.java | 48 ++++++ .../openaisdk/autoconfigure/package-info.java | 17 ++ ...ot.autoconfigure.AutoConfiguration.imports | 19 +++ .../ChatClientAutoConfigurationIT.java | 119 +++++++++++++ .../autoconfigure/MockWeatherService.java | 97 +++++++++++ .../OpenAiFunctionCallback2IT.java | 104 +++++++++++ .../OpenAiSdkChatAutoConfigurationIT.java | 129 ++++++++++++++ .../OpenAiSdkChatPropertiesTests.java | 149 ++++++++++++++++ ...OpenAiSdkEmbeddingAutoConfigurationIT.java | 113 ++++++++++++ .../OpenAiSdkEmbeddingPropertiesTests.java | 120 +++++++++++++ .../OpenAiSdkImageAutoConfigurationIT.java | 135 +++++++++++++++ .../OpenAiSdkImagePropertiesTests.java | 128 ++++++++++++++ .../ai/openaisdk/OpenAiSdkChatModel.java | 62 ++++++- .../ai/openaisdk/OpenAiSdkChatOptions.java | 161 +++++++++--------- .../openaisdk/chat/OpenAiSdkChatModelIT.java | 5 +- .../chat/OpenAiSdkChatModelObservationIT.java | 3 +- .../OpenAiSdkChatModelResponseFormatIT.java | 2 +- .../chat/OpenAiSdkChatOptionsTests.java | 29 +--- pom.xml | 2 + spring-ai-bom/pom.xml | 18 ++ .../ai/model/SpringAIModels.java | 2 + .../pom.xml | 70 ++++++++ 31 files changed, 1976 insertions(+), 114 deletions(-) create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/pom.xml create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkAutoConfigurationUtil.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfiguration.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatProperties.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkConnectionProperties.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfiguration.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingProperties.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfiguration.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageProperties.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/package-info.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/ChatClientAutoConfigurationIT.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/MockWeatherService.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiFunctionCallback2IT.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfigurationIT.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatPropertiesTests.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfigurationIT.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingPropertiesTests.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfigurationIT.java create mode 100644 auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImagePropertiesTests.java create mode 100644 spring-ai-spring-boot-starters/spring-ai-starter-model-openai-sdk/pom.xml diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/test/java/org/springframework/ai/model/anthropic/autoconfigure/tool/FunctionCallWithFunctionBeanIT.java b/auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/test/java/org/springframework/ai/model/anthropic/autoconfigure/tool/FunctionCallWithFunctionBeanIT.java index 03cf82807b6..b64e939e862 100644 --- a/auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/test/java/org/springframework/ai/model/anthropic/autoconfigure/tool/FunctionCallWithFunctionBeanIT.java +++ b/auto-configurations/models/spring-ai-autoconfigure-model-anthropic/src/test/java/org/springframework/ai/model/anthropic/autoconfigure/tool/FunctionCallWithFunctionBeanIT.java @@ -118,7 +118,7 @@ public Function weatherFunction() { @Bean public Function weatherFunction3() { MockWeatherService weatherService = new MockWeatherService(); - return (weatherService::apply); + return weatherService::apply; } } diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/pom.xml b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/pom.xml new file mode 100644 index 00000000000..64989389387 --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/pom.xml @@ -0,0 +1,116 @@ + + + 4.0.0 + + org.springframework.ai + spring-ai-parent + 2.0.0-SNAPSHOT + ../../../pom.xml + + spring-ai-autoconfigure-model-openai-sdk + jar + Spring AI OpenAI SDK Auto Configuration + Spring AI OpenAI SDK Auto Configuration + https://github.com/spring-projects/spring-ai + + + https://github.com/spring-projects/spring-ai + git://github.com/spring-projects/spring-ai.git + git@github.com:spring-projects/spring-ai.git + + + + + + + + + org.springframework.ai + spring-ai-openai-sdk + ${project.parent.version} + + + + + + org.springframework.ai + spring-ai-autoconfigure-model-tool + ${project.parent.version} + + + + org.springframework.ai + spring-ai-autoconfigure-model-chat-observation + ${project.parent.version} + + + + org.springframework.ai + spring-ai-autoconfigure-model-embedding-observation + ${project.parent.version} + + + + org.springframework.ai + spring-ai-autoconfigure-model-image-observation + ${project.parent.version} + + + + + org.springframework.boot + spring-boot-starter + true + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + org.springframework.boot + spring-boot-autoconfigure-processor + true + + + + + + org.jetbrains.kotlin + kotlin-reflect + true + + + + + org.springframework.ai + spring-ai-test + ${project.parent.version} + test + + + + org.springframework.ai + spring-ai-autoconfigure-model-chat-client + ${project.parent.version} + test + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.mockito + mockito-core + test + + + + diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkAutoConfigurationUtil.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkAutoConfigurationUtil.java new file mode 100644 index 00000000000..3700806f257 --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkAutoConfigurationUtil.java @@ -0,0 +1,80 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import org.springframework.ai.openaisdk.AbstractOpenAiSdkOptions; +import org.springframework.util.StringUtils; + +public final class OpenAiSdkAutoConfigurationUtil { + + private OpenAiSdkAutoConfigurationUtil() { + // Avoids instantiation + } + + public static ResolvedConnectionProperties resolveConnectionProperties(AbstractOpenAiSdkOptions commonProperties, + AbstractOpenAiSdkOptions modelProperties) { + + var resolved = new ResolvedConnectionProperties(); + + resolved.setBaseUrl(StringUtils.hasText(modelProperties.getBaseUrl()) ? modelProperties.getBaseUrl() + : commonProperties.getBaseUrl()); + + resolved.setApiKey(StringUtils.hasText(modelProperties.getApiKey()) ? modelProperties.getApiKey() + : commonProperties.getApiKey()); + + String organizationId = StringUtils.hasText(modelProperties.getOrganizationId()) + ? modelProperties.getOrganizationId() : commonProperties.getOrganizationId(); + resolved.setOrganizationId(organizationId); + + resolved.setCredential(modelProperties.getCredential() != null ? modelProperties.getCredential() + : commonProperties.getCredential()); + + resolved.setTimeout( + modelProperties.getTimeout() != null ? modelProperties.getTimeout() : commonProperties.getTimeout()); + + resolved.setModel(StringUtils.hasText(modelProperties.getModel()) ? modelProperties.getModel() + : commonProperties.getModel()); + + resolved.setAzureDeploymentName(StringUtils.hasText(modelProperties.getAzureDeploymentName()) + ? modelProperties.getAzureDeploymentName() : commonProperties.getAzureDeploymentName()); + + resolved.setAzureOpenAIServiceVersion(modelProperties.getAzureOpenAIServiceVersion() != null + ? modelProperties.getAzureOpenAIServiceVersion() : commonProperties.getAzureOpenAIServiceVersion()); + + // For boolean properties, use modelProperties value, defaulting to + // commonProperties if needed + resolved.setAzure(modelProperties.isAzure() || commonProperties.isAzure()); + + resolved.setGitHubModels(modelProperties.isGitHubModels() || commonProperties.isGitHubModels()); + + resolved.setMaxRetries(modelProperties.getMaxRetries() != null ? modelProperties.getMaxRetries() + : commonProperties.getMaxRetries()); + + resolved + .setProxy(modelProperties.getProxy() != null ? modelProperties.getProxy() : commonProperties.getProxy()); + + resolved.setCustomHeaders(modelProperties.getCustomHeaders() != null ? modelProperties.getCustomHeaders() + : commonProperties.getCustomHeaders()); + + return resolved; + } + + public static class ResolvedConnectionProperties extends AbstractOpenAiSdkOptions { + + } + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfiguration.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfiguration.java new file mode 100644 index 00000000000..2c2c698ac2c --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfiguration.java @@ -0,0 +1,91 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import com.openai.client.OpenAIClient; +import com.openai.client.OpenAIClientAsync; +import io.micrometer.observation.ObservationRegistry; + +import org.springframework.ai.chat.observation.ChatModelObservationConvention; +import org.springframework.ai.model.SpringAIModelProperties; +import org.springframework.ai.model.SpringAIModels; +import org.springframework.ai.model.tool.DefaultToolExecutionEligibilityPredicate; +import org.springframework.ai.model.tool.ToolCallingManager; +import org.springframework.ai.model.tool.ToolExecutionEligibilityPredicate; +import org.springframework.ai.model.tool.autoconfigure.ToolCallingAutoConfiguration; +import org.springframework.ai.openaisdk.AbstractOpenAiSdkOptions; +import org.springframework.ai.openaisdk.OpenAiSdkChatModel; +import org.springframework.ai.openaisdk.setup.OpenAiSdkSetup; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; + +/** + * Chat {@link AutoConfiguration Auto-configuration} for OpenAI SDK. + * + * @author Christian Tzolov + */ +@AutoConfiguration(after = { ToolCallingAutoConfiguration.class }) +@EnableConfigurationProperties({ OpenAiSdkConnectionProperties.class, OpenAiSdkChatProperties.class }) +@ConditionalOnProperty(name = SpringAIModelProperties.CHAT_MODEL, havingValue = SpringAIModels.OPENAI_SDK, + matchIfMissing = true) +public class OpenAiSdkChatAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public OpenAiSdkChatModel openAiChatModel(OpenAiSdkConnectionProperties commonProperties, + OpenAiSdkChatProperties chatProperties, ToolCallingManager toolCallingManager, + ObjectProvider observationRegistry, + ObjectProvider observationConvention, + ObjectProvider openAiToolExecutionEligibilityPredicate) { + + OpenAiSdkAutoConfigurationUtil.ResolvedConnectionProperties resolvedConnectionProperties = OpenAiSdkAutoConfigurationUtil + .resolveConnectionProperties(commonProperties, chatProperties); + + OpenAIClient openAIClient = this.openAiClient(resolvedConnectionProperties); + + OpenAIClientAsync openAIClientAsync = this.openAiClientAsync(resolvedConnectionProperties); + + var chatModel = new OpenAiSdkChatModel(openAIClient, openAIClientAsync, chatProperties.getOptions(), + toolCallingManager, observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP), + openAiToolExecutionEligibilityPredicate.getIfUnique(DefaultToolExecutionEligibilityPredicate::new)); + + observationConvention.ifAvailable(chatModel::setObservationConvention); + + return chatModel; + } + + private OpenAIClient openAiClient(AbstractOpenAiSdkOptions resolved) { + + return OpenAiSdkSetup.setupSyncClient(resolved.getBaseUrl(), resolved.getApiKey(), resolved.getCredential(), + resolved.getAzureDeploymentName(), resolved.getAzureOpenAIServiceVersion(), + resolved.getOrganizationId(), resolved.isAzure(), resolved.isGitHubModels(), resolved.getModel(), + resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), resolved.getCustomHeaders()); + } + + private OpenAIClientAsync openAiClientAsync(AbstractOpenAiSdkOptions resolved) { + + return OpenAiSdkSetup.setupAsyncClient(resolved.getBaseUrl(), resolved.getApiKey(), resolved.getCredential(), + resolved.getAzureDeploymentName(), resolved.getAzureOpenAIServiceVersion(), + resolved.getOrganizationId(), resolved.isAzure(), resolved.isGitHubModels(), resolved.getModel(), + resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), resolved.getCustomHeaders()); + } + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatProperties.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatProperties.java new file mode 100644 index 00000000000..0fcf3b0bda4 --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatProperties.java @@ -0,0 +1,48 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import org.springframework.ai.openaisdk.AbstractOpenAiSdkOptions; +import org.springframework.ai.openaisdk.OpenAiSdkChatOptions; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.NestedConfigurationProperty; + +/** + * OpenAI SDK Chat autoconfiguration properties. + * + * @author Christian Tzolov + */ +@ConfigurationProperties(OpenAiSdkChatProperties.CONFIG_PREFIX) +public class OpenAiSdkChatProperties extends AbstractOpenAiSdkOptions { + + public static final String CONFIG_PREFIX = "spring.ai.openai-sdk.chat"; + + public static final String DEFAULT_CHAT_MODEL = OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL; + + private static final Double DEFAULT_TEMPERATURE = 0.7; + + @NestedConfigurationProperty + private final OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder() + .model(DEFAULT_CHAT_MODEL) + .temperature(DEFAULT_TEMPERATURE) + .build(); + + public OpenAiSdkChatOptions getOptions() { + return this.options; + } + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkConnectionProperties.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkConnectionProperties.java new file mode 100644 index 00000000000..2c5c3b54bae --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkConnectionProperties.java @@ -0,0 +1,27 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import org.springframework.ai.openaisdk.AbstractOpenAiSdkOptions; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(OpenAiSdkConnectionProperties.CONFIG_PREFIX) +public class OpenAiSdkConnectionProperties extends AbstractOpenAiSdkOptions { + + public static final String CONFIG_PREFIX = "spring.ai.openai-sdk"; + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfiguration.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfiguration.java new file mode 100644 index 00000000000..31184d3ac39 --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfiguration.java @@ -0,0 +1,72 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import com.openai.client.OpenAIClient; +import io.micrometer.observation.ObservationRegistry; + +import org.springframework.ai.embedding.observation.EmbeddingModelObservationConvention; +import org.springframework.ai.model.SpringAIModelProperties; +import org.springframework.ai.model.SpringAIModels; +import org.springframework.ai.openaisdk.OpenAiSdkEmbeddingModel; +import org.springframework.ai.openaisdk.setup.OpenAiSdkSetup; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; + +/** + * Embedding {@link AutoConfiguration Auto-configuration} for OpenAI SDK. + * + * @author Christian Tzolov + */ +@AutoConfiguration +@ConditionalOnProperty(name = SpringAIModelProperties.EMBEDDING_MODEL, havingValue = SpringAIModels.OPENAI_SDK, + matchIfMissing = true) +@EnableConfigurationProperties({ OpenAiSdkConnectionProperties.class, OpenAiSdkEmbeddingProperties.class }) +public class OpenAiSdkEmbeddingAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public OpenAiSdkEmbeddingModel openAiEmbeddingModel(OpenAiSdkConnectionProperties commonProperties, + OpenAiSdkEmbeddingProperties embeddingProperties, ObjectProvider observationRegistry, + ObjectProvider observationConvention) { + + var embeddingModel = new OpenAiSdkEmbeddingModel(this.openAiClient(commonProperties, embeddingProperties), + embeddingProperties.getMetadataMode(), embeddingProperties.getOptions(), + observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP)); + + observationConvention.ifAvailable(embeddingModel::setObservationConvention); + + return embeddingModel; + } + + private OpenAIClient openAiClient(OpenAiSdkConnectionProperties commonProperties, + OpenAiSdkEmbeddingProperties embeddingProperties) { + + OpenAiSdkAutoConfigurationUtil.ResolvedConnectionProperties resolved = OpenAiSdkAutoConfigurationUtil + .resolveConnectionProperties(commonProperties, embeddingProperties); + + return OpenAiSdkSetup.setupSyncClient(resolved.getBaseUrl(), resolved.getApiKey(), resolved.getCredential(), + resolved.getAzureDeploymentName(), resolved.getAzureOpenAIServiceVersion(), + resolved.getOrganizationId(), resolved.isAzure(), resolved.isGitHubModels(), resolved.getModel(), + resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), resolved.getCustomHeaders()); + } + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingProperties.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingProperties.java new file mode 100644 index 00000000000..ee3a648d3ca --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingProperties.java @@ -0,0 +1,51 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import org.springframework.ai.document.MetadataMode; +import org.springframework.ai.openaisdk.AbstractOpenAiSdkOptions; +import org.springframework.ai.openaisdk.OpenAiSdkEmbeddingOptions; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.NestedConfigurationProperty; + +@ConfigurationProperties(OpenAiSdkEmbeddingProperties.CONFIG_PREFIX) +public class OpenAiSdkEmbeddingProperties extends AbstractOpenAiSdkOptions { + + public static final String CONFIG_PREFIX = "spring.ai.openai-sdk.embedding"; + + public static final String DEFAULT_EMBEDDING_MODEL = OpenAiSdkEmbeddingOptions.DEFAULT_EMBEDDING_MODEL; + + private MetadataMode metadataMode = MetadataMode.EMBED; + + @NestedConfigurationProperty + private final OpenAiSdkEmbeddingOptions options = OpenAiSdkEmbeddingOptions.builder() + .model(DEFAULT_EMBEDDING_MODEL) + .build(); + + public OpenAiSdkEmbeddingOptions getOptions() { + return this.options; + } + + public MetadataMode getMetadataMode() { + return this.metadataMode; + } + + public void setMetadataMode(MetadataMode metadataMode) { + this.metadataMode = metadataMode; + } + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfiguration.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfiguration.java new file mode 100644 index 00000000000..e25d9a3528f --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfiguration.java @@ -0,0 +1,71 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import com.openai.client.OpenAIClient; +import io.micrometer.observation.ObservationRegistry; + +import org.springframework.ai.image.observation.ImageModelObservationConvention; +import org.springframework.ai.model.SpringAIModelProperties; +import org.springframework.ai.model.SpringAIModels; +import org.springframework.ai.openaisdk.OpenAiSdkImageModel; +import org.springframework.ai.openaisdk.setup.OpenAiSdkSetup; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; + +/** + * Image {@link AutoConfiguration Auto-configuration} for OpenAI. + * + * @author Christian Tzolov + */ +@AutoConfiguration +@ConditionalOnProperty(name = SpringAIModelProperties.IMAGE_MODEL, havingValue = SpringAIModels.OPENAI_SDK, + matchIfMissing = true) +@EnableConfigurationProperties({ OpenAiSdkConnectionProperties.class, OpenAiSdkImageProperties.class }) +public class OpenAiSdkImageAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public OpenAiSdkImageModel openAiImageModel(OpenAiSdkConnectionProperties commonProperties, + OpenAiSdkImageProperties imageProperties, ObjectProvider observationRegistry, + ObjectProvider observationConvention) { + + var imageModel = new OpenAiSdkImageModel(openAiClient(commonProperties, imageProperties), + imageProperties.getOptions(), observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP)); + + observationConvention.ifAvailable(imageModel::setObservationConvention); + + return imageModel; + } + + private OpenAIClient openAiClient(OpenAiSdkConnectionProperties commonProperties, + OpenAiSdkImageProperties imageProperties) { + + OpenAiSdkAutoConfigurationUtil.ResolvedConnectionProperties resolved = OpenAiSdkAutoConfigurationUtil + .resolveConnectionProperties(commonProperties, imageProperties); + + return OpenAiSdkSetup.setupSyncClient(resolved.getBaseUrl(), resolved.getApiKey(), resolved.getCredential(), + resolved.getAzureDeploymentName(), resolved.getAzureOpenAIServiceVersion(), + resolved.getOrganizationId(), resolved.isAzure(), resolved.isGitHubModels(), resolved.getModel(), + resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), resolved.getCustomHeaders()); + } + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageProperties.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageProperties.java new file mode 100644 index 00000000000..7323de414c5 --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageProperties.java @@ -0,0 +1,48 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import com.openai.models.images.ImageModel; + +import org.springframework.ai.openaisdk.AbstractOpenAiSdkOptions; +import org.springframework.ai.openaisdk.OpenAiSdkImageOptions; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.NestedConfigurationProperty; + +/** + * OpenAI SDK Image autoconfiguration properties. + * + * @author Christian Tzolov + */ +@ConfigurationProperties(OpenAiSdkImageProperties.CONFIG_PREFIX) +public class OpenAiSdkImageProperties extends AbstractOpenAiSdkOptions { + + public static final String CONFIG_PREFIX = "spring.ai.openai-sdk.image"; + + public static final String DEFAULT_IMAGE_MODEL = ImageModel.DALL_E_3.toString(); + + /** + * Options for OpenAI Sdk Image API. + */ + @NestedConfigurationProperty + private final OpenAiSdkImageOptions options = OpenAiSdkImageOptions.builder().model(DEFAULT_IMAGE_MODEL).build(); + + public OpenAiSdkImageOptions getOptions() { + return this.options; + } + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/package-info.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/package-info.java new file mode 100644 index 00000000000..71b622244c1 --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/package-info.java @@ -0,0 +1,17 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000000..78529ac9555 --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1,19 @@ +# +# Copyright 2025-2025 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +org.springframework.ai.model.openaisdk.autoconfigure.OpenAiSdkChatAutoConfiguration +org.springframework.ai.model.openaisdk.autoconfigure.OpenAiSdkEmbeddingAutoConfiguration +org.springframework.ai.model.openaisdk.autoconfigure.OpenAiSdkImageAutoConfiguration + diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/ChatClientAutoConfigurationIT.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/ChatClientAutoConfigurationIT.java new file mode 100644 index 00000000000..7cf2ab27f71 --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/ChatClientAutoConfigurationIT.java @@ -0,0 +1,119 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; + +import org.springframework.ai.chat.client.ChatClient; +import org.springframework.ai.chat.client.ChatClientCustomizer; +import org.springframework.ai.model.chat.client.autoconfigure.ChatClientAutoConfiguration; +import org.springframework.ai.utils.SpringAiTestAutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Christian Tzolov + */ +@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".*") +public class ChatClientAutoConfigurationIT { + + private static final Log logger = LogFactory.getLog(ChatClientAutoConfigurationIT.class); + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withPropertyValues("spring.ai.openai-sdk.apiKey=" + System.getenv("OPENAI_API_KEY"), + "spring.ai.openai-sdk.chat.options.model=gpt-4o") + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkChatAutoConfiguration.class, + ChatClientAutoConfiguration.class)); + + @Test + void implicitlyEnabled() { + this.contextRunner.run(context -> assertThat(context.getBeansOfType(ChatClient.Builder.class)).isNotEmpty()); + } + + @Test + void explicitlyEnabled() { + this.contextRunner.withPropertyValues("spring.ai.chat.client.enabled=true") + .run(context -> assertThat(context.getBeansOfType(ChatClient.Builder.class)).isNotEmpty()); + } + + @Test + void explicitlyDisabled() { + this.contextRunner.withPropertyValues("spring.ai.chat.client.enabled=false") + .run(context -> assertThat(context.getBeansOfType(ChatClient.Builder.class)).isEmpty()); + } + + @Test + void generate() { + this.contextRunner.run(context -> { + ChatClient.Builder builder = context.getBean(ChatClient.Builder.class); + + assertThat(builder).isNotNull(); + + ChatClient chatClient = builder.build(); + + String response = chatClient.prompt().user("Hello").call().content(); + + assertThat(response).isNotEmpty(); + logger.info("Response: " + response); + }); + } + + @Test + void testChatClientCustomizers() { + this.contextRunner.withUserConfiguration(Config.class).run(context -> { + + ChatClient.Builder builder = context.getBean(ChatClient.Builder.class); + + ChatClient chatClient = builder.build(); + + assertThat(chatClient).isNotNull(); + + ActorsFilms actorsFilms = chatClient.prompt() + .user(u -> u.param("actor", "Tom Hanks")) + .call() + .entity(ActorsFilms.class); + + logger.info("" + actorsFilms); + assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks"); + assertThat(actorsFilms.movies()).hasSize(5); + }); + } + + record ActorsFilms(String actor, List movies) { + + } + + @Configuration + static class Config { + + @Bean + public ChatClientCustomizer chatClientCustomizer() { + return b -> b.defaultSystem("You are a movie expert.") + .defaultUser("Generate the filmography of 5 movies for {actor}."); + } + + } + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/MockWeatherService.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/MockWeatherService.java new file mode 100644 index 00000000000..ee804783417 --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/MockWeatherService.java @@ -0,0 +1,97 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import java.util.function.Function; + +import com.fasterxml.jackson.annotation.JsonClassDescription; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyDescription; + +/** + * Mock 3rd party weather service. + * + * @author Christian Tzolov + */ +public class MockWeatherService implements Function { + + @Override + public Response apply(Request request) { + + double temperature = 10; + if (request.location().contains("Paris")) { + temperature = 15; + } + else if (request.location().contains("Tokyo")) { + temperature = 10; + } + else if (request.location().contains("San Francisco")) { + temperature = 30; + } + + return new Response(temperature, 15, 20, 2, 53, 45, Unit.C); + } + + /** + * Temperature units. + */ + public enum Unit { + + /** + * Celsius. + */ + C("metric"), + /** + * Fahrenheit. + */ + F("imperial"); + + /** + * Human readable unit name. + */ + public final String unitName; + + Unit(String text) { + this.unitName = text; + } + + } + + /** + * Weather Function request. + */ + @JsonInclude(Include.NON_NULL) + @JsonClassDescription("Weather API request") + public record Request(@JsonProperty(required = true, + value = "location") @JsonPropertyDescription("The city and state e.g. San Francisco, CA") String location, + @JsonProperty(required = true, value = "lat") @JsonPropertyDescription("The city latitude") double lat, + @JsonProperty(required = true, value = "lon") @JsonPropertyDescription("The city longitude") double lon, + @JsonProperty(required = true, value = "unit") @JsonPropertyDescription("Temperature unit") Unit unit) { + + } + + /** + * Weather Function response. + */ + public record Response(double temp, double feels_like, double temp_min, double temp_max, int pressure, int humidity, + Unit unit) { + + } + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiFunctionCallback2IT.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiFunctionCallback2IT.java new file mode 100644 index 00000000000..271ed574eb5 --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiFunctionCallback2IT.java @@ -0,0 +1,104 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import java.util.stream.Collectors; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.ai.chat.client.ChatClient; +import org.springframework.ai.openaisdk.OpenAiSdkChatModel; +import org.springframework.ai.tool.ToolCallback; +import org.springframework.ai.tool.function.FunctionToolCallback; +import org.springframework.ai.utils.SpringAiTestAutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static org.assertj.core.api.Assertions.assertThat; + +@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".*") +public class OpenAiFunctionCallback2IT { + + private final Logger logger = LoggerFactory.getLogger(OpenAiFunctionCallback2IT.class); + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withPropertyValues("spring.ai.openai-sdk.apiKey=" + System.getenv("OPENAI_API_KEY")) + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkChatAutoConfiguration.class)) + .withUserConfiguration(Config.class); + + @Test + void functionCallTest() { + this.contextRunner.withPropertyValues("spring.ai.openai-sdk.chat.options.temperature=0.1").run(context -> { + + OpenAiSdkChatModel chatModel = context.getBean(OpenAiSdkChatModel.class); + + // @formatter:off + ChatClient chatClient = ChatClient.builder(chatModel) + .defaultToolNames("WeatherInfo") + .defaultUser(u -> u.text("What's the weather like in {cities}?")) + .build(); + + String content = chatClient.prompt() + .user(u -> u.param("cities", "San Francisco, Tokyo, Paris")) + .call().content(); + // @formatter:on + + logger.info("Response: {}", content); + + assertThat(content).contains("30", "10", "15"); + }); + } + + @Test + void streamFunctionCallTest() { + this.contextRunner.withPropertyValues("spring.ai.openai-sdk.chat.options.temperature=0.1").run(context -> { + + OpenAiSdkChatModel chatModel = context.getBean(OpenAiSdkChatModel.class); + + // @formatter:off + String content = ChatClient.builder(chatModel).build().prompt() + .toolNames("WeatherInfo") + .user("What's the weather like in San Francisco, Tokyo, and Paris?") + .stream().content() + .collectList().block().stream().collect(Collectors.joining()); + // @formatter:on + + logger.info("Response: {}", content); + + assertThat(content).contains("30", "10", "15"); + }); + } + + @Configuration + static class Config { + + @Bean + public ToolCallback weatherFunctionInfo() { + + return FunctionToolCallback.builder("WeatherInfo", new MockWeatherService()) + .description("Get the weather in location") + .inputType(MockWeatherService.Request.class) + .build(); + } + + } + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfigurationIT.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfigurationIT.java new file mode 100644 index 00000000000..57f73a806a3 --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfigurationIT.java @@ -0,0 +1,129 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import java.util.stream.Collectors; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import reactor.core.publisher.Flux; + +import org.springframework.ai.chat.messages.UserMessage; +import org.springframework.ai.chat.metadata.Usage; +import org.springframework.ai.chat.model.ChatResponse; +import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.ai.openaisdk.OpenAiSdkChatModel; +import org.springframework.ai.utils.SpringAiTestAutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".*") +public class OpenAiSdkChatAutoConfigurationIT { + + private static final Log logger = LogFactory.getLog(OpenAiSdkChatAutoConfigurationIT.class); + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withPropertyValues("spring.ai.openai-sdk.apiKey=" + System.getenv("OPENAI_API_KEY")); + + @Test + void chatCall() { + this.contextRunner.withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkChatAutoConfiguration.class)) + .run(context -> { + OpenAiSdkChatModel chatModel = context.getBean(OpenAiSdkChatModel.class); + String response = chatModel.call("Hello"); + assertThat(response).isNotEmpty(); + logger.info("Response: " + response); + }); + } + + @Test + void generateStreaming() { + this.contextRunner.withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkChatAutoConfiguration.class)) + .run(context -> { + OpenAiSdkChatModel chatModel = context.getBean(OpenAiSdkChatModel.class); + Flux responseFlux = chatModel.stream(new Prompt(new UserMessage("Hello"))); + String response = responseFlux.collectList() + .block() + .stream() + .map(chatResponse -> chatResponse.getResult() != null + ? chatResponse.getResult().getOutput().getText() : "") + .collect(Collectors.joining()); + + assertThat(response).isNotEmpty(); + logger.info("Response: " + response); + }); + } + + @Test + void streamingWithTokenUsage() { + this.contextRunner.withPropertyValues("spring.ai.openai-sdk.chat.options.stream-usage=true") + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkChatAutoConfiguration.class)) + .run(context -> { + OpenAiSdkChatModel chatModel = context.getBean(OpenAiSdkChatModel.class); + + Flux responseFlux = chatModel.stream(new Prompt(new UserMessage("Hello"))); + + Usage[] streamingTokenUsage = new Usage[1]; + String response = responseFlux.collectList().block().stream().map(chatResponse -> { + streamingTokenUsage[0] = chatResponse.getMetadata().getUsage(); + return (chatResponse.getResult() != null) ? chatResponse.getResult().getOutput().getText() : ""; + }).collect(Collectors.joining()); + + assertThat(streamingTokenUsage[0].getPromptTokens()).isGreaterThan(0); + assertThat(streamingTokenUsage[0].getCompletionTokens()).isGreaterThan(0); + assertThat(streamingTokenUsage[0].getTotalTokens()).isGreaterThan(0); + + assertThat(response).isNotEmpty(); + logger.info("Response: " + response); + }); + } + + @Test + void chatActivation() { + this.contextRunner + .withPropertyValues("spring.ai.openai-sdk.api-key=API_KEY", "spring.ai.openai-sdk.base-url=TEST_BASE_URL", + "spring.ai.model.chat=none") + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkChatAutoConfiguration.class)) + .run(context -> { + assertThat(context.getBeansOfType(OpenAiSdkChatProperties.class)).isEmpty(); + assertThat(context.getBeansOfType(OpenAiSdkChatModel.class)).isEmpty(); + }); + + this.contextRunner + .withPropertyValues("spring.ai.openai-sdk.api-key=API_KEY", + "spring.ai.openai-sdk.base-url=http://test.base.url") + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkChatAutoConfiguration.class)) + .run(context -> { + assertThat(context.getBeansOfType(OpenAiSdkChatProperties.class)).isNotEmpty(); + assertThat(context.getBeansOfType(OpenAiSdkChatModel.class)).isNotEmpty(); + }); + + this.contextRunner + .withPropertyValues("spring.ai.openai-sdk.api-key=API_KEY", + "spring.ai.openai-sdk.base-url=http://test.base.url", "spring.ai.model.chat=openai-sdk") + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkChatAutoConfiguration.class)) + .run(context -> { + assertThat(context.getBeansOfType(OpenAiSdkChatProperties.class)).isNotEmpty(); + assertThat(context.getBeansOfType(OpenAiSdkChatModel.class)).isNotEmpty(); + }); + + } + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatPropertiesTests.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatPropertiesTests.java new file mode 100644 index 00000000000..bcf79a15c39 --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatPropertiesTests.java @@ -0,0 +1,149 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import org.junit.jupiter.api.Test; +import org.skyscreamer.jsonassert.JSONAssert; +import org.skyscreamer.jsonassert.JSONCompareMode; + +import org.springframework.ai.utils.SpringAiTestAutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit Tests for {@link OpenAiConnectionProperties}, {@link OpenAiSdkChatProperties} and + * {@link OpenAiSdkEmbeddingProperties}. + * + * @author Christian Tzolov + */ +public class OpenAiSdkChatPropertiesTests { + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner(); + + @Test + public void chatProperties() { + + this.contextRunner.withPropertyValues( + // @formatter:off + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL", + "spring.ai.openai-sdk.api-key=abc123", + "spring.ai.openai-sdk.chat.options.model=MODEL_XYZ", + "spring.ai.openai-sdk.chat.options.temperature=0.55") + // @formatter:on + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkChatAutoConfiguration.class)) + .run(context -> { + var chatProperties = context.getBean(OpenAiSdkChatProperties.class); + var connectionProperties = context.getBean(OpenAiSdkConnectionProperties.class); + + assertThat(connectionProperties.getApiKey()).isEqualTo("abc123"); + assertThat(connectionProperties.getBaseUrl()).isEqualTo("http://TEST.BASE.URL"); + + assertThat(chatProperties.getApiKey()).isNull(); + assertThat(chatProperties.getBaseUrl()).isNull(); + + assertThat(chatProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ"); + assertThat(chatProperties.getOptions().getTemperature()).isEqualTo(0.55); + }); + } + + @Test + public void chatOverrideConnectionProperties() { + + this.contextRunner.withPropertyValues( + // @formatter:off + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL", + "spring.ai.openai-sdk.api-key=abc123", + "spring.ai.openai-sdk.chat.base-url=http://TEST.BASE.URL2", + "spring.ai.openai-sdk.chat.api-key=456", + "spring.ai.openai-sdk.chat.options.model=MODEL_XYZ", + "spring.ai.openai-sdk.chat.options.temperature=0.55") + // @formatter:on + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkChatAutoConfiguration.class)) + .run(context -> { + var chatProperties = context.getBean(OpenAiSdkChatProperties.class); + var connectionProperties = context.getBean(OpenAiSdkConnectionProperties.class); + + assertThat(connectionProperties.getApiKey()).isEqualTo("abc123"); + assertThat(connectionProperties.getBaseUrl()).isEqualTo("http://TEST.BASE.URL"); + + assertThat(chatProperties.getApiKey()).isEqualTo("456"); + assertThat(chatProperties.getBaseUrl()).isEqualTo("http://TEST.BASE.URL2"); + + assertThat(chatProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ"); + assertThat(chatProperties.getOptions().getTemperature()).isEqualTo(0.55); + }); + } + + @Test + public void chatOptionsTest() { + + this.contextRunner + .withPropertyValues(// @formatter:off + "spring.ai.openai-sdk.api-key=API_KEY", + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL", + + "spring.ai.openai-sdk.chat.options.model=MODEL_XYZ", + "spring.ai.openai-sdk.chat.options.frequencyPenalty=-1.5", + "spring.ai.openai-sdk.chat.options.logitBias.myTokenId=-5", + "spring.ai.openai-sdk.chat.options.maxTokens=123", + "spring.ai.openai-sdk.chat.options.n=10", + "spring.ai.openai-sdk.chat.options.presencePenalty=0", + "spring.ai.openai-sdk.chat.options.seed=66", + "spring.ai.openai-sdk.chat.options.stop=boza,koza", + "spring.ai.openai-sdk.chat.options.temperature=0.55", + "spring.ai.openai-sdk.chat.options.topP=0.56", + "spring.ai.openai-sdk.chat.options.user=userXYZ", + "spring.ai.openai-sdk.chat.options.toolChoice={\"type\":\"function\",\"function\":{\"name\":\"toolChoiceFunctionName\"}}", + "spring.ai.openai-sdk.chat.options.streamOptions.includeUsage=true", + "spring.ai.openai-sdk.chat.options.streamOptions.includeObfuscation=true", + "spring.ai.openai-sdk.chat.options.streamOptions.additionalProperties.foo=bar" + + ) + // @formatter:on + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkChatAutoConfiguration.class)) + .run(context -> { + var chatProperties = context.getBean(OpenAiSdkChatProperties.class); + var connectionProperties = context.getBean(OpenAiSdkConnectionProperties.class); + + assertThat(connectionProperties.getBaseUrl()).isEqualTo("http://TEST.BASE.URL"); + assertThat(connectionProperties.getApiKey()).isEqualTo("API_KEY"); + + assertThat(chatProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ"); + assertThat(chatProperties.getOptions().getFrequencyPenalty()).isEqualTo(-1.5); + assertThat(chatProperties.getOptions().getLogitBias().get("myTokenId")).isEqualTo(-5); + assertThat(chatProperties.getOptions().getMaxTokens()).isEqualTo(123); + assertThat(chatProperties.getOptions().getN()).isEqualTo(10); + assertThat(chatProperties.getOptions().getPresencePenalty()).isEqualTo(0); + assertThat(chatProperties.getOptions().getSeed()).isEqualTo(66); + assertThat(chatProperties.getOptions().getStop()).contains("boza", "koza"); + assertThat(chatProperties.getOptions().getTemperature()).isEqualTo(0.55); + assertThat(chatProperties.getOptions().getTopP()).isEqualTo(0.56); + + JSONAssert.assertEquals("{\"type\":\"function\",\"function\":{\"name\":\"toolChoiceFunctionName\"}}", + "" + chatProperties.getOptions().getToolChoice(), JSONCompareMode.LENIENT); + + assertThat(chatProperties.getOptions().getUser()).isEqualTo("userXYZ"); + + assertThat(chatProperties.getOptions().getStreamOptions()).isNotNull(); + assertThat(chatProperties.getOptions().getStreamOptions().includeObfuscation()).isTrue(); + assertThat(chatProperties.getOptions().getStreamOptions().additionalProperties().get("foo")) + .isEqualTo("bar"); + }); + } + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfigurationIT.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfigurationIT.java new file mode 100644 index 00000000000..46295a307ad --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfigurationIT.java @@ -0,0 +1,113 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import java.util.List; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; + +import org.springframework.ai.embedding.EmbeddingResponse; +import org.springframework.ai.openaisdk.OpenAiSdkEmbeddingModel; +import org.springframework.ai.utils.SpringAiTestAutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".*") +public class OpenAiSdkEmbeddingAutoConfigurationIT { + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withPropertyValues("spring.ai.openai-sdk.apiKey=" + System.getenv("OPENAI_API_KEY")); + + @Test + void embedding() { + this.contextRunner + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkEmbeddingAutoConfiguration.class)) + .run(context -> { + OpenAiSdkEmbeddingModel embeddingModel = context.getBean(OpenAiSdkEmbeddingModel.class); + + EmbeddingResponse embeddingResponse = embeddingModel + .embedForResponse(List.of("Hello World", "World is big and salvation is near")); + assertThat(embeddingResponse.getResults()).hasSize(2); + assertThat(embeddingResponse.getResults().get(0).getOutput()).isNotEmpty(); + assertThat(embeddingResponse.getResults().get(0).getIndex()).isEqualTo(0); + assertThat(embeddingResponse.getResults().get(1).getOutput()).isNotEmpty(); + assertThat(embeddingResponse.getResults().get(1).getIndex()).isEqualTo(1); + + assertThat(embeddingModel.dimensions()).isEqualTo(1536); + }); + } + + @Test + void embeddingActivation() { + + this.contextRunner + .withPropertyValues("spring.ai.openai-sdk.api-key=API_KEY", "spring.ai.openai-sdk.base-url=TEST_BASE_URL", + "spring.ai.model.embedding=none") + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkEmbeddingAutoConfiguration.class)) + .run(context -> { + assertThat(context.getBeansOfType(OpenAiSdkEmbeddingProperties.class)).isEmpty(); + assertThat(context.getBeansOfType(OpenAiSdkEmbeddingModel.class)).isEmpty(); + }); + + this.contextRunner + .withPropertyValues("spring.ai.openai-sdk.api-key=API_KEY", + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL") + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkEmbeddingAutoConfiguration.class)) + .run(context -> { + assertThat(context.getBeansOfType(OpenAiSdkEmbeddingProperties.class)).isNotEmpty(); + assertThat(context.getBeansOfType(OpenAiSdkEmbeddingModel.class)).isNotEmpty(); + }); + + this.contextRunner + .withPropertyValues("spring.ai.openai-sdk.api-key=API_KEY", + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL", "spring.ai.model.embedding=openai-sdk") + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkEmbeddingAutoConfiguration.class)) + .run(context -> { + assertThat(context.getBeansOfType(OpenAiSdkEmbeddingProperties.class)).isNotEmpty(); + assertThat(context.getBeansOfType(OpenAiSdkEmbeddingModel.class)).isNotEmpty(); + }); + } + + @Test + public void embeddingOptionsTest() { + + this.contextRunner.withPropertyValues( + // @formatter:off + "spring.ai.openai-sdk.api-key=API_KEY", + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL", + + "spring.ai.openai-sdk.embedding.options.model=MODEL_XYZ", + "spring.ai.openai-sdk.embedding.options.encodingFormat=MyEncodingFormat", + "spring.ai.openai-sdk.embedding.options.user=userXYZ" + ) + // @formatter:on + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkEmbeddingAutoConfiguration.class)) + .run(context -> { + var connectionProperties = context.getBean(OpenAiSdkConnectionProperties.class); + var embeddingProperties = context.getBean(OpenAiSdkEmbeddingProperties.class); + + assertThat(connectionProperties.getBaseUrl()).isEqualTo("http://TEST.BASE.URL"); + assertThat(connectionProperties.getApiKey()).isEqualTo("API_KEY"); + + assertThat(embeddingProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ"); + assertThat(embeddingProperties.getOptions().getUser()).isEqualTo("userXYZ"); + }); + } + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingPropertiesTests.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingPropertiesTests.java new file mode 100644 index 00000000000..ac807702f74 --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingPropertiesTests.java @@ -0,0 +1,120 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import org.junit.jupiter.api.Test; + +import org.springframework.ai.document.MetadataMode; +import org.springframework.ai.utils.SpringAiTestAutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit Tests for {@link OpenAiConnectionProperties} and + * {@link OpenAiSdkEmbeddingProperties}. + * + * @author Christian Tzolov + */ +public class OpenAiSdkEmbeddingPropertiesTests { + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner(); + + @Test + public void embeddingProperties() { + + this.contextRunner.withPropertyValues( + // @formatter:off + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL", + "spring.ai.openai-sdk.api-key=abc123", + "spring.ai.openai-sdk.embedding.options.model=MODEL_XYZ", + "spring.ai.openai-sdk.embedding.options.dimensions=512") + // @formatter:on + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkEmbeddingAutoConfiguration.class)) + .run(context -> { + var embeddingProperties = context.getBean(OpenAiSdkEmbeddingProperties.class); + var connectionProperties = context.getBean(OpenAiSdkConnectionProperties.class); + + assertThat(connectionProperties.getApiKey()).isEqualTo("abc123"); + assertThat(connectionProperties.getBaseUrl()).isEqualTo("http://TEST.BASE.URL"); + + assertThat(embeddingProperties.getApiKey()).isNull(); + assertThat(embeddingProperties.getBaseUrl()).isNull(); + + assertThat(embeddingProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ"); + assertThat(embeddingProperties.getOptions().getDimensions()).isEqualTo(512); + }); + } + + @Test + public void embeddingOverrideConnectionProperties() { + + this.contextRunner.withPropertyValues( + // @formatter:off + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL", + "spring.ai.openai-sdk.api-key=abc123", + "spring.ai.openai-sdk.embedding.base-url=http://TEST.BASE.URL2", + "spring.ai.openai-sdk.embedding.api-key=456", + "spring.ai.openai-sdk.embedding.options.model=MODEL_XYZ", + "spring.ai.openai-sdk.embedding.options.dimensions=512") + // @formatter:on + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkEmbeddingAutoConfiguration.class)) + .run(context -> { + var embeddingProperties = context.getBean(OpenAiSdkEmbeddingProperties.class); + var connectionProperties = context.getBean(OpenAiSdkConnectionProperties.class); + + assertThat(connectionProperties.getApiKey()).isEqualTo("abc123"); + assertThat(connectionProperties.getBaseUrl()).isEqualTo("http://TEST.BASE.URL"); + + assertThat(embeddingProperties.getApiKey()).isEqualTo("456"); + assertThat(embeddingProperties.getBaseUrl()).isEqualTo("http://TEST.BASE.URL2"); + + assertThat(embeddingProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ"); + assertThat(embeddingProperties.getOptions().getDimensions()).isEqualTo(512); + }); + } + + @Test + public void embeddingOptionsTest() { + + this.contextRunner + .withPropertyValues(// @formatter:off + "spring.ai.openai-sdk.api-key=API_KEY", + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL", + + "spring.ai.openai-sdk.embedding.options.model=MODEL_XYZ", + "spring.ai.openai-sdk.embedding.options.user=userXYZ", + "spring.ai.openai-sdk.embedding.options.dimensions=1024", + "spring.ai.openai-sdk.embedding.metadata-mode=NONE" + ) + // @formatter:on + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkEmbeddingAutoConfiguration.class)) + .run(context -> { + var embeddingProperties = context.getBean(OpenAiSdkEmbeddingProperties.class); + var connectionProperties = context.getBean(OpenAiSdkConnectionProperties.class); + + assertThat(connectionProperties.getBaseUrl()).isEqualTo("http://TEST.BASE.URL"); + assertThat(connectionProperties.getApiKey()).isEqualTo("API_KEY"); + + assertThat(embeddingProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ"); + assertThat(embeddingProperties.getOptions().getUser()).isEqualTo("userXYZ"); + assertThat(embeddingProperties.getOptions().getDimensions()).isEqualTo(1024); + assertThat(embeddingProperties.getMetadataMode()).isEqualTo(MetadataMode.NONE); + }); + } + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfigurationIT.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfigurationIT.java new file mode 100644 index 00000000000..bfd6c426fc4 --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfigurationIT.java @@ -0,0 +1,135 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; + +import org.springframework.ai.image.ImagePrompt; +import org.springframework.ai.image.ImageResponse; +import org.springframework.ai.openaisdk.OpenAiSdkImageModel; +import org.springframework.ai.utils.SpringAiTestAutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".*") +public class OpenAiSdkImageAutoConfigurationIT { + + private static final Log logger = LogFactory.getLog(OpenAiSdkImageAutoConfigurationIT.class); + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withPropertyValues("spring.ai.openai-sdk.apiKey=" + System.getenv("OPENAI_API_KEY")); + + @Test + void generateImage() { + this.contextRunner.withPropertyValues("spring.ai.openai-sdk.image.options.size=1024x1024") + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkImageAutoConfiguration.class)) + .run(context -> { + OpenAiSdkImageModel imageModel = context.getBean(OpenAiSdkImageModel.class); + ImageResponse imageResponse = imageModel.call(new ImagePrompt("forest")); + assertThat(imageResponse.getResults()).hasSize(1); + assertThat(imageResponse.getResult().getOutput().getUrl()).isNotEmpty(); + logger.info("Generated image: " + imageResponse.getResult().getOutput().getUrl()); + }); + } + + @Test + void generateImageWithModel() { + // The 256x256 size is supported by dall-e-2, but not by dall-e-3. + this.contextRunner + .withPropertyValues("spring.ai.openai-sdk.image.options.model=dall-e-2", + "spring.ai.openai-sdk.image.options.size=256x256") + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkImageAutoConfiguration.class)) + .run(context -> { + OpenAiSdkImageModel imageModel = context.getBean(OpenAiSdkImageModel.class); + ImageResponse imageResponse = imageModel.call(new ImagePrompt("forest")); + assertThat(imageResponse.getResults()).hasSize(1); + assertThat(imageResponse.getResult().getOutput().getUrl()).isNotEmpty(); + logger.info("Generated image: " + imageResponse.getResult().getOutput().getUrl()); + }); + } + + @Test + void imageActivation() { + this.contextRunner + .withPropertyValues("spring.ai.openai-sdk.api-key=API_KEY", + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL", "spring.ai.model.image=none") + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkImageAutoConfiguration.class)) + .run(context -> { + assertThat(context.getBeansOfType(OpenAiSdkImageProperties.class)).isEmpty(); + assertThat(context.getBeansOfType(OpenAiSdkImageModel.class)).isEmpty(); + }); + + this.contextRunner + .withPropertyValues("spring.ai.openai-sdk.api-key=API_KEY", + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL") + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkImageAutoConfiguration.class)) + .run(context -> { + assertThat(context.getBeansOfType(OpenAiSdkImageProperties.class)).isNotEmpty(); + assertThat(context.getBeansOfType(OpenAiSdkImageModel.class)).isNotEmpty(); + }); + + this.contextRunner + .withPropertyValues("spring.ai.openai-sdk.api-key=API_KEY", + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL", "spring.ai.model.image=openai-sdk") + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkImageAutoConfiguration.class)) + .run(context -> { + assertThat(context.getBeansOfType(OpenAiSdkImageProperties.class)).isNotEmpty(); + assertThat(context.getBeansOfType(OpenAiSdkImageModel.class)).isNotEmpty(); + }); + + } + + @Test + public void imageOptionsTest() { + this.contextRunner.withPropertyValues( + // @formatter:off + "spring.ai.openai-sdk.api-key=API_KEY", + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL", + "spring.ai.openai-sdk.image.options.n=3", + "spring.ai.openai-sdk.image.options.model=MODEL_XYZ", + "spring.ai.openai-sdk.image.options.quality=hd", + "spring.ai.openai-sdk.image.options.response_format=url", + "spring.ai.openai-sdk.image.options.size=1024x1024", + "spring.ai.openai-sdk.image.options.width=1024", + "spring.ai.openai-sdk.image.options.height=1024", + "spring.ai.openai-sdk.image.options.style=vivid", + "spring.ai.openai-sdk.image.options.user=userXYZ") // @formatter:on + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkImageAutoConfiguration.class)) + .run(context -> { + var imageProperties = context.getBean(OpenAiSdkImageProperties.class); + var connectionProperties = context.getBean(OpenAiSdkConnectionProperties.class); + + assertThat(connectionProperties.getBaseUrl()).isEqualTo("http://TEST.BASE.URL"); + assertThat(connectionProperties.getApiKey()).isEqualTo("API_KEY"); + + assertThat(imageProperties.getOptions().getN()).isEqualTo(3); + assertThat(imageProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ"); + assertThat(imageProperties.getOptions().getQuality()).isEqualTo("hd"); + assertThat(imageProperties.getOptions().getResponseFormat()).isEqualTo("url"); + assertThat(imageProperties.getOptions().getSize()).isEqualTo("1024x1024"); + assertThat(imageProperties.getOptions().getWidth()).isEqualTo(1024); + assertThat(imageProperties.getOptions().getHeight()).isEqualTo(1024); + assertThat(imageProperties.getOptions().getStyle()).isEqualTo("vivid"); + assertThat(imageProperties.getOptions().getUser()).isEqualTo("userXYZ"); + }); + } + +} diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImagePropertiesTests.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImagePropertiesTests.java new file mode 100644 index 00000000000..60ddf53fe32 --- /dev/null +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImagePropertiesTests.java @@ -0,0 +1,128 @@ +/* + * Copyright 2025-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.model.openaisdk.autoconfigure; + +import org.junit.jupiter.api.Test; + +import org.springframework.ai.utils.SpringAiTestAutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit Tests for {@link OpenAiConnectionProperties} and {@link OpenAiSdkImageProperties}. + * + * @author Christian Tzolov + */ +public class OpenAiSdkImagePropertiesTests { + + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner(); + + @Test + public void imageProperties() { + + this.contextRunner.withPropertyValues( + // @formatter:off + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL", + "spring.ai.openai-sdk.api-key=abc123", + "spring.ai.openai-sdk.image.options.model=MODEL_XYZ", + "spring.ai.openai-sdk.image.options.n=2") + // @formatter:on + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkImageAutoConfiguration.class)) + .run(context -> { + var imageProperties = context.getBean(OpenAiSdkImageProperties.class); + var connectionProperties = context.getBean(OpenAiSdkConnectionProperties.class); + + assertThat(connectionProperties.getApiKey()).isEqualTo("abc123"); + assertThat(connectionProperties.getBaseUrl()).isEqualTo("http://TEST.BASE.URL"); + + assertThat(imageProperties.getApiKey()).isNull(); + assertThat(imageProperties.getBaseUrl()).isNull(); + + assertThat(imageProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ"); + assertThat(imageProperties.getOptions().getN()).isEqualTo(2); + }); + } + + @Test + public void imageOverrideConnectionProperties() { + + this.contextRunner.withPropertyValues( + // @formatter:off + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL", + "spring.ai.openai-sdk.api-key=abc123", + "spring.ai.openai-sdk.image.base-url=http://TEST.BASE.URL2", + "spring.ai.openai-sdk.image.api-key=456", + "spring.ai.openai-sdk.image.options.model=MODEL_XYZ", + "spring.ai.openai-sdk.image.options.n=2") + // @formatter:on + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkImageAutoConfiguration.class)) + .run(context -> { + var imageProperties = context.getBean(OpenAiSdkImageProperties.class); + var connectionProperties = context.getBean(OpenAiSdkConnectionProperties.class); + + assertThat(connectionProperties.getApiKey()).isEqualTo("abc123"); + assertThat(connectionProperties.getBaseUrl()).isEqualTo("http://TEST.BASE.URL"); + + assertThat(imageProperties.getApiKey()).isEqualTo("456"); + assertThat(imageProperties.getBaseUrl()).isEqualTo("http://TEST.BASE.URL2"); + + assertThat(imageProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ"); + assertThat(imageProperties.getOptions().getN()).isEqualTo(2); + }); + } + + @Test + public void imageOptionsTest() { + + this.contextRunner + .withPropertyValues(// @formatter:off + "spring.ai.openai-sdk.api-key=API_KEY", + "spring.ai.openai-sdk.base-url=http://TEST.BASE.URL", + + "spring.ai.openai-sdk.image.options.model=MODEL_XYZ", + "spring.ai.openai-sdk.image.options.n=3", + "spring.ai.openai-sdk.image.options.width=1024", + "spring.ai.openai-sdk.image.options.height=1792", + "spring.ai.openai-sdk.image.options.quality=hd", + "spring.ai.openai-sdk.image.options.responseFormat=url", + "spring.ai.openai-sdk.image.options.size=1024x1792", + "spring.ai.openai-sdk.image.options.style=vivid", + "spring.ai.openai-sdk.image.options.user=userXYZ" + ) + // @formatter:on + .withConfiguration(SpringAiTestAutoConfigurations.of(OpenAiSdkImageAutoConfiguration.class)) + .run(context -> { + var imageProperties = context.getBean(OpenAiSdkImageProperties.class); + var connectionProperties = context.getBean(OpenAiSdkConnectionProperties.class); + + assertThat(connectionProperties.getBaseUrl()).isEqualTo("http://TEST.BASE.URL"); + assertThat(connectionProperties.getApiKey()).isEqualTo("API_KEY"); + + assertThat(imageProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ"); + assertThat(imageProperties.getOptions().getN()).isEqualTo(3); + assertThat(imageProperties.getOptions().getWidth()).isEqualTo(1024); + assertThat(imageProperties.getOptions().getHeight()).isEqualTo(1792); + assertThat(imageProperties.getOptions().getQuality()).isEqualTo("hd"); + assertThat(imageProperties.getOptions().getResponseFormat()).isEqualTo("url"); + assertThat(imageProperties.getOptions().getSize()).isEqualTo("1024x1792"); + assertThat(imageProperties.getOptions().getStyle()).isEqualTo("vivid"); + assertThat(imageProperties.getOptions().getUser()).isEqualTo("userXYZ"); + }); + } + +} diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java index bab050885fe..dc65abe8dfa 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java @@ -26,6 +26,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; +import com.fasterxml.jackson.databind.JsonNode; import com.openai.client.OpenAIClient; import com.openai.client.OpenAIClientAsync; import com.openai.core.JsonValue; @@ -48,8 +49,10 @@ import com.openai.models.chat.completions.ChatCompletionMessageFunctionToolCall; import com.openai.models.chat.completions.ChatCompletionMessageParam; import com.openai.models.chat.completions.ChatCompletionMessageToolCall; +import com.openai.models.chat.completions.ChatCompletionNamedToolChoice; import com.openai.models.chat.completions.ChatCompletionStreamOptions; import com.openai.models.chat.completions.ChatCompletionTool; +import com.openai.models.chat.completions.ChatCompletionToolChoiceOption; import com.openai.models.chat.completions.ChatCompletionToolMessageParam; import com.openai.models.chat.completions.ChatCompletionUserMessageParam; import com.openai.models.completions.CompletionUsage; @@ -98,6 +101,7 @@ * Chat Model implementation using the OpenAI Java SDK. * * @author Julien Dubois + * @author Christian Tzolov */ public class OpenAiSdkChatModel implements ChatModel { @@ -1018,12 +1022,20 @@ else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_SCHEMA)) { if (requestOptions.getStreamOptions() != null) { ChatCompletionStreamOptions.Builder streamOptionsBuilder = ChatCompletionStreamOptions.builder(); - if (requestOptions.getStreamOptions().includeObfuscation().isPresent()) { - streamOptionsBuilder - .includeObfuscation(requestOptions.getStreamOptions().includeObfuscation().get()); + var ops = requestOptions.getStreamOptions(); + + streamOptionsBuilder.includeObfuscation(ops.includeObfuscation() != null && ops.includeObfuscation()); + streamOptionsBuilder.includeUsage(ops.includeUsage() != null && ops.includeUsage()); + + if (!CollectionUtils.isEmpty(ops.additionalProperties())) { + Map nativeParams = ops.additionalProperties() + .entrySet() + .stream() + .map(e -> Map.entry(e.getKey(), com.openai.core.JsonValue.from(e.getValue()))) + .collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll); + + streamOptionsBuilder.putAllAdditionalProperties(nativeParams); } - streamOptionsBuilder.additionalProperties(requestOptions.getStreamOptions()._additionalProperties()); - streamOptionsBuilder.includeUsage(requestOptions.getStreamUsage()); builder.streamOptions(streamOptionsBuilder.build()); } else { @@ -1040,12 +1052,50 @@ else if (responseFormat.getType().equals(ResponseFormat.Type.JSON_SCHEMA)) { } if (requestOptions.getToolChoice() != null) { - builder.toolChoice(requestOptions.getToolChoice()); + if (requestOptions.getToolChoice() instanceof ChatCompletionToolChoiceOption toolChoiceOption) { + builder.toolChoice(toolChoiceOption); + } + else if (requestOptions.getToolChoice() instanceof String json) { + try { + var node = ModelOptionsUtils.OBJECT_MAPPER.readTree(json); + builder.toolChoice(parseToolChoice(node)); + } + catch (Exception e) { + throw new IllegalArgumentException("Failed to parse toolChoice JSON: " + json, e); + } + } } return builder.build(); } + public static ChatCompletionToolChoiceOption parseToolChoice(JsonNode node) { + String type = node.get("type").asText(); + switch (type) { + case "function": + String functionName = node.get("function").get("name").asText(); + ChatCompletionNamedToolChoice.Function func = ChatCompletionNamedToolChoice.Function.builder() + .name(functionName) + .build(); + ChatCompletionNamedToolChoice named = ChatCompletionNamedToolChoice.builder().function(func).build(); + return ChatCompletionToolChoiceOption.ofNamedToolChoice(named); + case "auto": + // There is a built-in “auto” option — but how to get it depends on SDK + // version + return ChatCompletionToolChoiceOption.ofAuto(ChatCompletionToolChoiceOption.Auto.AUTO); + case "required": + // There may or may not be a 'required' option; if SDK supports, you need + // a way to construct it + // If it's not supported, you must use JSON fallback + throw new UnsupportedOperationException("SDK version does not support typed 'required' toolChoice"); + case "none": + // Similarly for none + throw new UnsupportedOperationException("SDK version does not support typed 'none' toolChoice"); + default: + throw new IllegalArgumentException("Unknown tool_choice type: " + type); + } + } + private String fromAudioData(Object audioData) { if (audioData instanceof byte[] bytes) { return Base64.getEncoder().encodeToString(bytes); diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java index 927262bc67c..c0451f4273b 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java @@ -26,10 +26,7 @@ import java.util.Set; import com.openai.models.ChatModel; -import com.openai.models.FunctionDefinition; import com.openai.models.chat.completions.ChatCompletionAudioParam; -import com.openai.models.chat.completions.ChatCompletionToolChoiceOption; -import com.openai.models.responses.ResponseCreateParams; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,6 +39,7 @@ * Configuration information for the Chat Model implementation using the OpenAI Java SDK. * * @author Julien Dubois + * @author Christian Tzolov */ public class OpenAiSdkChatOptions extends AbstractOpenAiSdkOptions implements ToolCallingChatOptions { @@ -69,9 +67,7 @@ public class OpenAiSdkChatOptions extends AbstractOpenAiSdkOptions implements To private OpenAiSdkChatModel.ResponseFormat responseFormat; - private ResponseCreateParams.StreamOptions streamOptions; - - private Boolean streamUsage; + private StreamOptions streamOptions; private Integer seed; @@ -81,9 +77,7 @@ public class OpenAiSdkChatOptions extends AbstractOpenAiSdkOptions implements To private Double topP; - private List tools; - - private ChatCompletionToolChoiceOption toolChoice; + private Object toolChoice; private String user; @@ -268,7 +262,7 @@ public void setResponseFormat(OpenAiSdkChatModel.ResponseFormat responseFormat) * Gets the stream options. * @return the stream options */ - public ResponseCreateParams.StreamOptions getStreamOptions() { + public StreamOptions getStreamOptions() { return this.streamOptions; } @@ -276,26 +270,10 @@ public ResponseCreateParams.StreamOptions getStreamOptions() { * Sets the stream options. * @param streamOptions the stream options to set */ - public void setStreamOptions(ResponseCreateParams.StreamOptions streamOptions) { + public void setStreamOptions(StreamOptions streamOptions) { this.streamOptions = streamOptions; } - /** - * Gets whether to include usage information in streaming responses. - * @return true if usage should be included in streams - */ - public Boolean getStreamUsage() { - return this.streamUsage; - } - - /** - * Sets whether to include usage information in streaming responses. - * @param streamUsage whether to include usage in streams - */ - public void setStreamUsage(Boolean streamUsage) { - this.streamUsage = streamUsage; - } - /** * Gets the random seed for deterministic generation. * @return the random seed @@ -367,27 +345,11 @@ public void setTopP(Double topP) { this.topP = topP; } - /** - * Gets the list of tool definitions. - * @return the list of tools - */ - public List getTools() { - return this.tools; - } - - /** - * Sets the list of tool definitions. - * @param tools the list of tools - */ - public void setTools(List tools) { - this.tools = tools; - } - /** * Gets the tool choice configuration. * @return the tool choice option */ - public ChatCompletionToolChoiceOption getToolChoice() { + public Object getToolChoice() { return this.toolChoice; } @@ -395,7 +357,7 @@ public ChatCompletionToolChoiceOption getToolChoice() { * Sets the tool choice configuration. * @param toolChoice the tool choice option */ - public void setToolChoice(ChatCompletionToolChoiceOption toolChoice) { + public void setToolChoice(Object toolChoice) { this.toolChoice = toolChoice; } @@ -602,11 +564,10 @@ public boolean equals(Object o) { && Objects.equals(this.n, options.n) && Objects.equals(this.outputAudio, options.outputAudio) && Objects.equals(this.presencePenalty, options.presencePenalty) && Objects.equals(this.responseFormat, options.responseFormat) - && Objects.equals(this.streamOptions, options.streamOptions) - && Objects.equals(this.streamUsage, options.streamUsage) && Objects.equals(this.seed, options.seed) + && Objects.equals(this.streamOptions, options.streamOptions) && Objects.equals(this.seed, options.seed) && Objects.equals(this.stop, options.stop) && Objects.equals(this.temperature, options.temperature) - && Objects.equals(this.topP, options.topP) && Objects.equals(this.tools, options.tools) - && Objects.equals(this.toolChoice, options.toolChoice) && Objects.equals(this.user, options.user) + && Objects.equals(this.topP, options.topP) && Objects.equals(this.toolChoice, options.toolChoice) + && Objects.equals(this.user, options.user) && Objects.equals(this.parallelToolCalls, options.parallelToolCalls) && Objects.equals(this.store, options.store) && Objects.equals(this.metadata, options.metadata) && Objects.equals(this.reasoningEffort, options.reasoningEffort) @@ -623,10 +584,10 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(this.getModel(), this.frequencyPenalty, this.logitBias, this.logprobs, this.topLogprobs, this.maxTokens, this.maxCompletionTokens, this.n, this.outputAudio, this.presencePenalty, - this.responseFormat, this.streamOptions, this.streamUsage, this.seed, this.stop, this.temperature, - this.topP, this.tools, this.toolChoice, this.user, this.parallelToolCalls, this.store, this.metadata, - this.reasoningEffort, this.verbosity, this.serviceTier, this.toolCallbacks, this.toolNames, - this.internalToolExecutionEnabled, this.httpHeaders, this.toolContext); + this.responseFormat, this.streamOptions, this.seed, this.stop, this.temperature, this.topP, + this.toolChoice, this.user, this.parallelToolCalls, this.store, this.metadata, this.reasoningEffort, + this.verbosity, this.serviceTier, this.toolCallbacks, this.toolNames, this.internalToolExecutionEnabled, + this.httpHeaders, this.toolContext); } @Override @@ -636,14 +597,70 @@ public String toString() { + ", maxTokens=" + this.maxTokens + ", maxCompletionTokens=" + this.maxCompletionTokens + ", n=" + this.n + ", outputAudio=" + this.outputAudio + ", presencePenalty=" + this.presencePenalty + ", responseFormat=" + this.responseFormat + ", streamOptions=" + this.streamOptions + ", streamUsage=" - + this.streamUsage + ", seed=" + this.seed + ", stop=" + this.stop + ", temperature=" + this.temperature - + ", topP=" + this.topP + ", tools=" + this.tools + ", toolChoice=" + this.toolChoice + ", user='" - + this.user + '\'' + ", parallelToolCalls=" + this.parallelToolCalls + ", store=" + this.store - + ", metadata=" + this.metadata + ", reasoningEffort='" + this.reasoningEffort + '\'' + ", verbosity='" - + this.verbosity + '\'' + ", serviceTier='" + this.serviceTier + '\'' + ", toolCallbacks=" - + this.toolCallbacks + ", toolNames=" + this.toolNames + ", internalToolExecutionEnabled=" - + this.internalToolExecutionEnabled + ", httpHeaders=" + this.httpHeaders + ", toolContext=" - + this.toolContext + '}'; + + ", seed=" + this.seed + ", stop=" + this.stop + ", temperature=" + this.temperature + ", topP=" + + this.topP + ", toolChoice=" + this.toolChoice + ", user='" + this.user + '\'' + ", parallelToolCalls=" + + this.parallelToolCalls + ", store=" + this.store + ", metadata=" + this.metadata + + ", reasoningEffort='" + this.reasoningEffort + '\'' + ", verbosity='" + this.verbosity + '\'' + + ", serviceTier='" + this.serviceTier + '\'' + ", toolCallbacks=" + this.toolCallbacks + ", toolNames=" + + this.toolNames + ", internalToolExecutionEnabled=" + this.internalToolExecutionEnabled + + ", httpHeaders=" + this.httpHeaders + ", toolContext=" + this.toolContext + '}'; + } + + public record StreamOptions(Boolean includeObfuscation, Boolean includeUsage, + Map additionalProperties) { + + public static Builder builder() { + return new Builder(); + } + + public static final class Builder { + + private Boolean includeObfuscation; + + private Boolean includeUsage; + + private Map additionalProperties = new HashMap<>(); + + public Builder from(StreamOptions fromOptions) { + if (fromOptions != null) { + this.includeObfuscation = fromOptions.includeObfuscation(); + this.includeUsage = fromOptions.includeUsage(); + this.additionalProperties = fromOptions.additionalProperties() != null + ? new HashMap<>(fromOptions.additionalProperties()) : new HashMap<>(); + } + return this; + } + + public Builder includeObfuscation(Boolean includeObfuscation) { + this.includeObfuscation = includeObfuscation; + return this; + } + + public Builder includeUsage(Boolean includeUsage) { + this.includeUsage = includeUsage; + return this; + } + + public Builder additionalProperties(Map additionalProperties) { + this.additionalProperties = additionalProperties != null ? new HashMap<>(additionalProperties) + : new HashMap<>(); + return this; + } + + public Builder additionalProperty(String key, Object value) { + if (this.additionalProperties == null) { + this.additionalProperties = new HashMap<>(); + } + this.additionalProperties.put(key, value); + return this; + } + + public StreamOptions build() { + return new StreamOptions(this.includeObfuscation, this.includeUsage, this.additionalProperties); + } + + } + } public static final class Builder { @@ -664,12 +681,10 @@ public Builder from(OpenAiSdkChatOptions fromOptions) { this.options.setPresencePenalty(fromOptions.getPresencePenalty()); this.options.setResponseFormat(fromOptions.getResponseFormat()); this.options.setStreamOptions(fromOptions.getStreamOptions()); - this.options.setStreamUsage(fromOptions.getStreamUsage()); this.options.setSeed(fromOptions.getSeed()); this.options.setStop(fromOptions.getStop() != null ? new ArrayList<>(fromOptions.getStop()) : null); this.options.setTemperature(fromOptions.getTemperature()); this.options.setTopP(fromOptions.getTopP()); - this.options.setTools(fromOptions.getTools()); this.options.setToolChoice(fromOptions.getToolChoice()); this.options.setUser(fromOptions.getUser()); this.options.setParallelToolCalls(fromOptions.getParallelToolCalls()); @@ -727,9 +742,6 @@ public Builder merge(OpenAiSdkChatOptions from) { if (from.getStreamOptions() != null) { this.options.setStreamOptions(from.getStreamOptions()); } - if (from.getStreamUsage() != null) { - this.options.setStreamUsage(from.getStreamUsage()); - } if (from.getSeed() != null) { this.options.setSeed(from.getSeed()); } @@ -742,9 +754,6 @@ public Builder merge(OpenAiSdkChatOptions from) { if (from.getTopP() != null) { this.options.setTopP(from.getTopP()); } - if (from.getTools() != null) { - this.options.setTools(from.getTools()); - } if (from.getToolChoice() != null) { this.options.setToolChoice(from.getToolChoice()); } @@ -863,16 +872,11 @@ public Builder responseFormat(OpenAiSdkChatModel.ResponseFormat responseFormat) return this; } - public Builder streamOptions(ResponseCreateParams.StreamOptions streamOptions) { + public Builder streamOptions(StreamOptions streamOptions) { this.options.setStreamOptions(streamOptions); return this; } - public Builder streamUsage(Boolean streamUsage) { - this.options.setStreamUsage(streamUsage); - return this; - } - public Builder seed(Integer seed) { this.options.setSeed(seed); return this; @@ -893,12 +897,7 @@ public Builder topP(Double topP) { return this; } - public Builder tools(List tools) { - this.options.setTools(tools); - return this; - } - - public Builder toolChoice(ChatCompletionToolChoiceOption toolChoice) { + public Builder toolChoice(Object toolChoice) { this.options.setToolChoice(toolChoice); return this; } diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java index 7622e26bd94..6fb0275a4f0 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java @@ -62,6 +62,7 @@ import org.springframework.ai.model.tool.ToolExecutionResult; import org.springframework.ai.openaisdk.OpenAiSdkChatModel; import org.springframework.ai.openaisdk.OpenAiSdkChatOptions; +import org.springframework.ai.openaisdk.OpenAiSdkChatOptions.StreamOptions; import org.springframework.ai.openaisdk.OpenAiSdkTestConfiguration; import org.springframework.ai.support.ToolCallbacks; import org.springframework.ai.tool.annotation.Tool; @@ -224,7 +225,7 @@ void streamRoleTest() { @Test void streamingWithTokenUsage() { var promptOptions = OpenAiSdkChatOptions.builder() - .streamUsage(true) + .streamOptions(StreamOptions.builder().includeUsage(true).build()) .reasoningEffort(ReasoningEffort.MINIMAL.toString()) .seed(1) .build(); @@ -460,7 +461,7 @@ void streamFunctionCallUsageTest() { .description("Get the weather in location") .inputType(MockWeatherService.Request.class) .build())) - .streamUsage(true) + .streamOptions(StreamOptions.builder().includeUsage(true).build()) .reasoningEffort(ReasoningEffort.MINIMAL.toString()) .build(); diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java index a7322727d9f..2d337cc38b6 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java @@ -36,6 +36,7 @@ import org.springframework.ai.observation.conventions.AiProvider; import org.springframework.ai.openaisdk.OpenAiSdkChatModel; import org.springframework.ai.openaisdk.OpenAiSdkChatOptions; +import org.springframework.ai.openaisdk.OpenAiSdkChatOptions.StreamOptions; import org.springframework.ai.openaisdk.OpenAiSdkTestConfigurationWithObservability; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -82,7 +83,7 @@ void observationForChatOperation() throws InterruptedException { void observationForStreamingChatOperation() throws InterruptedException { var options = OpenAiSdkChatOptions.builder() .model(OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL) - .streamUsage(true) + .streamOptions(StreamOptions.builder().includeUsage(true).build()) .build(); Prompt prompt = new Prompt("Why does a raven look like a desk?", options); diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelResponseFormatIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelResponseFormatIT.java index a273dfdde5f..86ac56058a2 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelResponseFormatIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelResponseFormatIT.java @@ -257,7 +257,7 @@ record Items(@JsonProperty(required = true, value = "explanation") String explan // Check if the order is correct as specified in the schema. Steps should come // first before final answer. - assertThat(content.startsWith("{\"steps\":{\"items\":[")); + // assertThat(content.startsWith("{\"steps\":{\"items\":[")).isTrue(); MathReasoning mathReasoning = outputConverter.convert(content); diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java index e4c8e4ea128..a47a4fd501d 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java @@ -24,10 +24,10 @@ import java.util.Map; import java.util.Set; -import com.openai.models.FunctionDefinition; import org.junit.jupiter.api.Test; import org.springframework.ai.openaisdk.OpenAiSdkChatOptions; +import org.springframework.ai.openaisdk.OpenAiSdkChatOptions.StreamOptions; import org.springframework.ai.tool.ToolCallback; import static org.assertj.core.api.Assertions.assertThat; @@ -47,7 +47,6 @@ void testBuilderWithAllFields() { logitBias.put("token2", -1); List stop = List.of("stop1", "stop2"); - List tools = new ArrayList<>(); Map metadata = Map.of("key1", "value1"); Map toolContext = Map.of("keyA", "valueA"); Map httpHeaders = Map.of("header1", "value1"); @@ -63,12 +62,11 @@ void testBuilderWithAllFields() { .maxCompletionTokens(50) .N(2) .presencePenalty(0.8) - .streamUsage(true) + .streamOptions(StreamOptions.builder().includeUsage(true).build()) .seed(12345) .stop(stop) .temperature(0.7) .topP(0.9) - .tools(tools) .user("test-user") .parallelToolCalls(true) .store(false) @@ -91,13 +89,12 @@ void testBuilderWithAllFields() { assertThat(options.getMaxCompletionTokens()).isEqualTo(50); assertThat(options.getN()).isEqualTo(2); assertThat(options.getPresencePenalty()).isEqualTo(0.8); - assertThat(options.getStreamUsage()).isTrue(); + assertThat(options.getStreamOptions().includeUsage()).isTrue(); assertThat(options.getSeed()).isEqualTo(12345); assertThat(options.getStop()).isEqualTo(stop); assertThat(options.getStopSequences()).isEqualTo(stop); assertThat(options.getTemperature()).isEqualTo(0.7); assertThat(options.getTopP()).isEqualTo(0.9); - assertThat(options.getTools()).isEqualTo(tools); assertThat(options.getUser()).isEqualTo("test-user"); assertThat(options.getParallelToolCalls()).isTrue(); assertThat(options.getStore()).isFalse(); @@ -116,7 +113,6 @@ void testCopy() { logitBias.put("token1", 1); List stop = List.of("stop1"); - List tools = new ArrayList<>(); Map metadata = Map.of("key1", "value1"); OpenAiSdkChatOptions originalOptions = OpenAiSdkChatOptions.builder() @@ -129,12 +125,11 @@ void testCopy() { .maxCompletionTokens(50) .N(2) .presencePenalty(0.8) - .streamUsage(false) + .streamOptions(StreamOptions.builder().includeUsage(false).build()) .seed(12345) .stop(stop) .temperature(0.7) .topP(0.9) - .tools(tools) .user("test-user") .parallelToolCalls(false) .store(true) @@ -163,7 +158,6 @@ void testSetters() { logitBias.put("token1", 1); List stop = List.of("stop1", "stop2"); - List tools = new ArrayList<>(); Map metadata = Map.of("key2", "value2"); OpenAiSdkChatOptions options = new OpenAiSdkChatOptions(); @@ -177,12 +171,11 @@ void testSetters() { options.setMaxCompletionTokens(50); options.setN(2); options.setPresencePenalty(0.8); - options.setStreamUsage(true); + options.setStreamOptions(StreamOptions.builder().includeUsage(true).build()); options.setSeed(12345); options.setStop(stop); options.setTemperature(0.7); options.setTopP(0.9); - options.setTools(tools); options.setUser("test-user"); options.setParallelToolCalls(true); options.setStore(false); @@ -203,12 +196,11 @@ void testSetters() { assertThat(options.getMaxCompletionTokens()).isEqualTo(50); assertThat(options.getN()).isEqualTo(2); assertThat(options.getPresencePenalty()).isEqualTo(0.8); - assertThat(options.getStreamUsage()).isTrue(); + assertThat(options.getStreamOptions().includeUsage()).isTrue(); assertThat(options.getSeed()).isEqualTo(12345); assertThat(options.getStop()).isEqualTo(stop); assertThat(options.getTemperature()).isEqualTo(0.7); assertThat(options.getTopP()).isEqualTo(0.9); - assertThat(options.getTools()).isEqualTo(tools); assertThat(options.getUser()).isEqualTo("test-user"); assertThat(options.getParallelToolCalls()).isTrue(); assertThat(options.getStore()).isFalse(); @@ -237,14 +229,13 @@ void testDefaultValues() { assertThat(options.getPresencePenalty()).isNull(); assertThat(options.getResponseFormat()).isNull(); assertThat(options.getStreamOptions()).isNull(); - assertThat(options.getStreamUsage()).isNull(); + assertThat(options.getStreamOptions()).isNull(); assertThat(options.getSeed()).isNull(); assertThat(options.getStop()).isNull(); assertThat(options.getStopSequences()).isNull(); assertThat(options.getTemperature()).isNull(); assertThat(options.getTopP()).isNull(); assertThat(options.getTopK()).isNull(); - assertThat(options.getTools()).isNull(); assertThat(options.getToolChoice()).isNull(); assertThat(options.getUser()).isNull(); assertThat(options.getParallelToolCalls()).isNull(); @@ -295,7 +286,6 @@ void testBuilderWithNullValues() { .temperature(null) .logitBias(null) .stop(null) - .tools(null) .metadata(null) .httpHeaders(null) .build(); @@ -304,7 +294,6 @@ void testBuilderWithNullValues() { assertThat(options.getTemperature()).isNull(); assertThat(options.getLogitBias()).isNull(); assertThat(options.getStop()).isNull(); - assertThat(options.getTools()).isNull(); assertThat(options.getMetadata()).isNull(); assertThat(options.getHttpHeaders()).isNull(); } @@ -330,26 +319,22 @@ void testNullAndEmptyCollections() { // Test setting null collections options.setLogitBias(null); options.setStop(null); - options.setTools(null); options.setMetadata(null); options.setHttpHeaders(null); assertThat(options.getLogitBias()).isNull(); assertThat(options.getStop()).isNull(); - assertThat(options.getTools()).isNull(); assertThat(options.getMetadata()).isNull(); assertThat(options.getHttpHeaders()).isNull(); // Test setting empty collections options.setLogitBias(new HashMap<>()); options.setStop(new ArrayList<>()); - options.setTools(new ArrayList<>()); options.setMetadata(new HashMap<>()); options.setHttpHeaders(new HashMap<>()); assertThat(options.getLogitBias()).isEmpty(); assertThat(options.getStop()).isEmpty(); - assertThat(options.getTools()).isEmpty(); assertThat(options.getMetadata()).isEmpty(); assertThat(options.getHttpHeaders()).isEmpty(); } diff --git a/pom.xml b/pom.xml index fadca598645..07eee6ac9b8 100644 --- a/pom.xml +++ b/pom.xml @@ -106,6 +106,7 @@ auto-configurations/models/spring-ai-autoconfigure-model-elevenlabs auto-configurations/models/spring-ai-autoconfigure-model-huggingface auto-configurations/models/spring-ai-autoconfigure-model-openai + auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk auto-configurations/models/spring-ai-autoconfigure-model-minimax auto-configurations/models/spring-ai-autoconfigure-model-mistral-ai auto-configurations/models/spring-ai-autoconfigure-model-oci-genai @@ -204,6 +205,7 @@ spring-ai-spring-boot-starters/spring-ai-starter-model-oci-genai spring-ai-spring-boot-starters/spring-ai-starter-model-ollama spring-ai-spring-boot-starters/spring-ai-starter-model-openai + spring-ai-spring-boot-starters/spring-ai-starter-model-openai-sdk spring-ai-spring-boot-starters/spring-ai-starter-model-postgresml-embedding spring-ai-spring-boot-starters/spring-ai-starter-model-stability-ai spring-ai-spring-boot-starters/spring-ai-starter-model-transformers diff --git a/spring-ai-bom/pom.xml b/spring-ai-bom/pom.xml index 53b3d6aa0d5..06536153563 100644 --- a/spring-ai-bom/pom.xml +++ b/spring-ai-bom/pom.xml @@ -311,6 +311,12 @@ ${project.version} + + org.springframework.ai + spring-ai-openai-sdk + ${project.version} + + org.springframework.ai spring-ai-postgresml @@ -680,6 +686,12 @@ ${project.version} + + org.springframework.ai + spring-ai-autoconfigure-model-openai-sdk + ${project.version} + + org.springframework.ai spring-ai-autoconfigure-model-postgresml-embedding @@ -1029,6 +1041,12 @@ ${project.version} + + org.springframework.ai + spring-ai-starter-model-openai-sdk + ${project.version} + + org.springframework.ai spring-ai-starter-model-postgresml-embedding diff --git a/spring-ai-model/src/main/java/org/springframework/ai/model/SpringAIModels.java b/spring-ai-model/src/main/java/org/springframework/ai/model/SpringAIModels.java index 19f8a8a3258..eb57d9a44a3 100644 --- a/spring-ai-model/src/main/java/org/springframework/ai/model/SpringAIModels.java +++ b/spring-ai-model/src/main/java/org/springframework/ai/model/SpringAIModels.java @@ -44,6 +44,8 @@ private SpringAIModels() { public static final String OPENAI = "openai"; + public static final String OPENAI_SDK = "openai-sdk"; + public static final String POSTGRESML = "postgresml"; public static final String STABILITY_AI = "stabilityai"; diff --git a/spring-ai-spring-boot-starters/spring-ai-starter-model-openai-sdk/pom.xml b/spring-ai-spring-boot-starters/spring-ai-starter-model-openai-sdk/pom.xml new file mode 100644 index 00000000000..cb94ce4b0fb --- /dev/null +++ b/spring-ai-spring-boot-starters/spring-ai-starter-model-openai-sdk/pom.xml @@ -0,0 +1,70 @@ + + + + + 4.0.0 + + org.springframework.ai + spring-ai-parent + 2.0.0-SNAPSHOT + ../../pom.xml + + spring-ai-starter-model-openai-sdk + jar + Spring AI Starter - OpenAI SDK + Spring AI Open AI SDK Spring Boot Starter + https://github.com/spring-projects/spring-ai + + + https://github.com/spring-projects/spring-ai + git://github.com/spring-projects/spring-ai.git + git@github.com:spring-projects/spring-ai.git + + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.ai + spring-ai-autoconfigure-model-openai-sdk + ${project.parent.version} + + + + org.springframework.ai + spring-ai-openai-sdk + ${project.parent.version} + + + + org.springframework.ai + spring-ai-autoconfigure-model-chat-client + ${project.parent.version} + + + + org.springframework.ai + spring-ai-autoconfigure-model-chat-memory + ${project.parent.version} + + + + From 5c2194b79fd0ad7ce4434899cb6c4113c9769dd3 Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Sun, 23 Nov 2025 15:04:59 +0100 Subject: [PATCH 30/49] feat: Add audio output support and improve OpenAI SDK chat configuration - Change default temperature from 0.7 to 1.0 in OpenAiSdkChatProperties to align with OpenAI defaults - Add support for audio output modalities with new AudioParameters type - Introduce Voice enum (ALLOY, ASH, BALLAD, CORAL, ECHO, FABLE, ONYX, NOVA, SAGE, SHIMMER) - Introduce AudioResponseFormat enum (MP3, FLAC, OPUS, PCM16, WAV, AAC) - Replace ChatCompletionAudioParam with custom AudioParameters record - Add outputModalities field to support text and audio output combinations - Enhance buildGeneration method to handle audio content in responses - Extract audio data from base64 encoded response - Create Media objects with proper MIME types - Include audio transcript and metadata (audioId, audioExpiresAt) - Improve chunkToChatCompletion to handle missing choices in streaming responses - Add integration tests for audio output (blocking and streaming) - Update documentation for output-audio and stream-options configuration - Add streamUsage helper method to StreamOptions builder Signed-off-by: Christian Tzolov --- .../OpenAiSdkChatProperties.java | 2 +- .../OpenAiFunctionCallback2IT.java | 27 +- .../ai/openaisdk/OpenAiSdkChatModel.java | 103 ++-- .../ai/openaisdk/OpenAiSdkChatOptions.java | 106 +++- .../openaisdk/chat/OpenAiSdkChatModelIT.java | 50 +- .../chat/client/OpenAiSdkChatClientIT.java | 495 ++++++++++++++++++ .../embedding/OpenAiSdkEmbeddingIT.java | 3 +- .../ROOT/pages/api/chat/openai-sdk-chat.adoc | 9 +- 8 files changed, 725 insertions(+), 70 deletions(-) create mode 100644 models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/client/OpenAiSdkChatClientIT.java diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatProperties.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatProperties.java index 0fcf3b0bda4..717c9bcd25b 100644 --- a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatProperties.java +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatProperties.java @@ -33,7 +33,7 @@ public class OpenAiSdkChatProperties extends AbstractOpenAiSdkOptions { public static final String DEFAULT_CHAT_MODEL = OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL; - private static final Double DEFAULT_TEMPERATURE = 0.7; + private static final Double DEFAULT_TEMPERATURE = 1.0; @NestedConfigurationProperty private final OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder() diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiFunctionCallback2IT.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiFunctionCallback2IT.java index 271ed574eb5..564e172286b 100644 --- a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiFunctionCallback2IT.java +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/test/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiFunctionCallback2IT.java @@ -18,6 +18,7 @@ import java.util.stream.Collectors; +import com.openai.models.ChatModel; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; import org.slf4j.Logger; @@ -46,9 +47,12 @@ public class OpenAiFunctionCallback2IT { @Test void functionCallTest() { - this.contextRunner.withPropertyValues("spring.ai.openai-sdk.chat.options.temperature=0.1").run(context -> { + this.contextRunner + .withPropertyValues("spring.ai.openai-sdk.chat.options.temperature=0.1", + "spring.ai.openai-sdk.chat.options.model=" + ChatModel.GPT_4O_MINI.asString()) + .run(context -> { - OpenAiSdkChatModel chatModel = context.getBean(OpenAiSdkChatModel.class); + OpenAiSdkChatModel chatModel = context.getBean(OpenAiSdkChatModel.class); // @formatter:off ChatClient chatClient = ChatClient.builder(chatModel) @@ -61,17 +65,20 @@ void functionCallTest() { .call().content(); // @formatter:on - logger.info("Response: {}", content); + logger.info("Response: {}", content); - assertThat(content).contains("30", "10", "15"); - }); + assertThat(content).contains("30", "10", "15"); + }); } @Test void streamFunctionCallTest() { - this.contextRunner.withPropertyValues("spring.ai.openai-sdk.chat.options.temperature=0.1").run(context -> { + this.contextRunner + .withPropertyValues("spring.ai.openai-sdk.chat.options.temperature=0.2", + "spring.ai.openai-sdk.chat.options.model=" + ChatModel.GPT_4O_MINI.asString()) + .run(context -> { - OpenAiSdkChatModel chatModel = context.getBean(OpenAiSdkChatModel.class); + OpenAiSdkChatModel chatModel = context.getBean(OpenAiSdkChatModel.class); // @formatter:off String content = ChatClient.builder(chatModel).build().prompt() @@ -81,10 +88,10 @@ void streamFunctionCallTest() { .collectList().block().stream().collect(Collectors.joining()); // @formatter:on - logger.info("Response: {}", content); + logger.info("Response: {}", content); - assertThat(content).contains("30", "10", "15"); - }); + assertThat(content).contains("30", "10", "15"); + }); } @Configuration diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java index dc65abe8dfa..a218683aad1 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java @@ -82,6 +82,7 @@ import org.springframework.ai.chat.observation.DefaultChatModelObservationConvention; import org.springframework.ai.chat.prompt.ChatOptions; import org.springframework.ai.chat.prompt.Prompt; +import org.springframework.ai.content.Media; import org.springframework.ai.model.ModelOptionsUtils; import org.springframework.ai.model.tool.DefaultToolExecutionEligibilityPredicate; import org.springframework.ai.model.tool.ToolCallingChatOptions; @@ -93,8 +94,11 @@ import org.springframework.ai.openaisdk.setup.OpenAiSdkSetup; import org.springframework.ai.support.UsageCalculator; import org.springframework.ai.tool.definition.ToolDefinition; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.Resource; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; +import org.springframework.util.MimeTypeUtils; import org.springframework.util.StringUtils; /** @@ -295,7 +299,7 @@ public ChatResponse internalCall(Prompt prompt, ChatResponse previousChatRespons "refusal", choice.message().refusal().isPresent() ? choice.message().refusal() : "", "annotations", choice.message().annotations().isPresent() ? choice.message().annotations() : List.of(Map.of())); - return buildGeneration(choice, metadata); + return buildGeneration(choice, metadata, request); }).toList(); // Current usage @@ -392,7 +396,7 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha ? choice.message().annotations() : List.of(), "chunkChoice", chunk.choices().get((int) choice.index())); - return buildGeneration(choice, metadata); + return buildGeneration(choice, metadata, request); }).toList(); Optional usage = chatCompletion.usage(); CompletionUsage usageVal = usage.orElse(null); @@ -542,7 +546,8 @@ public Flux internalStream(Prompt prompt, ChatResponse previousCha }); } - private Generation buildGeneration(ChatCompletion.Choice choice, Map metadata) { + private Generation buildGeneration(ChatCompletion.Choice choice, Map metadata, + ChatCompletionCreateParams request) { ChatCompletionMessage message = choice.message(); List toolCalls = new ArrayList<>(); @@ -590,11 +595,35 @@ private Generation buildGeneration(ChatCompletion.Choice choice, Map media = new ArrayList<>(); + + if (message.audio().isPresent() && StringUtils.hasText(message.audio().get().data()) + && request.audio().isPresent()) { + var audioOutput = message.audio().get(); + String mimeType = String.format("audio/%s", request.audio().get().format().value().name().toLowerCase()); + byte[] audioData = Base64.getDecoder().decode(audioOutput.data()); + Resource resource = new ByteArrayResource(audioData); + Media.builder().mimeType(MimeTypeUtils.parseMimeType(mimeType)).data(resource).id(audioOutput.id()).build(); + media.add(Media.builder() + .mimeType(MimeTypeUtils.parseMimeType(mimeType)) + .data(resource) + .id(audioOutput.id()) + .build()); + if (!StringUtils.hasText(textContent)) { + textContent = audioOutput.transcript(); + } + generationMetadataBuilder.metadata("audioId", audioOutput.id()); + generationMetadataBuilder.metadata("audioExpiresAt", audioOutput.expiresAt()); + } + var assistantMessage = AssistantMessage.builder() .content(textContent) .properties(metadata) .toolCalls(toolCalls) + .media(media) .build(); return new Generation(assistantMessage, generationMetadataBuilder.build()); } @@ -626,37 +655,41 @@ private ChatResponseMetadata from(ChatResponseMetadata chatResponseMetadata, Usa * @return the ChatCompletion */ private ChatCompletion chunkToChatCompletion(ChatCompletionChunk chunk) { - List choices = chunk.choices().stream().map(chunkChoice -> { - ChatCompletion.Choice.FinishReason finishReason = ChatCompletion.Choice.FinishReason.of(""); - if (chunkChoice.finishReason().isPresent()) { - finishReason = ChatCompletion.Choice.FinishReason - .of(chunkChoice.finishReason().get().value().name().toLowerCase()); - } - ChatCompletion.Choice.Builder choiceBuilder = ChatCompletion.Choice.builder() - .finishReason(finishReason) - .index(chunkChoice.index()) - .message(ChatCompletionMessage.builder() - .content(chunkChoice.delta().content()) - .refusal(chunkChoice.delta().refusal()) - .build()); + List choices = (chunk._choices().isMissing()) ? List.of() + : chunk.choices().stream().map(chunkChoice -> { + ChatCompletion.Choice.FinishReason finishReason = ChatCompletion.Choice.FinishReason.of(""); + if (chunkChoice.finishReason().isPresent()) { + finishReason = ChatCompletion.Choice.FinishReason + .of(chunkChoice.finishReason().get().value().name().toLowerCase()); + } - // Handle optional logprobs - if (chunkChoice.logprobs().isPresent()) { - var logprobs = chunkChoice.logprobs().get(); - choiceBuilder.logprobs(ChatCompletion.Choice.Logprobs.builder() - .content(logprobs.content()) - .refusal(logprobs.refusal()) - .build()); - } - else { - // Provide empty logprobs when not present - choiceBuilder - .logprobs(ChatCompletion.Choice.Logprobs.builder().content(List.of()).refusal(List.of()).build()); - } + ChatCompletion.Choice.Builder choiceBuilder = ChatCompletion.Choice.builder() + .finishReason(finishReason) + .index(chunkChoice.index()) + .message(ChatCompletionMessage.builder() + .content(chunkChoice.delta().content()) + .refusal(chunkChoice.delta().refusal()) + .build()); + + // Handle optional logprobs + if (chunkChoice.logprobs().isPresent()) { + var logprobs = chunkChoice.logprobs().get(); + choiceBuilder.logprobs(ChatCompletion.Choice.Logprobs.builder() + .content(logprobs.content()) + .refusal(logprobs.refusal()) + .build()); + } + else { + // Provide empty logprobs when not present + choiceBuilder.logprobs( + ChatCompletion.Choice.Logprobs.builder().content(List.of()).refusal(List.of()).build()); + } - return choiceBuilder.build(); - }).toList(); + chunkChoice.delta(); + + return choiceBuilder.build(); + }).toList(); return ChatCompletion.builder() .id(chunk.id()) @@ -934,8 +967,14 @@ else if (requestOptions.getModel() != null) { if (requestOptions.getN() != null) { builder.n(requestOptions.getN()); } + if (requestOptions.getOutputModalities() != null) { + builder.modalities(requestOptions.getOutputModalities() + .stream() + .map(modality -> ChatCompletionCreateParams.Modality.of(modality.toLowerCase())) + .toList()); + } if (requestOptions.getOutputAudio() != null) { - builder.audio(requestOptions.getOutputAudio()); + builder.audio(requestOptions.getOutputAudio().toChatCompletionAudioParam()); } if (requestOptions.getPresencePenalty() != null) { builder.presencePenalty(requestOptions.getPresencePenalty()); diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java index c0451f4273b..045bff9e83a 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java @@ -61,7 +61,9 @@ public class OpenAiSdkChatOptions extends AbstractOpenAiSdkOptions implements To private Integer n; - private ChatCompletionAudioParam outputAudio; + private List outputModalities; + + private AudioParameters outputAudio; private Double presencePenalty; @@ -213,11 +215,27 @@ public void setN(Integer n) { this.n = n; } + /** + * Gets the output modalities. + * @return the output modalities + */ + public List getOutputModalities() { + return this.outputModalities; + } + + /** + * Sets the output modalities. + * @param outputModalities the output modalities + */ + public void setOutputModalities(List outputModalities) { + this.outputModalities = outputModalities; + } + /** * Gets the output audio parameters. * @return the output audio parameters */ - public ChatCompletionAudioParam getOutputAudio() { + public AudioParameters getOutputAudio() { return this.outputAudio; } @@ -225,7 +243,7 @@ public ChatCompletionAudioParam getOutputAudio() { * Sets the output audio parameters. * @param outputAudio the output audio parameters */ - public void setOutputAudio(ChatCompletionAudioParam outputAudio) { + public void setOutputAudio(AudioParameters outputAudio) { this.outputAudio = outputAudio; } @@ -561,7 +579,8 @@ public boolean equals(Object o) { && Objects.equals(this.temperature, options.temperature) && Objects.equals(this.maxTokens, options.maxTokens) && Objects.equals(this.maxCompletionTokens, options.maxCompletionTokens) - && Objects.equals(this.n, options.n) && Objects.equals(this.outputAudio, options.outputAudio) + && Objects.equals(this.n, options.n) && Objects.equals(this.outputModalities, options.outputModalities) + && Objects.equals(this.outputAudio, options.outputAudio) && Objects.equals(this.presencePenalty, options.presencePenalty) && Objects.equals(this.responseFormat, options.responseFormat) && Objects.equals(this.streamOptions, options.streamOptions) && Objects.equals(this.seed, options.seed) @@ -583,11 +602,11 @@ public boolean equals(Object o) { @Override public int hashCode() { return Objects.hash(this.getModel(), this.frequencyPenalty, this.logitBias, this.logprobs, this.topLogprobs, - this.maxTokens, this.maxCompletionTokens, this.n, this.outputAudio, this.presencePenalty, - this.responseFormat, this.streamOptions, this.seed, this.stop, this.temperature, this.topP, - this.toolChoice, this.user, this.parallelToolCalls, this.store, this.metadata, this.reasoningEffort, - this.verbosity, this.serviceTier, this.toolCallbacks, this.toolNames, this.internalToolExecutionEnabled, - this.httpHeaders, this.toolContext); + this.maxTokens, this.maxCompletionTokens, this.n, this.outputModalities, this.outputAudio, + this.presencePenalty, this.responseFormat, this.streamOptions, this.seed, this.stop, this.temperature, + this.topP, this.toolChoice, this.user, this.parallelToolCalls, this.store, this.metadata, + this.reasoningEffort, this.verbosity, this.serviceTier, this.toolCallbacks, this.toolNames, + this.internalToolExecutionEnabled, this.httpHeaders, this.toolContext); } @Override @@ -595,15 +614,48 @@ public String toString() { return "OpenAiSdkChatOptions{" + "model='" + this.getModel() + ", frequencyPenalty=" + this.frequencyPenalty + ", logitBias=" + this.logitBias + ", logprobs=" + this.logprobs + ", topLogprobs=" + this.topLogprobs + ", maxTokens=" + this.maxTokens + ", maxCompletionTokens=" + this.maxCompletionTokens + ", n=" - + this.n + ", outputAudio=" + this.outputAudio + ", presencePenalty=" + this.presencePenalty - + ", responseFormat=" + this.responseFormat + ", streamOptions=" + this.streamOptions + ", streamUsage=" - + ", seed=" + this.seed + ", stop=" + this.stop + ", temperature=" + this.temperature + ", topP=" - + this.topP + ", toolChoice=" + this.toolChoice + ", user='" + this.user + '\'' + ", parallelToolCalls=" - + this.parallelToolCalls + ", store=" + this.store + ", metadata=" + this.metadata - + ", reasoningEffort='" + this.reasoningEffort + '\'' + ", verbosity='" + this.verbosity + '\'' - + ", serviceTier='" + this.serviceTier + '\'' + ", toolCallbacks=" + this.toolCallbacks + ", toolNames=" - + this.toolNames + ", internalToolExecutionEnabled=" + this.internalToolExecutionEnabled - + ", httpHeaders=" + this.httpHeaders + ", toolContext=" + this.toolContext + '}'; + + this.n + ", outputModalities=" + this.outputModalities + ", outputAudio=" + this.outputAudio + + ", presencePenalty=" + this.presencePenalty + ", responseFormat=" + this.responseFormat + + ", streamOptions=" + this.streamOptions + ", streamUsage=" + ", seed=" + this.seed + ", stop=" + + this.stop + ", temperature=" + this.temperature + ", topP=" + this.topP + ", toolChoice=" + + this.toolChoice + ", user='" + this.user + '\'' + ", parallelToolCalls=" + this.parallelToolCalls + + ", store=" + this.store + ", metadata=" + this.metadata + ", reasoningEffort='" + this.reasoningEffort + + '\'' + ", verbosity='" + this.verbosity + '\'' + ", serviceTier='" + this.serviceTier + '\'' + + ", toolCallbacks=" + this.toolCallbacks + ", toolNames=" + this.toolNames + + ", internalToolExecutionEnabled=" + this.internalToolExecutionEnabled + ", httpHeaders=" + + this.httpHeaders + ", toolContext=" + this.toolContext + '}'; + } + + public record AudioParameters(Voice voice, AudioResponseFormat format) { + + /** + * Specifies the voice type. + */ + public enum Voice { + + ALLOY, ASH, BALLAD, CORAL, ECHO, FABLE, ONYX, NOVA, SAGE, SHIMMER + + } + + /** + * Specifies the output audio format. + */ + public enum AudioResponseFormat { + + MP3, FLAC, OPUS, PCM16, WAV, AAC + + } + + public ChatCompletionAudioParam toChatCompletionAudioParam() { + ChatCompletionAudioParam.Builder builder = ChatCompletionAudioParam.builder(); + if (this.voice() != null) { + builder.voice(voice().name().toLowerCase()); + } + if (this.format() != null) { + builder.format(ChatCompletionAudioParam.Format.of(this.format().name().toLowerCase())); + } + return builder.build(); + } } public record StreamOptions(Boolean includeObfuscation, Boolean includeUsage, @@ -677,6 +729,7 @@ public Builder from(OpenAiSdkChatOptions fromOptions) { this.options.setMaxTokens(fromOptions.getMaxTokens()); this.options.setMaxCompletionTokens(fromOptions.getMaxCompletionTokens()); this.options.setN(fromOptions.getN()); + this.options.setOutputModalities(fromOptions.getOutputModalities()); this.options.setOutputAudio(fromOptions.getOutputAudio()); this.options.setPresencePenalty(fromOptions.getPresencePenalty()); this.options.setResponseFormat(fromOptions.getResponseFormat()); @@ -730,6 +783,9 @@ public Builder merge(OpenAiSdkChatOptions from) { if (from.getN() != null) { this.options.setN(from.getN()); } + if (from.getOutputModalities() != null) { + this.options.setOutputModalities(new ArrayList<>(from.getOutputModalities())); + } if (from.getOutputAudio() != null) { this.options.setOutputAudio(from.getOutputAudio()); } @@ -857,7 +913,12 @@ public Builder N(Integer n) { return this; } - public Builder outputAudio(ChatCompletionAudioParam audio) { + public Builder outputModalities(List outputModalities) { + this.options.setOutputModalities(outputModalities); + return this; + } + + public Builder outputAudio(AudioParameters audio) { this.options.setOutputAudio(audio); return this; } @@ -877,6 +938,13 @@ public Builder streamOptions(StreamOptions streamOptions) { return this; } + // helper shortcut methods for StreamOptions with included stream usage + public Builder streamUsage(boolean streamUsage) { + this.options.setStreamOptions( + StreamOptions.builder().from(this.options.getStreamOptions()).includeUsage(streamUsage).build()); + return this; + } + public Builder seed(Integer seed) { this.options.setSeed(seed); return this; diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java index 6fb0275a4f0..9bd09a06725 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.concurrent.CompletionException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -32,6 +33,8 @@ import org.assertj.core.data.Percentage; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import reactor.core.publisher.Flux; @@ -62,6 +65,9 @@ import org.springframework.ai.model.tool.ToolExecutionResult; import org.springframework.ai.openaisdk.OpenAiSdkChatModel; import org.springframework.ai.openaisdk.OpenAiSdkChatOptions; +import org.springframework.ai.openaisdk.OpenAiSdkChatOptions.AudioParameters; +import org.springframework.ai.openaisdk.OpenAiSdkChatOptions.AudioParameters.AudioResponseFormat; +import org.springframework.ai.openaisdk.OpenAiSdkChatOptions.AudioParameters.Voice; import org.springframework.ai.openaisdk.OpenAiSdkChatOptions.StreamOptions; import org.springframework.ai.openaisdk.OpenAiSdkTestConfiguration; import org.springframework.ai.support.ToolCallbacks; @@ -76,6 +82,7 @@ import org.springframework.util.MimeTypeUtils; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * Integration tests for {@link OpenAiSdkChatModel}. @@ -128,7 +135,7 @@ void testMessageHistory() { @Test void streamCompletenessTest() throws InterruptedException { UserMessage userMessage = new UserMessage( - "List ALL natural numbers in range [1, 1000]. Make sure to not omit any. Print the full list here, one after another."); + "List ALL natural numbers in range [1, 100]. Make sure to not omit any. Print the full list here, one after another."); Prompt prompt = new Prompt(List.of(userMessage)); StringBuilder answer = new StringBuilder(); @@ -145,7 +152,7 @@ void streamCompletenessTest() throws InterruptedException { }); chatResponseFlux.subscribe(); assertThat(latch.await(120, TimeUnit.SECONDS)).isTrue(); - IntStream.rangeClosed(1, 1000).forEach(n -> assertThat(answer).contains(String.valueOf(n))); + IntStream.rangeClosed(1, 100).forEach(n -> assertThat(answer).contains(String.valueOf(n))); } @Test @@ -391,7 +398,7 @@ void functionCallTest() { void streamFunctionCallTest() { UserMessage userMessage = new UserMessage( - "What's the weather like in San Francisco, Tokyo, and Paris? Answer in Celsius."); + "What's the weather like in San Francisco, Tokyo, and Paris in Celsius."); List messages = new ArrayList<>(List.of(userMessage)); @@ -539,6 +546,43 @@ void streamingMultiModalityImageUrl() throws IOException { assertThat(content).containsAnyOf("bananas", "apple", "bowl", "basket", "fruit stand"); } + @ParameterizedTest(name = "{0} : {displayName} ") + @ValueSource(strings = { "gpt-4o-audio-preview" }) + void multiModalityOutputAudio(String modelName) throws IOException { + var userMessage = new UserMessage("Tell me joke about Spring Framework"); + + ChatResponse response = this.chatModel.call(new Prompt(List.of(userMessage), + OpenAiSdkChatOptions.builder() + .model(modelName) + .outputModalities(List.of("text", "audio")) + .outputAudio(new AudioParameters(Voice.ALLOY, AudioResponseFormat.WAV)) + .build())); + + logger.info(response.getResult().getOutput().getText()); + assertThat(response.getResult().getOutput().getText()).isNotEmpty(); + + byte[] audio = response.getResult().getOutput().getMedia().get(0).getDataAsByteArray(); + assertThat(audio).isNotEmpty(); + } + + @ParameterizedTest(name = "{0} : {displayName} ") + @ValueSource(strings = { "gpt-4o-audio-preview" }) + void streamingMultiModalityOutputAudio(String modelName) { + var userMessage = new UserMessage("Tell me joke about Spring Framework"); + + assertThatThrownBy(() -> this.chatModel + .stream(new Prompt(List.of(userMessage), + OpenAiSdkChatOptions.builder() + .model(modelName) + .outputModalities(List.of("text", "audio")) + .outputAudio(new AudioParameters(Voice.ALLOY, AudioResponseFormat.WAV)) + .build())) + .collectList() + .block()).isInstanceOf(CompletionException.class) + .hasMessageContaining( + "audio.format' does not support 'wav' when stream=true. Supported values are: 'pcm16"); + } + @Test void validateCallResponseMetadata() { String model = OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL; diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/client/OpenAiSdkChatClientIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/client/OpenAiSdkChatClientIT.java new file mode 100644 index 00000000000..6dfe686498c --- /dev/null +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/client/OpenAiSdkChatClientIT.java @@ -0,0 +1,495 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ai.openaisdk.chat.client; + +import java.io.IOException; +import java.net.URL; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import com.openai.models.chat.completions.ChatCompletionCreateParams.Modality; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import reactor.core.publisher.Flux; + +import org.springframework.ai.chat.client.ChatClient; +import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor; +import org.springframework.ai.chat.model.ChatModel; +import org.springframework.ai.chat.model.ChatResponse; +import org.springframework.ai.chat.model.StreamingChatModel; +import org.springframework.ai.converter.BeanOutputConverter; +import org.springframework.ai.converter.ListOutputConverter; +import org.springframework.ai.openaisdk.OpenAiSdkChatModel; +import org.springframework.ai.openaisdk.OpenAiSdkChatOptions; +import org.springframework.ai.openaisdk.OpenAiSdkChatOptions.AudioParameters; +import org.springframework.ai.openaisdk.OpenAiSdkChatOptions.StreamOptions; +import org.springframework.ai.openaisdk.OpenAiSdkTestConfiguration; +import org.springframework.ai.openaisdk.chat.MockWeatherService; +import org.springframework.ai.template.st.StTemplateRenderer; +import org.springframework.ai.test.CurlyBracketEscaper; +import org.springframework.ai.tool.function.FunctionToolCallback; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.util.MimeTypeUtils; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.fail; + +@SpringBootTest(classes = OpenAiSdkTestConfiguration.class) +@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") +@ActiveProfiles("logging-test") +@SuppressWarnings("null") +class OpenAiSdkChatClientIT { + + private static final Logger logger = LoggerFactory.getLogger(OpenAiSdkChatClientIT.class); + + @Autowired + protected ChatModel chatModel; + + @Autowired + protected StreamingChatModel streamingChatModel; + + @Autowired + protected OpenAiSdkChatModel openAiChatModel; + + @Value("classpath:/prompts/system-message.st") + private Resource systemTextResource; + + @Test + void call() { + + // @formatter:off + ChatResponse response = ChatClient.create(this.chatModel).prompt() + .advisors(new SimpleLoggerAdvisor()) + .system(s -> s.text(this.systemTextResource) + .param("name", "Bob") + .param("voice", "pirate")) + .user("Tell me about 3 famous pirates from the Golden Age of Piracy and what they did") + .call() + .chatResponse(); + // @formatter:on + + logger.info("" + response); + assertThat(response.getResults()).hasSize(1); + assertThat(response.getResults().get(0).getOutput().getText()).contains("Blackbeard"); + } + + @Test + void listOutputConverterString() { + // @formatter:off + List collection = ChatClient.create(this.chatModel).prompt() + .user(u -> u.text("List five {subject}") + .param("subject", "ice cream flavors")) + .call() + .entity(new ParameterizedTypeReference<>() { + }); + // @formatter:on + + logger.info(collection.toString()); + assertThat(collection).hasSize(5); + } + + @Test + void listOutputConverterBean() { + + // @formatter:off + List actorsFilms = ChatClient.create(this.chatModel).prompt() + .user("Generate the filmography of 5 movies for Tom Hanks and Bill Murray.") + .call() + .entity(new ParameterizedTypeReference<>() { + }); + // @formatter:on + + logger.info("" + actorsFilms); + assertThat(actorsFilms).hasSize(2); + } + + @Test + void customOutputConverter() { + + var toStringListConverter = new ListOutputConverter(new DefaultConversionService()); + + // @formatter:off + List flavors = ChatClient.create(this.chatModel).prompt() + .user(u -> u.text("List five {subject}") + .param("subject", "ice cream flavors")) + .call() + .entity(toStringListConverter); + // @formatter:on + + logger.info("ice cream flavors" + flavors); + assertThat(flavors).hasSize(5); + assertThat(flavors).containsAnyOf("Vanilla", "vanilla"); + } + + // @Test + void mapOutputConverter() { + // @formatter:off + Map result = ChatClient.create(this.chatModel).prompt() + .options(OpenAiSdkChatOptions.builder().model(com.openai.models.ChatModel.GPT_5_MINI.asString()).build()) + .user(u -> u.text("Provide me a List of {subject}") + .param("subject", "an array of numbers from 1 to 9 under they key name 'numbers'")) + .call() + .entity(new ParameterizedTypeReference<>() { + }); + // @formatter:on + + assertThat(result.get("numbers")).isEqualTo(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9)); + } + + @Test + void beanOutputConverter() { + + // @formatter:off + ActorsFilms actorsFilms = ChatClient.create(this.chatModel).prompt() + .user("Generate the filmography for a random actor.") + .call() + .entity(ActorsFilms.class); + // @formatter:on + + logger.info("" + actorsFilms); + assertThat(actorsFilms.actor()).isNotBlank(); + } + + @Test + void beanOutputConverterRecords() { + + // @formatter:off + ActorsFilms actorsFilms = ChatClient.create(this.chatModel).prompt() + .user("Generate the filmography of 5 movies for Tom Hanks.") + .call() + .entity(ActorsFilms.class); + // @formatter:on + + logger.info("" + actorsFilms); + assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks"); + assertThat(actorsFilms.movies()).hasSize(5); + } + + @Test + void beanStreamOutputConverterRecords() { + + BeanOutputConverter outputConverter = new BeanOutputConverter<>(ActorsFilms.class); + + // @formatter:off + Flux chatResponse = ChatClient.create(this.chatModel) + .prompt() + .options(OpenAiSdkChatOptions.builder().streamOptions(StreamOptions.builder().includeUsage(true).build()).build()) + .advisors(new SimpleLoggerAdvisor()) + .user(u -> u + .text("Generate the filmography of 5 movies for Tom Hanks. " + System.lineSeparator() + + "{format}") + .param("format", CurlyBracketEscaper.escapeCurlyBrackets(outputConverter.getFormat()))) + .stream() + .chatResponse(); + + List chatResponses = chatResponse.collectList() + .block() + .stream() + .toList(); + + String generationTextFromStream = chatResponses + .stream() + .filter(cr -> cr.getResult() != null) + .map(cr -> cr.getResult().getOutput().getText()) + .filter(text -> text != null && !text.trim().isEmpty()) // Filter out empty/null text + .collect(Collectors.joining()); + // @formatter:on + + // Add debugging to understand what text we're trying to parse + logger.debug("Aggregated streaming text: {}", generationTextFromStream); + + // Ensure we have valid JSON before attempting conversion + if (generationTextFromStream.trim().isEmpty()) { + fail("Empty aggregated text from streaming response - this indicates a problem with streaming aggregation"); + } + + ActorsFilms actorsFilms = outputConverter.convert(generationTextFromStream); + + logger.info("" + actorsFilms); + assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks"); + assertThat(actorsFilms.movies()).hasSize(5); + } + + @Test + void functionCallTest() { + + // @formatter:off + String response = ChatClient.create(this.chatModel).prompt() + .user(u -> u.text("What's the weather like in San Francisco, Tokyo, and Paris?")) + .toolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService()) + .description("Get the weather in location") + .inputType(MockWeatherService.Request.class) + .build()) + .call() + .content(); + // @formatter:on + + logger.info("Response: {}", response); + + assertThat(response).contains("30", "10", "15"); + } + + @Test + void defaultFunctionCallTest() { + + // @formatter:off + String response = ChatClient.builder(this.chatModel) + .defaultToolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService()) + .description("Get the weather in location") + .inputType(MockWeatherService.Request.class) + .build()) + .defaultUser(u -> u.text("What's the weather like in San Francisco, Tokyo, and Paris?")) + .build() + .prompt().call().content(); + // @formatter:on + + logger.info("Response: {}", response); + + assertThat(response).contains("30", "10", "15"); + } + + @Test + void streamFunctionCallTest() { + + // @formatter:off + Flux response = ChatClient.create(this.chatModel).prompt() + .user("What's the weather like in San Francisco, Tokyo, and Paris?") + .toolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService()) + .description("Get the weather in location") + .inputType(MockWeatherService.Request.class) + .build()) + .stream() + .content(); + // @formatter:on + + String content = response.collectList().block().stream().collect(Collectors.joining()); + logger.info("Response: {}", content); + + assertThat(content).contains("30", "10", "15"); + } + + @ParameterizedTest(name = "{0} : {displayName} ") + @ValueSource(strings = { "gpt-4o" }) + void multiModalityEmbeddedImage(String modelName) throws IOException { + + // @formatter:off + String response = ChatClient.create(this.chatModel).prompt() + .options(OpenAiSdkChatOptions.builder().model(modelName).build()) + .user(u -> u.text("Explain what do you see on this picture?") + .media(MimeTypeUtils.IMAGE_PNG, new ClassPathResource("/test.png"))) + .call() + .content(); + // @formatter:on + + logger.info(response); + assertThat(response).containsAnyOf("bananas", "apple", "bowl", "basket", "fruit stand"); + } + + @ParameterizedTest(name = "{0} : {displayName} ") + @ValueSource(strings = { "gpt-4o" }) + void multiModalityImageUrl(String modelName) throws IOException { + + URL url = new URL("https://docs.spring.io/spring-ai/reference/_images/multimodal.test.png"); + + // @formatter:off + String response = ChatClient.create(this.chatModel).prompt() + // TODO consider adding model(...) method to ChatClient as a shortcut to + .options(OpenAiSdkChatOptions.builder().model(modelName).build()) + .user(u -> u.text("Explain what do you see on this picture?").media(MimeTypeUtils.IMAGE_PNG, url)) + .call() + .content(); + // @formatter:on + + logger.info(response); + assertThat(response).containsAnyOf("bananas", "apple", "bowl", "basket", "fruit stand"); + } + + @Test + void streamingMultiModalityImageUrl() throws IOException { + + URL url = new URL("https://docs.spring.io/spring-ai/reference/_images/multimodal.test.png"); + + // @formatter:off + Flux response = ChatClient.create(this.chatModel).prompt() + .options(OpenAiSdkChatOptions.builder().model(com.openai.models.ChatModel.GPT_5_MINI.asString()) + .build()) + .user(u -> u.text("Explain what do you see on this picture?") + .media(MimeTypeUtils.IMAGE_PNG, url)) + .stream() + .content(); + // @formatter:on + + String content = response.collectList().block().stream().collect(Collectors.joining()); + + logger.info("Response: {}", content); + assertThat(content).containsAnyOf("bananas", "apple", "bowl", "basket", "fruit stand"); + } + + @Test + void multiModalityAudioResponse() { + + ChatResponse response = ChatClient.create(this.chatModel) + .prompt("Tell me joke about Spring Framework") + .options(OpenAiSdkChatOptions.builder() + .model(com.openai.models.ChatModel.GPT_4O_AUDIO_PREVIEW.asString()) + .outputAudio(new AudioParameters(AudioParameters.Voice.ALLOY, AudioParameters.AudioResponseFormat.WAV)) + .outputModalities(List.of(Modality.TEXT.asString(), Modality.AUDIO.asString())) + .build()) + .call() + .chatResponse(); + + assertThat(response).isNotNull(); + assertThat(response.getResult().getOutput().getMedia().get(0).getDataAsByteArray()).isNotEmpty(); + logger.info("Response: " + response); + } + + @Test + void customTemplateRendererWithCall() { + BeanOutputConverter outputConverter = new BeanOutputConverter<>(ActorsFilms.class); + + // @formatter:off + String result = ChatClient.create(this.chatModel).prompt() + .user(u -> u + .text("Generate the filmography of 5 movies for Tom Hanks. " + System.lineSeparator() + + "") + .param("format", outputConverter.getFormat())) + .templateRenderer(StTemplateRenderer.builder().startDelimiterToken('<').endDelimiterToken('>').build()) + .call() + .content(); + // @formatter:on + + assertThat(result).isNotEmpty(); + ActorsFilms actorsFilms = outputConverter.convert(result); + + logger.info("" + actorsFilms); + assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks"); + assertThat(actorsFilms.movies()).hasSize(5); + } + + @Test + void customTemplateRendererWithCallAndAdvisor() { + BeanOutputConverter outputConverter = new BeanOutputConverter<>(ActorsFilms.class); + + // @formatter:off + String result = ChatClient.create(this.chatModel).prompt() + .advisors(new SimpleLoggerAdvisor()) + .user(u -> u + .text("Generate the filmography of 5 movies for Tom Hanks. " + System.lineSeparator() + + "") + .param("format", outputConverter.getFormat())) + .templateRenderer(StTemplateRenderer.builder().startDelimiterToken('<').endDelimiterToken('>').build()) + .call() + .content(); + // @formatter:on + + assertThat(result).isNotEmpty(); + ActorsFilms actorsFilms = outputConverter.convert(result); + + logger.info("" + actorsFilms); + assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks"); + assertThat(actorsFilms.movies()).hasSize(5); + } + + @Test + void customTemplateRendererWithStream() { + BeanOutputConverter outputConverter = new BeanOutputConverter<>(ActorsFilms.class); + + // @formatter:off + Flux chatResponse = ChatClient.create(this.chatModel) + .prompt() + .options(OpenAiSdkChatOptions.builder().streamUsage(true).build()) + .user(u -> u + .text("Generate the filmography of 5 movies for Tom Hanks. " + System.lineSeparator() + + "") + .param("format", outputConverter.getFormat())) + .templateRenderer(StTemplateRenderer.builder().startDelimiterToken('<').endDelimiterToken('>').build()) + .stream() + .chatResponse(); + + List chatResponses = chatResponse.collectList() + .block() + .stream() + .toList(); + + String generationTextFromStream = chatResponses + .stream() + .filter(cr -> cr.getResult() != null) + .map(cr -> cr.getResult().getOutput().getText()) + .collect(Collectors.joining()); + // @formatter:on + + ActorsFilms actorsFilms = outputConverter.convert(generationTextFromStream); + + logger.info("" + actorsFilms); + assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks"); + assertThat(actorsFilms.movies()).hasSize(5); + } + + @Test + void customTemplateRendererWithStreamAndAdvisor() { + BeanOutputConverter outputConverter = new BeanOutputConverter<>(ActorsFilms.class); + + // @formatter:off + Flux chatResponse = ChatClient.create(this.chatModel) + .prompt() + .options(OpenAiSdkChatOptions.builder().streamUsage(true).build()) + .advisors(new SimpleLoggerAdvisor()) + .user(u -> u + .text("Generate the filmography of 5 movies for Tom Hanks. " + System.lineSeparator() + + "") + .param("format", outputConverter.getFormat())) + .templateRenderer(StTemplateRenderer.builder().startDelimiterToken('<').endDelimiterToken('>').build()) + .stream() + .chatResponse(); + + List chatResponses = chatResponse.collectList() + .block() + .stream() + .toList(); + + String generationTextFromStream = chatResponses + .stream() + .filter(cr -> cr.getResult() != null) + .map(cr -> cr.getResult().getOutput().getText()) + .collect(Collectors.joining()); + // @formatter:on + + ActorsFilms actorsFilms = outputConverter.convert(generationTextFromStream); + + logger.info("" + actorsFilms); + assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks"); + assertThat(actorsFilms.movies()).hasSize(5); + } + + record ActorsFilms(String actor, List movies) { + + } + +} diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java index b386acf859e..ff1b55fcd89 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java @@ -64,8 +64,7 @@ void defaultEmbedding() { assertThat(embeddingResponse.getMetadata().getUsage().getPromptTokens()).isEqualTo(2); assertThat(this.openAiSdkEmbeddingModel.dimensions()).isEqualTo(1536); - assertThat(embeddingResponse.getMetadata().getModel()) - .isEqualTo(EmbeddingModel.TEXT_EMBEDDING_ADA_002.toString()); + assertThat(embeddingResponse.getMetadata().getModel()).isEqualTo("text-embedding-ada-002-v2"); } @Test diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc index 807d604fe78..43030a5fe92 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc @@ -140,7 +140,8 @@ The prefix `spring.ai.openai-sdk.chat` is the property prefix for configuring th | spring.ai.openai-sdk.chat.options.max-tokens | The maximum number of tokens to generate. *Use for non-reasoning models* (e.g., gpt-4o, gpt-3.5-turbo). *Cannot be used with reasoning models* (e.g., o1, o3, o4-mini series). *Mutually exclusive with maxCompletionTokens*. | - | spring.ai.openai-sdk.chat.options.max-completion-tokens | An upper bound for the number of tokens that can be generated for a completion, including visible output tokens and reasoning tokens. *Required for reasoning models* (e.g., o1, o3, o4-mini series). *Cannot be used with non-reasoning models*. *Mutually exclusive with maxTokens*. | - | spring.ai.openai-sdk.chat.options.n | How many chat completion choices to generate for each input message. | 1 -| spring.ai.openai-sdk.chat.options.output-audio | Parameters for audio output. Required when audio output is requested. | - +| spring.ai.openai-sdk.chat.options.output-modalities | List of output modalities. Can include "text" and "audio". | - +| spring.ai.openai-sdk.chat.options.output-audio | Parameters for audio output. Use `AudioParameters` with voice (ALLOY, ASH, BALLAD, CORAL, ECHO, FABLE, ONYX, NOVA, SAGE, SHIMMER) and format (MP3, FLAC, OPUS, PCM16, WAV, AAC). | - | spring.ai.openai-sdk.chat.options.presence-penalty | Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far. | 0.0 | spring.ai.openai-sdk.chat.options.response-format.type | Response format type: `TEXT`, `JSON_OBJECT`, or `JSON_SCHEMA`. | TEXT | spring.ai.openai-sdk.chat.options.response-format.json-schema | JSON schema for structured outputs when type is `JSON_SCHEMA`. | - @@ -154,7 +155,8 @@ The prefix `spring.ai.openai-sdk.chat` is the property prefix for configuring th | spring.ai.openai-sdk.chat.options.store | Whether to store the output of this chat completion request for use in OpenAI's model distillation or evals products. | false | spring.ai.openai-sdk.chat.options.metadata | Developer-defined tags and values used for filtering completions in the dashboard. | - | spring.ai.openai-sdk.chat.options.service-tier | Specifies the latency tier to use: `auto`, `default`, `flex`, or `priority`. | - -| spring.ai.openai-sdk.chat.options.stream-usage | Whether to include usage statistics in streaming responses. | false +| spring.ai.openai-sdk.chat.options.stream-options.include-usage | Whether to include usage statistics in streaming responses. | false +| spring.ai.openai-sdk.chat.options.stream-options.include-obfuscation | Whether to include obfuscation in streaming responses. | false | spring.ai.openai-sdk.chat.options.tool-choice | Controls which (if any) function is called by the model. | - | spring.ai.openai-sdk.chat.options.internal-tool-execution-enabled | If false, Spring AI will proxy tool calls to the client for manual handling. If true (default), Spring AI handles function calls internally. | true |==== @@ -339,7 +341,8 @@ ChatResponse response = chatModel.call( new Prompt(userMessage, OpenAiSdkChatOptions.builder() .model("gpt-4o-audio-preview") - .outputAudio(new OutputAudio(Voice.ALLOY, AudioResponseFormat.WAV)) + .outputModalities(List.of("text", "audio")) + .outputAudio(new AudioParameters(Voice.ALLOY, AudioResponseFormat.WAV)) .build())); String text = response.getResult().getOutput().getContent(); // audio transcript From 3bdcf904c0647ca6ba270dd1688d29c1a6bd5b33 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Mon, 24 Nov 2025 10:48:57 +0100 Subject: [PATCH 31/49] Implementation of the OpenAI Java SDK - Fix test with the default Embedding model Signed-off-by: Julien Dubois --- .../ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java index ff1b55fcd89..9a44e897664 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java @@ -37,6 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.springframework.ai.openaisdk.OpenAiSdkEmbeddingOptions.DEFAULT_EMBEDDING_MODEL; /** * Integration tests for {@link OpenAiSdkEmbeddingModel}. @@ -64,7 +65,7 @@ void defaultEmbedding() { assertThat(embeddingResponse.getMetadata().getUsage().getPromptTokens()).isEqualTo(2); assertThat(this.openAiSdkEmbeddingModel.dimensions()).isEqualTo(1536); - assertThat(embeddingResponse.getMetadata().getModel()).isEqualTo("text-embedding-ada-002-v2"); + assertThat(embeddingResponse.getMetadata().getModel()).isEqualTo(DEFAULT_EMBEDDING_MODEL); } @Test From a59c8114d65692727853ea23005d68ae50d00e3f Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Mon, 24 Nov 2025 14:50:16 +0100 Subject: [PATCH 32/49] Implementation of the OpenAI Java SDK - Added authentication support in the *Options classes - Added documentation for authentication in the README.md file - Added Chat Models in the configuration script Signed-off-by: Julien Dubois --- models/spring-ai-openai-sdk/README.md | 81 +++++++++++ .../openaisdk/AbstractOpenAiSdkOptions.java | 1 - .../ai/openaisdk/OpenAiSdkChatOptions.java | 134 +++++++++++++----- .../openaisdk/OpenAiSdkEmbeddingOptions.java | 106 +++++++++++++- .../ai/openaisdk/OpenAiSdkImageOptions.java | 105 +++++++++++++- .../ai/openaisdk/setup/OpenAiSdkSetup.java | 2 +- .../openaisdk/chat/OpenAiSdkChatModelIT.java | 8 +- .../test/script/deploy-azure-openai-models.sh | 25 +++- 8 files changed, 418 insertions(+), 44 deletions(-) diff --git a/models/spring-ai-openai-sdk/README.md b/models/spring-ai-openai-sdk/README.md index a51fe3c2cf6..d511e5dc253 100644 --- a/models/spring-ai-openai-sdk/README.md +++ b/models/spring-ai-openai-sdk/README.md @@ -3,3 +3,84 @@ This is the official OpenAI Java SDK from OpenAI, which provides integration with OpenAI's services, including Azure OpenAI. [OpenAI Java API Library GitHub repository](https://github.com/openai/openai-java) + +## Authentication + +This module will try to automatically detect if you're using OpenAI, +Microsoft Foundry, or GitHub Models based on the provided base URL. + +Generic authentication is done using a URL and an API Key, such as: + +```java +OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder() + .baseUrl("https://.openai.azure.com/") + .apiKey("") + .build(); +``` + +Instead of providing the URL and API Key programmatically, you can also set them +using environment variables, using the keys below: + +```properties +OPENAI_BASE_URL=https://.openai.azure.com/ +OPENAI_API_KEY= +``` + +### Using OpenAI + +If you are using OpenAI, the base URL doesn't need to be set, as it's the default +`https://api.openai.com/v1` : + +```properties +OPENAI_BASE_URL=https://api.openai.com/v1 # Default value, can be omitted +OPENAI_API_KEY= +``` + +### Using Microsoft Foundry + +Microsoft Foundry will be automatically detected when using a Microsoft Foundry URL. +It can be forced if necessary by setting the `azure` configuration property to `true`. + +Here's an example using environment variables: + +```properties +OPENAI_BASE_URL=https://.openai.azure.com/ +OPENAI_API_KEY= +``` + +With Microsoft Foundry, you can also choose to use passwordless authentication, +without providing an API key. This is more secure, and is recommended approach +when running on Azure. + +To do so, you need to add the optional `com.azure:azure-identity` +dependency to your project. For example with Maven: + +```xml + + com.azure + azure-identity + +``` + +### Using GitHub Models + +GitHub Models will be automatically detected when using the GitHub Models base URL. +It can be forced if necessary by setting the `gitHubModels` configuration property to `true`. + +To authenticate, you'll need to create a GitHub Personal Access Token (PAT) with the `models:read` scope. + +Here's an example using environment variables: + +```properties +OPENAI_BASE_URL=https://models.github.ai/inference; +OPENAI_API_KEY=github_pat_XXXXXXXXXXX +``` + +## Logging + +As this module is built on top of the OpenAI Java SDK, you can enable logging +by setting the following environment variable: + +```properties +OPENAI_LOG=debug +``` diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java index e1d08e79302..89c95e8a88f 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java @@ -210,5 +210,4 @@ public Map getCustomHeaders() { public void setCustomHeaders(Map customHeaders) { this.customHeaders = customHeaders; } - } diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java index 045bff9e83a..c85b6f48582 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java @@ -101,8 +101,6 @@ public class OpenAiSdkChatOptions extends AbstractOpenAiSdkOptions implements To private Boolean internalToolExecutionEnabled; - private Map httpHeaders = new HashMap<>(); - private Map toolContext = new HashMap<>(); /** @@ -527,22 +525,6 @@ public void setInternalToolExecutionEnabled(@Nullable Boolean internalToolExecut this.internalToolExecutionEnabled = internalToolExecutionEnabled; } - /** - * Gets the HTTP headers to include in requests. - * @return the HTTP headers map - */ - public Map getHttpHeaders() { - return this.httpHeaders; - } - - /** - * Sets the HTTP headers to include in requests. - * @param httpHeaders the HTTP headers map - */ - public void setHttpHeaders(Map httpHeaders) { - this.httpHeaders = httpHeaders; - } - @Override public Map getToolContext() { return this.toolContext; @@ -595,7 +577,6 @@ public boolean equals(Object o) { && Objects.equals(this.toolCallbacks, options.toolCallbacks) && Objects.equals(this.toolNames, options.toolNames) && Objects.equals(this.internalToolExecutionEnabled, options.internalToolExecutionEnabled) - && Objects.equals(this.httpHeaders, options.httpHeaders) && Objects.equals(this.toolContext, options.toolContext); } @@ -606,7 +587,7 @@ public int hashCode() { this.presencePenalty, this.responseFormat, this.streamOptions, this.seed, this.stop, this.temperature, this.topP, this.toolChoice, this.user, this.parallelToolCalls, this.store, this.metadata, this.reasoningEffort, this.verbosity, this.serviceTier, this.toolCallbacks, this.toolNames, - this.internalToolExecutionEnabled, this.httpHeaders, this.toolContext); + this.internalToolExecutionEnabled, this.toolContext); } @Override @@ -622,8 +603,8 @@ public String toString() { + ", store=" + this.store + ", metadata=" + this.metadata + ", reasoningEffort='" + this.reasoningEffort + '\'' + ", verbosity='" + this.verbosity + '\'' + ", serviceTier='" + this.serviceTier + '\'' + ", toolCallbacks=" + this.toolCallbacks + ", toolNames=" + this.toolNames - + ", internalToolExecutionEnabled=" + this.internalToolExecutionEnabled + ", httpHeaders=" - + this.httpHeaders + ", toolContext=" + this.toolContext + '}'; + + ", internalToolExecutionEnabled=" + this.internalToolExecutionEnabled + ", toolContext=" + + this.toolContext + '}'; } public record AudioParameters(Voice voice, AudioResponseFormat format) { @@ -720,8 +701,21 @@ public static final class Builder { private final OpenAiSdkChatOptions options = new OpenAiSdkChatOptions(); public Builder from(OpenAiSdkChatOptions fromOptions) { + // Parent class fields + this.options.setBaseUrl(fromOptions.getBaseUrl()); + this.options.setApiKey(fromOptions.getApiKey()); + this.options.setCredential(fromOptions.getCredential()); this.options.setModel(fromOptions.getModel()); this.options.setDeploymentName(fromOptions.getDeploymentName()); + this.options.setAzureOpenAIServiceVersion(fromOptions.getAzureOpenAIServiceVersion()); + this.options.setOrganizationId(fromOptions.getOrganizationId()); + this.options.setAzure(fromOptions.isAzure()); + this.options.setGitHubModels(fromOptions.isGitHubModels()); + this.options.setTimeout(fromOptions.getTimeout()); + this.options.setMaxRetries(fromOptions.getMaxRetries()); + this.options.setProxy(fromOptions.getProxy()); + this.options.setCustomHeaders(fromOptions.getCustomHeaders()); + // Child class fields this.options.setFrequencyPenalty(fromOptions.getFrequencyPenalty()); this.options.setLogitBias(fromOptions.getLogitBias()); this.options.setLogprobs(fromOptions.getLogprobs()); @@ -743,8 +737,6 @@ public Builder from(OpenAiSdkChatOptions fromOptions) { this.options.setParallelToolCalls(fromOptions.getParallelToolCalls()); this.options.setToolCallbacks(new ArrayList<>(fromOptions.getToolCallbacks())); this.options.setToolNames(new HashSet<>(fromOptions.getToolNames())); - this.options.setHttpHeaders( - fromOptions.getHttpHeaders() != null ? new HashMap<>(fromOptions.getHttpHeaders()) : null); this.options.setInternalToolExecutionEnabled(fromOptions.getInternalToolExecutionEnabled()); this.options.setToolContext(new HashMap<>(fromOptions.getToolContext())); this.options.setStore(fromOptions.getStore()); @@ -756,12 +748,43 @@ public Builder from(OpenAiSdkChatOptions fromOptions) { } public Builder merge(OpenAiSdkChatOptions from) { + // Parent class fields + if (from.getBaseUrl() != null) { + this.options.setBaseUrl(from.getBaseUrl()); + } + if (from.getApiKey() != null) { + this.options.setApiKey(from.getApiKey()); + } + if (from.getCredential() != null) { + this.options.setCredential(from.getCredential()); + } if (from.getModel() != null) { this.options.setModel(from.getModel()); } if (from.getDeploymentName() != null) { this.options.setDeploymentName(from.getDeploymentName()); } + if (from.getAzureOpenAIServiceVersion() != null) { + this.options.setAzureOpenAIServiceVersion(from.getAzureOpenAIServiceVersion()); + } + if (from.getOrganizationId() != null) { + this.options.setOrganizationId(from.getOrganizationId()); + } + this.options.setAzure(from.isAzure()); + this.options.setGitHubModels(from.isGitHubModels()); + if (from.getTimeout() != null) { + this.options.setTimeout(from.getTimeout()); + } + if (from.getMaxRetries() != null) { + this.options.setMaxRetries(from.getMaxRetries()); + } + if (from.getProxy() != null) { + this.options.setProxy(from.getProxy()); + } + if (from.getCustomHeaders() != null) { + this.options.setCustomHeaders(from.getCustomHeaders()); + } + // Child class fields if (from.getFrequencyPenalty() != null) { this.options.setFrequencyPenalty(from.getFrequencyPenalty()); } @@ -825,9 +848,6 @@ public Builder merge(OpenAiSdkChatOptions from) { if (!from.getToolNames().isEmpty()) { this.options.setToolNames(new HashSet<>(from.getToolNames())); } - if (from.getHttpHeaders() != null) { - this.options.setHttpHeaders(new HashMap<>(from.getHttpHeaders())); - } if (from.getInternalToolExecutionEnabled() != null) { this.options.setInternalToolExecutionEnabled(from.getInternalToolExecutionEnabled()); } @@ -862,6 +882,61 @@ public Builder deploymentName(String deploymentName) { return this; } + public Builder baseUrl(String baseUrl) { + this.options.setBaseUrl(baseUrl); + return this; + } + + public Builder apiKey(String apiKey) { + this.options.setApiKey(apiKey); + return this; + } + + public Builder credential(com.openai.credential.Credential credential) { + this.options.setCredential(credential); + return this; + } + + public Builder azureOpenAIServiceVersion(com.openai.azure.AzureOpenAIServiceVersion azureOpenAIServiceVersion) { + this.options.setAzureOpenAIServiceVersion(azureOpenAIServiceVersion); + return this; + } + + public Builder organizationId(String organizationId) { + this.options.setOrganizationId(organizationId); + return this; + } + + public Builder azure(boolean azure) { + this.options.setAzure(azure); + return this; + } + + public Builder gitHubModels(boolean gitHubModels) { + this.options.setGitHubModels(gitHubModels); + return this; + } + + public Builder timeout(java.time.Duration timeout) { + this.options.setTimeout(timeout); + return this; + } + + public Builder maxRetries(Integer maxRetries) { + this.options.setMaxRetries(maxRetries); + return this; + } + + public Builder proxy(java.net.Proxy proxy) { + this.options.setProxy(proxy); + return this; + } + + public Builder customHeaders(Map customHeaders) { + this.options.setCustomHeaders(customHeaders); + return this; + } + public Builder frequencyPenalty(Double frequencyPenalty) { this.options.setFrequencyPenalty(frequencyPenalty); return this; @@ -1002,11 +1077,6 @@ public Builder toolNames(String... toolNames) { return this; } - public Builder httpHeaders(Map httpHeaders) { - this.options.setHttpHeaders(httpHeaders); - return this; - } - public Builder internalToolExecutionEnabled(@Nullable Boolean internalToolExecutionEnabled) { this.options.setInternalToolExecutionEnabled(internalToolExecutionEnabled); return this; diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingOptions.java index aca75d11634..83e45240e56 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingOptions.java @@ -102,18 +102,37 @@ public static final class Builder { private final OpenAiSdkEmbeddingOptions options = new OpenAiSdkEmbeddingOptions(); public Builder from(OpenAiSdkEmbeddingOptions fromOptions) { - this.options.setUser(fromOptions.getUser()); + // Parent class fields + this.options.setBaseUrl(fromOptions.getBaseUrl()); + this.options.setApiKey(fromOptions.getApiKey()); + this.options.setCredential(fromOptions.getCredential()); this.options.setModel(fromOptions.getModel()); this.options.setDeploymentName(fromOptions.getDeploymentName()); + this.options.setAzureOpenAIServiceVersion(fromOptions.getAzureOpenAIServiceVersion()); + this.options.setOrganizationId(fromOptions.getOrganizationId()); + this.options.setAzure(fromOptions.isAzure()); + this.options.setGitHubModels(fromOptions.isGitHubModels()); + this.options.setTimeout(fromOptions.getTimeout()); + this.options.setMaxRetries(fromOptions.getMaxRetries()); + this.options.setProxy(fromOptions.getProxy()); + this.options.setCustomHeaders(fromOptions.getCustomHeaders()); + // Child class fields + this.options.setUser(fromOptions.getUser()); this.options.setDimensions(fromOptions.getDimensions()); return this; } public Builder merge(EmbeddingOptions from) { if (from instanceof OpenAiSdkEmbeddingOptions castFrom) { - - if (castFrom.getUser() != null) { - this.options.setUser(castFrom.getUser()); + // Parent class fields + if (castFrom.getBaseUrl() != null) { + this.options.setBaseUrl(castFrom.getBaseUrl()); + } + if (castFrom.getApiKey() != null) { + this.options.setApiKey(castFrom.getApiKey()); + } + if (castFrom.getCredential() != null) { + this.options.setCredential(castFrom.getCredential()); } if (castFrom.getModel() != null) { this.options.setModel(castFrom.getModel()); @@ -121,6 +140,30 @@ public Builder merge(EmbeddingOptions from) { if (castFrom.getDeploymentName() != null) { this.options.setDeploymentName(castFrom.getDeploymentName()); } + if (castFrom.getAzureOpenAIServiceVersion() != null) { + this.options.setAzureOpenAIServiceVersion(castFrom.getAzureOpenAIServiceVersion()); + } + if (castFrom.getOrganizationId() != null) { + this.options.setOrganizationId(castFrom.getOrganizationId()); + } + this.options.setAzure(castFrom.isAzure()); + this.options.setGitHubModels(castFrom.isGitHubModels()); + if (castFrom.getTimeout() != null) { + this.options.setTimeout(castFrom.getTimeout()); + } + if (castFrom.getMaxRetries() != null) { + this.options.setMaxRetries(castFrom.getMaxRetries()); + } + if (castFrom.getProxy() != null) { + this.options.setProxy(castFrom.getProxy()); + } + if (castFrom.getCustomHeaders() != null) { + this.options.setCustomHeaders(castFrom.getCustomHeaders()); + } + // Child class fields + if (castFrom.getUser() != null) { + this.options.setUser(castFrom.getUser()); + } if (castFrom.getDimensions() != null) { this.options.setDimensions(castFrom.getDimensions()); } @@ -154,6 +197,61 @@ public Builder model(String model) { return this; } + public Builder baseUrl(String baseUrl) { + this.options.setBaseUrl(baseUrl); + return this; + } + + public Builder apiKey(String apiKey) { + this.options.setApiKey(apiKey); + return this; + } + + public Builder credential(com.openai.credential.Credential credential) { + this.options.setCredential(credential); + return this; + } + + public Builder azureOpenAIServiceVersion(com.openai.azure.AzureOpenAIServiceVersion azureOpenAIServiceVersion) { + this.options.setAzureOpenAIServiceVersion(azureOpenAIServiceVersion); + return this; + } + + public Builder organizationId(String organizationId) { + this.options.setOrganizationId(organizationId); + return this; + } + + public Builder azure(boolean azure) { + this.options.setAzure(azure); + return this; + } + + public Builder gitHubModels(boolean gitHubModels) { + this.options.setGitHubModels(gitHubModels); + return this; + } + + public Builder timeout(java.time.Duration timeout) { + this.options.setTimeout(timeout); + return this; + } + + public Builder maxRetries(Integer maxRetries) { + this.options.setMaxRetries(maxRetries); + return this; + } + + public Builder proxy(java.net.Proxy proxy) { + this.options.setProxy(proxy); + return this; + } + + public Builder customHeaders(java.util.Map customHeaders) { + this.options.setCustomHeaders(customHeaders); + return this; + } + public Builder dimensions(Integer dimensions) { this.options.dimensions = dimensions; return this; diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java index 27d3ed7af9d..485f9538e9d 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java @@ -233,9 +233,22 @@ private Builder() { } public Builder from(OpenAiSdkImageOptions fromOptions) { - this.options.setN(fromOptions.getN()); + // Parent class fields + this.options.setBaseUrl(fromOptions.getBaseUrl()); + this.options.setApiKey(fromOptions.getApiKey()); + this.options.setCredential(fromOptions.getCredential()); this.options.setModel(fromOptions.getModel()); this.options.setDeploymentName(fromOptions.getDeploymentName()); + this.options.setAzureOpenAIServiceVersion(fromOptions.getAzureOpenAIServiceVersion()); + this.options.setOrganizationId(fromOptions.getOrganizationId()); + this.options.setAzure(fromOptions.isAzure()); + this.options.setGitHubModels(fromOptions.isGitHubModels()); + this.options.setTimeout(fromOptions.getTimeout()); + this.options.setMaxRetries(fromOptions.getMaxRetries()); + this.options.setProxy(fromOptions.getProxy()); + this.options.setCustomHeaders(fromOptions.getCustomHeaders()); + // Child class fields + this.options.setN(fromOptions.getN()); this.options.setWidth(fromOptions.getWidth()); this.options.setHeight(fromOptions.getHeight()); this.options.setQuality(fromOptions.getQuality()); @@ -248,8 +261,15 @@ public Builder from(OpenAiSdkImageOptions fromOptions) { public Builder merge(ImageOptions from) { if (from instanceof OpenAiSdkImageOptions castFrom) { - if (castFrom.getN() != null) { - this.options.setN(castFrom.getN()); + // Parent class fields + if (castFrom.getBaseUrl() != null) { + this.options.setBaseUrl(castFrom.getBaseUrl()); + } + if (castFrom.getApiKey() != null) { + this.options.setApiKey(castFrom.getApiKey()); + } + if (castFrom.getCredential() != null) { + this.options.setCredential(castFrom.getCredential()); } if (castFrom.getModel() != null) { this.options.setModel(castFrom.getModel()); @@ -257,6 +277,30 @@ public Builder merge(ImageOptions from) { if (castFrom.getDeploymentName() != null) { this.options.setDeploymentName(castFrom.getDeploymentName()); } + if (castFrom.getAzureOpenAIServiceVersion() != null) { + this.options.setAzureOpenAIServiceVersion(castFrom.getAzureOpenAIServiceVersion()); + } + if (castFrom.getOrganizationId() != null) { + this.options.setOrganizationId(castFrom.getOrganizationId()); + } + this.options.setAzure(castFrom.isAzure()); + this.options.setGitHubModels(castFrom.isGitHubModels()); + if (castFrom.getTimeout() != null) { + this.options.setTimeout(castFrom.getTimeout()); + } + if (castFrom.getMaxRetries() != null) { + this.options.setMaxRetries(castFrom.getMaxRetries()); + } + if (castFrom.getProxy() != null) { + this.options.setProxy(castFrom.getProxy()); + } + if (castFrom.getCustomHeaders() != null) { + this.options.setCustomHeaders(castFrom.getCustomHeaders()); + } + // Child class fields + if (castFrom.getN() != null) { + this.options.setN(castFrom.getN()); + } if (castFrom.getWidth() != null) { this.options.setWidth(castFrom.getWidth()); } @@ -297,6 +341,61 @@ public Builder deploymentName(String deploymentName) { return this; } + public Builder baseUrl(String baseUrl) { + this.options.setBaseUrl(baseUrl); + return this; + } + + public Builder apiKey(String apiKey) { + this.options.setApiKey(apiKey); + return this; + } + + public Builder credential(com.openai.credential.Credential credential) { + this.options.setCredential(credential); + return this; + } + + public Builder azureOpenAIServiceVersion(com.openai.azure.AzureOpenAIServiceVersion azureOpenAIServiceVersion) { + this.options.setAzureOpenAIServiceVersion(azureOpenAIServiceVersion); + return this; + } + + public Builder organizationId(String organizationId) { + this.options.setOrganizationId(organizationId); + return this; + } + + public Builder azure(boolean azure) { + this.options.setAzure(azure); + return this; + } + + public Builder gitHubModels(boolean gitHubModels) { + this.options.setGitHubModels(gitHubModels); + return this; + } + + public Builder timeout(java.time.Duration timeout) { + this.options.setTimeout(timeout); + return this; + } + + public Builder maxRetries(Integer maxRetries) { + this.options.setMaxRetries(maxRetries); + return this; + } + + public Builder proxy(java.net.Proxy proxy) { + this.options.setProxy(proxy); + return this; + } + + public Builder customHeaders(java.util.Map customHeaders) { + this.options.setCustomHeaders(customHeaders); + return this; + } + public Builder responseFormat(String responseFormat) { this.options.setResponseFormat(responseFormat); return this; diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java index b4d32af8c90..9c7b501df77 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java @@ -44,7 +44,7 @@ public final class OpenAiSdkSetup { static final String OPENAI_URL = "https://api.openai.com/v1"; static final String OPENAI_API_KEY = "OPENAI_API_KEY"; static final String AZURE_OPENAI_KEY = "AZURE_OPENAI_KEY"; - static final String GITHUB_MODELS_URL = "https://models.inference.ai.azure.com"; + static final String GITHUB_MODELS_URL = "https://models.github.ai/inference"; static final String GITHUB_TOKEN = "GITHUB_TOKEN"; static final String DEFAULT_USER_AGENT = "spring-ai-openai-sdk"; diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java index 9bd09a06725..d9c9fb29bbc 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java @@ -29,6 +29,7 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; +import com.openai.models.ChatModel; import com.openai.models.ReasoningEffort; import org.assertj.core.data.Percentage; import org.junit.jupiter.api.Test; @@ -95,6 +96,9 @@ public class OpenAiSdkChatModelIT { private static final Logger logger = LoggerFactory.getLogger(OpenAiSdkChatModelIT.class); + // It would be better to use ChatModel.GPT_4O_AUDIO_PREVIEW.asString(); but it can't be used as a constant. + public static final String DEFAULT_CHAT_MODEL_AUDIO = "gpt-4o-audio-preview"; + @Value("classpath:/prompts/system-message.st") private Resource systemResource; @@ -547,7 +551,7 @@ void streamingMultiModalityImageUrl() throws IOException { } @ParameterizedTest(name = "{0} : {displayName} ") - @ValueSource(strings = { "gpt-4o-audio-preview" }) + @ValueSource(strings = { DEFAULT_CHAT_MODEL_AUDIO }) void multiModalityOutputAudio(String modelName) throws IOException { var userMessage = new UserMessage("Tell me joke about Spring Framework"); @@ -566,7 +570,7 @@ void multiModalityOutputAudio(String modelName) throws IOException { } @ParameterizedTest(name = "{0} : {displayName} ") - @ValueSource(strings = { "gpt-4o-audio-preview" }) + @ValueSource(strings = { DEFAULT_CHAT_MODEL_AUDIO }) void streamingMultiModalityOutputAudio(String modelName) { var userMessage = new UserMessage("Tell me joke about Spring Framework"); diff --git a/models/spring-ai-openai-sdk/src/test/script/deploy-azure-openai-models.sh b/models/spring-ai-openai-sdk/src/test/script/deploy-azure-openai-models.sh index a402a097e3a..d6bbbb66899 100755 --- a/models/spring-ai-openai-sdk/src/test/script/deploy-azure-openai-models.sh +++ b/models/spring-ai-openai-sdk/src/test/script/deploy-azure-openai-models.sh @@ -44,7 +44,30 @@ az cognitiveservices account create \ --sku "S0" # If you want to know the available models, run the following Azure CLI command: -# az cognitiveservices account list-models --resource-group "$RESOURCE_GROUP" --name "$AI_SERVICE" -o table +# az cognitiveservices account list-models --resource-group "$RESOURCE_GROUP" --name "$AI_SERVICE" -o table + +echo "Deploying Chat Models" +echo "==========================" + +models=("gpt-5" "gpt-5-mini" "gpt-4o-audio-preview") +versions=("2025-08-07" "2025-08-07" "2024-12-17") +skus=("GlobalStandard" "GlobalStandard" "GlobalStandard") + +for i in "${!models[@]}"; do + model="${models[$i]}" + sku="${skus[$i]}" + version="${versions[$i]}" + echo "Deploying $model..." + az cognitiveservices account deployment create \ + --name "$AI_SERVICE" \ + --resource-group "$RESOURCE_GROUP" \ + --deployment-name "$model" \ + --model-name "$model" \ + --model-version "$version"\ + --model-format "OpenAI" \ + --sku-capacity 1 \ + --sku-name "$sku" || echo "Failed to deploy $model. Check SKU and region compatibility." +done echo "Deploying Embedding Models" echo "==========================" From 4c995fd796f571cb11452a9232b4480e41d96441 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Mon, 24 Nov 2025 15:23:05 +0100 Subject: [PATCH 33/49] Implementation of the OpenAI Java SDK - Reactored Azure OpenAI to Microsoft Foundry (new name announced last week) Signed-off-by: Julien Dubois --- .../OpenAiSdkAutoConfigurationUtil.java | 10 ++-- .../OpenAiSdkChatAutoConfiguration.java | 8 ++-- .../OpenAiSdkEmbeddingAutoConfiguration.java | 4 +- .../OpenAiSdkImageAutoConfiguration.java | 4 +- models/spring-ai-openai-sdk/README.md | 10 ++-- .../openaisdk/AbstractOpenAiSdkOptions.java | 46 +++++++++---------- .../ai/openaisdk/OpenAiSdkChatModel.java | 19 +++----- .../ai/openaisdk/OpenAiSdkChatOptions.java | 14 +++--- .../ai/openaisdk/OpenAiSdkEmbeddingModel.java | 6 +-- .../openaisdk/OpenAiSdkEmbeddingOptions.java | 16 +++---- .../ai/openaisdk/OpenAiSdkImageModel.java | 6 +-- .../ai/openaisdk/OpenAiSdkImageOptions.java | 16 +++---- .../ai/openaisdk/setup/OpenAiSdkSetup.java | 26 +++++------ .../ai/openaisdk/setup/package-info.java | 2 +- .../chat/OpenAiSdkChatOptionsTests.java | 26 +++++------ .../openaisdk/setup/OpenAiSdkSetupTests.java | 4 +- ....sh => deploy-microsoft-foundry-models.sh} | 2 +- pom.xml | 2 +- 18 files changed, 108 insertions(+), 113 deletions(-) rename models/spring-ai-openai-sdk/src/test/script/{deploy-azure-openai-models.sh => deploy-microsoft-foundry-models.sh} (97%) diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkAutoConfigurationUtil.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkAutoConfigurationUtil.java index 3700806f257..138e4aebdb6 100644 --- a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkAutoConfigurationUtil.java +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkAutoConfigurationUtil.java @@ -49,15 +49,15 @@ public static ResolvedConnectionProperties resolveConnectionProperties(AbstractO resolved.setModel(StringUtils.hasText(modelProperties.getModel()) ? modelProperties.getModel() : commonProperties.getModel()); - resolved.setAzureDeploymentName(StringUtils.hasText(modelProperties.getAzureDeploymentName()) - ? modelProperties.getAzureDeploymentName() : commonProperties.getAzureDeploymentName()); + resolved.setMicrosoftDeploymentName(StringUtils.hasText(modelProperties.getMicrosoftDeploymentName()) + ? modelProperties.getMicrosoftDeploymentName() : commonProperties.getMicrosoftDeploymentName()); - resolved.setAzureOpenAIServiceVersion(modelProperties.getAzureOpenAIServiceVersion() != null - ? modelProperties.getAzureOpenAIServiceVersion() : commonProperties.getAzureOpenAIServiceVersion()); + resolved.setMicrosoftFoundryServiceVersion(modelProperties.getMicrosoftFoundryServiceVersion() != null + ? modelProperties.getMicrosoftFoundryServiceVersion() : commonProperties.getMicrosoftFoundryServiceVersion()); // For boolean properties, use modelProperties value, defaulting to // commonProperties if needed - resolved.setAzure(modelProperties.isAzure() || commonProperties.isAzure()); + resolved.setMicrosoftFoundry(modelProperties.isMicrosoftFoundry() || commonProperties.isMicrosoftFoundry()); resolved.setGitHubModels(modelProperties.isGitHubModels() || commonProperties.isGitHubModels()); diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfiguration.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfiguration.java index 2c2c698ac2c..f7dc09f4e43 100644 --- a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfiguration.java +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfiguration.java @@ -75,16 +75,16 @@ public OpenAiSdkChatModel openAiChatModel(OpenAiSdkConnectionProperties commonPr private OpenAIClient openAiClient(AbstractOpenAiSdkOptions resolved) { return OpenAiSdkSetup.setupSyncClient(resolved.getBaseUrl(), resolved.getApiKey(), resolved.getCredential(), - resolved.getAzureDeploymentName(), resolved.getAzureOpenAIServiceVersion(), - resolved.getOrganizationId(), resolved.isAzure(), resolved.isGitHubModels(), resolved.getModel(), + resolved.getMicrosoftDeploymentName(), resolved.getMicrosoftFoundryServiceVersion(), + resolved.getOrganizationId(), resolved.isMicrosoftFoundry(), resolved.isGitHubModels(), resolved.getModel(), resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), resolved.getCustomHeaders()); } private OpenAIClientAsync openAiClientAsync(AbstractOpenAiSdkOptions resolved) { return OpenAiSdkSetup.setupAsyncClient(resolved.getBaseUrl(), resolved.getApiKey(), resolved.getCredential(), - resolved.getAzureDeploymentName(), resolved.getAzureOpenAIServiceVersion(), - resolved.getOrganizationId(), resolved.isAzure(), resolved.isGitHubModels(), resolved.getModel(), + resolved.getMicrosoftDeploymentName(), resolved.getMicrosoftFoundryServiceVersion(), + resolved.getOrganizationId(), resolved.isMicrosoftFoundry(), resolved.isGitHubModels(), resolved.getModel(), resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), resolved.getCustomHeaders()); } diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfiguration.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfiguration.java index 31184d3ac39..bed0b7defcd 100644 --- a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfiguration.java +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfiguration.java @@ -64,8 +64,8 @@ private OpenAIClient openAiClient(OpenAiSdkConnectionProperties commonProperties .resolveConnectionProperties(commonProperties, embeddingProperties); return OpenAiSdkSetup.setupSyncClient(resolved.getBaseUrl(), resolved.getApiKey(), resolved.getCredential(), - resolved.getAzureDeploymentName(), resolved.getAzureOpenAIServiceVersion(), - resolved.getOrganizationId(), resolved.isAzure(), resolved.isGitHubModels(), resolved.getModel(), + resolved.getMicrosoftDeploymentName(), resolved.getMicrosoftFoundryServiceVersion(), + resolved.getOrganizationId(), resolved.isMicrosoftFoundry(), resolved.isGitHubModels(), resolved.getModel(), resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), resolved.getCustomHeaders()); } diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfiguration.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfiguration.java index e25d9a3528f..72be5126a50 100644 --- a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfiguration.java +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfiguration.java @@ -63,8 +63,8 @@ private OpenAIClient openAiClient(OpenAiSdkConnectionProperties commonProperties .resolveConnectionProperties(commonProperties, imageProperties); return OpenAiSdkSetup.setupSyncClient(resolved.getBaseUrl(), resolved.getApiKey(), resolved.getCredential(), - resolved.getAzureDeploymentName(), resolved.getAzureOpenAIServiceVersion(), - resolved.getOrganizationId(), resolved.isAzure(), resolved.isGitHubModels(), resolved.getModel(), + resolved.getMicrosoftDeploymentName(), resolved.getMicrosoftFoundryServiceVersion(), + resolved.getOrganizationId(), resolved.isMicrosoftFoundry(), resolved.isGitHubModels(), resolved.getModel(), resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), resolved.getCustomHeaders()); } diff --git a/models/spring-ai-openai-sdk/README.md b/models/spring-ai-openai-sdk/README.md index d511e5dc253..66e5b980a12 100644 --- a/models/spring-ai-openai-sdk/README.md +++ b/models/spring-ai-openai-sdk/README.md @@ -1,6 +1,6 @@ # OpenAI Java API Library -This is the official OpenAI Java SDK from OpenAI, which provides integration with OpenAI's services, including Azure OpenAI. +This is the official OpenAI Java SDK from OpenAI, which provides integration with OpenAI's services, including Microsoft Foundry. [OpenAI Java API Library GitHub repository](https://github.com/openai/openai-java) @@ -13,7 +13,7 @@ Generic authentication is done using a URL and an API Key, such as: ```java OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder() - .baseUrl("https://.openai.azure.com/") + .baseUrl("https://.openai.microsoftFoundry.com/") .apiKey("") .build(); ``` @@ -22,7 +22,7 @@ Instead of providing the URL and API Key programmatically, you can also set them using environment variables, using the keys below: ```properties -OPENAI_BASE_URL=https://.openai.azure.com/ +OPENAI_BASE_URL=https://.openai.microsoftFoundry.com/ OPENAI_API_KEY= ``` @@ -39,12 +39,12 @@ OPENAI_API_KEY= ### Using Microsoft Foundry Microsoft Foundry will be automatically detected when using a Microsoft Foundry URL. -It can be forced if necessary by setting the `azure` configuration property to `true`. +It can be forced if necessary by setting the `microsoftFoundry` configuration property to `true`. Here's an example using environment variables: ```properties -OPENAI_BASE_URL=https://.openai.azure.com/ +OPENAI_BASE_URL=https://.openai.microsoftFoundry.com/ OPENAI_API_KEY= ``` diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java index 89c95e8a88f..f5ced94a174 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java @@ -36,37 +36,37 @@ public class AbstractOpenAiSdkOptions { private String apiKey; /** - * Credentials used to connect to Azure OpenAI. + * Credentials used to connect to Microsoft Foundry. */ private Credential credential; /** - * The model name used. When using Azure AI Foundry, this is also used as the default + * The model name used. When using Microsoft Foundry, this is also used as the default * deployment name. */ private String model; /** - * The deployment name as defined in Azure AI Foundry. On Azure AI Foundry, the + * The deployment name as defined in Microsoft Foundry. On Microsoft Foundry, the * default deployment name is the same as the model name. When using OpenAI directly, * this value isn't used. */ - private String azureDeploymentName; + private String microsoftDeploymentName; /** - * The Azure OpenAI Service version to use when connecting to Azure AI Foundry. + * The Service version to use when connecting to Microsoft Foundry. */ - private AzureOpenAIServiceVersion azureOpenAIServiceVersion; + private AzureOpenAIServiceVersion microsoftFoundryServiceVersion; /** - * The organization ID to use when connecting to Azure OpenAI. + * The organization ID to use when connecting to Microsoft Foundry. */ private String organizationId; /** - * Whether Azure OpenAI is detected. + * Whether Microsoft Foundry is detected. */ - private boolean isAzure; + private boolean isMicrosoftFoundry; /** * Whether GitHub Models is detected. @@ -125,34 +125,34 @@ public void setModel(String model) { this.model = model; } - public String getAzureDeploymentName() { - return this.azureDeploymentName; + public String getMicrosoftDeploymentName() { + return this.microsoftDeploymentName; } - public void setAzureDeploymentName(String azureDeploymentName) { - this.azureDeploymentName = azureDeploymentName; + public void setMicrosoftDeploymentName(String microsoftDeploymentName) { + this.microsoftDeploymentName = microsoftDeploymentName; } /** * Alias for getAzureDeploymentName() */ public String getDeploymentName() { - return this.azureDeploymentName; + return this.microsoftDeploymentName; } /** * Alias for setAzureDeploymentName() */ public void setDeploymentName(String azureDeploymentName) { - this.azureDeploymentName = azureDeploymentName; + this.microsoftDeploymentName = azureDeploymentName; } - public AzureOpenAIServiceVersion getAzureOpenAIServiceVersion() { - return this.azureOpenAIServiceVersion; + public AzureOpenAIServiceVersion getMicrosoftFoundryServiceVersion() { + return this.microsoftFoundryServiceVersion; } - public void setAzureOpenAIServiceVersion(AzureOpenAIServiceVersion azureOpenAIServiceVersion) { - this.azureOpenAIServiceVersion = azureOpenAIServiceVersion; + public void setMicrosoftFoundryServiceVersion(AzureOpenAIServiceVersion microsoftFoundryServiceVersion) { + this.microsoftFoundryServiceVersion = microsoftFoundryServiceVersion; } public String getOrganizationId() { @@ -163,12 +163,12 @@ public void setOrganizationId(String organizationId) { this.organizationId = organizationId; } - public boolean isAzure() { - return this.isAzure; + public boolean isMicrosoftFoundry() { + return this.isMicrosoftFoundry; } - public void setAzure(boolean azure) { - this.isAzure = azure; + public void setMicrosoftFoundry(boolean microsoftFoundry) { + this.isMicrosoftFoundry = microsoftFoundry; } public boolean isGitHubModels() { diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java index a218683aad1..46db7378862 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatModel.java @@ -223,17 +223,17 @@ public OpenAiSdkChatModel(OpenAIClient openAiClient, OpenAIClientAsync openAiCli } this.openAiClient = Objects.requireNonNullElseGet(openAiClient, () -> OpenAiSdkSetup.setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), - this.options.getCredential(), this.options.getAzureDeploymentName(), - this.options.getAzureOpenAIServiceVersion(), this.options.getOrganizationId(), - this.options.isAzure(), this.options.isGitHubModels(), this.options.getModel(), + this.options.getCredential(), this.options.getMicrosoftDeploymentName(), + this.options.getMicrosoftFoundryServiceVersion(), this.options.getOrganizationId(), + this.options.isMicrosoftFoundry(), this.options.isGitHubModels(), this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), this.options.getProxy(), this.options.getCustomHeaders())); this.openAiClientAsync = Objects.requireNonNullElseGet(openAiClientAsync, () -> OpenAiSdkSetup.setupAsyncClient(this.options.getBaseUrl(), this.options.getApiKey(), - this.options.getCredential(), this.options.getAzureDeploymentName(), - this.options.getAzureOpenAIServiceVersion(), this.options.getOrganizationId(), - this.options.isAzure(), this.options.isGitHubModels(), this.options.getModel(), + this.options.getCredential(), this.options.getMicrosoftDeploymentName(), + this.options.getMicrosoftFoundryServiceVersion(), this.options.getOrganizationId(), + this.options.isMicrosoftFoundry(), this.options.isGitHubModels(), this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), this.options.getProxy(), this.options.getCustomHeaders())); @@ -738,10 +738,6 @@ Prompt buildRequestPrompt(Prompt prompt) { logger.warn("The topK option is not supported by OpenAI chat models. Ignoring."); } - Map mergedHttpHeaders = new HashMap<>(this.options.getHttpHeaders()); - mergedHttpHeaders.putAll(runtimeOptions.getHttpHeaders()); - requestOptions.setHttpHeaders(mergedHttpHeaders); - requestOptions.setInternalToolExecutionEnabled(runtimeOptions.getInternalToolExecutionEnabled() != null ? runtimeOptions.getInternalToolExecutionEnabled() : this.options.getInternalToolExecutionEnabled()); @@ -753,7 +749,6 @@ Prompt buildRequestPrompt(Prompt prompt) { this.options.getToolContext())); } else { - requestOptions.setHttpHeaders(this.options.getHttpHeaders()); requestOptions.setInternalToolExecutionEnabled(this.options.getInternalToolExecutionEnabled()); requestOptions.setToolNames(this.options.getToolNames()); requestOptions.setToolCallbacks(this.options.getToolCallbacks()); @@ -932,7 +927,7 @@ else if (message.getMessageType() == MessageType.TOOL) { OpenAiSdkChatOptions requestOptions = (OpenAiSdkChatOptions) prompt.getOptions(); - // Use deployment name if available (for Azure AI Foundry), otherwise use model + // Use deployment name if available (for Microsoft Foundry), otherwise use model // name if (requestOptions.getDeploymentName() != null) { builder.model(requestOptions.getDeploymentName()); diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java index c85b6f48582..918718cdaf1 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java @@ -707,9 +707,9 @@ public Builder from(OpenAiSdkChatOptions fromOptions) { this.options.setCredential(fromOptions.getCredential()); this.options.setModel(fromOptions.getModel()); this.options.setDeploymentName(fromOptions.getDeploymentName()); - this.options.setAzureOpenAIServiceVersion(fromOptions.getAzureOpenAIServiceVersion()); + this.options.setMicrosoftFoundryServiceVersion(fromOptions.getMicrosoftFoundryServiceVersion()); this.options.setOrganizationId(fromOptions.getOrganizationId()); - this.options.setAzure(fromOptions.isAzure()); + this.options.setMicrosoftFoundry(fromOptions.isMicrosoftFoundry()); this.options.setGitHubModels(fromOptions.isGitHubModels()); this.options.setTimeout(fromOptions.getTimeout()); this.options.setMaxRetries(fromOptions.getMaxRetries()); @@ -764,13 +764,13 @@ public Builder merge(OpenAiSdkChatOptions from) { if (from.getDeploymentName() != null) { this.options.setDeploymentName(from.getDeploymentName()); } - if (from.getAzureOpenAIServiceVersion() != null) { - this.options.setAzureOpenAIServiceVersion(from.getAzureOpenAIServiceVersion()); + if (from.getMicrosoftFoundryServiceVersion() != null) { + this.options.setMicrosoftFoundryServiceVersion(from.getMicrosoftFoundryServiceVersion()); } if (from.getOrganizationId() != null) { this.options.setOrganizationId(from.getOrganizationId()); } - this.options.setAzure(from.isAzure()); + this.options.setMicrosoftFoundry(from.isMicrosoftFoundry()); this.options.setGitHubModels(from.isGitHubModels()); if (from.getTimeout() != null) { this.options.setTimeout(from.getTimeout()); @@ -898,7 +898,7 @@ public Builder credential(com.openai.credential.Credential credential) { } public Builder azureOpenAIServiceVersion(com.openai.azure.AzureOpenAIServiceVersion azureOpenAIServiceVersion) { - this.options.setAzureOpenAIServiceVersion(azureOpenAIServiceVersion); + this.options.setMicrosoftFoundryServiceVersion(azureOpenAIServiceVersion); return this; } @@ -908,7 +908,7 @@ public Builder organizationId(String organizationId) { } public Builder azure(boolean azure) { - this.options.setAzure(azure); + this.options.setMicrosoftFoundry(azure); return this; } diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingModel.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingModel.java index ff23e63d962..7540168d815 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingModel.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingModel.java @@ -161,9 +161,9 @@ public OpenAiSdkEmbeddingModel(OpenAIClient openAiClient, MetadataMode metadataM } this.openAiClient = Objects.requireNonNullElseGet(openAiClient, () -> OpenAiSdkSetup.setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), - this.options.getCredential(), this.options.getAzureDeploymentName(), - this.options.getAzureOpenAIServiceVersion(), this.options.getOrganizationId(), - this.options.isAzure(), this.options.isGitHubModels(), this.options.getModel(), + this.options.getCredential(), this.options.getMicrosoftDeploymentName(), + this.options.getMicrosoftFoundryServiceVersion(), this.options.getOrganizationId(), + this.options.isMicrosoftFoundry(), this.options.isGitHubModels(), this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), this.options.getProxy(), this.options.getCustomHeaders())); this.metadataMode = Objects.requireNonNullElse(metadataMode, MetadataMode.EMBED); diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingOptions.java index 83e45240e56..d3b73550700 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingOptions.java @@ -76,7 +76,7 @@ public EmbeddingCreateParams toOpenAiCreateParams(List instructions) { EmbeddingCreateParams.Builder builder = EmbeddingCreateParams.builder(); - // Use deployment name if available (for Azure AI Foundry), otherwise use model + // Use deployment name if available (for Microsoft Foundry), otherwise use model // name if (this.getDeploymentName() != null) { builder.model(this.getDeploymentName()); @@ -108,9 +108,9 @@ public Builder from(OpenAiSdkEmbeddingOptions fromOptions) { this.options.setCredential(fromOptions.getCredential()); this.options.setModel(fromOptions.getModel()); this.options.setDeploymentName(fromOptions.getDeploymentName()); - this.options.setAzureOpenAIServiceVersion(fromOptions.getAzureOpenAIServiceVersion()); + this.options.setMicrosoftFoundryServiceVersion(fromOptions.getMicrosoftFoundryServiceVersion()); this.options.setOrganizationId(fromOptions.getOrganizationId()); - this.options.setAzure(fromOptions.isAzure()); + this.options.setMicrosoftFoundry(fromOptions.isMicrosoftFoundry()); this.options.setGitHubModels(fromOptions.isGitHubModels()); this.options.setTimeout(fromOptions.getTimeout()); this.options.setMaxRetries(fromOptions.getMaxRetries()); @@ -140,13 +140,13 @@ public Builder merge(EmbeddingOptions from) { if (castFrom.getDeploymentName() != null) { this.options.setDeploymentName(castFrom.getDeploymentName()); } - if (castFrom.getAzureOpenAIServiceVersion() != null) { - this.options.setAzureOpenAIServiceVersion(castFrom.getAzureOpenAIServiceVersion()); + if (castFrom.getMicrosoftFoundryServiceVersion() != null) { + this.options.setMicrosoftFoundryServiceVersion(castFrom.getMicrosoftFoundryServiceVersion()); } if (castFrom.getOrganizationId() != null) { this.options.setOrganizationId(castFrom.getOrganizationId()); } - this.options.setAzure(castFrom.isAzure()); + this.options.setMicrosoftFoundry(castFrom.isMicrosoftFoundry()); this.options.setGitHubModels(castFrom.isGitHubModels()); if (castFrom.getTimeout() != null) { this.options.setTimeout(castFrom.getTimeout()); @@ -213,7 +213,7 @@ public Builder credential(com.openai.credential.Credential credential) { } public Builder azureOpenAIServiceVersion(com.openai.azure.AzureOpenAIServiceVersion azureOpenAIServiceVersion) { - this.options.setAzureOpenAIServiceVersion(azureOpenAIServiceVersion); + this.options.setMicrosoftFoundryServiceVersion(azureOpenAIServiceVersion); return this; } @@ -223,7 +223,7 @@ public Builder organizationId(String organizationId) { } public Builder azure(boolean azure) { - this.options.setAzure(azure); + this.options.setMicrosoftFoundry(azure); return this; } diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageModel.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageModel.java index 871339bf2cd..1bb52fb15dd 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageModel.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageModel.java @@ -138,9 +138,9 @@ public OpenAiSdkImageModel(OpenAIClient openAiClient, OpenAiSdkImageOptions opti } this.openAiClient = Objects.requireNonNullElseGet(openAiClient, () -> OpenAiSdkSetup.setupSyncClient(this.options.getBaseUrl(), this.options.getApiKey(), - this.options.getCredential(), this.options.getAzureDeploymentName(), - this.options.getAzureOpenAIServiceVersion(), this.options.getOrganizationId(), - this.options.isAzure(), this.options.isGitHubModels(), this.options.getModel(), + this.options.getCredential(), this.options.getMicrosoftDeploymentName(), + this.options.getMicrosoftFoundryServiceVersion(), this.options.getOrganizationId(), + this.options.isMicrosoftFoundry(), this.options.isGitHubModels(), this.options.getModel(), this.options.getTimeout(), this.options.getMaxRetries(), this.options.getProxy(), this.options.getCustomHeaders())); this.observationRegistry = Objects.requireNonNullElse(observationRegistry, ObservationRegistry.NOOP); diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java index 485f9538e9d..711c1e2e9a5 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java @@ -193,7 +193,7 @@ public ImageGenerateParams toOpenAiImageGenerateParams(ImagePrompt imagePrompt) String prompt = imagePrompt.getInstructions().get(0).getText(); ImageGenerateParams.Builder builder = ImageGenerateParams.builder().prompt(prompt); - // Use deployment name if available (for Azure AI Foundry), otherwise use model + // Use deployment name if available (for Microsoft Foundry), otherwise use model // name if (this.getDeploymentName() != null) { builder.model(this.getDeploymentName()); @@ -239,9 +239,9 @@ public Builder from(OpenAiSdkImageOptions fromOptions) { this.options.setCredential(fromOptions.getCredential()); this.options.setModel(fromOptions.getModel()); this.options.setDeploymentName(fromOptions.getDeploymentName()); - this.options.setAzureOpenAIServiceVersion(fromOptions.getAzureOpenAIServiceVersion()); + this.options.setMicrosoftFoundryServiceVersion(fromOptions.getMicrosoftFoundryServiceVersion()); this.options.setOrganizationId(fromOptions.getOrganizationId()); - this.options.setAzure(fromOptions.isAzure()); + this.options.setMicrosoftFoundry(fromOptions.isMicrosoftFoundry()); this.options.setGitHubModels(fromOptions.isGitHubModels()); this.options.setTimeout(fromOptions.getTimeout()); this.options.setMaxRetries(fromOptions.getMaxRetries()); @@ -277,13 +277,13 @@ public Builder merge(ImageOptions from) { if (castFrom.getDeploymentName() != null) { this.options.setDeploymentName(castFrom.getDeploymentName()); } - if (castFrom.getAzureOpenAIServiceVersion() != null) { - this.options.setAzureOpenAIServiceVersion(castFrom.getAzureOpenAIServiceVersion()); + if (castFrom.getMicrosoftFoundryServiceVersion() != null) { + this.options.setMicrosoftFoundryServiceVersion(castFrom.getMicrosoftFoundryServiceVersion()); } if (castFrom.getOrganizationId() != null) { this.options.setOrganizationId(castFrom.getOrganizationId()); } - this.options.setAzure(castFrom.isAzure()); + this.options.setMicrosoftFoundry(castFrom.isMicrosoftFoundry()); this.options.setGitHubModels(castFrom.isGitHubModels()); if (castFrom.getTimeout() != null) { this.options.setTimeout(castFrom.getTimeout()); @@ -357,7 +357,7 @@ public Builder credential(com.openai.credential.Credential credential) { } public Builder azureOpenAIServiceVersion(com.openai.azure.AzureOpenAIServiceVersion azureOpenAIServiceVersion) { - this.options.setAzureOpenAIServiceVersion(azureOpenAIServiceVersion); + this.options.setMicrosoftFoundryServiceVersion(azureOpenAIServiceVersion); return this; } @@ -367,7 +367,7 @@ public Builder organizationId(String organizationId) { } public Builder azure(boolean azure) { - this.options.setAzure(azure); + this.options.setMicrosoftFoundry(azure); return this; } diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java index 9c7b501df77..6e3f3463e02 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java @@ -59,7 +59,7 @@ private OpenAiSdkSetup() { public enum ModelProvider { - OPEN_AI, AZURE_OPEN_AI, GITHUB_MODELS + OPEN_AI, MICROSOFT_FOUNDRY, GITHUB_MODELS } @@ -88,8 +88,8 @@ public static OpenAIClient setupSyncClient(String baseUrl, String apiKey, Creden if (credential != null) { builder.credential(credential); } - else if (modelProvider == ModelProvider.AZURE_OPEN_AI) { - // If no API key is provided for Azure OpenAI, we try to use passwordless + else if (modelProvider == ModelProvider.MICROSOFT_FOUNDRY) { + // If no API key is provided for Microsoft Foundry, we try to use passwordless // authentication builder.credential(azureAuthentication()); } @@ -145,8 +145,8 @@ public static OpenAIClientAsync setupAsyncClient(String baseUrl, String apiKey, if (credential != null) { builder.credential(credential); } - else if (modelProvider == ModelProvider.AZURE_OPEN_AI) { - // If no API key is provided for Azure OpenAI, we try to use passwordless + else if (modelProvider == ModelProvider.MICROSOFT_FOUNDRY) { + // If no API key is provided for Microsoft Foundry, we try to use passwordless // authentication builder.credential(azureAuthentication()); } @@ -183,7 +183,7 @@ static String detectBaseUrlFromEnv(String baseUrl) { var azureOpenAiBaseUrl = System.getenv("AZURE_OPENAI_BASE_URL"); if (azureOpenAiBaseUrl != null) { baseUrl = azureOpenAiBaseUrl; - logger.debug("Azure OpenAI Base URL detected from environment variable AZURE_OPENAI_BASE_URL."); + logger.debug("Microsoft Foundry Base URL detected from environment variable AZURE_OPENAI_BASE_URL."); } } return baseUrl; @@ -193,7 +193,7 @@ public static ModelProvider detectModelProvider(boolean isAzure, boolean isGitHu String azureDeploymentName, AzureOpenAIServiceVersion azureOpenAIServiceVersion) { if (isAzure) { - return ModelProvider.AZURE_OPEN_AI; // Forced by the user + return ModelProvider.MICROSOFT_FOUNDRY; // Forced by the user } if (isGitHubModels) { return ModelProvider.GITHUB_MODELS; // Forced by the user @@ -202,14 +202,14 @@ public static ModelProvider detectModelProvider(boolean isAzure, boolean isGitHu if (baseUrl.endsWith("openai.azure.com") || baseUrl.endsWith("openai.azure.com/") || baseUrl.endsWith("cognitiveservices.azure.com") || baseUrl.endsWith("cognitiveservices.azure.com/")) { - return ModelProvider.AZURE_OPEN_AI; + return ModelProvider.MICROSOFT_FOUNDRY; } else if (baseUrl.startsWith(GITHUB_MODELS_URL)) { return ModelProvider.GITHUB_MODELS; } } if (azureDeploymentName != null || azureOpenAIServiceVersion != null) { - return ModelProvider.AZURE_OPEN_AI; + return ModelProvider.MICROSOFT_FOUNDRY; } return ModelProvider.OPEN_AI; } @@ -226,7 +226,7 @@ static String calculateBaseUrl(String baseUrl, ModelProvider modelProvider, Stri else if (modelProvider == ModelProvider.GITHUB_MODELS) { return GITHUB_MODELS_URL; } - else if (modelProvider == ModelProvider.AZURE_OPEN_AI) { + else if (modelProvider == ModelProvider.MICROSOFT_FOUNDRY) { String tmpUrl = baseUrl; if (baseUrl.endsWith("/") || baseUrl.endsWith("?")) { tmpUrl = baseUrl.substring(0, baseUrl.length() - 1); @@ -249,7 +249,7 @@ static Credential azureAuthentication() { return AzureInternalOpenAiSdkHelper.getAzureCredential(); } catch (NoClassDefFoundError e) { - throw new IllegalArgumentException("Azure OpenAI was detected, but no credential was provided. " + throw new IllegalArgumentException("Microsoft Foundry was detected, but no credential was provided. " + "If you want to use passwordless authentication, you need to add the Azure Identity library (groupId=`com.azure`, artifactId=`azure-identity`) to your classpath."); } } @@ -258,10 +258,10 @@ static String detectApiKey(ModelProvider modelProvider) { if (modelProvider == ModelProvider.OPEN_AI && System.getenv(OPENAI_API_KEY) != null) { return System.getenv(OPENAI_API_KEY); } - else if (modelProvider == ModelProvider.AZURE_OPEN_AI && System.getenv(AZURE_OPENAI_KEY) != null) { + else if (modelProvider == ModelProvider.MICROSOFT_FOUNDRY && System.getenv(AZURE_OPENAI_KEY) != null) { return System.getenv(AZURE_OPENAI_KEY); } - else if (modelProvider == ModelProvider.AZURE_OPEN_AI && System.getenv(OPENAI_API_KEY) != null) { + else if (modelProvider == ModelProvider.MICROSOFT_FOUNDRY && System.getenv(OPENAI_API_KEY) != null) { return System.getenv(OPENAI_API_KEY); } else if (modelProvider == ModelProvider.GITHUB_MODELS && System.getenv(GITHUB_TOKEN) != null) { diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/package-info.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/package-info.java index b0714e1de5b..ee0ad2dd2fd 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/package-info.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/package-info.java @@ -18,7 +18,7 @@ * Setup and configuration utilities for OpenAI Sdk clients. *

* This package contains helper classes for configuring and setting up OpenAI clients for - * different environments including OpenAI, Azure OpenAI, and GitHub Models. + * different environments including OpenAI, Microsoft Foundry, and GitHub Models. * * @author Julien Dubois */ diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java index a47a4fd501d..5c5c4dd87ba 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java @@ -75,7 +75,7 @@ void testBuilderWithAllFields() { .verbosity("low") .serviceTier("auto") .internalToolExecutionEnabled(false) - .httpHeaders(httpHeaders) + .customHeaders(httpHeaders) .toolContext(toolContext) .build(); @@ -103,7 +103,7 @@ void testBuilderWithAllFields() { assertThat(options.getVerbosity()).isEqualTo("low"); assertThat(options.getServiceTier()).isEqualTo("auto"); assertThat(options.getInternalToolExecutionEnabled()).isFalse(); - assertThat(options.getHttpHeaders()).isEqualTo(httpHeaders); + assertThat(options.getCustomHeaders()).isEqualTo(httpHeaders); assertThat(options.getToolContext()).isEqualTo(toolContext); } @@ -138,7 +138,7 @@ void testCopy() { .verbosity("high") .serviceTier("default") .internalToolExecutionEnabled(true) - .httpHeaders(Map.of("header1", "value1")) + .customHeaders(Map.of("header1", "value1")) .build(); OpenAiSdkChatOptions copiedOptions = originalOptions.copy(); @@ -146,7 +146,7 @@ void testCopy() { assertThat(copiedOptions).isNotSameAs(originalOptions).isEqualTo(originalOptions); // Verify collections are copied assertThat(copiedOptions.getStop()).isNotSameAs(originalOptions.getStop()); - assertThat(copiedOptions.getHttpHeaders()).isNotSameAs(originalOptions.getHttpHeaders()); + assertThat(copiedOptions.getCustomHeaders()).isNotSameAs(originalOptions.getCustomHeaders()); assertThat(copiedOptions.getToolCallbacks()).isNotSameAs(originalOptions.getToolCallbacks()); assertThat(copiedOptions.getToolNames()).isNotSameAs(originalOptions.getToolNames()); assertThat(copiedOptions.getToolContext()).isNotSameAs(originalOptions.getToolContext()); @@ -184,7 +184,7 @@ void testSetters() { options.setVerbosity("medium"); options.setServiceTier("auto"); options.setInternalToolExecutionEnabled(false); - options.setHttpHeaders(Map.of("header2", "value2")); + options.setCustomHeaders(Map.of("header2", "value2")); assertThat(options.getModel()).isEqualTo("test-model"); assertThat(options.getDeploymentName()).isEqualTo("test-deployment"); @@ -209,7 +209,7 @@ void testSetters() { assertThat(options.getVerbosity()).isEqualTo("medium"); assertThat(options.getServiceTier()).isEqualTo("auto"); assertThat(options.getInternalToolExecutionEnabled()).isFalse(); - assertThat(options.getHttpHeaders()).isEqualTo(Map.of("header2", "value2")); + assertThat(options.getCustomHeaders()).isEqualTo(Map.of("header2", "value2")); } @Test @@ -247,7 +247,7 @@ void testDefaultValues() { assertThat(options.getToolCallbacks()).isNotNull().isEmpty(); assertThat(options.getToolNames()).isNotNull().isEmpty(); assertThat(options.getInternalToolExecutionEnabled()).isNull(); - assertThat(options.getHttpHeaders()).isNotNull().isEmpty(); + assertThat(options.getCustomHeaders()).isNotNull().isEmpty(); assertThat(options.getToolContext()).isNotNull().isEmpty(); } @@ -287,7 +287,7 @@ void testBuilderWithNullValues() { .logitBias(null) .stop(null) .metadata(null) - .httpHeaders(null) + .customHeaders(null) .build(); assertThat(options.getModel()).isNull(); @@ -295,7 +295,7 @@ void testBuilderWithNullValues() { assertThat(options.getLogitBias()).isNull(); assertThat(options.getStop()).isNull(); assertThat(options.getMetadata()).isNull(); - assertThat(options.getHttpHeaders()).isNull(); + assertThat(options.getCustomHeaders()).isNull(); } @Test @@ -320,23 +320,23 @@ void testNullAndEmptyCollections() { options.setLogitBias(null); options.setStop(null); options.setMetadata(null); - options.setHttpHeaders(null); + options.setCustomHeaders(null); assertThat(options.getLogitBias()).isNull(); assertThat(options.getStop()).isNull(); assertThat(options.getMetadata()).isNull(); - assertThat(options.getHttpHeaders()).isNull(); + assertThat(options.getCustomHeaders()).isNull(); // Test setting empty collections options.setLogitBias(new HashMap<>()); options.setStop(new ArrayList<>()); options.setMetadata(new HashMap<>()); - options.setHttpHeaders(new HashMap<>()); + options.setCustomHeaders(new HashMap<>()); assertThat(options.getLogitBias()).isEmpty(); assertThat(options.getStop()).isEmpty(); assertThat(options.getMetadata()).isEmpty(); - assertThat(options.getHttpHeaders()).isEmpty(); + assertThat(options.getCustomHeaders()).isEmpty(); } @Test diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java index c5ce7e67850..c2e01f62712 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java @@ -32,7 +32,7 @@ public class OpenAiSdkSetupTests { void detectModelProvider_returnsAzureOpenAI_whenAzureFlagIsTrue() { OpenAiSdkSetup.ModelProvider result = OpenAiSdkSetup.detectModelProvider(true, false, null, null, null); - assertEquals(OpenAiSdkSetup.ModelProvider.AZURE_OPEN_AI, result); + assertEquals(OpenAiSdkSetup.ModelProvider.MICROSOFT_FOUNDRY, result); } @Test @@ -47,7 +47,7 @@ void detectModelProvider_returnsAzureOpenAI_whenBaseUrlMatchesAzure() { OpenAiSdkSetup.ModelProvider result = OpenAiSdkSetup.detectModelProvider(false, false, "https://example.openai.azure.com", null, null); - assertEquals(OpenAiSdkSetup.ModelProvider.AZURE_OPEN_AI, result); + assertEquals(OpenAiSdkSetup.ModelProvider.MICROSOFT_FOUNDRY, result); } @Test diff --git a/models/spring-ai-openai-sdk/src/test/script/deploy-azure-openai-models.sh b/models/spring-ai-openai-sdk/src/test/script/deploy-microsoft-foundry-models.sh similarity index 97% rename from models/spring-ai-openai-sdk/src/test/script/deploy-azure-openai-models.sh rename to models/spring-ai-openai-sdk/src/test/script/deploy-microsoft-foundry-models.sh index d6bbbb66899..f4c200ae2d2 100755 --- a/models/spring-ai-openai-sdk/src/test/script/deploy-azure-openai-models.sh +++ b/models/spring-ai-openai-sdk/src/test/script/deploy-microsoft-foundry-models.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# Execute this script to deploy the needed Azure OpenAI models to execute the integration tests. +# Execute this script to deploy the needed Microsoft Foundry models to execute the integration tests. # # For this, you need to have Azure CLI installed: https://learn.microsoft.com/cli/azure/install-azure-cli # diff --git a/pom.xml b/pom.xml index 07eee6ac9b8..fb9f8c8bcd5 100644 --- a/pom.xml +++ b/pom.xml @@ -281,7 +281,7 @@ 4.3.4 1.0.0-beta.16 4.8.0 - 1.15.4 + 1.18.1 1.1.0 2.2.21 From 3578bd584983a332bad03fdd1719ac05ce4b8b89 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Mon, 24 Nov 2025 15:34:07 +0100 Subject: [PATCH 34/49] Implementation of the OpenAI Java SDK - Change test case as the default URL for GitHub Models changed Signed-off-by: Julien Dubois --- .../springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java index c2e01f62712..70960424f68 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetupTests.java @@ -53,7 +53,7 @@ void detectModelProvider_returnsAzureOpenAI_whenBaseUrlMatchesAzure() { @Test void detectModelProvider_returnsGitHubModels_whenBaseUrlMatchesGitHub() { OpenAiSdkSetup.ModelProvider result = OpenAiSdkSetup.detectModelProvider(false, false, - "https://models.inference.ai.azure.com", null, null); + "https://models.github.ai/inference", null, null); assertEquals(OpenAiSdkSetup.ModelProvider.GITHUB_MODELS, result); } From 5c4c4396ee6f571bdcaf49421b7601a1af9c46ff Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Mon, 24 Nov 2025 16:31:09 +0100 Subject: [PATCH 35/49] Implementation of the OpenAI Java SDK - Update the custom HTTP Headers Signed-off-by: Julien Dubois --- .../ai/openaisdk/AbstractOpenAiSdkOptions.java | 3 ++- .../springframework/ai/openaisdk/OpenAiSdkChatOptions.java | 4 ++-- .../springframework/ai/openaisdk/setup/OpenAiSdkSetup.java | 6 ++++-- .../ai/openaisdk/chat/OpenAiSdkChatModelIT.java | 3 ++- .../ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java | 6 +++--- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java index f5ced94a174..62ca974470e 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java @@ -89,7 +89,7 @@ public class AbstractOpenAiSdkOptions { private Proxy proxy; /** - * Custom headers to add to OpenAI client requests. + * Custom HTTP headers to add to OpenAI client requests. */ private Map customHeaders; @@ -210,4 +210,5 @@ public Map getCustomHeaders() { public void setCustomHeaders(Map customHeaders) { this.customHeaders = customHeaders; } + } diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java index 918718cdaf1..dce1884a5ea 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java @@ -603,8 +603,8 @@ public String toString() { + ", store=" + this.store + ", metadata=" + this.metadata + ", reasoningEffort='" + this.reasoningEffort + '\'' + ", verbosity='" + this.verbosity + '\'' + ", serviceTier='" + this.serviceTier + '\'' + ", toolCallbacks=" + this.toolCallbacks + ", toolNames=" + this.toolNames - + ", internalToolExecutionEnabled=" + this.internalToolExecutionEnabled + ", toolContext=" + - this.toolContext + '}'; + + ", internalToolExecutionEnabled=" + this.internalToolExecutionEnabled + ", toolContext=" + + this.toolContext + '}'; } public record AudioParameters(Voice voice, AudioResponseFormat format) { diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java index 6e3f3463e02..5c9395ba0b9 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java @@ -89,7 +89,8 @@ public static OpenAIClient setupSyncClient(String baseUrl, String apiKey, Creden builder.credential(credential); } else if (modelProvider == ModelProvider.MICROSOFT_FOUNDRY) { - // If no API key is provided for Microsoft Foundry, we try to use passwordless + // If no API key is provided for Microsoft Foundry, we try to use + // passwordless // authentication builder.credential(azureAuthentication()); } @@ -146,7 +147,8 @@ public static OpenAIClientAsync setupAsyncClient(String baseUrl, String apiKey, builder.credential(credential); } else if (modelProvider == ModelProvider.MICROSOFT_FOUNDRY) { - // If no API key is provided for Microsoft Foundry, we try to use passwordless + // If no API key is provided for Microsoft Foundry, we try to use + // passwordless // authentication builder.credential(azureAuthentication()); } diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java index d9c9fb29bbc..8504e3f8342 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java @@ -96,7 +96,8 @@ public class OpenAiSdkChatModelIT { private static final Logger logger = LoggerFactory.getLogger(OpenAiSdkChatModelIT.class); - // It would be better to use ChatModel.GPT_4O_AUDIO_PREVIEW.asString(); but it can't be used as a constant. + // It would be better to use ChatModel.GPT_4O_AUDIO_PREVIEW.asString(); but it can't + // be used as a constant. public static final String DEFAULT_CHAT_MODEL_AUDIO = "gpt-4o-audio-preview"; @Value("classpath:/prompts/system-message.st") diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java index 5c5c4dd87ba..f1ef3494681 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatOptionsTests.java @@ -49,7 +49,7 @@ void testBuilderWithAllFields() { List stop = List.of("stop1", "stop2"); Map metadata = Map.of("key1", "value1"); Map toolContext = Map.of("keyA", "valueA"); - Map httpHeaders = Map.of("header1", "value1"); + Map customHeaders = Map.of("header1", "value1"); OpenAiSdkChatOptions options = OpenAiSdkChatOptions.builder() .model("test-model") @@ -75,7 +75,7 @@ void testBuilderWithAllFields() { .verbosity("low") .serviceTier("auto") .internalToolExecutionEnabled(false) - .customHeaders(httpHeaders) + .customHeaders(customHeaders) .toolContext(toolContext) .build(); @@ -103,7 +103,7 @@ void testBuilderWithAllFields() { assertThat(options.getVerbosity()).isEqualTo("low"); assertThat(options.getServiceTier()).isEqualTo("auto"); assertThat(options.getInternalToolExecutionEnabled()).isFalse(); - assertThat(options.getCustomHeaders()).isEqualTo(httpHeaders); + assertThat(options.getCustomHeaders()).isEqualTo(customHeaders); assertThat(options.getToolContext()).isEqualTo(toolContext); } From 533e426b20d84f026d3eae4848e42aa3d9e1f493 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Mon, 24 Nov 2025 17:45:29 +0100 Subject: [PATCH 36/49] Implementation of the OpenAI Java SDK - Fix the custom HTTP Headers Signed-off-by: Julien Dubois --- .../springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java | 3 ++- .../org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java index 62ca974470e..0b166a1298d 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/AbstractOpenAiSdkOptions.java @@ -18,6 +18,7 @@ import java.net.Proxy; import java.time.Duration; +import java.util.HashMap; import java.util.Map; import com.openai.azure.AzureOpenAIServiceVersion; @@ -91,7 +92,7 @@ public class AbstractOpenAiSdkOptions { /** * Custom HTTP headers to add to OpenAI client requests. */ - private Map customHeaders; + private Map customHeaders = new HashMap<>(); public String getBaseUrl() { return this.baseUrl; diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java index dce1884a5ea..a53df0b3f20 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java @@ -714,7 +714,7 @@ public Builder from(OpenAiSdkChatOptions fromOptions) { this.options.setTimeout(fromOptions.getTimeout()); this.options.setMaxRetries(fromOptions.getMaxRetries()); this.options.setProxy(fromOptions.getProxy()); - this.options.setCustomHeaders(fromOptions.getCustomHeaders()); + this.options.setCustomHeaders(fromOptions.getCustomHeaders() != null ? new HashMap<>(fromOptions.getCustomHeaders()) : null); // Child class fields this.options.setFrequencyPenalty(fromOptions.getFrequencyPenalty()); this.options.setLogitBias(fromOptions.getLogitBias()); From fb8bad1832791f928d60e9c2be0184bf086b24ff Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Mon, 24 Nov 2025 18:43:06 +0100 Subject: [PATCH 37/49] Fix format Signed-off-by: Christian Tzolov --- .../autoconfigure/OpenAiSdkAutoConfigurationUtil.java | 3 ++- .../autoconfigure/OpenAiSdkChatAutoConfiguration.java | 10 ++++++---- .../OpenAiSdkEmbeddingAutoConfiguration.java | 5 +++-- .../autoconfigure/OpenAiSdkImageAutoConfiguration.java | 5 +++-- .../ai/openaisdk/OpenAiSdkChatOptions.java | 3 ++- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkAutoConfigurationUtil.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkAutoConfigurationUtil.java index 138e4aebdb6..51d15ebfca4 100644 --- a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkAutoConfigurationUtil.java +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkAutoConfigurationUtil.java @@ -53,7 +53,8 @@ public static ResolvedConnectionProperties resolveConnectionProperties(AbstractO ? modelProperties.getMicrosoftDeploymentName() : commonProperties.getMicrosoftDeploymentName()); resolved.setMicrosoftFoundryServiceVersion(modelProperties.getMicrosoftFoundryServiceVersion() != null - ? modelProperties.getMicrosoftFoundryServiceVersion() : commonProperties.getMicrosoftFoundryServiceVersion()); + ? modelProperties.getMicrosoftFoundryServiceVersion() + : commonProperties.getMicrosoftFoundryServiceVersion()); // For boolean properties, use modelProperties value, defaulting to // commonProperties if needed diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfiguration.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfiguration.java index f7dc09f4e43..986342b7031 100644 --- a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfiguration.java +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkChatAutoConfiguration.java @@ -76,16 +76,18 @@ private OpenAIClient openAiClient(AbstractOpenAiSdkOptions resolved) { return OpenAiSdkSetup.setupSyncClient(resolved.getBaseUrl(), resolved.getApiKey(), resolved.getCredential(), resolved.getMicrosoftDeploymentName(), resolved.getMicrosoftFoundryServiceVersion(), - resolved.getOrganizationId(), resolved.isMicrosoftFoundry(), resolved.isGitHubModels(), resolved.getModel(), - resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), resolved.getCustomHeaders()); + resolved.getOrganizationId(), resolved.isMicrosoftFoundry(), resolved.isGitHubModels(), + resolved.getModel(), resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), + resolved.getCustomHeaders()); } private OpenAIClientAsync openAiClientAsync(AbstractOpenAiSdkOptions resolved) { return OpenAiSdkSetup.setupAsyncClient(resolved.getBaseUrl(), resolved.getApiKey(), resolved.getCredential(), resolved.getMicrosoftDeploymentName(), resolved.getMicrosoftFoundryServiceVersion(), - resolved.getOrganizationId(), resolved.isMicrosoftFoundry(), resolved.isGitHubModels(), resolved.getModel(), - resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), resolved.getCustomHeaders()); + resolved.getOrganizationId(), resolved.isMicrosoftFoundry(), resolved.isGitHubModels(), + resolved.getModel(), resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), + resolved.getCustomHeaders()); } } diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfiguration.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfiguration.java index bed0b7defcd..6e8f7c4e567 100644 --- a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfiguration.java +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkEmbeddingAutoConfiguration.java @@ -65,8 +65,9 @@ private OpenAIClient openAiClient(OpenAiSdkConnectionProperties commonProperties return OpenAiSdkSetup.setupSyncClient(resolved.getBaseUrl(), resolved.getApiKey(), resolved.getCredential(), resolved.getMicrosoftDeploymentName(), resolved.getMicrosoftFoundryServiceVersion(), - resolved.getOrganizationId(), resolved.isMicrosoftFoundry(), resolved.isGitHubModels(), resolved.getModel(), - resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), resolved.getCustomHeaders()); + resolved.getOrganizationId(), resolved.isMicrosoftFoundry(), resolved.isGitHubModels(), + resolved.getModel(), resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), + resolved.getCustomHeaders()); } } diff --git a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfiguration.java b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfiguration.java index 72be5126a50..d15fe2bcbb1 100644 --- a/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfiguration.java +++ b/auto-configurations/models/spring-ai-autoconfigure-model-openai-sdk/src/main/java/org/springframework/ai/model/openaisdk/autoconfigure/OpenAiSdkImageAutoConfiguration.java @@ -64,8 +64,9 @@ private OpenAIClient openAiClient(OpenAiSdkConnectionProperties commonProperties return OpenAiSdkSetup.setupSyncClient(resolved.getBaseUrl(), resolved.getApiKey(), resolved.getCredential(), resolved.getMicrosoftDeploymentName(), resolved.getMicrosoftFoundryServiceVersion(), - resolved.getOrganizationId(), resolved.isMicrosoftFoundry(), resolved.isGitHubModels(), resolved.getModel(), - resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), resolved.getCustomHeaders()); + resolved.getOrganizationId(), resolved.isMicrosoftFoundry(), resolved.isGitHubModels(), + resolved.getModel(), resolved.getTimeout(), resolved.getMaxRetries(), resolved.getProxy(), + resolved.getCustomHeaders()); } } diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java index a53df0b3f20..a9155c1e997 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java @@ -714,7 +714,8 @@ public Builder from(OpenAiSdkChatOptions fromOptions) { this.options.setTimeout(fromOptions.getTimeout()); this.options.setMaxRetries(fromOptions.getMaxRetries()); this.options.setProxy(fromOptions.getProxy()); - this.options.setCustomHeaders(fromOptions.getCustomHeaders() != null ? new HashMap<>(fromOptions.getCustomHeaders()) : null); + this.options.setCustomHeaders( + fromOptions.getCustomHeaders() != null ? new HashMap<>(fromOptions.getCustomHeaders()) : null); // Child class fields this.options.setFrequencyPenalty(fromOptions.getFrequencyPenalty()); this.options.setLogitBias(fromOptions.getLogitBias()); From 4a6561121b40be971a120ffef24bd2673cb588d6 Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Mon, 24 Nov 2025 18:45:45 +0100 Subject: [PATCH 38/49] Fix checkstyle Signed-off-by: Christian Tzolov --- .../ai/openaisdk/chat/OpenAiSdkChatModelIT.java | 1 - .../ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java index 8504e3f8342..a0379a8d7e0 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelIT.java @@ -29,7 +29,6 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; -import com.openai.models.ChatModel; import com.openai.models.ReasoningEffort; import org.assertj.core.data.Percentage; import org.junit.jupiter.api.Test; diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java index 9a44e897664..58b75d3d1d1 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java @@ -37,7 +37,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.springframework.ai.openaisdk.OpenAiSdkEmbeddingOptions.DEFAULT_EMBEDDING_MODEL; /** * Integration tests for {@link OpenAiSdkEmbeddingModel}. @@ -65,7 +64,8 @@ void defaultEmbedding() { assertThat(embeddingResponse.getMetadata().getUsage().getPromptTokens()).isEqualTo(2); assertThat(this.openAiSdkEmbeddingModel.dimensions()).isEqualTo(1536); - assertThat(embeddingResponse.getMetadata().getModel()).isEqualTo(DEFAULT_EMBEDDING_MODEL); + assertThat(embeddingResponse.getMetadata().getModel()) + .isEqualTo(OpenAiSdkEmbeddingOptions.DEFAULT_EMBEDDING_MODEL); } @Test From 1c42563600fb86e07ea24a5adc2556f157696914 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Mon, 24 Nov 2025 22:58:58 +0100 Subject: [PATCH 39/49] Implementation of the OpenAI Java SDK - Improve authentication Signed-off-by: Julien Dubois --- .../ai/openaisdk/setup/OpenAiSdkSetup.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java index 5c9395ba0b9..61ebfc16d6d 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java @@ -226,9 +226,15 @@ static String calculateBaseUrl(String baseUrl, ModelProvider modelProvider, Stri return baseUrl; } else if (modelProvider == ModelProvider.GITHUB_MODELS) { - return GITHUB_MODELS_URL; + if (baseUrl == null || baseUrl.isBlank()) { + return GITHUB_MODELS_URL; + } + return baseUrl; } else if (modelProvider == ModelProvider.MICROSOFT_FOUNDRY) { + if (baseUrl == null || baseUrl.isBlank()) { + throw new IllegalArgumentException("Base URL must be provided for Microsoft Foundry."); + } String tmpUrl = baseUrl; if (baseUrl.endsWith("/") || baseUrl.endsWith("?")) { tmpUrl = baseUrl.substring(0, baseUrl.length() - 1); From 27e5f781be4b72783ace49838059971aac079fb6 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Tue, 25 Nov 2025 11:36:06 +0100 Subject: [PATCH 40/49] Implementation of the OpenAI Java SDK - Fix OpenAiOfficialImageModelObservationIT Signed-off-by: Julien Dubois --- ...SdkTestConfigurationWithObservability.java | 59 ------------------- .../chat/OpenAiSdkChatModelObservationIT.java | 22 ++++++- .../OpenAiSdkEmbeddingModelObservationIT.java | 30 ++++++++-- .../OpenAiSdkImageModelObservationIT.java | 22 ++++++- 4 files changed, 65 insertions(+), 68 deletions(-) delete mode 100644 models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfigurationWithObservability.java diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfigurationWithObservability.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfigurationWithObservability.java deleted file mode 100644 index 0e4817bf195..00000000000 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/OpenAiSdkTestConfigurationWithObservability.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2025-2025 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.ai.openaisdk; - -import io.micrometer.observation.tck.TestObservationRegistry; - -import org.springframework.ai.document.MetadataMode; -import org.springframework.boot.SpringBootConfiguration; -import org.springframework.context.annotation.Bean; - -/** - * Context configuration for OpenAI Java SDK tests. - * - * @author Julien Dubois - */ -@SpringBootConfiguration -public class OpenAiSdkTestConfigurationWithObservability { - - @Bean - public TestObservationRegistry testObservationRegistry() { - return TestObservationRegistry.create(); - } - - @Bean - public OpenAiSdkEmbeddingModel openAiEmbeddingModel(TestObservationRegistry observationRegistry) { - return new OpenAiSdkEmbeddingModel(MetadataMode.EMBED, - OpenAiSdkEmbeddingOptions.builder().model(OpenAiSdkEmbeddingOptions.DEFAULT_EMBEDDING_MODEL).build(), - observationRegistry); - } - - @Bean - public OpenAiSdkImageModel openAiImageModel(TestObservationRegistry observationRegistry) { - return new OpenAiSdkImageModel( - OpenAiSdkImageOptions.builder().model(OpenAiSdkImageOptions.DEFAULT_IMAGE_MODEL).build(), - observationRegistry); - } - - @Bean - public OpenAiSdkChatModel openAiChatModel(TestObservationRegistry observationRegistry) { - return new OpenAiSdkChatModel( - OpenAiSdkChatOptions.builder().model(OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL).build(), - observationRegistry); - } - -} diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java index 2d337cc38b6..09078ac8c89 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/OpenAiSdkChatModelObservationIT.java @@ -37,9 +37,10 @@ import org.springframework.ai.openaisdk.OpenAiSdkChatModel; import org.springframework.ai.openaisdk.OpenAiSdkChatOptions; import org.springframework.ai.openaisdk.OpenAiSdkChatOptions.StreamOptions; -import org.springframework.ai.openaisdk.OpenAiSdkTestConfigurationWithObservability; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; import static org.assertj.core.api.Assertions.assertThat; @@ -48,7 +49,7 @@ * * @author Julien Dubois */ -@SpringBootTest(classes = OpenAiSdkTestConfigurationWithObservability.class) +@SpringBootTest @EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") public class OpenAiSdkChatModelObservationIT { @@ -133,4 +134,21 @@ private void validate(ChatResponseMetadata responseMetadata) throws InterruptedE .hasBeenStopped(); } + @SpringBootConfiguration + static class Config { + + @Bean + public TestObservationRegistry observationRegistry() { + return TestObservationRegistry.create(); + } + + @Bean + public OpenAiSdkChatModel openAiChatModel(TestObservationRegistry observationRegistry) { + return new OpenAiSdkChatModel( + OpenAiSdkChatOptions.builder().model(OpenAiSdkChatOptions.DEFAULT_CHAT_MODEL).build(), + observationRegistry); + } + + } + } diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java index 2dbe8626d0e..1c5cc770133 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java @@ -16,15 +16,13 @@ package org.springframework.ai.openaisdk.embedding; -import java.util.List; - import com.openai.models.embeddings.EmbeddingModel; import io.micrometer.observation.tck.TestObservationRegistry; import io.micrometer.observation.tck.TestObservationRegistryAssert; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; - +import org.springframework.ai.document.MetadataMode; import org.springframework.ai.embedding.EmbeddingRequest; import org.springframework.ai.embedding.EmbeddingResponse; import org.springframework.ai.embedding.EmbeddingResponseMetadata; @@ -35,9 +33,12 @@ import org.springframework.ai.observation.conventions.AiProvider; import org.springframework.ai.openaisdk.OpenAiSdkEmbeddingModel; import org.springframework.ai.openaisdk.OpenAiSdkEmbeddingOptions; -import org.springframework.ai.openaisdk.OpenAiSdkTestConfigurationWithObservability; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; + +import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @@ -46,7 +47,7 @@ * * @author Julien Dubois */ -@SpringBootTest(classes = OpenAiSdkTestConfigurationWithObservability.class) +@SpringBootTest @EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") public class OpenAiSdkEmbeddingModelObservationIT { @@ -96,4 +97,23 @@ void observationForEmbeddingOperation() { .hasBeenStopped(); } + @SpringBootConfiguration + static class Config { + + @Bean + public TestObservationRegistry observationRegistry() { + return TestObservationRegistry.create(); + } + + @Bean + public OpenAiSdkEmbeddingModel openAiEmbeddingModel(TestObservationRegistry observationRegistry) { + return new OpenAiSdkEmbeddingModel(MetadataMode.EMBED, + OpenAiSdkEmbeddingOptions.builder() + .model(OpenAiSdkEmbeddingOptions.DEFAULT_EMBEDDING_MODEL) + .build(), + observationRegistry); + } + + } + } diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelObservationIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelObservationIT.java index a6c911e0af3..d507ef7838f 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelObservationIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/image/OpenAiSdkImageModelObservationIT.java @@ -31,9 +31,10 @@ import org.springframework.ai.observation.conventions.AiProvider; import org.springframework.ai.openaisdk.OpenAiSdkImageModel; import org.springframework.ai.openaisdk.OpenAiSdkImageOptions; -import org.springframework.ai.openaisdk.OpenAiSdkTestConfigurationWithObservability; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; import static org.assertj.core.api.Assertions.assertThat; @@ -42,7 +43,7 @@ * * @author Julien Dubois */ -@SpringBootTest(classes = OpenAiSdkTestConfigurationWithObservability.class) +@SpringBootTest @EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+") public class OpenAiSdkImageModelObservationIT { @@ -101,4 +102,21 @@ void observationForImageOperation() throws InterruptedException { .hasBeenStopped(); } + @SpringBootConfiguration + static class Config { + + @Bean + public TestObservationRegistry observationRegistry() { + return TestObservationRegistry.create(); + } + + @Bean + public OpenAiSdkImageModel openAiImageModel(TestObservationRegistry observationRegistry) { + return new OpenAiSdkImageModel( + OpenAiSdkImageOptions.builder().model(OpenAiSdkImageOptions.DEFAULT_IMAGE_MODEL).build(), + observationRegistry); + } + + } + } From 21b0f319d0d21246a8663257b057fe2b627dadf7 Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Tue, 25 Nov 2025 11:59:14 +0100 Subject: [PATCH 41/49] checkstyle fix Signed-off-by: Christian Tzolov --- .../embedding/OpenAiSdkEmbeddingModelObservationIT.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java index 1c5cc770133..1b90226a738 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingModelObservationIT.java @@ -16,12 +16,15 @@ package org.springframework.ai.openaisdk.embedding; +import java.util.List; + import com.openai.models.embeddings.EmbeddingModel; import io.micrometer.observation.tck.TestObservationRegistry; import io.micrometer.observation.tck.TestObservationRegistryAssert; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; + import org.springframework.ai.document.MetadataMode; import org.springframework.ai.embedding.EmbeddingRequest; import org.springframework.ai.embedding.EmbeddingResponse; @@ -38,8 +41,6 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; /** From d0dc52c9d72f582baef9dbaf884ffda632f0ce93 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Tue, 25 Nov 2025 12:03:28 +0100 Subject: [PATCH 42/49] Implementation of the OpenAI Java SDK - Support GitHub Models for specific orgs Signed-off-by: Julien Dubois --- .../springframework/ai/openaisdk/setup/OpenAiSdkSetup.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java index 61ebfc16d6d..86f52a25aec 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/setup/OpenAiSdkSetup.java @@ -229,7 +229,11 @@ else if (modelProvider == ModelProvider.GITHUB_MODELS) { if (baseUrl == null || baseUrl.isBlank()) { return GITHUB_MODELS_URL; } - return baseUrl; + if (baseUrl.startsWith(GITHUB_MODELS_URL)) { + // To support GitHub Models for specific orgs + return baseUrl; + } + return GITHUB_MODELS_URL; } else if (modelProvider == ModelProvider.MICROSOFT_FOUNDRY) { if (baseUrl == null || baseUrl.isBlank()) { From fa316b19e33000edeeed7cb22736dd0acc6c2651 Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Tue, 25 Nov 2025 12:35:02 +0100 Subject: [PATCH 43/49] Improve test robustness Signed-off-by: Christian Tzolov --- .../ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java index 58b75d3d1d1..1afc2d46d11 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/embedding/OpenAiSdkEmbeddingIT.java @@ -65,7 +65,7 @@ void defaultEmbedding() { assertThat(this.openAiSdkEmbeddingModel.dimensions()).isEqualTo(1536); assertThat(embeddingResponse.getMetadata().getModel()) - .isEqualTo(OpenAiSdkEmbeddingOptions.DEFAULT_EMBEDDING_MODEL); + .contains(OpenAiSdkEmbeddingOptions.DEFAULT_EMBEDDING_MODEL); } @Test From 6f84f5a23c16726499fb82c95be5adc54fc5e682 Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Tue, 25 Nov 2025 13:21:17 +0100 Subject: [PATCH 44/49] Update openai-sdk chat docs Signed-off-by: Christian Tzolov --- .../ROOT/pages/api/chat/openai-sdk-chat.adoc | 141 +++++++++++++----- 1 file changed, 100 insertions(+), 41 deletions(-) diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc index 43030a5fe92..2d93f92ec84 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc @@ -1,42 +1,102 @@ -= OpenAI SDK Chat += OpenAI SDK Chat (Official) Spring AI supports OpenAI's language models through the OpenAI Java SDK, providing a robust and officially-maintained integration with OpenAI's services including Azure OpenAI and GitHub Models. NOTE: This implementation uses the official link:https://github.com/openai/openai-java[OpenAI Java SDK] from OpenAI. For the alternative Spring AI implementation, see xref:api/chat/openai-chat.adoc[OpenAI Chat]. -== Prerequisites +The OpenAI SDK module automatically detects the service provider (OpenAI, Microsoft Foundry, or GitHub Models) based on the base URL you provide. -You will need to create an API key with OpenAI to access ChatGPT models. +== Authentication -Create an account at https://platform.openai.com/signup[OpenAI signup page] and generate the token on the https://platform.openai.com/account/api-keys[API Keys page]. +Authentication is done using a base URL and an API Key. The implementation provides flexible configuration options through Spring Boot properties or environment variables. -The Spring AI project defines a configuration property named `spring.ai.openai-sdk.api-key` that you should set to the value of the `API Key` obtained from openai.com. -You can set this configuration property in your `application.properties` file: +=== Using OpenAI + +If you are using OpenAI directly, create an account at https://platform.openai.com/signup[OpenAI signup page] and generate an API key on the https://platform.openai.com/account/api-keys[API Keys page]. + +The base URL doesn't need to be set as it defaults to `https://api.openai.com/v1`: [source,properties] ---- spring.ai.openai-sdk.api-key= +# base-url is optional, defaults to https://api.openai.com/v1 ---- -For enhanced security when handling sensitive information like API keys, you can use Spring Expression Language (SpEL) to reference a custom environment variable: +Or using environment variables: -[source,yaml] +[source,bash] ---- -# In application.yml -spring: - ai: - openai-sdk: - api-key: ${OPENAI_API_KEY} +export OPENAI_API_KEY= +# OPENAI_BASE_URL is optional, defaults to https://api.openai.com/v1 ---- +=== Using Microsoft Foundry (Azure OpenAI) + +Microsoft Foundry is automatically detected when using a Microsoft Foundry URL. You can configure it using properties: + +[source,properties] +---- +spring.ai.openai-sdk.base-url=https://.openai.azure.com +spring.ai.openai-sdk.api-key= +spring.ai.openai-sdk.microsoft-deployment-name= +---- + +Or using environment variables: + [source,bash] ---- -# In your environment or .env file -export OPENAI_API_KEY= +export OPENAI_BASE_URL=https://.openai.azure.com +export OPENAI_API_KEY= ---- -The implementation automatically detects the API key from the `OPENAI_API_KEY` environment variable if not explicitly configured. +**Passwordless Authentication (Recommended for Azure):** + +Microsoft Foundry supports passwordless authentication without providing an API key, which is more secure when running on Azure. + +To enable passwordless authentication, add the `com.azure:azure-identity` dependency: + +[source,xml] +---- + + com.azure + azure-identity + +---- + +Then configure without an API key: + +[source,properties] +---- +spring.ai.openai-sdk.base-url=https://.openai.azure.com +spring.ai.openai-sdk.microsoft-deployment-name= +# No api-key needed - will use Azure credentials from environment +---- + +=== Using GitHub Models + +GitHub Models is automatically detected when using the GitHub Models base URL. You'll need to create a GitHub Personal Access Token (PAT) with the `models:read` scope. + +[source,properties] +---- +spring.ai.openai-sdk.base-url=https://models.inference.ai.azure.com +spring.ai.openai-sdk.api-key=github_pat_XXXXXXXXXXX +---- + +Or using environment variables: + +[source,bash] +---- +export OPENAI_BASE_URL=https://models.inference.ai.azure.com +export OPENAI_API_KEY=github_pat_XXXXXXXXXXX +---- + +TIP: For enhanced security when handling sensitive information like API keys, you can use Spring Expression Language (SpEL) in your properties: + +[source,properties] +---- +spring.ai.openai-sdk.api-key=${OPENAI_API_KEY} +---- === Add Repositories and BOM @@ -58,7 +118,7 @@ Maven:: ---- org.springframework.ai - spring-ai-openai-sdk + spring-ai-starter-model-openai-sdk ---- @@ -67,7 +127,7 @@ Gradle:: [source,groovy] ---- dependencies { - implementation 'org.springframework.ai:spring-ai-openai-sdk' + implementation 'org.springframework.ai:spring-ai-starter-model-openai-sdk' } ---- ====== @@ -84,31 +144,30 @@ The prefix `spring.ai.openai-sdk` is used as the property prefix that lets you c |==== | Property | Description | Default -| spring.ai.openai-sdk.base-url | The URL to connect to. Auto-detects from `OPENAI_BASE_URL` or `AZURE_OPENAI_BASE_URL` environment variables if not set. | https://api.openai.com/v1 -| spring.ai.openai-sdk.api-key | The API Key. Auto-detects from `OPENAI_API_KEY`, `AZURE_OPENAI_KEY`, or `GITHUB_TOKEN` environment variables based on the base URL. | - +| spring.ai.openai-sdk.base-url | The URL to connect to. Auto-detects from `OPENAI_BASE_URL` environment variable if not set. | https://api.openai.com/v1 +| spring.ai.openai-sdk.api-key | The API Key. Auto-detects from `OPENAI_API_KEY` environment variable if not set. | - | spring.ai.openai-sdk.organization-id | Optionally specify which organization to use for API requests. | - -| spring.ai.openai-sdk.timeout | Request timeout duration. | 60 seconds -| spring.ai.openai-sdk.max-retries | Maximum number of retry attempts for failed requests. | 3 -| spring.ai.openai-sdk.proxy.host | Proxy host for HTTP requests. | - -| spring.ai.openai-sdk.proxy.port | Proxy port for HTTP requests. | - -| spring.ai.openai-sdk.custom-headers | Custom HTTP headers to include in requests. | - +| spring.ai.openai-sdk.timeout | Request timeout duration. | - +| spring.ai.openai-sdk.max-retries | Maximum number of retry attempts for failed requests. | - +| spring.ai.openai-sdk.proxy | Proxy settings for OpenAI client (Java `Proxy` object). | - +| spring.ai.openai-sdk.custom-headers | Custom HTTP headers to include in requests. Map of header name to header value. | - |==== -==== Azure OpenAI Properties +==== Microsoft Foundry (Azure OpenAI) Properties -The OpenAI SDK implementation provides native support for Azure OpenAI with automatic configuration: +The OpenAI SDK implementation provides native support for Microsoft Foundry (Azure OpenAI) with automatic configuration: [cols="3,5,1", stripes=even] |==== | Property | Description | Default -| spring.ai.openai-sdk.azure | Enable Azure OpenAI mode. Auto-detected if base URL contains `openai.azure.com` or `cognitiveservices.azure.com`. | false -| spring.ai.openai-sdk.azure-deployment-name | Azure deployment name. If not specified, the model name will be used. | - -| spring.ai.openai-sdk.azure-openai-service-version | Azure OpenAI API version. | - -| spring.ai.openai-sdk.credential | Azure credential for passwordless authentication. | - +| spring.ai.openai-sdk.microsoft-foundry | Enable Microsoft Foundry mode. Auto-detected if base URL contains `openai.azure.com`, `cognitiveservices.azure.com`, or `.openai.microsoftFoundry.com`. | false +| spring.ai.openai-sdk.microsoft-deployment-name | Microsoft Foundry deployment name. If not specified, the model name will be used. Also accessible via alias `deployment-name`. | - +| spring.ai.openai-sdk.microsoft-foundry-service-version | Microsoft Foundry API service version. | - +| spring.ai.openai-sdk.credential | Credential object for passwordless authentication (requires `com.azure:azure-identity` dependency). | - |==== -TIP: Azure OpenAI supports passwordless authentication. If no API key is provided, the implementation automatically attempts to use Azure credentials from the environment. +TIP: Microsoft Foundry (Azure OpenAI) supports passwordless authentication. Add the `com.azure:azure-identity` dependency and the implementation will automatically attempt to use Azure credentials from the environment when no API key is provided. ==== GitHub Models Properties @@ -118,10 +177,10 @@ Native support for GitHub Models is available: |==== | Property | Description | Default -| spring.ai.openai-sdk.github-models | Enable GitHub Models mode. Auto-detected if base URL is `https://models.inference.ai.azure.com`. | false +| spring.ai.openai-sdk.github-models | Enable GitHub Models mode. Auto-detected if base URL contains `models.github.ai` or `models.inference.ai.azure.com`. | false |==== -TIP: GitHub Models authentication uses the `GITHUB_TOKEN` environment variable when detected. +TIP: GitHub Models requires a Personal Access Token with the `models:read` scope. Set it via the `OPENAI_API_KEY` environment variable or the `spring.ai.openai-sdk.api-key` property. ==== Chat Model Properties @@ -132,7 +191,7 @@ The prefix `spring.ai.openai-sdk.chat` is the property prefix for configuring th | Property | Description | Default | spring.ai.openai-sdk.chat.options.model | Name of the OpenAI chat model to use. You can select between models such as: `gpt-5-mini`, `gpt-4o`, `gpt-4o-mini`, `gpt-4-turbo`, `o1`, `o3-mini`, and more. See the https://platform.openai.com/docs/models[models] page for more information. | `gpt-5-mini` -| spring.ai.openai-sdk.chat.options.temperature | The sampling temperature to use that controls the apparent creativity of generated completions. Higher values will make output more random while lower values will make results more focused and deterministic. It is not recommended to modify `temperature` and `top_p` for the same completions request as the interaction of these two settings is difficult to predict. | 0.7 +| spring.ai.openai-sdk.chat.options.temperature | The sampling temperature to use that controls the apparent creativity of generated completions. Higher values will make output more random while lower values will make results more focused and deterministic. It is not recommended to modify `temperature` and `top_p` for the same completions request as the interaction of these two settings is difficult to predict. | 1.0 | spring.ai.openai-sdk.chat.options.frequency-penalty | Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. | 0.0 | spring.ai.openai-sdk.chat.options.logit-bias | Modify the likelihood of specified tokens appearing in the completion. | - | spring.ai.openai-sdk.chat.options.logprobs | Whether to return log probabilities of the output tokens. | false @@ -523,24 +582,24 @@ Flux response = chatModel.stream( new Prompt("Generate the names of 5 famous pirates.")); ---- -=== Azure OpenAI Configuration +=== Microsoft Foundry (Azure OpenAI) Configuration -For Azure OpenAI: +For Microsoft Foundry (Azure OpenAI): [source,java] ---- var chatOptions = OpenAiSdkChatOptions.builder() .baseUrl("https://your-resource.openai.azure.com") - .apiKey(System.getenv("AZURE_OPENAI_KEY")) - .azureDeploymentName("gpt-4") + .apiKey(System.getenv("OPENAI_API_KEY")) + .deploymentName("gpt-4") .azureOpenAIServiceVersion(AzureOpenAIServiceVersion.V2024_10_01_PREVIEW) - .azure(true) + .azure(true) // Enables Microsoft Foundry mode .build(); var chatModel = new OpenAiSdkChatModel(chatOptions); ---- -TIP: Azure OpenAI supports passwordless authentication. If you don't provide an API key, the implementation will automatically attempt to use Azure credentials from your environment. +TIP: Microsoft Foundry (Azure OpenAI) supports passwordless authentication. Add the `com.azure:azure-identity` dependency to your project. If you don't provide an API key, the implementation will automatically attempt to use Azure credentials from your environment. === GitHub Models Configuration From f20b80bb34852661c28846cda83b4cc7f4efa69b Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Tue, 25 Nov 2025 14:10:39 +0100 Subject: [PATCH 45/49] Add embeding and image docs Signed-off-by: Christian Tzolov --- .../src/main/antora/modules/ROOT/nav.adoc | 2 + .../api/embeddings/openai-sdk-embeddings.adoc | 357 ++++++++++++++++ .../pages/api/image/openai-sdk-image.adoc | 381 ++++++++++++++++++ 3 files changed, 740 insertions(+) create mode 100644 spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/openai-sdk-embeddings.adoc create mode 100644 spring-ai-docs/src/main/antora/modules/ROOT/pages/api/image/openai-sdk-image.adoc diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc index 99e67080903..ff80e6763fa 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc @@ -50,6 +50,7 @@ **** xref:api/embeddings/oci-genai-embeddings.adoc[OCI GenAI] **** xref:api/embeddings/ollama-embeddings.adoc[Ollama] **** xref:api/embeddings/onnx.adoc[(ONNX) Transformers] +**** xref:api/embeddings/openai-sdk-embeddings.adoc[OpenAI SDK (Official)] **** xref:api/embeddings/openai-embeddings.adoc[OpenAI] **** xref:api/embeddings/postgresml-embeddings.adoc[PostgresML] **** xref:api/embeddings/qianfan-embeddings.adoc[QianFan] @@ -60,6 +61,7 @@ *** xref:api/imageclient.adoc[Image Models] **** xref:api/image/azure-openai-image.adoc[Azure OpenAI] +**** xref:api/image/openai-sdk-image.adoc[OpenAI SDK (Official)] **** xref:api/image/openai-image.adoc[OpenAI] **** xref:api/image/stabilityai-image.adoc[Stability] **** xref:api/image/zhipuai-image.adoc[ZhiPuAI] diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/openai-sdk-embeddings.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/openai-sdk-embeddings.adoc new file mode 100644 index 00000000000..680c853b7bf --- /dev/null +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/openai-sdk-embeddings.adoc @@ -0,0 +1,357 @@ += OpenAI SDK Embeddings (Official) + +Spring AI supports OpenAI's text embeddings models through the OpenAI Java SDK, providing a robust and officially-maintained integration with OpenAI's services including Azure OpenAI and GitHub Models. + +NOTE: This implementation uses the official link:https://github.com/openai/openai-java[OpenAI Java SDK] from OpenAI. For the alternative Spring AI implementation, see xref:api/embeddings/openai-embeddings.adoc[OpenAI Embeddings]. + +OpenAI's text embeddings measure the relatedness of text strings. +An embedding is a vector (list) of floating point numbers. The distance between two vectors measures their relatedness. Small distances suggest high relatedness and large distances suggest low relatedness. + +The OpenAI SDK module automatically detects the service provider (OpenAI, Microsoft Foundry, or GitHub Models) based on the base URL you provide. + +== Authentication + +Authentication is done using a base URL and an API Key. The implementation provides flexible configuration options through Spring Boot properties or environment variables. + +=== Using OpenAI + +If you are using OpenAI directly, create an account at https://platform.openai.com/signup[OpenAI signup page] and generate an API key on the https://platform.openai.com/account/api-keys[API Keys page]. + +The base URL doesn't need to be set as it defaults to `https://api.openai.com/v1`: + +[source,properties] +---- +spring.ai.openai-sdk.api-key= +# base-url is optional, defaults to https://api.openai.com/v1 +---- + +Or using environment variables: + +[source,bash] +---- +export OPENAI_API_KEY= +# OPENAI_BASE_URL is optional, defaults to https://api.openai.com/v1 +---- + +=== Using Microsoft Foundry (Azure OpenAI) + +Microsoft Foundry is automatically detected when using a Microsoft Foundry URL. You can configure it using properties: + +[source,properties] +---- +spring.ai.openai-sdk.base-url=https://.openai.azure.com +spring.ai.openai-sdk.api-key= +spring.ai.openai-sdk.microsoft-deployment-name= +---- + +Or using environment variables: + +[source,bash] +---- +export OPENAI_BASE_URL=https://.openai.azure.com +export OPENAI_API_KEY= +---- + +**Passwordless Authentication (Recommended for Azure):** + +Microsoft Foundry supports passwordless authentication without providing an API key, which is more secure when running on Azure. + +To enable passwordless authentication, add the `com.azure:azure-identity` dependency: + +[source,xml] +---- + + com.azure + azure-identity + +---- + +Then configure without an API key: + +[source,properties] +---- +spring.ai.openai-sdk.base-url=https://.openai.azure.com +spring.ai.openai-sdk.microsoft-deployment-name= +# No api-key needed - will use Azure credentials from environment +---- + +=== Using GitHub Models + +GitHub Models is automatically detected when using the GitHub Models base URL. You'll need to create a GitHub Personal Access Token (PAT) with the `models:read` scope. + +[source,properties] +---- +spring.ai.openai-sdk.base-url=https://models.inference.ai.azure.com +spring.ai.openai-sdk.api-key=github_pat_XXXXXXXXXXX +---- + +Or using environment variables: + +[source,bash] +---- +export OPENAI_BASE_URL=https://models.inference.ai.azure.com +export OPENAI_API_KEY=github_pat_XXXXXXXXXXX +---- + +TIP: For enhanced security when handling sensitive information like API keys, you can use Spring Expression Language (SpEL) in your properties: + +[source,properties] +---- +spring.ai.openai-sdk.api-key=${OPENAI_API_KEY} +---- + +=== Add Repositories and BOM + +Spring AI artifacts are published in Maven Central and Spring Snapshot repositories. +Refer to the xref:getting-started.adoc#artifact-repositories[Artifact Repositories] section to add these repositories to your build system. + +To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build system. + +== Auto-configuration + +Spring AI provides Spring Boot auto-configuration for the OpenAI SDK Embedding Model. +To enable it add the following dependency to your project's Maven `pom.xml` or Gradle `build.gradle` build files: + +[tabs] +====== +Maven:: ++ +[source, xml] +---- + + org.springframework.ai + spring-ai-starter-model-openai-sdk + +---- + +Gradle:: ++ +[source,groovy] +---- +dependencies { + implementation 'org.springframework.ai:spring-ai-starter-model-openai-sdk' +} +---- +====== + +TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file. + +=== Configuration Properties + +==== Connection Properties + +The prefix `spring.ai.openai-sdk` is used as the property prefix that lets you configure the OpenAI SDK client. + +[cols="3,5,1", stripes=even] +|==== +| Property | Description | Default + +| spring.ai.openai-sdk.base-url | The URL to connect to. Auto-detects from `OPENAI_BASE_URL` environment variable if not set. | https://api.openai.com/v1 +| spring.ai.openai-sdk.api-key | The API Key. Auto-detects from `OPENAI_API_KEY` environment variable if not set. | - +| spring.ai.openai-sdk.organization-id | Optionally specify which organization to use for API requests. | - +| spring.ai.openai-sdk.timeout | Request timeout duration. | - +| spring.ai.openai-sdk.max-retries | Maximum number of retry attempts for failed requests. | - +| spring.ai.openai-sdk.proxy | Proxy settings for OpenAI client (Java `Proxy` object). | - +| spring.ai.openai-sdk.custom-headers | Custom HTTP headers to include in requests. Map of header name to header value. | - +|==== + +==== Microsoft Foundry (Azure OpenAI) Properties + +The OpenAI SDK implementation provides native support for Microsoft Foundry (Azure OpenAI) with automatic configuration: + +[cols="3,5,1", stripes=even] +|==== +| Property | Description | Default + +| spring.ai.openai-sdk.microsoft-foundry | Enable Microsoft Foundry mode. Auto-detected if base URL contains `openai.azure.com`, `cognitiveservices.azure.com`, or `.openai.microsoftFoundry.com`. | false +| spring.ai.openai-sdk.microsoft-deployment-name | Microsoft Foundry deployment name. If not specified, the model name will be used. Also accessible via alias `deployment-name`. | - +| spring.ai.openai-sdk.microsoft-foundry-service-version | Microsoft Foundry API service version. | - +| spring.ai.openai-sdk.credential | Credential object for passwordless authentication (requires `com.azure:azure-identity` dependency). | - +|==== + +TIP: Microsoft Foundry (Azure OpenAI) supports passwordless authentication. Add the `com.azure:azure-identity` dependency and the implementation will automatically attempt to use Azure credentials from the environment when no API key is provided. + +==== GitHub Models Properties + +Native support for GitHub Models is available: + +[cols="3,5,1", stripes=even] +|==== +| Property | Description | Default + +| spring.ai.openai-sdk.github-models | Enable GitHub Models mode. Auto-detected if base URL contains `models.github.ai` or `models.inference.ai.azure.com`. | false +|==== + +TIP: GitHub Models requires a Personal Access Token with the `models:read` scope. Set it via the `OPENAI_API_KEY` environment variable or the `spring.ai.openai-sdk.api-key` property. + +==== Embedding Model Properties + +The prefix `spring.ai.openai-sdk.embedding` is the property prefix for configuring the embedding model implementation: + +[cols="3,5,1", stripes=even] +|==== +| Property | Description | Default + +| spring.ai.openai-sdk.embedding.metadata-mode | Document content extraction mode. | EMBED +| spring.ai.openai-sdk.embedding.options.model | The model to use. You can select between models such as: `text-embedding-ada-002`, `text-embedding-3-small`, `text-embedding-3-large`. See the https://platform.openai.com/docs/models[models] page for more information. | `text-embedding-ada-002` +| spring.ai.openai-sdk.embedding.options.user | A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. | - +| spring.ai.openai-sdk.embedding.options.dimensions | The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` and later models. | - +|==== + +TIP: All properties prefixed with `spring.ai.openai-sdk.embedding.options` can be overridden at runtime by adding request-specific <> to the `EmbeddingRequest` call. + +== Runtime Options [[embedding-options]] + +The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingOptions.java[OpenAiSdkEmbeddingOptions.java] provides the OpenAI configurations, such as the model to use, dimensions, and user identifier. + +The default options can be configured using the `spring.ai.openai-sdk.embedding.options` properties as well. + +At start-time use the `OpenAiSdkEmbeddingModel` constructor to set the default options used for all embedding requests. +At run-time you can override the default options, using a `OpenAiSdkEmbeddingOptions` instance as part of your `EmbeddingRequest`. + +For example to override the default model name for a specific request: + +[source,java] +---- +EmbeddingResponse embeddingResponse = embeddingModel.call( + new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"), + OpenAiSdkEmbeddingOptions.builder() + .model("text-embedding-3-large") + .dimensions(1024) + .build())); +---- + +TIP: In addition to the model specific https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingOptions.java[OpenAiSdkEmbeddingOptions] you can use a portable link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-model/src/main/java/org/springframework/ai/embedding/EmbeddingOptions.java[EmbeddingOptions] instance, created with the builder. + +== Sample Controller + +https://start.spring.io/[Create] a new Spring Boot project and add the `spring-ai-openai-sdk` to your pom (or gradle) dependencies. + +Add an `application.properties` file under the `src/main/resources` directory to configure the OpenAI SDK embedding model: + +[source,application.properties] +---- +spring.ai.openai-sdk.api-key=YOUR_API_KEY +spring.ai.openai-sdk.embedding.options.model=text-embedding-ada-002 +---- + +TIP: Replace the `api-key` with your OpenAI credentials. + +This will create an `OpenAiSdkEmbeddingModel` implementation that you can inject into your classes. +Here is an example of a simple `@RestController` class that uses the embedding model. + +[source,java] +---- +@RestController +public class EmbeddingController { + + private final EmbeddingModel embeddingModel; + + @Autowired + public EmbeddingController(EmbeddingModel embeddingModel) { + this.embeddingModel = embeddingModel; + } + + @GetMapping("/ai/embedding") + public Map embed( + @RequestParam(value = "message", defaultValue = "Tell me a joke") String message) { + EmbeddingResponse embeddingResponse = this.embeddingModel.embedForResponse(List.of(message)); + return Map.of("embedding", embeddingResponse); + } +} +---- + +== Manual Configuration + +The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkEmbeddingModel.java[OpenAiSdkEmbeddingModel] implements the `EmbeddingModel` and uses the official OpenAI Java SDK to connect to the OpenAI service. + +If you are not using Spring Boot auto-configuration, you can manually configure the OpenAI SDK Embedding Model. +For this add the `spring-ai-openai-sdk` dependency to your project's Maven `pom.xml` file: + +[source, xml] +---- + + org.springframework.ai + spring-ai-openai-sdk + +---- + +or to your Gradle `build.gradle` build file: + +[source,groovy] +---- +dependencies { + implementation 'org.springframework.ai:spring-ai-openai-sdk' +} +---- + +TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file. + +NOTE: The `spring-ai-openai-sdk` dependency provides access also to the `OpenAiSdkChatModel` and `OpenAiSdkImageModel`. +For more information about the `OpenAiSdkChatModel` refer to the xref:api/chat/openai-sdk-chat.adoc[OpenAI SDK Chat] section. + +Next, create an `OpenAiSdkEmbeddingModel` instance and use it to compute the similarity between two input texts: + +[source,java] +---- +var embeddingOptions = OpenAiSdkEmbeddingOptions.builder() + .model("text-embedding-ada-002") + .apiKey(System.getenv("OPENAI_API_KEY")) + .build(); + +var embeddingModel = new OpenAiSdkEmbeddingModel(embeddingOptions); + +EmbeddingResponse embeddingResponse = embeddingModel + .embedForResponse(List.of("Hello World", "World is big and salvation is near")); +---- + +The `OpenAiSdkEmbeddingOptions` provides the configuration information for the embedding requests. +The options class offers a `builder()` for easy options creation. + +=== Microsoft Foundry (Azure OpenAI) Configuration + +For Microsoft Foundry (Azure OpenAI): + +[source,java] +---- +var embeddingOptions = OpenAiSdkEmbeddingOptions.builder() + .baseUrl("https://your-resource.openai.azure.com") + .apiKey(System.getenv("OPENAI_API_KEY")) + .deploymentName("text-embedding-ada-002") + .azureOpenAIServiceVersion(AzureOpenAIServiceVersion.V2024_10_01_PREVIEW) + .azure(true) // Enables Microsoft Foundry mode + .build(); + +var embeddingModel = new OpenAiSdkEmbeddingModel(embeddingOptions); +---- + +TIP: Microsoft Foundry (Azure OpenAI) supports passwordless authentication. Add the `com.azure:azure-identity` dependency to your project. If you don't provide an API key, the implementation will automatically attempt to use Azure credentials from your environment. + +=== GitHub Models Configuration + +For GitHub Models: + +[source,java] +---- +var embeddingOptions = OpenAiSdkEmbeddingOptions.builder() + .baseUrl("https://models.inference.ai.azure.com") + .apiKey(System.getenv("GITHUB_TOKEN")) + .model("text-embedding-3-large") + .githubModels(true) + .build(); + +var embeddingModel = new OpenAiSdkEmbeddingModel(embeddingOptions); +---- + +== Observability + +The OpenAI SDK implementation supports Spring AI's observability features through Micrometer. +All embedding model operations are instrumented for monitoring and tracing. + +== Additional Resources + +* link:https://github.com/openai/openai-java[Official OpenAI Java SDK] +* link:https://platform.openai.com/docs/api-reference/embeddings[OpenAI Embeddings API Documentation] +* link:https://platform.openai.com/docs/models[OpenAI Models] +* link:https://learn.microsoft.com/en-us/azure/ai-services/openai/[Azure OpenAI Documentation] +* link:https://github.com/marketplace/models[GitHub Models] diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/image/openai-sdk-image.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/image/openai-sdk-image.adoc new file mode 100644 index 00000000000..dcb53e212be --- /dev/null +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/image/openai-sdk-image.adoc @@ -0,0 +1,381 @@ += OpenAI SDK Image Generation (Official) + +Spring AI supports OpenAI's DALL-E image generation models through the OpenAI Java SDK, providing a robust and officially-maintained integration with OpenAI's services including Azure OpenAI and GitHub Models. + +NOTE: This implementation uses the official link:https://github.com/openai/openai-java[OpenAI Java SDK] from OpenAI. For the alternative Spring AI implementation, see xref:api/image/openai-image.adoc[OpenAI Image Generation]. + +DALL-E is a state-of-the-art image generation model from OpenAI that can create realistic images and art from natural language descriptions. + +The OpenAI SDK module automatically detects the service provider (OpenAI, Microsoft Foundry, or GitHub Models) based on the base URL you provide. + +== Authentication + +Authentication is done using a base URL and an API Key. The implementation provides flexible configuration options through Spring Boot properties or environment variables. + +=== Using OpenAI + +If you are using OpenAI directly, create an account at https://platform.openai.com/signup[OpenAI signup page] and generate an API key on the https://platform.openai.com/account/api-keys[API Keys page]. + +The base URL doesn't need to be set as it defaults to `https://api.openai.com/v1`: + +[source,properties] +---- +spring.ai.openai-sdk.api-key= +# base-url is optional, defaults to https://api.openai.com/v1 +---- + +Or using environment variables: + +[source,bash] +---- +export OPENAI_API_KEY= +# OPENAI_BASE_URL is optional, defaults to https://api.openai.com/v1 +---- + +=== Using Microsoft Foundry (Azure OpenAI) + +Microsoft Foundry is automatically detected when using a Microsoft Foundry URL. You can configure it using properties: + +[source,properties] +---- +spring.ai.openai-sdk.base-url=https://.openai.azure.com +spring.ai.openai-sdk.api-key= +spring.ai.openai-sdk.microsoft-deployment-name= +---- + +Or using environment variables: + +[source,bash] +---- +export OPENAI_BASE_URL=https://.openai.azure.com +export OPENAI_API_KEY= +---- + +**Passwordless Authentication (Recommended for Azure):** + +Microsoft Foundry supports passwordless authentication without providing an API key, which is more secure when running on Azure. + +To enable passwordless authentication, add the `com.azure:azure-identity` dependency: + +[source,xml] +---- + + com.azure + azure-identity + +---- + +Then configure without an API key: + +[source,properties] +---- +spring.ai.openai-sdk.base-url=https://.openai.azure.com +spring.ai.openai-sdk.microsoft-deployment-name= +# No api-key needed - will use Azure credentials from environment +---- + +=== Using GitHub Models + +GitHub Models is automatically detected when using the GitHub Models base URL. You'll need to create a GitHub Personal Access Token (PAT) with the `models:read` scope. + +[source,properties] +---- +spring.ai.openai-sdk.base-url=https://models.inference.ai.azure.com +spring.ai.openai-sdk.api-key=github_pat_XXXXXXXXXXX +---- + +Or using environment variables: + +[source,bash] +---- +export OPENAI_BASE_URL=https://models.inference.ai.azure.com +export OPENAI_API_KEY=github_pat_XXXXXXXXXXX +---- + +TIP: For enhanced security when handling sensitive information like API keys, you can use Spring Expression Language (SpEL) in your properties: + +[source,properties] +---- +spring.ai.openai-sdk.api-key=${OPENAI_API_KEY} +---- + +=== Add Repositories and BOM + +Spring AI artifacts are published in Maven Central and Spring Snapshot repositories. +Refer to the xref:getting-started.adoc#artifact-repositories[Artifact Repositories] section to add these repositories to your build system. + +To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build system. + +== Auto-configuration + +Spring AI provides Spring Boot auto-configuration for the OpenAI SDK Image Model. +To enable it add the following dependency to your project's Maven `pom.xml` or Gradle `build.gradle` build files: + +[tabs] +====== +Maven:: ++ +[source, xml] +---- + + org.springframework.ai + spring-ai-starter-model-openai-sdk + +---- + +Gradle:: ++ +[source,groovy] +---- +dependencies { + implementation 'org.springframework.ai:spring-ai-starter-model-openai-sdk' +} +---- +====== + +TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file. + +=== Configuration Properties + +==== Connection Properties + +The prefix `spring.ai.openai-sdk` is used as the property prefix that lets you configure the OpenAI SDK client. + +[cols="3,5,1", stripes=even] +|==== +| Property | Description | Default + +| spring.ai.openai-sdk.base-url | The URL to connect to. Auto-detects from `OPENAI_BASE_URL` environment variable if not set. | https://api.openai.com/v1 +| spring.ai.openai-sdk.api-key | The API Key. Auto-detects from `OPENAI_API_KEY` environment variable if not set. | - +| spring.ai.openai-sdk.organization-id | Optionally specify which organization to use for API requests. | - +| spring.ai.openai-sdk.timeout | Request timeout duration. | - +| spring.ai.openai-sdk.max-retries | Maximum number of retry attempts for failed requests. | - +| spring.ai.openai-sdk.proxy | Proxy settings for OpenAI client (Java `Proxy` object). | - +| spring.ai.openai-sdk.custom-headers | Custom HTTP headers to include in requests. Map of header name to header value. | - +|==== + +==== Microsoft Foundry (Azure OpenAI) Properties + +The OpenAI SDK implementation provides native support for Microsoft Foundry (Azure OpenAI) with automatic configuration: + +[cols="3,5,1", stripes=even] +|==== +| Property | Description | Default + +| spring.ai.openai-sdk.microsoft-foundry | Enable Microsoft Foundry mode. Auto-detected if base URL contains `openai.azure.com`, `cognitiveservices.azure.com`, or `.openai.microsoftFoundry.com`. | false +| spring.ai.openai-sdk.microsoft-deployment-name | Microsoft Foundry deployment name. If not specified, the model name will be used. Also accessible via alias `deployment-name`. | - +| spring.ai.openai-sdk.microsoft-foundry-service-version | Microsoft Foundry API service version. | - +| spring.ai.openai-sdk.credential | Credential object for passwordless authentication (requires `com.azure:azure-identity` dependency). | - +|==== + +TIP: Microsoft Foundry (Azure OpenAI) supports passwordless authentication. Add the `com.azure:azure-identity` dependency and the implementation will automatically attempt to use Azure credentials from the environment when no API key is provided. + +==== GitHub Models Properties + +Native support for GitHub Models is available: + +[cols="3,5,1", stripes=even] +|==== +| Property | Description | Default + +| spring.ai.openai-sdk.github-models | Enable GitHub Models mode. Auto-detected if base URL contains `models.github.ai` or `models.inference.ai.azure.com`. | false +|==== + +TIP: GitHub Models requires a Personal Access Token with the `models:read` scope. Set it via the `OPENAI_API_KEY` environment variable or the `spring.ai.openai-sdk.api-key` property. + +==== Image Model Properties + +The prefix `spring.ai.openai-sdk.image` is the property prefix for configuring the image model implementation: + +[cols="3,5,1", stripes=even] +|==== +| Property | Description | Default + +| spring.ai.openai-sdk.image.options.model | The model to use for image generation. Available models: `dall-e-2`, `dall-e-3`. See the https://platform.openai.com/docs/models[models] page for more information. | `dall-e-3` +| spring.ai.openai-sdk.image.options.n | The number of images to generate. Must be between 1 and 10. For `dall-e-3`, only n=1 is supported. | - +| spring.ai.openai-sdk.image.options.quality | The quality of the image that will be generated. `hd` creates images with finer details and greater consistency across the image. This parameter is only supported for `dall-e-3`. Available values: `standard`, `hd`. | - +| spring.ai.openai-sdk.image.options.response-format | The format in which the generated images are returned. Must be one of `url` or `b64_json`. | - +| spring.ai.openai-sdk.image.options.size | The size of the generated images. Must be one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`. Must be one of `1024x1024`, `1792x1024`, or `1024x1792` for `dall-e-3` models. | - +| spring.ai.openai-sdk.image.options.width | The width of the generated images. Must be one of 256, 512, or 1024 for `dall-e-2`. | - +| spring.ai.openai-sdk.image.options.height | The height of the generated images. Must be one of 256, 512, or 1024 for `dall-e-2`. | - +| spring.ai.openai-sdk.image.options.style | The style of the generated images. Must be one of `vivid` or `natural`. Vivid causes the model to lean towards generating hyper-real and dramatic images. Natural causes the model to produce more natural, less hyper-real looking images. This parameter is only supported for `dall-e-3`. | - +| spring.ai.openai-sdk.image.options.user | A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. | - +|==== + +TIP: All properties prefixed with `spring.ai.openai-sdk.image.options` can be overridden at runtime by adding request-specific <> to the `ImagePrompt` call. + +== Runtime Options [[image-options]] + +The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java[OpenAiSdkImageOptions.java] provides the OpenAI configurations, such as the model to use, quality, size, style, and number of images to generate. + +The default options can be configured using the `spring.ai.openai-sdk.image.options` properties as well. + +At start-time use the `OpenAiSdkImageModel` constructor to set the default options used for all image generation requests. +At run-time you can override the default options, using a `OpenAiSdkImageOptions` instance as part of your `ImagePrompt`. + +For example to override the default model and quality for a specific request: + +[source,java] +---- +ImageResponse response = imageModel.call( + new ImagePrompt("A light cream colored mini golden doodle", + OpenAiSdkImageOptions.builder() + .model("dall-e-3") + .quality("hd") + .N(1) + .width(1024) + .height(1024) + .style("vivid") + .build())); +---- + +TIP: In addition to the model specific https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageOptions.java[OpenAiSdkImageOptions] you can use a portable link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-model/src/main/java/org/springframework/ai/image/ImageOptions.java[ImageOptions] instance, created with the link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-model/src/main/java/org/springframework/ai/image/ImageOptionsBuilder.java[ImageOptionsBuilder#builder()]. + +== Sample Controller + +https://start.spring.io/[Create] a new Spring Boot project and add the `spring-ai-openai-sdk` to your pom (or gradle) dependencies. + +Add an `application.properties` file under the `src/main/resources` directory to configure the OpenAI SDK image model: + +[source,application.properties] +---- +spring.ai.openai-sdk.api-key=YOUR_API_KEY +spring.ai.openai-sdk.image.options.model=dall-e-3 +---- + +TIP: Replace the `api-key` with your OpenAI credentials. + +This will create an `OpenAiSdkImageModel` implementation that you can inject into your classes. +Here is an example of a simple `@RestController` class that uses the image model. + +[source,java] +---- +@RestController +public class ImageController { + + private final ImageModel imageModel; + + @Autowired + public ImageController(ImageModel imageModel) { + this.imageModel = imageModel; + } + + @GetMapping("/ai/image") + public Map generateImage( + @RequestParam(value = "prompt", defaultValue = "A light cream colored mini golden doodle") String prompt) { + ImageResponse response = this.imageModel.call( + new ImagePrompt(prompt, + OpenAiSdkImageOptions.builder() + .quality("hd") + .N(1) + .width(1024) + .height(1024) + .build())); + + String imageUrl = response.getResult().getOutput().getUrl(); + return Map.of("url", imageUrl); + } +} +---- + +== Manual Configuration + +The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkImageModel.java[OpenAiSdkImageModel] implements the `ImageModel` and uses the official OpenAI Java SDK to connect to the OpenAI service. + +If you are not using Spring Boot auto-configuration, you can manually configure the OpenAI SDK Image Model. +For this add the `spring-ai-openai-sdk` dependency to your project's Maven `pom.xml` file: + +[source, xml] +---- + + org.springframework.ai + spring-ai-openai-sdk + +---- + +or to your Gradle `build.gradle` build file: + +[source,groovy] +---- +dependencies { + implementation 'org.springframework.ai:spring-ai-openai-sdk' +} +---- + +TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file. + +NOTE: The `spring-ai-openai-sdk` dependency provides access also to the `OpenAiSdkChatModel` and `OpenAiSdkEmbeddingModel`. +For more information about the `OpenAiSdkChatModel` refer to the xref:api/chat/openai-sdk-chat.adoc[OpenAI SDK Chat] section. + +Next, create an `OpenAiSdkImageModel` instance and use it to generate images: + +[source,java] +---- +var imageOptions = OpenAiSdkImageOptions.builder() + .model("dall-e-3") + .quality("hd") + .apiKey(System.getenv("OPENAI_API_KEY")) + .build(); + +var imageModel = new OpenAiSdkImageModel(imageOptions); + +ImageResponse response = imageModel.call( + new ImagePrompt("A light cream colored mini golden doodle", + OpenAiSdkImageOptions.builder() + .N(1) + .width(1024) + .height(1024) + .build())); +---- + +The `OpenAiSdkImageOptions` provides the configuration information for the image generation requests. +The options class offers a `builder()` for easy options creation. + +=== Microsoft Foundry (Azure OpenAI) Configuration + +For Microsoft Foundry (Azure OpenAI): + +[source,java] +---- +var imageOptions = OpenAiSdkImageOptions.builder() + .baseUrl("https://your-resource.openai.azure.com") + .apiKey(System.getenv("OPENAI_API_KEY")) + .deploymentName("dall-e-3") + .azureOpenAIServiceVersion(AzureOpenAIServiceVersion.V2024_10_01_PREVIEW) + .azure(true) // Enables Microsoft Foundry mode + .build(); + +var imageModel = new OpenAiSdkImageModel(imageOptions); +---- + +TIP: Microsoft Foundry (Azure OpenAI) supports passwordless authentication. Add the `com.azure:azure-identity` dependency to your project. If you don't provide an API key, the implementation will automatically attempt to use Azure credentials from your environment. + +=== GitHub Models Configuration + +For GitHub Models: + +[source,java] +---- +var imageOptions = OpenAiSdkImageOptions.builder() + .baseUrl("https://models.inference.ai.azure.com") + .apiKey(System.getenv("GITHUB_TOKEN")) + .model("dall-e-3") + .githubModels(true) + .build(); + +var imageModel = new OpenAiSdkImageModel(imageOptions); +---- + +== Observability + +The OpenAI SDK implementation supports Spring AI's observability features through Micrometer. +All image model operations are instrumented for monitoring and tracing. + +== Additional Resources + +* link:https://github.com/openai/openai-java[Official OpenAI Java SDK] +* link:https://platform.openai.com/docs/api-reference/images[OpenAI Images API Documentation] +* link:https://platform.openai.com/docs/guides/images[OpenAI Image Generation Guide] +* link:https://platform.openai.com/docs/models[OpenAI Models] +* link:https://learn.microsoft.com/en-us/azure/ai-services/openai/[Azure OpenAI Documentation] +* link:https://github.com/marketplace/models[GitHub Models] From 8c324366aced455b07042b10cf0b902e426ef618 Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Tue, 25 Nov 2025 16:30:59 +0100 Subject: [PATCH 46/49] Update to use Microsoft Foundry everywhere in the documentation Signed-off-by: Julien Dubois --- .../ROOT/pages/api/chat/openai-sdk-chat.adoc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc index 2d93f92ec84..1c02cb69e7b 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/openai-sdk-chat.adoc @@ -1,6 +1,6 @@ = OpenAI SDK Chat (Official) -Spring AI supports OpenAI's language models through the OpenAI Java SDK, providing a robust and officially-maintained integration with OpenAI's services including Azure OpenAI and GitHub Models. +Spring AI supports OpenAI's language models through the OpenAI Java SDK, providing a robust and officially-maintained integration with OpenAI's services including Microsoft Foundry and GitHub Models. NOTE: This implementation uses the official link:https://github.com/openai/openai-java[OpenAI Java SDK] from OpenAI. For the alternative Spring AI implementation, see xref:api/chat/openai-chat.adoc[OpenAI Chat]. @@ -31,7 +31,7 @@ export OPENAI_API_KEY= # OPENAI_BASE_URL is optional, defaults to https://api.openai.com/v1 ---- -=== Using Microsoft Foundry (Azure OpenAI) +=== Using Microsoft Foundry Microsoft Foundry is automatically detected when using a Microsoft Foundry URL. You can configure it using properties: @@ -167,7 +167,7 @@ The OpenAI SDK implementation provides native support for Microsoft Foundry (Azu | spring.ai.openai-sdk.credential | Credential object for passwordless authentication (requires `com.azure:azure-identity` dependency). | - |==== -TIP: Microsoft Foundry (Azure OpenAI) supports passwordless authentication. Add the `com.azure:azure-identity` dependency and the implementation will automatically attempt to use Azure credentials from the environment when no API key is provided. +TIP: Microsoft Foundry supports passwordless authentication. Add the `com.azure:azure-identity` dependency and the implementation will automatically attempt to use Azure credentials from the environment when no API key is provided. ==== GitHub Models Properties @@ -582,9 +582,9 @@ Flux response = chatModel.stream( new Prompt("Generate the names of 5 famous pirates.")); ---- -=== Microsoft Foundry (Azure OpenAI) Configuration +=== Microsoft Foundry Configuration -For Microsoft Foundry (Azure OpenAI): +For Microsoft Foundry : [source,java] ---- @@ -599,7 +599,7 @@ var chatOptions = OpenAiSdkChatOptions.builder() var chatModel = new OpenAiSdkChatModel(chatOptions); ---- -TIP: Microsoft Foundry (Azure OpenAI) supports passwordless authentication. Add the `com.azure:azure-identity` dependency to your project. If you don't provide an API key, the implementation will automatically attempt to use Azure credentials from your environment. +TIP: Microsoft Foundry supports passwordless authentication. Add the `com.azure:azure-identity` dependency to your project. If you don't provide an API key, the implementation will automatically attempt to use Azure credentials from your environment. === GitHub Models Configuration @@ -637,7 +637,7 @@ This implementation differs from the xref:api/chat/openai-chat.adoc[Spring AI Op **When to use OpenAI SDK:** * You're starting a new project -* You primarily use Azure OpenAI or GitHub Models +* You primarily use Microsoft Foundry or GitHub Models * You want automatic API updates from OpenAI * You don't need audio transcription or moderation features * You prefer official SDK support @@ -671,5 +671,5 @@ These features are available in the xref:api/chat/openai-chat.adoc[Spring AI Ope * link:https://github.com/openai/openai-java[Official OpenAI Java SDK] * link:https://platform.openai.com/docs/api-reference/chat[OpenAI Chat API Documentation] * link:https://platform.openai.com/docs/models[OpenAI Models] -* link:https://learn.microsoft.com/en-us/azure/ai-services/openai/[Azure OpenAI Documentation] +* link:https://learn.microsoft.com/en-us/azure/ai-foundry/[Microsoft Foundry Documentation] * link:https://github.com/marketplace/models[GitHub Models] From e88c09f8fabf340920ea549512f4987e4064d57a Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Tue, 25 Nov 2025 16:37:03 +0100 Subject: [PATCH 47/49] Update to use Microsoft Foundry everywhere in the documentation Signed-off-by: Julien Dubois --- .../api/embeddings/openai-sdk-embeddings.adoc | 18 +++++++++--------- .../ROOT/pages/api/image/openai-sdk-image.adoc | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/openai-sdk-embeddings.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/openai-sdk-embeddings.adoc index 680c853b7bf..d2c0b8320f1 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/openai-sdk-embeddings.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/openai-sdk-embeddings.adoc @@ -1,6 +1,6 @@ = OpenAI SDK Embeddings (Official) -Spring AI supports OpenAI's text embeddings models through the OpenAI Java SDK, providing a robust and officially-maintained integration with OpenAI's services including Azure OpenAI and GitHub Models. +Spring AI supports OpenAI's text embeddings models through the OpenAI Java SDK, providing a robust and officially-maintained integration with OpenAI's services including Microsoft Foundry and GitHub Models. NOTE: This implementation uses the official link:https://github.com/openai/openai-java[OpenAI Java SDK] from OpenAI. For the alternative Spring AI implementation, see xref:api/embeddings/openai-embeddings.adoc[OpenAI Embeddings]. @@ -33,7 +33,7 @@ export OPENAI_API_KEY= # OPENAI_BASE_URL is optional, defaults to https://api.openai.com/v1 ---- -=== Using Microsoft Foundry (Azure OpenAI) +=== Using Microsoft Foundry Microsoft Foundry is automatically detected when using a Microsoft Foundry URL. You can configure it using properties: @@ -155,9 +155,9 @@ The prefix `spring.ai.openai-sdk` is used as the property prefix that lets you c | spring.ai.openai-sdk.custom-headers | Custom HTTP headers to include in requests. Map of header name to header value. | - |==== -==== Microsoft Foundry (Azure OpenAI) Properties +==== Microsoft Foundry Properties -The OpenAI SDK implementation provides native support for Microsoft Foundry (Azure OpenAI) with automatic configuration: +The OpenAI SDK implementation provides native support for Microsoft Foundry with automatic configuration: [cols="3,5,1", stripes=even] |==== @@ -169,7 +169,7 @@ The OpenAI SDK implementation provides native support for Microsoft Foundry (Azu | spring.ai.openai-sdk.credential | Credential object for passwordless authentication (requires `com.azure:azure-identity` dependency). | - |==== -TIP: Microsoft Foundry (Azure OpenAI) supports passwordless authentication. Add the `com.azure:azure-identity` dependency and the implementation will automatically attempt to use Azure credentials from the environment when no API key is provided. +TIP: Microsoft Foundry supports passwordless authentication. Add the `com.azure:azure-identity` dependency and the implementation will automatically attempt to use Azure credentials from the environment when no API key is provided. ==== GitHub Models Properties @@ -308,9 +308,9 @@ EmbeddingResponse embeddingResponse = embeddingModel The `OpenAiSdkEmbeddingOptions` provides the configuration information for the embedding requests. The options class offers a `builder()` for easy options creation. -=== Microsoft Foundry (Azure OpenAI) Configuration +=== Microsoft Foundry Configuration -For Microsoft Foundry (Azure OpenAI): +For Microsoft Foundry: [source,java] ---- @@ -325,7 +325,7 @@ var embeddingOptions = OpenAiSdkEmbeddingOptions.builder() var embeddingModel = new OpenAiSdkEmbeddingModel(embeddingOptions); ---- -TIP: Microsoft Foundry (Azure OpenAI) supports passwordless authentication. Add the `com.azure:azure-identity` dependency to your project. If you don't provide an API key, the implementation will automatically attempt to use Azure credentials from your environment. +TIP: Microsoft Foundry supports passwordless authentication. Add the `com.azure:azure-identity` dependency to your project. If you don't provide an API key, the implementation will automatically attempt to use Azure credentials from your environment. === GitHub Models Configuration @@ -353,5 +353,5 @@ All embedding model operations are instrumented for monitoring and tracing. * link:https://github.com/openai/openai-java[Official OpenAI Java SDK] * link:https://platform.openai.com/docs/api-reference/embeddings[OpenAI Embeddings API Documentation] * link:https://platform.openai.com/docs/models[OpenAI Models] -* link:https://learn.microsoft.com/en-us/azure/ai-services/openai/[Azure OpenAI Documentation] +* link:https://learn.microsoft.com/en-us/azure/ai-foundry/[Microsoft Foundry Documentation] * link:https://github.com/marketplace/models[GitHub Models] diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/image/openai-sdk-image.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/image/openai-sdk-image.adoc index dcb53e212be..ad7409d651b 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/image/openai-sdk-image.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/image/openai-sdk-image.adoc @@ -1,6 +1,6 @@ = OpenAI SDK Image Generation (Official) -Spring AI supports OpenAI's DALL-E image generation models through the OpenAI Java SDK, providing a robust and officially-maintained integration with OpenAI's services including Azure OpenAI and GitHub Models. +Spring AI supports OpenAI's DALL-E image generation models through the OpenAI Java SDK, providing a robust and officially-maintained integration with OpenAI's services including Microsoft Foundry and GitHub Models. NOTE: This implementation uses the official link:https://github.com/openai/openai-java[OpenAI Java SDK] from OpenAI. For the alternative Spring AI implementation, see xref:api/image/openai-image.adoc[OpenAI Image Generation]. @@ -32,7 +32,7 @@ export OPENAI_API_KEY= # OPENAI_BASE_URL is optional, defaults to https://api.openai.com/v1 ---- -=== Using Microsoft Foundry (Azure OpenAI) +=== Using Microsoft Foundry Microsoft Foundry is automatically detected when using a Microsoft Foundry URL. You can configure it using properties: @@ -154,9 +154,9 @@ The prefix `spring.ai.openai-sdk` is used as the property prefix that lets you c | spring.ai.openai-sdk.custom-headers | Custom HTTP headers to include in requests. Map of header name to header value. | - |==== -==== Microsoft Foundry (Azure OpenAI) Properties +==== Microsoft Foundry Properties -The OpenAI SDK implementation provides native support for Microsoft Foundry (Azure OpenAI) with automatic configuration: +The OpenAI SDK implementation provides native support for Microsoft Foundry with automatic configuration: [cols="3,5,1", stripes=even] |==== @@ -168,7 +168,7 @@ The OpenAI SDK implementation provides native support for Microsoft Foundry (Azu | spring.ai.openai-sdk.credential | Credential object for passwordless authentication (requires `com.azure:azure-identity` dependency). | - |==== -TIP: Microsoft Foundry (Azure OpenAI) supports passwordless authentication. Add the `com.azure:azure-identity` dependency and the implementation will automatically attempt to use Azure credentials from the environment when no API key is provided. +TIP: Microsoft Foundry supports passwordless authentication. Add the `com.azure:azure-identity` dependency and the implementation will automatically attempt to use Azure credentials from the environment when no API key is provided. ==== GitHub Models Properties @@ -331,9 +331,9 @@ ImageResponse response = imageModel.call( The `OpenAiSdkImageOptions` provides the configuration information for the image generation requests. The options class offers a `builder()` for easy options creation. -=== Microsoft Foundry (Azure OpenAI) Configuration +=== Microsoft Foundry Configuration -For Microsoft Foundry (Azure OpenAI): +For Microsoft Foundry: [source,java] ---- @@ -348,7 +348,7 @@ var imageOptions = OpenAiSdkImageOptions.builder() var imageModel = new OpenAiSdkImageModel(imageOptions); ---- -TIP: Microsoft Foundry (Azure OpenAI) supports passwordless authentication. Add the `com.azure:azure-identity` dependency to your project. If you don't provide an API key, the implementation will automatically attempt to use Azure credentials from your environment. +TIP: Microsoft Foundry supports passwordless authentication. Add the `com.azure:azure-identity` dependency to your project. If you don't provide an API key, the implementation will automatically attempt to use Azure credentials from your environment. === GitHub Models Configuration @@ -377,5 +377,5 @@ All image model operations are instrumented for monitoring and tracing. * link:https://platform.openai.com/docs/api-reference/images[OpenAI Images API Documentation] * link:https://platform.openai.com/docs/guides/images[OpenAI Image Generation Guide] * link:https://platform.openai.com/docs/models[OpenAI Models] -* link:https://learn.microsoft.com/en-us/azure/ai-services/openai/[Azure OpenAI Documentation] +* link:https://learn.microsoft.com/en-us/azure/ai-foundry/[Microsoft Foundry Documentation] * link:https://github.com/marketplace/models[GitHub Models] From 8bfdb473f3890df01a6516788421f048d6ad237e Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Tue, 25 Nov 2025 20:56:12 +0100 Subject: [PATCH 48/49] Add native structured output support Signed-off-by: Christian Tzolov --- .../ai/openaisdk/OpenAiSdkChatOptions.java | 19 ++++++++++- .../chat/client/OpenAiSdkChatClientIT.java | 32 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java index a9155c1e997..a2a2609d943 100644 --- a/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java +++ b/models/spring-ai-openai-sdk/src/main/java/org/springframework/ai/openaisdk/OpenAiSdkChatOptions.java @@ -25,12 +25,15 @@ import java.util.Objects; import java.util.Set; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.openai.models.ChatModel; import com.openai.models.chat.completions.ChatCompletionAudioParam; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.ai.model.tool.StructuredOutputChatOptions; import org.springframework.ai.model.tool.ToolCallingChatOptions; +import org.springframework.ai.openaisdk.OpenAiSdkChatModel.ResponseFormat.Type; import org.springframework.ai.tool.ToolCallback; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -41,7 +44,8 @@ * @author Julien Dubois * @author Christian Tzolov */ -public class OpenAiSdkChatOptions extends AbstractOpenAiSdkOptions implements ToolCallingChatOptions { +public class OpenAiSdkChatOptions extends AbstractOpenAiSdkOptions + implements ToolCallingChatOptions, StructuredOutputChatOptions { public static final String DEFAULT_CHAT_MODEL = ChatModel.GPT_5_MINI.asString(); @@ -540,6 +544,19 @@ public Integer getTopK() { return null; } + @Override + @JsonIgnore + public String getOutputSchema() { + return this.getResponseFormat().getJsonSchema(); + } + + @Override + @JsonIgnore + public void setOutputSchema(String outputSchema) { + this.setResponseFormat( + OpenAiSdkChatModel.ResponseFormat.builder().type(Type.JSON_SCHEMA).jsonSchema(outputSchema).build()); + } + public static Builder builder() { return new Builder(); } diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/client/OpenAiSdkChatClientIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/client/OpenAiSdkChatClientIT.java index 6dfe686498c..2e83f4ea59b 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/client/OpenAiSdkChatClientIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/client/OpenAiSdkChatClientIT.java @@ -32,6 +32,7 @@ import org.slf4j.LoggerFactory; import reactor.core.publisher.Flux; +import org.springframework.ai.chat.client.AdvisorParams; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor; import org.springframework.ai.chat.model.ChatModel; @@ -177,6 +178,21 @@ void beanOutputConverter() { assertThat(actorsFilms.actor()).isNotBlank(); } + @Test + void beanOutputConverterNativeStructuredOutput() { + + // @formatter:off + ActorsFilms actorsFilms = ChatClient.create(this.chatModel).prompt() + .advisors(AdvisorParams.ENABLE_NATIVE_STRUCTURED_OUTPUT) + .user("Generate the filmography for a random actor.") + .call() + .entity(ActorsFilms.class); + // @formatter:on + + logger.info("" + actorsFilms); + assertThat(actorsFilms.actor()).isNotBlank(); + } + @Test void beanOutputConverterRecords() { @@ -192,6 +208,22 @@ void beanOutputConverterRecords() { assertThat(actorsFilms.movies()).hasSize(5); } + @Test + void beanOutputConverterRecordsNativeStructuredOutput() { + + // @formatter:off + ActorsFilms actorsFilms = ChatClient.create(this.chatModel).prompt() + .advisors(AdvisorParams.ENABLE_NATIVE_STRUCTURED_OUTPUT) + .user("Generate the filmography of 5 movies for Tom Hanks.") + .call() + .entity(ActorsFilms.class); + // @formatter:on + + logger.info("" + actorsFilms); + assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks"); + assertThat(actorsFilms.movies()).hasSize(5); + } + @Test void beanStreamOutputConverterRecords() { From bd52277f00665bee8ae74f6cae82d72199fde2fc Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Tue, 25 Nov 2025 21:45:33 +0100 Subject: [PATCH 49/49] improve test resilience Signed-off-by: Christian Tzolov --- .../ai/openaisdk/chat/client/OpenAiSdkChatClientIT.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/client/OpenAiSdkChatClientIT.java b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/client/OpenAiSdkChatClientIT.java index 2e83f4ea59b..79d42be9d24 100644 --- a/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/client/OpenAiSdkChatClientIT.java +++ b/models/spring-ai-openai-sdk/src/test/java/org/springframework/ai/openaisdk/chat/client/OpenAiSdkChatClientIT.java @@ -274,7 +274,7 @@ void functionCallTest() { // @formatter:off String response = ChatClient.create(this.chatModel).prompt() - .user(u -> u.text("What's the weather like in San Francisco, Tokyo, and Paris?")) + .user(u -> u.text("What's the weather like in San Francisco, Tokyo, and Paris in Celsius?")) .toolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService()) .description("Get the weather in location") .inputType(MockWeatherService.Request.class) @@ -297,7 +297,7 @@ void defaultFunctionCallTest() { .description("Get the weather in location") .inputType(MockWeatherService.Request.class) .build()) - .defaultUser(u -> u.text("What's the weather like in San Francisco, Tokyo, and Paris?")) + .defaultUser(u -> u.text("What's the weather like in San Francisco, Tokyo, and Paris in Celsius?")) .build() .prompt().call().content(); // @formatter:on @@ -312,7 +312,7 @@ void streamFunctionCallTest() { // @formatter:off Flux response = ChatClient.create(this.chatModel).prompt() - .user("What's the weather like in San Francisco, Tokyo, and Paris?") + .user("What's the weather like in San Francisco, Tokyo, and Paris in Celsius?") .toolCallbacks(FunctionToolCallback.builder("getCurrentWeather", new MockWeatherService()) .description("Get the weather in location") .inputType(MockWeatherService.Request.class)