-
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 6be3de2
Showing
5 changed files
with
61 additions
and
51 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.
25 changes: 25 additions & 0 deletions
25
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,25 @@ | ||
package io.quarkiverse.langchain4j.samples; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
|
||
import dev.langchain4j.memory.ChatMemory; | ||
import dev.langchain4j.memory.chat.ChatMemoryProvider; | ||
import dev.langchain4j.memory.chat.MessageWindowChatMemory; | ||
import dev.langchain4j.store.memory.chat.ChatMemoryStore; | ||
|
||
@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(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
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,14 @@ | ||
package io.quarkiverse.langchain4j.samples; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import dev.langchain4j.store.memory.chat.ChatMemoryStore; | ||
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore; | ||
|
||
public class ChatMemoryStoreProducer { | ||
|
||
@ApplicationScoped | ||
ChatMemoryStore produceChatMemoryStore() { | ||
return new InMemoryChatMemoryStore(); | ||
} | ||
} |
32 changes: 12 additions & 20 deletions
32
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,24 @@ | ||
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 jakarta.inject.Inject; | ||
|
||
import dev.langchain4j.memory.chat.ChatMemoryProvider; | ||
import dev.langchain4j.memory.chat.MessageWindowChatMemory; | ||
import io.quarkiverse.langchain4j.RemovableChatMemoryProvider; | ||
import dev.langchain4j.store.memory.chat.ChatMemoryStore; | ||
|
||
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