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

How InMemoryChatMemory() Works !! #1585

Open
account123456789 opened this issue Oct 23, 2024 · 1 comment
Open

How InMemoryChatMemory() Works !! #1585

account123456789 opened this issue Oct 23, 2024 · 1 comment

Comments

@account123456789
Copy link

Dear All,

I am creating a controller service class where the chatClient object is a reference in the service class.

I am using InMemoryChatMemory() chat memory.

My question is : In case of having a rest controller with multiple user access ... will it continue to maintain each memory in a separate memory storage or it will be shared between all users ... Thanks a lot and sorry for disturbing you.

public ChatControllerService(ChatClient.Builder builder)
{

    this.chatClient = builder
            .defaultSystem(systemPrompt)
            .defaultAdvisors(
                    new PromptChatMemoryAdvisor( new InMemoryChatMemory() ) ,
                    new SimpleLoggerAdvisor()
            )
            .build();

}
@habuma
Copy link
Member

habuma commented Oct 23, 2024

The chat memory is determined by the conversation ID. Because you didn't specify otherwise, it uses the default conversation, so the chat memory is shared across all users.

But...at prompt time you can specify the conversation ID like this:

String conversationId = ...;

String answer = chatClient.prompt()

   ...
   
    .advisors(advisorSpec -> advisorSpec
        .param(CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId))
    .call()
    .content();

What I didn't specify here is how conversationId is set. It could really be anything you want. If you only want it to be per-user, then you could just set it to the user ID and be done with it. Or if you want each user to potentially be part of multiple conversations, then you could bake up a conversation ID that is made up of the username and some other ID that represents the conversation for that user. So long as the conversation ID is unique, then the chat interaction will take place in a that unique conversation.

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

No branches or pull requests

2 participants