-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update chat memory example and separate ChatMemoryStore from ChatMemory usage #107
Closed
andreas-eberle
wants to merge
1
commit into
quarkiverse:main
from
andreas-eberle:feature/update-chat-memory-example
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without eviction or4 cleanup, this will lead to OOM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to see exactly how this is meant to work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, there is currently no eviction mechanism. However, for a simple example, that would be hard to do. The current idea of always evicting when the request ends makes a chat with API calls impossible.
I added a note to the documentation clarifying this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation should mention or have a more complex example with eviction.
I'm still unsure about the request scope part. I think, we would need to introduce a "conversation" concept soon.