这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
Expand Up @@ -38,12 +38,12 @@ public static int toIntByMD5(String value) {
}

public static int toIntByLookup(String value) {
var found = lookupMap.get(value);
Integer found = lookupMap.get(value);
if (found != null) {
return found;
}

var intValue = counter.incrementAndGet();
Integer intValue = counter.incrementAndGet();
lookupMap.put(value, intValue);
return intValue;
}
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@
<module>spring-ai</module>
<module>spring-ai-2</module>
<module>spring-ai-3</module>
<module>spring-ai-4</module>
<module>spring-ai-modules</module>
<module>spring-aop</module>
<module>spring-aop-2</module>
Expand Down Expand Up @@ -1196,6 +1197,7 @@
<module>spring-ai</module>
<module>spring-ai-2</module>
<module>spring-ai-3</module>
<module>spring-ai-4</module>
<module>spring-ai-modules</module>
<module>spring-aop</module>
<module>spring-aop-2</module>
Expand Down
119 changes: 119 additions & 0 deletions spring-ai-4/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>spring-ai-4</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>spring-ai-4</name>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-3</relativePath>
</parent>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-model-chat-memory-repository-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>

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

<profiles>
<profile>
<id>chat-memory</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.boot.mainclass>com.baeldung.springai.memory.Application</spring.boot.mainclass>
</properties>
</profile>
</profiles>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${spring.boot.mainclass}</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>21</release>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<junit-jupiter.version>5.9.0</junit-jupiter.version>
<spring-boot.version>3.5.0</spring-boot.version>
<spring-ai.version>1.0.0</spring-ai.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.springai.memory;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setAdditionalProfiles("memory");
app.run(args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.baeldung.springai.memory;

import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.repository.jdbc.HsqldbChatMemoryRepositoryDialect;
import org.springframework.ai.chat.memory.repository.jdbc.JdbcChatMemoryRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class ChatConfig {

@Bean
public ChatMemoryRepository getChatMemoryRepository(JdbcTemplate jdbcTemplate) {
return JdbcChatMemoryRepository.builder()
.jdbcTemplate(jdbcTemplate)
.dialect(new HsqldbChatMemoryRepositoryDialect())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.baeldung.springai.memory;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
public class ChatController {

private final ChatService chatService;

public ChatController(ChatService chatService) {
this.chatService = chatService;
}

@PostMapping("/chat")
public ResponseEntity<String> chat(@RequestBody @Valid ChatRequest request) {
String response = chatService.chat(request.getPrompt());
return ResponseEntity.ok(response);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.springai.memory;

import javax.validation.constraints.NotNull;

public class ChatRequest {

@NotNull
private String prompt;

public String getPrompt() {
return prompt;
}

public void setPrompt(String prompt) {
this.prompt = prompt;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.baeldung.springai.memory;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.SessionScope;

import java.util.UUID;

@Component
@SessionScope
public class ChatService {

private final ChatClient chatClient;
private final String conversationId;

public ChatService(ChatModel chatModel, ChatMemory chatMemory) {
this.chatClient = ChatClient.builder(chatModel)
.defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())
.build();
this.conversationId = UUID.randomUUID().toString();
}

public String getConversationId() {
return conversationId;
}

public String chat(String prompt) {
return chatClient.prompt()
.user(userMessage -> userMessage.text(prompt))
.advisors(a -> a.param(ChatMemory.CONVERSATION_ID, conversationId))
.call()
.content();
}

}
15 changes: 15 additions & 0 deletions spring-ai-4/src/main/resources/application-memory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
spring:
ai:
openai:
api-key: "<YOUR-API-KEY>"

datasource:
url: jdbc:hsqldb:mem:chatdb
driver-class-name: org.hsqldb.jdbc.JDBCDriver
username: sa
password:

sql:
init:
mode: always
schema-locations: classpath:org/springframework/ai/chat/memory/repository/jdbc/schema-hsqldb.sql
15 changes: 15 additions & 0 deletions spring-ai-4/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] [%p] [%c{1}] - %m%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>

<logger name="org.springframework" level="INFO" additivity="false">
<appender-ref ref="CONSOLE" />
</logger>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.baeldung.springai.memory;

import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

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

@SpringBootTest
@ActiveProfiles("memory")
class ChatServiceLiveTest {

private static final String PROMPT_1ST = "Tell me a joke";
private static final String PROMPT_2ND = "Tell me another one";

@Autowired
private ChatMemory chatMemory;

@Autowired
private ChatService chatService;

@Test
void whenChatServiceIsCalledTwice_thenChatMemoryHasCorrectNumberOfEntries() {
String conversationId = chatService.getConversationId();

// 1st request
String response1 = chatService.chat(PROMPT_1ST);
assertThat(response1).isNotEmpty();
assertThat(chatMemory.get(conversationId)).hasSize(2);

// 2nd request
String response2 = chatService.chat(PROMPT_2ND);
assertThat(response2).isNotEmpty();
assertThat(chatMemory.get(conversationId)).hasSize(4);
}

}