-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update chat memory example and separate ChatMemoryStore from ChatMemo…
…ry usage
- Loading branch information
1 parent
685203f
commit c7c4821
Showing
5 changed files
with
60 additions
and
53 deletions.
There are no files selected for viewing
29 changes: 0 additions & 29 deletions
29
docs/modules/ROOT/examples/io/quarkiverse/langchain4j/samples/ChatMemoryBean.java
This file was deleted.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
docs/modules/ROOT/examples/io/quarkiverse/langchain4j/samples/ChatMemoryProviderBean.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,24 @@ | ||
package io.quarkiverse.langchain4j.samples; | ||
|
||
import dev.langchain4j.memory.ChatMemory; | ||
import dev.langchain4j.memory.chat.ChatMemoryProvider; | ||
import dev.langchain4j.memory.chat.MessageWindowChatMemory; | ||
import dev.langchain4j.store.memory.chat.ChatMemoryStore; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
|
||
@ApplicationScoped | ||
public class ChatMemoryProviderBean implements ChatMemoryProvider { | ||
|
||
@Inject | ||
ChatMemoryStore store; | ||
|
||
@Override | ||
public ChatMemory get(Object memoryId) { | ||
return MessageWindowChatMemory.builder() | ||
.id(memoryId) | ||
.maxMessages(20) | ||
.chatMemoryStore(store) | ||
.build(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
docs/modules/ROOT/examples/io/quarkiverse/langchain4j/samples/ChatMemoryStoreProducer.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,13 @@ | ||
package io.quarkiverse.langchain4j.samples; | ||
|
||
import dev.langchain4j.store.memory.chat.ChatMemoryStore; | ||
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
public class ChatMemoryStoreProducer { | ||
|
||
@ApplicationScoped | ||
ChatMemoryStore produceChatMemoryStore() { | ||
return new InMemoryChatMemoryStore(); | ||
} | ||
} |
35 changes: 13 additions & 22 deletions
35
docs/modules/ROOT/examples/io/quarkiverse/langchain4j/samples/MySmallMemoryProvider.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 |
---|---|---|
@@ -1,32 +1,23 @@ | ||
package io.quarkiverse.langchain4j.samples; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Supplier; | ||
|
||
import dev.langchain4j.memory.ChatMemory; | ||
import dev.langchain4j.memory.chat.ChatMemoryProvider; | ||
import dev.langchain4j.memory.chat.MessageWindowChatMemory; | ||
import io.quarkiverse.langchain4j.RemovableChatMemoryProvider; | ||
import dev.langchain4j.store.memory.chat.ChatMemoryStore; | ||
import jakarta.inject.Inject; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public class MySmallMemoryProvider implements Supplier<ChatMemoryProvider> { | ||
@Override | ||
public ChatMemoryProvider get() { | ||
return new RemovableChatMemoryProvider() { | ||
private final Map<Object, ChatMemory> memories = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public ChatMemory get(Object memoryId) { | ||
return memories.computeIfAbsent(memoryId, id -> MessageWindowChatMemory.builder() | ||
.maxMessages(20) | ||
.id(memoryId) | ||
.build()); | ||
} | ||
@Inject | ||
ChatMemoryStore store; | ||
|
||
@Override | ||
public void remove(Object id) { | ||
memories.remove(id); | ||
} | ||
}; | ||
@Override | ||
public ChatMemoryProvider get() { | ||
return memoryId -> MessageWindowChatMemory.builder() | ||
.id(memoryId) | ||
.maxMessages(20) | ||
.chatMemoryStore(store) | ||
.build(); | ||
} | ||
} |
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