Skip to content
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

Overhaul how chat memory is configured #112

Merged
merged 1 commit into from
Dec 10, 2023
Merged

Overhaul how chat memory is configured #112

merged 1 commit into from
Dec 10, 2023

Conversation

geoand
Copy link
Collaborator

@geoand geoand commented Dec 8, 2023

The basic ideas are:

  • A default ChatMemoryStore bean using in memory storage is now provided
    • This can be overridden by users simply by providing their own bean
  • A default ChatMemoryProvider bean is now provided
    • It uses the ChatMemoryStore bean under the hood
    • It also leverages configuration for use either MessageWindowChatMemory or TokenWindowChatMemory

ChatMemory objects continue to be removed when the AI service class goes out of scope, however now there is also now a programmatic way of removing ChatMemory by using ChatMemoryRemover. This is necessary for when the AI service is not request scoped.

* @param aiService The bean that implements the AI Service annotated with {@link RegisterAiService}
* @param memoryId The object used as memory IDs for which the corresponding {@link ChatMemory} should be removed
*/
public static void remove(Object aiService, Object memoryId) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like using two objects here as users could easily swap them, but I don't have a better way of handling it...

@maxandersen
Copy link
Member

context (no pun intended) For #47 :)

* It uses whatever bean `dev.langchain4j.store.memory.chat.ChatMemoryStore` bean is configured, as the backing store. The default implementation is `dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore`
** If the application provides its own `ChatMemoryStore` bean, that will be used instead of the default `InMemoryChatMemoryStore`,
* It leverages the available configuration options under `quarkus.langchain4j.chat-memory` to construct the `ChatMemoryProvider`.
** The default configuration values result in the usage of `dev.langchain4j.memory.chat.MessageWindowChatMemory` with a window size of ten
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to make it unbounded window?

if user wanted to keep the window but instead passivate/persist memory that have not been accessed for a while is that where ChatMemoryRemover come into play?

Copy link
Collaborator Author

@geoand geoand Dec 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to make it unbounded window?

Nope, Langchain4j doesn't have that - you would need to write you own

if user wanted to keep the window but instead passivate/persist memory that have not been accessed for a while is that where ChatMemoryRemover come into play?

Nope, see the updated samples that use ChatMemoryRemover to see when it's meant to be used

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okey so maybe i'm appraoching this wrong ..but do I grok this right as its purely just a way to demarcate when okey to disappear? like - exactly what a scope begin/end does in CDI?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw. no objections to it as it makes sense to have something like this available - just trying to imagine how this would look if we actually had some clear CDI scopes available for websocket et.al.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we have clear scopes (or when the existing request scope is used), everything works magically 😄.

I mean that the extension already has the logic to remove chat messages from the store

@geoand
Copy link
Collaborator Author

geoand commented Dec 8, 2023

I'd like @andreas-eberle to also have a look since he raised the concern about ChatMemoryStore yesterday

@geoand
Copy link
Collaborator Author

geoand commented Dec 8, 2023

Once this is in, I'll do a 0.4.0 release

return MessageWindowChatMemory.builder()
.maxMessages(20)
.id(memoryId)
.chatMemoryStore(new InMemoryChatMemoryStore())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to create a new store here every time?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I fixed it

@andreas-eberle
Copy link
Contributor

Looks good to me. Thanks @geoand !

The basic ideas are now:

* A default ChatMemoryStore bean using in memory storage is now provided
** This can be overridden by users simply by providing their own bean
* A default ChatMemoryProvider bean is now provided
** It uses the ChatMemoryStore bean under the hood
** It also leverages configuration for use either MessageWindowChatMemory or TokenWindowChatMemory

ChatMemory objects continue to be removed when the AI service
class goes out of scope, however now there is also a programmatic
way of removing ChatMemory by using ChatMemoryRemover.
This is necessary for when the AI service is not request scoped.
@geoand geoand merged commit fbd0212 into main Dec 10, 2023
2 checks passed
@geoand geoand deleted the memory-overhaul branch December 10, 2023 06:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants