spring-projects / spring-projects/spring-ai

Improve before method in MessageChatMemoryAdvisor

Open
#4,500 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

advisors chat memory
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

Below is the implementation code of the method before.

	public ChatClientRequest before(ChatClientRequest chatClientRequest, AdvisorChain advisorChain) {
		String conversationId = getConversationId(chatClientRequest.context(), this.defaultConversationId);

		// 1. Retrieve the chat memory for the current conversation.
		List<Message> memoryMessages = this.chatMemory.get(conversationId);

		// 2. Advise the request messages list.
		List<Message> processedMessages = new ArrayList<>(memoryMessages);
		processedMessages.addAll(chatClientRequest.prompt().getInstructions());

		// 3. Create a new request with the advised messages.
		ChatClientRequest processedChatClientRequest = chatClientRequest.mutate()
			.prompt(chatClientRequest.prompt().mutate().messages(processedMessages).build())
			.build();

		// 4. Add the new user message to the conversation memory.
		UserMessage userMessage = processedChatClientRequest.prompt().getUserMessage();
		this.chatMemory.add(conversationId, userMessage);

		return processedChatClientRequest;
	}

In this method, it will invoke chatMemoryRepository.findByConversationId(conversationId) twice, there are
List<Message> memoryMessages = this.chatMemory.get(conversationId);
this.chatMemory.add(conversationId, userMessage);

Maybe there are some ways to improve it, like add a method in ChatMemory defined like
add(String conversationId, Message message, List<Message> memoryMessages )
So we can use invoke it with userMessage, memoryMessages args.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with MessageChatMemoryAdvisor.before and the ChatMemory calls shown in the issue, then trace how chatMemoryRepository.findByConversationId is reached by get and add. Define completion as avoiding the duplicate lookup while preserving the processed request and conversation-memory behavior; inspect existing ChatMemory tests before changing the API.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
ai, backend-api-design
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.