Skip to content

feat: Add metadata support to PromptUserSpec and PromptSystemSpec in ChatClient #3989

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

YunKuiLu
Copy link
Contributor

@YunKuiLu YunKuiLu commented Aug 4, 2025

  • Add metadata methods to PromptUserSpec and PromptSystemSpec interfaces
  • Update DefaultChatClientRequestSpec to handle user and system metadata
  • Modify DefaultChatClientUtils to include metadata in SystemMessage and UserMessage builders
  • Add comprehensive test coverage for metadata functionality

This PR allows users to set the metadata field for UserMessage and SystemMessage through Consumer<PromptUserSpec> and Consumer<PromptSystemSpec>.

Users can set information like message ID through metadata, and then get them in ChatMemoryRepository.
Example as follows:

  private static final ChatMemoryRepository myChatMemoryRepository = new ChatMemoryRepository() {
      @Override
      public void saveAll(String conversationId, List<Message> messages) {
          log.info("messages={}", messages);
          log.info("id={}", messages.getLast().getMetadata().get("id"));
      }
  };
  
  @GetMapping("/ai/tool")
  public String tool() {
      String userMessageId = UUID.randomUUID().toString();
  
      ChatResponse result = chatClient.prompt()
              .system(s -> s.text("You are a helpful assistant.")
                           // Allow setting metadata for SystemMessage.
                           .metadata("version", "v1.0"))
              .tools(this)
              .user(u -> u.text("What is the weather in New York?")
                          // Allow setting metadata for UserMessage.
                          .metadata("id", userMessageId))
              .advisors(MessageChatMemoryAdvisor.builder(
                      MessageWindowChatMemory.builder()
                              .chatMemoryRepository(myChatMemoryRepository)
                              .maxMessages(10).build()
              ).build())
              .call()
              .chatResponse();
  
      return result.getResult().getOutput().getText();
  }

- Add metadata methods to PromptUserSpec and PromptSystemSpec interfaces
- Update DefaultChatClientRequestSpec to handle user and system metadata
- Modify DefaultChatClientUtils to include metadata in SystemMessage and UserMessage builders
- Add comprehensive test coverage for metadata functionality

Signed-off-by: YunKui Lu <luyunkui95@gmail.com>
@YunKuiLu YunKuiLu changed the title feat: Add metadata support to user and system messages in ChatClient feat: Add metadata support to PromptUserSpec and PromptSystemSpec in ChatClient Aug 4, 2025
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.

1 participant