Skip to content

Commit 59d1fb9

Browse files
committed
added IT for ChatMemory with prototype scope
1 parent 0c98393 commit 59d1fb9

File tree

5 files changed

+86
-1
lines changed

5 files changed

+86
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import static dev.langchain4j.service.spring.mode.ApiKeys.OPENAI_API_KEY;
99
import static org.assertj.core.api.Assertions.assertThat;
1010

11-
class AiServiceWithChatMemoryProviderIT {
11+
class AiServiceWithChatMemoryIT {
1212

1313
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
1414
.withConfiguration(AutoConfigurations.of(AiServicesAutoConfig.class));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dev.langchain4j.service.spring.mode.automatic.withChatMemoryPrototype;
2+
3+
import dev.langchain4j.service.spring.AiService;
4+
5+
@AiService
6+
interface AiServiceWithChatMemoryPrototype1 {
7+
8+
String chat(String userMessage);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dev.langchain4j.service.spring.mode.automatic.withChatMemoryPrototype;
2+
3+
import dev.langchain4j.service.spring.AiService;
4+
5+
@AiService
6+
interface AiServiceWithChatMemoryPrototype2 {
7+
8+
String chat(String userMessage);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package dev.langchain4j.service.spring.mode.automatic.withChatMemoryPrototype;
2+
3+
import dev.langchain4j.memory.ChatMemory;
4+
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Scope;
9+
10+
@SpringBootApplication
11+
class AiServiceWithChatMemoryPrototypeApplication {
12+
13+
@Bean
14+
@Scope("prototype")
15+
ChatMemory chatMemory() {
16+
return MessageWindowChatMemory.withMaxMessages(10);
17+
}
18+
19+
public static void main(String[] args) {
20+
SpringApplication.run(AiServiceWithChatMemoryPrototypeApplication.class, args);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package dev.langchain4j.service.spring.mode.automatic.withChatMemoryPrototype;
2+
3+
import dev.langchain4j.service.spring.AiServicesAutoConfig;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.boot.autoconfigure.AutoConfigurations;
6+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
7+
8+
import static dev.langchain4j.service.spring.mode.ApiKeys.OPENAI_API_KEY;
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
class AiServiceWithChatMemoryPrototypeProviderIT {
12+
13+
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
14+
.withConfiguration(AutoConfigurations.of(AiServicesAutoConfig.class));
15+
16+
@Test
17+
void should_create_AI_services_with_separate_chat_memories() {
18+
contextRunner
19+
.withPropertyValues(
20+
"langchain4j.open-ai.chat-model.api-key=" + OPENAI_API_KEY,
21+
"langchain4j.open-ai.chat-model.model-name=gpt-4o-mini",
22+
"langchain4j.open-ai.chat-model.temperature=0.0",
23+
"langchain4j.open-ai.chat-model.max-tokens=20"
24+
)
25+
.withUserConfiguration(AiServiceWithChatMemoryPrototypeApplication.class)
26+
.run(context -> {
27+
28+
// given
29+
AiServiceWithChatMemoryPrototype1 aiService1 = context.getBean(AiServiceWithChatMemoryPrototype1.class);
30+
aiService1.chat("My name is Klaus");
31+
// when
32+
String answer = aiService1.chat("What is my name?");
33+
// then
34+
assertThat(answer).containsIgnoringCase("Klaus");
35+
36+
37+
// given
38+
AiServiceWithChatMemoryPrototype2 aiService2 = context.getBean(AiServiceWithChatMemoryPrototype2.class);
39+
// when
40+
String answer2 = aiService2.chat("What is my name?");
41+
// then
42+
assertThat(answer2).doesNotContainIgnoringCase("Klaus");
43+
});
44+
}
45+
}

0 commit comments

Comments
 (0)