Skip to content
Open
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,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.google.genai.autoconfigure.tts;

import org.springframework.ai.google.genai.tts.GeminiTtsModel;
import org.springframework.ai.google.genai.tts.api.GeminiTtsApi;
import org.springframework.ai.model.SpringAIModelProperties;
import org.springframework.ai.model.SpringAIModels;
import org.springframework.ai.retry.autoconfigure.SpringAiRetryAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfiguration;
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.annotation.Bean;
import org.springframework.core.retry.RetryTemplate;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Auto-configuration for Google GenAI Text-to-Speech.
*
* @author Alexandros Pappas
* @since 1.1.0
*/
@AutoConfiguration(after = SpringAiRetryAutoConfiguration.class)
@ConditionalOnClass({ GeminiTtsApi.class, GeminiTtsModel.class })
@ConditionalOnProperty(name = SpringAIModelProperties.AUDIO_SPEECH_MODEL, havingValue = SpringAIModels.GOOGLE_GEN_AI,
matchIfMissing = false)
@EnableConfigurationProperties({ GoogleGenAiTtsProperties.class, GoogleGenAiTtsConnectionProperties.class })
public class GoogleGenAiTtsAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public GeminiTtsApi geminiTtsApi(GoogleGenAiTtsConnectionProperties connectionProperties) {
Assert.hasText(connectionProperties.getApiKey(), "Google GenAI API key must be set!");

if (StringUtils.hasText(connectionProperties.getBaseUrl())) {
return new GeminiTtsApi(connectionProperties.getApiKey(), connectionProperties.getBaseUrl());
}

return new GeminiTtsApi(connectionProperties.getApiKey());
}

@Bean
@ConditionalOnMissingBean
public GeminiTtsModel geminiTtsModel(GeminiTtsApi geminiTtsApi, GoogleGenAiTtsProperties ttsProperties,
RetryTemplate retryTemplate) {

return GeminiTtsModel.builder()
.geminiTtsApi(geminiTtsApi)
.defaultOptions(ttsProperties.getOptions())
.retryTemplate(retryTemplate)
.build();
}

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

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Connection properties for Google GenAI TTS.
*
* @author Alexandros Pappas
* @since 1.1.0
*/
@ConfigurationProperties(GoogleGenAiTtsConnectionProperties.CONFIG_PREFIX)
public class GoogleGenAiTtsConnectionProperties {

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

/**
* Google GenAI API Key for TTS.
*/
private String apiKey;

/**
* Base URL for the Google GenAI TTS API.
*/
private String baseUrl = "https://generativelanguage.googleapis.com";

public String getApiKey() {
return this.apiKey;
}

public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}

public String getBaseUrl() {
return this.baseUrl;
}

public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}

}
Original file line number Diff line number Diff line change
@@ -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.google.genai.autoconfigure.tts;

import org.springframework.ai.google.genai.tts.GeminiTtsOptions;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

/**
* Configuration properties for Google GenAI Text-to-Speech.
*
* @author Alexandros Pappas
* @since 1.1.0
*/
@ConfigurationProperties(GoogleGenAiTtsProperties.CONFIG_PREFIX)
public class GoogleGenAiTtsProperties {

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

public static final String DEFAULT_MODEL = "gemini-2.5-flash-preview-tts";

public static final String DEFAULT_VOICE = "Kore";

/**
* Google GenAI TTS options.
*/
@NestedConfigurationProperty
private final GeminiTtsOptions options = GeminiTtsOptions.builder()
.model(DEFAULT_MODEL)
.voice(DEFAULT_VOICE)
.build();

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
org.springframework.ai.model.google.genai.autoconfigure.chat.GoogleGenAiChatAutoConfiguration
org.springframework.ai.model.google.genai.autoconfigure.embedding.GoogleGenAiEmbeddingConnectionAutoConfiguration
org.springframework.ai.model.google.genai.autoconfigure.embedding.GoogleGenAiTextEmbeddingAutoConfiguration
org.springframework.ai.model.google.genai.autoconfigure.tts.GoogleGenAiTtsAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.google.genai.autoconfigure.tts;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;

import org.springframework.ai.google.genai.tts.GeminiTtsModel;
import org.springframework.ai.utils.SpringAiTestAutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Integration tests for the {@link GoogleGenAiTtsAutoConfiguration}.
*
* @author Alexandros Pappas
*/
@EnabledIfEnvironmentVariable(named = "GOOGLE_API_KEY", matches = ".*")
public class GoogleGenAiTtsAutoConfigurationIT {

private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
.getLog(GoogleGenAiTtsAutoConfigurationIT.class);

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("spring.ai.google.genai.api-key=" + System.getenv("GOOGLE_API_KEY"),
"spring.ai.model.audio.speech=google-genai")
.withConfiguration(SpringAiTestAutoConfigurations.of(GoogleGenAiTtsAutoConfiguration.class));

@Test
void ttsModelBeanCreation() {
this.contextRunner.run(context -> {
assertThat(context).hasSingleBean(GeminiTtsModel.class);
GeminiTtsModel ttsModel = context.getBean(GeminiTtsModel.class);
assertThat(ttsModel).isNotNull();
});
}

@Test
void ttsModelApiCall() {
this.contextRunner.run(context -> {
GeminiTtsModel ttsModel = context.getBean(GeminiTtsModel.class);
byte[] response = ttsModel.call("Hello");

assertThat(response).isNotNull();
assertThat(response).isNotEmpty();
// PCM audio should be substantial (24kHz * 2 bytes/sample * ~1 second)
assertThat(response.length).isGreaterThan(1000);

logger.debug("PCM audio response size: " + response.length + " bytes");
});
}

@Test
void ttsModelWithCustomVoice() {
this.contextRunner
.withPropertyValues("spring.ai.google.genai.tts.options.voice=Puck",
"spring.ai.google.genai.tts.options.model=gemini-2.5-flash-preview-tts")
.run(context -> {
GeminiTtsModel ttsModel = context.getBean(GeminiTtsModel.class);
byte[] response = ttsModel.call("Testing custom voice");

assertThat(response).isNotNull();
assertThat(response).isNotEmpty();
assertThat(response.length).isGreaterThan(1000);
});
}

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

import org.junit.jupiter.api.Test;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Configuration;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Unit tests for Google GenAI TTS properties binding.
*
* @author Alexandros Pappas
*/
public class GoogleGenAiTtsPropertiesTests {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withUserConfiguration(PropertiesTestConfiguration.class);

@Test
void connectionPropertiesBinding() {
this.contextRunner
.withPropertyValues("spring.ai.google.genai.api-key=test-tts-key",
"spring.ai.google.genai.base-url=https://test.api.google.com")
.run(context -> {
GoogleGenAiTtsConnectionProperties connectionProperties = context
.getBean(GoogleGenAiTtsConnectionProperties.class);
assertThat(connectionProperties.getApiKey()).isEqualTo("test-tts-key");
assertThat(connectionProperties.getBaseUrl()).isEqualTo("https://test.api.google.com");
});
}

@Test
void ttsPropertiesBinding() {
this.contextRunner
.withPropertyValues("spring.ai.google.genai.tts.options.model=gemini-2.5-pro-preview-tts",
"spring.ai.google.genai.tts.options.voice=Puck", "spring.ai.google.genai.tts.options.speed=1.2")
.run(context -> {
GoogleGenAiTtsProperties ttsProperties = context.getBean(GoogleGenAiTtsProperties.class);
assertThat(ttsProperties.getOptions().getModel()).isEqualTo("gemini-2.5-pro-preview-tts");
assertThat(ttsProperties.getOptions().getVoice()).isEqualTo("Puck");
assertThat(ttsProperties.getOptions().getSpeed()).isEqualTo(1.2);
});
}

@Test
void ttsDefaultValuesBinding() {
// Test that defaults are applied when not specified
this.contextRunner.run(context -> {
GoogleGenAiTtsProperties ttsProperties = context.getBean(GoogleGenAiTtsProperties.class);
assertThat(ttsProperties.getOptions().getModel()).isEqualTo("gemini-2.5-flash-preview-tts");
assertThat(ttsProperties.getOptions().getVoice()).isEqualTo("Kore");
assertThat(ttsProperties.getOptions().getFormat()).isEqualTo("pcm");
});
}

@Test
void connectionDefaultBaseUrl() {
this.contextRunner.withPropertyValues("spring.ai.google.genai.api-key=test-key").run(context -> {
GoogleGenAiTtsConnectionProperties connectionProperties = context
.getBean(GoogleGenAiTtsConnectionProperties.class);
assertThat(connectionProperties.getBaseUrl()).isEqualTo("https://generativelanguage.googleapis.com");
});
}

@Configuration
@EnableConfigurationProperties({ GoogleGenAiTtsConnectionProperties.class, GoogleGenAiTtsProperties.class })
static class PropertiesTestConfiguration {

}

}
Loading