Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Migration Guide: Spring AI Google GenAI Autoconfiguration

## Overview

This guide helps you migrate from the old Vertex AI-based autoconfiguration to the new Google GenAI SDK-based autoconfiguration.

## Key Changes

### 1. Property Namespace Changes

Old properties:
```properties
spring.ai.vertex.ai.gemini.project-id=my-project
spring.ai.vertex.ai.gemini.location=us-central1
spring.ai.vertex.ai.gemini.chat.options.model=gemini-pro
spring.ai.vertex.ai.embedding.text.options.model=textembedding-gecko
```

New properties:
```properties
# For Vertex AI mode
spring.ai.google.genai.project-id=my-project
spring.ai.google.genai.location=us-central1
spring.ai.google.genai.chat.options.model=gemini-2.0-flash

# For Gemini Developer API mode (new!)
spring.ai.google.genai.api-key=your-api-key
spring.ai.google.genai.chat.options.model=gemini-2.0-flash

# Embedding properties
spring.ai.google.genai.embedding.project-id=my-project
spring.ai.google.genai.embedding.location=us-central1
spring.ai.google.genai.embedding.text.options.model=text-embedding-004
```

### 2. New Authentication Options

The new SDK supports both:
- **Vertex AI mode**: Using Google Cloud credentials (same as before)
- **Gemini Developer API mode**: Using API keys (new!)

### 3. Removed Features

- `transport` property is no longer needed
- Multimodal embedding autoconfiguration has been removed (pending support in new SDK)

### 4. Bean Name Changes

If you were autowiring beans by name:
- `vertexAi` → `googleGenAiClient`
- `vertexAiGeminiChat` → `googleGenAiChatModel`
- `textEmbedding` → `googleGenAiTextEmbedding`

### 5. Class Changes

If you were importing classes directly:
- `com.google.cloud.vertexai.VertexAI` → `com.google.genai.Client`
- `org.springframework.ai.vertexai.gemini.*` → `org.springframework.ai.google.genai.*`

## Migration Steps

1. Update your application properties:
- Replace `spring.ai.vertex.ai.*` with `spring.ai.google.genai.*`
- Remove any `transport` configuration

2. If using API key authentication:
- Set `spring.ai.google.genai.api-key` property
- Remove project-id and location for chat (not needed with API key)

3. Update any custom configurations or bean references

4. Test your application thoroughly

## Environment Variables
```bash
export GOOGLE_CLOUD_PROJECT=my-project
export GOOGLE_CLOUD_LOCATION=us-central1
```

New (additional option):
```bash
export GOOGLE_API_KEY=your-api-key
```

## Backward Compatibility

The old autoconfiguration module is still available but deprecated. We recommend migrating to the new module as soon as possible.
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-parent</artifactId>
<version>1.1.0-SNAPSHOT</version>
<relativePath>../../../pom.xml</relativePath>
</parent>
<artifactId>spring-ai-autoconfigure-model-google-genai</artifactId>
<packaging>jar</packaging>
<name>Spring AI Google GenAI Auto Configuration</name>
<description>Spring AI Google GenAI Auto Configuration</description>
<url>https://github.com/spring-projects/spring-ai</url>

<scm>
<url>https://github.com/spring-projects/spring-ai</url>
<connection>git://github.com/spring-projects/spring-ai.git</connection>
<developerConnection>[email protected]:spring-projects/spring-ai.git</developerConnection>
</scm>


<dependencies>

<!-- Spring AI dependencies -->

<!-- Google GenAI Embedding -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-google-genai-embedding</artifactId>
<version>${project.parent.version}</version>
<optional>true</optional>
</dependency>

<!-- Google GenAI -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-google-genai</artifactId>
<version>${project.parent.version}</version>
<optional>true</optional>
</dependency>

<!-- Spring AI auto configurations -->

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-autoconfigure-model-tool</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-autoconfigure-retry</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-autoconfigure-model-chat-observation</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-autoconfigure-model-embedding-observation</artifactId>
<version>${project.parent.version}</version>
</dependency>

<!-- Boot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-test</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>ollama</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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.model.google.genai.autoconfigure.chat;

import java.io.IOException;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.genai.Client;
import io.micrometer.observation.ObservationRegistry;

import org.springframework.ai.chat.observation.ChatModelObservationConvention;
import org.springframework.ai.google.genai.GoogleGenAiChatModel;
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.retry.autoconfigure.SpringAiRetryAutoConfiguration;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Auto-configuration for Google GenAI Chat.
*
* @author Christian Tzolov
* @author Soby Chacko
* @author Mark Pollack
* @author Ilayaperumal Gopinathan
* @since 1.1.0
*/
@AutoConfiguration(after = { SpringAiRetryAutoConfiguration.class, ToolCallingAutoConfiguration.class })
@ConditionalOnClass({ Client.class, GoogleGenAiChatModel.class })
@ConditionalOnProperty(name = SpringAIModelProperties.CHAT_MODEL, havingValue = SpringAIModels.GOOGLE_GEN_AI,
matchIfMissing = true)
@EnableConfigurationProperties({ GoogleGenAiChatProperties.class, GoogleGenAiConnectionProperties.class })
@ImportAutoConfiguration(classes = { SpringAiRetryAutoConfiguration.class, ToolCallingAutoConfiguration.class })
public class GoogleGenAiChatAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public Client googleGenAiClient(GoogleGenAiConnectionProperties connectionProperties) throws IOException {

Client.Builder clientBuilder = Client.builder();

if (StringUtils.hasText(connectionProperties.getApiKey())) {
// Gemini Developer API mode
clientBuilder.apiKey(connectionProperties.getApiKey());
}
else {
// Vertex AI mode
Assert.hasText(connectionProperties.getProjectId(), "Google GenAI project-id must be set!");
Assert.hasText(connectionProperties.getLocation(), "Google GenAI location must be set!");

clientBuilder.project(connectionProperties.getProjectId())
.location(connectionProperties.getLocation())
.vertexAI(true);

if (connectionProperties.getCredentialsUri() != null) {
GoogleCredentials credentials = GoogleCredentials
.fromStream(connectionProperties.getCredentialsUri().getInputStream());
// Note: The new SDK doesn't have a direct setCredentials method,
// credentials are handled automatically when vertexAI is true
}
}

return clientBuilder.build();
}

@Bean
@ConditionalOnMissingBean
public GoogleGenAiChatModel googleGenAiChatModel(Client googleGenAiClient, GoogleGenAiChatProperties chatProperties,
ToolCallingManager toolCallingManager, ApplicationContext context, RetryTemplate retryTemplate,
ObjectProvider<ObservationRegistry> observationRegistry,
ObjectProvider<ChatModelObservationConvention> observationConvention,
ObjectProvider<ToolExecutionEligibilityPredicate> toolExecutionEligibilityPredicate) {

GoogleGenAiChatModel chatModel = GoogleGenAiChatModel.builder()
.genAiClient(googleGenAiClient)
.defaultOptions(chatProperties.getOptions())
.toolCallingManager(toolCallingManager)
.toolExecutionEligibilityPredicate(
toolExecutionEligibilityPredicate.getIfUnique(() -> new DefaultToolExecutionEligibilityPredicate()))
.retryTemplate(retryTemplate)
.observationRegistry(observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP))
.build();

observationConvention.ifAvailable(chatModel::setObservationConvention);

return chatModel;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.model.google.genai.autoconfigure.chat;

import org.springframework.ai.google.genai.GoogleGenAiChatModel;
import org.springframework.ai.google.genai.GoogleGenAiChatOptions;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

/**
* Configuration properties for Google GenAI Chat.
*
* @author Christian Tzolov
* @author Hyunsang Han
* @since 1.1.0
*/
@ConfigurationProperties(GoogleGenAiChatProperties.CONFIG_PREFIX)
public class GoogleGenAiChatProperties {

public static final String CONFIG_PREFIX = "spring.ai.google.genai.chat";

public static final String DEFAULT_MODEL = GoogleGenAiChatModel.ChatModel.GEMINI_2_0_FLASH.getValue();

/**
* Google GenAI API generative options.
*/
@NestedConfigurationProperty
private GoogleGenAiChatOptions options = GoogleGenAiChatOptions.builder()
.temperature(0.7)
.candidateCount(1)
.model(DEFAULT_MODEL)
.build();

public GoogleGenAiChatOptions getOptions() {
return this.options;
}

public void setOptions(GoogleGenAiChatOptions options) {
this.options = options;
}

}
Loading
Loading