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,74 @@
<?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>2.0.0-SNAPSHOT</version>
<relativePath>../../../pom.xml</relativePath>
</parent>
<artifactId>spring-ai-autoconfigure-model-openai-batch-repository-jdbc</artifactId>
<packaging>jar</packaging>
<name>Spring AI JDBC OpenAI Batch Execution Repository Auto Configuration</name>
<description>Spring AI JDBC OpenAI Batch Execution Repository Auto Configuration</description>
<url>https://github.com/spring-projects/spring-ai</url>

<scm>
<url>https://github.com/spring-projects/spring-ai</url>
<connection>scm:git:git://github.com/spring-projects/spring-ai.git</connection>
<developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-ai.git</developerConnection>
</scm>

<dependencies>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-batch-repository-jdbc</artifactId>
<version>${project.parent.version}</version>
</dependency>

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

<!-- Boot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</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.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2023-present 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.openai.batch.repository.jdbc.autoconfigure;

import javax.sql.DataSource;

import org.springframework.ai.model.openai.autoconfigure.OpenAiBatchAutoConfiguration;
import org.springframework.ai.openai.batch.BatchExecutionRepository;
import org.springframework.ai.openai.batch.repository.jdbc.JdbcBatchExecutionRepository;
import org.springframework.ai.openai.batch.repository.jdbc.JdbcBatchExecutionRepositoryDialect;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.sql.autoconfigure.init.OnDatabaseInitializationCondition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.jdbc.core.JdbcTemplate;

/**
* Auto-configuration for JDBC-based {@link BatchExecutionRepository}.
*
* @author Yasin Akbas
* @since 2.0.0
*/
@AutoConfiguration(before = OpenAiBatchAutoConfiguration.class)
@ConditionalOnClass({ JdbcBatchExecutionRepository.class, DataSource.class, JdbcTemplate.class })
@EnableConfigurationProperties(JdbcBatchExecutionRepositoryProperties.class)
public class JdbcBatchExecutionRepositoryAutoConfiguration {

@Bean
@ConditionalOnMissingBean(BatchExecutionRepository.class)
JdbcBatchExecutionRepository jdbcBatchExecutionRepository(JdbcTemplate jdbcTemplate, DataSource dataSource) {
JdbcBatchExecutionRepositoryDialect dialect = JdbcBatchExecutionRepositoryDialect.from(dataSource);
return JdbcBatchExecutionRepository.builder().jdbcTemplate(jdbcTemplate).dialect(dialect).build();
}

@Bean
@ConditionalOnMissingBean
@Conditional(OnJdbcBatchExecutionRepositoryDatasourceInitializationCondition.class)
JdbcBatchExecutionRepositorySchemaInitializer jdbcBatchExecutionScriptDatabaseInitializer(DataSource dataSource,
JdbcBatchExecutionRepositoryProperties properties) {
return new JdbcBatchExecutionRepositorySchemaInitializer(dataSource, properties);
}

static class OnJdbcBatchExecutionRepositoryDatasourceInitializationCondition
extends OnDatabaseInitializationCondition {

OnJdbcBatchExecutionRepositoryDatasourceInitializationCondition() {
super("Jdbc Batch Execution Repository",
JdbcBatchExecutionRepositoryProperties.CONFIG_PREFIX + ".initialize-schema");
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2023-present 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.openai.batch.repository.jdbc.autoconfigure;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.init.DatabaseInitializationProperties;

/**
* Configuration properties for JDBC Batch Execution Repository.
*
* @author Yasin Akbas
* @since 2.0.0
*/
@ConfigurationProperties(JdbcBatchExecutionRepositoryProperties.CONFIG_PREFIX)
public class JdbcBatchExecutionRepositoryProperties extends DatabaseInitializationProperties {

public static final String CONFIG_PREFIX = "spring.ai.openai.batch.repository.jdbc";

private static final String DEFAULT_SCHEMA_LOCATION = "classpath:org/springframework/ai/openai/batch/repository/jdbc/schema-@@platform@@.sql";

@Override
public String getDefaultSchemaLocation() {
return DEFAULT_SCHEMA_LOCATION;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2023-present 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.openai.batch.repository.jdbc.autoconfigure;

import javax.sql.DataSource;

import org.springframework.boot.jdbc.init.PropertiesBasedDataSourceScriptDatabaseInitializer;

/**
* Performs database initialization for the JDBC Batch Execution Repository.
*
* @author Yasin Akbas
* @since 2.0.0
*/
class JdbcBatchExecutionRepositorySchemaInitializer
extends PropertiesBasedDataSourceScriptDatabaseInitializer<JdbcBatchExecutionRepositoryProperties> {

JdbcBatchExecutionRepositorySchemaInitializer(DataSource dataSource,
JdbcBatchExecutionRepositoryProperties properties) {
super(dataSource, properties);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2023-present 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.
*/

@NullMarked
package org.springframework.ai.model.openai.batch.repository.jdbc.autoconfigure;

import org.jspecify.annotations.NullMarked;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright 2023-present 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.openai.batch.repository.jdbc.autoconfigure.JdbcBatchExecutionRepositoryAutoConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2023-present 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.openai.autoconfigure;

import java.util.List;

import com.openai.client.OpenAIClient;

import org.springframework.ai.openai.AbstractOpenAiOptions;
import org.springframework.ai.openai.batch.BatchExecutionRepository;
import org.springframework.ai.openai.batch.BatchRequestHandler;
import org.springframework.ai.openai.batch.InMemoryBatchExecutionRepository;
import org.springframework.ai.openai.batch.OpenAiBatchApi;
import org.springframework.ai.openai.batch.OpenAiBatchListener;
import org.springframework.ai.openai.batch.OpenAiBatchModel;
import org.springframework.ai.openai.setup.OpenAiSetup;
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;

/**
* Batch API {@link AutoConfiguration Auto-configuration} for OpenAI SDK.
* <p>
* Enabled only when {@code spring.ai.openai.batch.enabled=true} is set. Wires together
* the {@link OpenAiBatchApi}, {@link OpenAiBatchModel}, registered
* {@link BatchRequestHandler}s, and {@link OpenAiBatchListener}s.
*
* @author Yasin Akbas
* @since 2.0.0
*/
@AutoConfiguration
@EnableConfigurationProperties({ OpenAiConnectionProperties.class, OpenAiBatchProperties.class })
@ConditionalOnProperty(name = "spring.ai.openai.batch.enabled", havingValue = "true")
public class OpenAiBatchAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public OpenAiBatchApi openAiBatchApi(OpenAiConnectionProperties commonProperties,
OpenAiBatchProperties batchProperties) {
OpenAiAutoConfigurationUtil.ResolvedConnectionProperties resolved = OpenAiAutoConfigurationUtil
.resolveConnectionProperties(commonProperties, batchProperties);

OpenAIClient openAIClient = this.openAiClient(resolved);

return new OpenAiBatchApi(openAIClient, new com.fasterxml.jackson.databind.ObjectMapper());
}

@Bean
@ConditionalOnMissingBean
public BatchExecutionRepository batchExecutionRepository() {
return new InMemoryBatchExecutionRepository();
}

@Bean
@ConditionalOnMissingBean
public OpenAiBatchModel openAiBatchModel(OpenAiBatchProperties batchProperties, OpenAiBatchApi batchApi,
BatchExecutionRepository executionRepository, ObjectProvider<List<BatchRequestHandler<?>>> handlers,
ObjectProvider<List<OpenAiBatchListener>> listeners) {
return OpenAiBatchModel.builder()
.batchApi(batchApi)
.options(batchProperties.getOptions())
.executionRepository(executionRepository)
.handlers(handlers.getIfAvailable(List::of))
.listeners(listeners.getIfAvailable(List::of))
.build();
}

private OpenAIClient openAiClient(AbstractOpenAiOptions resolved) {
return OpenAiSetup.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());
}

}
Loading