diff --git a/genai-function-calling/spring-ai/Dockerfile b/genai-function-calling/spring-ai/Dockerfile index 048451b..0373d32 100644 --- a/genai-function-calling/spring-ai/Dockerfile +++ b/genai-function-calling/spring-ai/Dockerfile @@ -2,11 +2,14 @@ FROM eclipse-temurin:21-jdk-alpine AS build WORKDIR /build -# Copy the local code to the container -COPY mvnw pom.xml ./ -COPY src ./src +# Install dependencies (verify resolves more than dependency:go-offline) +COPY mvnw ./ COPY .mvn ./.mvn +COPY pom.xml ./ +RUN ./mvnw verify -DskipTests +# Copy source code and build the application +COPY src ./src RUN ./mvnw package FROM eclipse-temurin:21-jre-alpine diff --git a/genai-function-calling/spring-ai/pom.xml b/genai-function-calling/spring-ai/pom.xml index bea1b2e..5f139ae 100644 --- a/genai-function-calling/spring-ai/pom.xml +++ b/genai-function-calling/spring-ai/pom.xml @@ -14,8 +14,8 @@ genai-function-calling Function Calling with Spring AI - 17 - 1.0.0-M6 + 21 + 1.0.0-M7 @@ -24,11 +24,11 @@ org.springframework.ai - spring-ai-azure-openai-spring-boot-starter + spring-ai-starter-model-azure-openai org.springframework.ai - spring-ai-openai-spring-boot-starter + spring-ai-starter-model-openai org.springframework.boot @@ -93,7 +93,7 @@ org.codehaus.mojo exec-maven-plugin - 3.4.1 + 3.5.0 diff --git a/genai-function-calling/spring-ai/src/main/java/example/Main.java b/genai-function-calling/spring-ai/src/main/java/example/Main.java index 9b74db3..45ef720 100644 --- a/genai-function-calling/spring-ai/src/main/java/example/Main.java +++ b/genai-function-calling/spring-ai/src/main/java/example/Main.java @@ -6,10 +6,12 @@ import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.model.ChatModel; +import org.springframework.ai.model.SpringAIModelProperties; +import org.springframework.ai.model.SpringAIModels; import org.springframework.ai.model.tool.DefaultToolCallingChatOptions; import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; @@ -17,12 +19,12 @@ public class Main { @Component - static class VersionAgent implements CommandLineRunner { + static class VersionAgent implements CommandLineRunner { private final ChatClient chat; private final ElasticsearchTools tools; - VersionAgent(ChatModel chat, ElasticsearchTools tools) { + VersionAgent(ChatModel chat, ElasticsearchTools tools) { this.chat = ChatClient.builder(chat).build(); this.tools = tools; } @@ -51,7 +53,15 @@ OpenTelemetry openTelemetry() { } public static void main(String[] args) { - SpringApplication.run(Main.class, args); + // Choose between Azure OpenAI and OpenAI based on the presence of the official SDK + // environment variable AZURE_OPENAI_API_KEY. Otherwise, we'd create two beans. + String azureApiKey = System.getenv("AZURE_OPENAI_API_KEY"); + String chatModel = azureApiKey != null && !azureApiKey.trim().isEmpty() + ? SpringAIModels.AZURE_OPENAI + : SpringAIModels.OPENAI; + new SpringApplicationBuilder(Main.class) + .properties(SpringAIModelProperties.CHAT_MODEL + "=" + chatModel) + .run(args); } } diff --git a/genai-function-calling/spring-ai/src/main/resources/application.yml b/genai-function-calling/spring-ai/src/main/resources/application.yml index ea4d2ba..bda5c0d 100644 --- a/genai-function-calling/spring-ai/src/main/resources/application.yml +++ b/genai-function-calling/spring-ai/src/main/resources/application.yml @@ -4,6 +4,13 @@ spring: web-application-type: none banner-mode: "off" ai: + # Right now, we cannot select azure or openai with a single property. + # See https://github.com/spring-projects/spring-ai/issues/2712 + model: + embedding: ${spring.ai.model.chat} + audio: + transcription: ${spring.ai.model.chat} + image: ${spring.ai.model.chat} openai: base-url: ${OPENAI_BASE_URL:https://api.openai.com/v1} api-key: ${OPENAI_API_KEY:enter-your-api-key} @@ -30,11 +37,18 @@ management.observations.annotations.enabled: true logging: level: root: WARN + io: + netty: + resolver: + dns: + # Hush warnings about native DNS on MacOS + DnsServerAddressStreamProviders: OFF org: springframework: ai: - autoconfigure: + model: chat: observation: - # Hush warnings about prompt and completion logging - ChatObservationAutoConfiguration: OFF + autoconfigure: + # Hush warnings about prompt and completion logging + ChatObservationAutoConfiguration: OFF