Function process_messages

Source
pub fn process_messages(messages: &[Message]) -> Vec<Message>
Expand description

process_messages take arbitrary user input messages and process them to ensure them conform to Anthropic API requirements. the requirements are:

  1. start with user message
  2. alternate between user and assistant message
  3. the last assistant message cannot have trailing empty space

This function will:

  1. drop any empty message
  2. concatenate consecutive messages of the same role
  3. add a user message to the start of the conversation if the first message is of role assistant
  4. trim trailing empty space from the last message if it is of role assistant