-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added IT for ChatMemory with prototype scope
- Loading branch information
1 parent
0c98393
commit 59d1fb9
Showing
5 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...vice/spring/mode/automatic/withChatMemoryPrototype/AiServiceWithChatMemoryPrototype1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
9 changes: 9 additions & 0 deletions
9
...vice/spring/mode/automatic/withChatMemoryPrototype/AiServiceWithChatMemoryPrototype2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
22 changes: 22 additions & 0 deletions
22
...g/mode/automatic/withChatMemoryPrototype/AiServiceWithChatMemoryPrototypeApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ng/mode/automatic/withChatMemoryPrototype/AiServiceWithChatMemoryPrototypeProviderIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
} | ||
} |