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,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.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-chat-memory-repository-cosmos-db</artifactId>
<name>Spring AI Auto Configuration - Chat Memory Repository - CosmosDB</name>
<description>Spring AI Auto Configuration for CosmosDB Chat Memory Repository</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>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-model-chat-memory-repository-cosmos-db</artifactId>
<version>${project.version}</version>
</dependency>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</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>

<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-spring-data-cosmos</artifactId>
<version>${azure-cosmos.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>${azure-identity.version}</version>
</dependency>

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

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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.model.chat.memory.repository.cosmosdb.autoconfigure;

import com.azure.cosmos.CosmosAsyncClient;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.identity.DefaultAzureCredentialBuilder;

import org.springframework.ai.chat.memory.repository.cosmosdb.CosmosDBChatMemoryRepository;
import org.springframework.ai.chat.memory.repository.cosmosdb.CosmosDBChatMemoryRepositoryConfig;
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;

/**
* {@link AutoConfiguration Auto-configuration} for {@link CosmosDBChatMemoryRepository}.
*
* @author Theo van Kraay
* @since 1.0.0
*/
@AutoConfiguration
@ConditionalOnClass({ CosmosDBChatMemoryRepository.class, CosmosAsyncClient.class })
@EnableConfigurationProperties(CosmosDBChatMemoryRepositoryProperties.class)
public class CosmosDBChatMemoryRepositoryAutoConfiguration {

private final String agentSuffix = "SpringAI-CDBNoSQL-ChatMemoryRepository";

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.ai.chat.memory.repository.cosmosdb", name = "endpoint")
public CosmosAsyncClient cosmosClient(CosmosDBChatMemoryRepositoryProperties properties) {
if (properties.getEndpoint() == null || properties.getEndpoint().isEmpty()) {
throw new IllegalArgumentException(
"Cosmos DB endpoint must be provided via spring.ai.chat.memory.repository.cosmosdb.endpoint property");
}

String mode = properties.getConnectionMode();
if (mode == null) {
properties.setConnectionMode("gateway");
}
else if (!mode.equals("direct") && !mode.equals("gateway")) {
throw new IllegalArgumentException("Connection mode must be either 'direct' or 'gateway'");
}

CosmosClientBuilder builder = new CosmosClientBuilder().endpoint(properties.getEndpoint())
.userAgentSuffix(this.agentSuffix);

if (properties.getKey() == null || properties.getKey().isEmpty()) {
builder.credential(new DefaultAzureCredentialBuilder().build());
}
else {
builder.key(properties.getKey());
}

return ("direct".equals(properties.getConnectionMode()) ? builder.directMode() : builder.gatewayMode())
.buildAsyncClient();
}

@Bean
@ConditionalOnMissingBean
public CosmosDBChatMemoryRepositoryConfig cosmosDBChatMemoryRepositoryConfig(
CosmosDBChatMemoryRepositoryProperties properties, CosmosAsyncClient cosmosAsyncClient) {

return CosmosDBChatMemoryRepositoryConfig.builder()
.withCosmosClient(cosmosAsyncClient)
.withDatabaseName(properties.getDatabaseName())
.withContainerName(properties.getContainerName())
.withPartitionKeyPath(properties.getPartitionKeyPath())
.build();
}

@Bean
@ConditionalOnMissingBean
public CosmosDBChatMemoryRepository cosmosDBChatMemoryRepository(CosmosDBChatMemoryRepositoryConfig config) {
return CosmosDBChatMemoryRepository.create(config);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.chat.memory.repository.cosmosdb.autoconfigure;

import org.springframework.ai.chat.memory.repository.cosmosdb.CosmosDBChatMemoryRepositoryConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* Configuration properties for CosmosDB chat memory.
*
* @author Theo van Kraay
* @since 1.0.0
*/
@ConfigurationProperties(CosmosDBChatMemoryRepositoryProperties.CONFIG_PREFIX)
public class CosmosDBChatMemoryRepositoryProperties {

public static final String CONFIG_PREFIX = "spring.ai.chat.memory.repository.cosmosdb";

private String endpoint;

private String key;

private String connectionMode = "gateway";

private String databaseName = CosmosDBChatMemoryRepositoryConfig.DEFAULT_DATABASE_NAME;

private String containerName = CosmosDBChatMemoryRepositoryConfig.DEFAULT_CONTAINER_NAME;

private String partitionKeyPath = CosmosDBChatMemoryRepositoryConfig.DEFAULT_PARTITION_KEY_PATH;

public String getEndpoint() {
return this.endpoint;
}

public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}

public String getKey() {
return this.key;
}

public void setKey(String key) {
this.key = key;
}

public String getConnectionMode() {
return this.connectionMode;
}

public void setConnectionMode(String connectionMode) {
this.connectionMode = connectionMode;
}

public String getDatabaseName() {
return this.databaseName;
}

public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}

public String getContainerName() {
return this.containerName;
}

public void setContainerName(String containerName) {
this.containerName = containerName;
}

public String getPartitionKeyPath() {
return this.partitionKeyPath;
}

public void setPartitionKeyPath(String partitionKeyPath) {
this.partitionKeyPath = partitionKeyPath;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.ai.model.chat.memory.repository.cosmosdb.autoconfigure.CosmosDBChatMemoryRepositoryAutoConfiguration
Loading