spring-projects / spring-ai

An Application Framework for AI Engineering
https://docs.spring.io/spring-ai/reference/index.html
Apache License 2.0
3.16k stars 786 forks source link

How InMemoryChatMemory() Works !! #1585

Open account123456789 opened 4 days ago

account123456789 commented 4 days ago

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 commented 4 days ago

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.