Skip to content

Commit

Permalink
added IT for ChatMemory with prototype scope
Browse files Browse the repository at this point in the history
  • Loading branch information
dliubarskyi committed Feb 11, 2025
1 parent 0c98393 commit 59d1fb9
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static dev.langchain4j.service.spring.mode.ApiKeys.OPENAI_API_KEY;
import static org.assertj.core.api.Assertions.assertThat;

class AiServiceWithChatMemoryProviderIT {
class AiServiceWithChatMemoryIT {

ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(AiServicesAutoConfig.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.langchain4j.service.spring.mode.automatic.withChatMemoryPrototype;

import dev.langchain4j.service.spring.AiService;

@AiService
interface AiServiceWithChatMemoryPrototype1 {

String chat(String userMessage);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.langchain4j.service.spring.mode.automatic.withChatMemoryPrototype;

import dev.langchain4j.service.spring.AiService;

@AiService
interface AiServiceWithChatMemoryPrototype2 {

String chat(String userMessage);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.langchain4j.service.spring.mode.automatic.withChatMemoryPrototype;

import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;

@SpringBootApplication
class AiServiceWithChatMemoryPrototypeApplication {

@Bean
@Scope("prototype")
ChatMemory chatMemory() {
return MessageWindowChatMemory.withMaxMessages(10);
}

public static void main(String[] args) {
SpringApplication.run(AiServiceWithChatMemoryPrototypeApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dev.langchain4j.service.spring.mode.automatic.withChatMemoryPrototype;

import dev.langchain4j.service.spring.AiServicesAutoConfig;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import static dev.langchain4j.service.spring.mode.ApiKeys.OPENAI_API_KEY;
import static org.assertj.core.api.Assertions.assertThat;

class AiServiceWithChatMemoryPrototypeProviderIT {

ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(AiServicesAutoConfig.class));

@Test
void should_create_AI_services_with_separate_chat_memories() {
contextRunner
.withPropertyValues(
"langchain4j.open-ai.chat-model.api-key=" + OPENAI_API_KEY,
"langchain4j.open-ai.chat-model.model-name=gpt-4o-mini",
"langchain4j.open-ai.chat-model.temperature=0.0",
"langchain4j.open-ai.chat-model.max-tokens=20"
)
.withUserConfiguration(AiServiceWithChatMemoryPrototypeApplication.class)
.run(context -> {

// given
AiServiceWithChatMemoryPrototype1 aiService1 = context.getBean(AiServiceWithChatMemoryPrototype1.class);
aiService1.chat("My name is Klaus");
// when
String answer = aiService1.chat("What is my name?");
// then
assertThat(answer).containsIgnoringCase("Klaus");


// given
AiServiceWithChatMemoryPrototype2 aiService2 = context.getBean(AiServiceWithChatMemoryPrototype2.class);
// when
String answer2 = aiService2.chat("What is my name?");
// then
assertThat(answer2).doesNotContainIgnoringCase("Klaus");
});
}
}

0 comments on commit 59d1fb9

Please # to comment.