Skip to contents

This function creates and validates a chat_history object, ensuring that it matches the expected format with 'role' and 'content' columns. It has separate methods for data.frame and character inputs and includes a helper function to add a system prompt to the chat history.

Usage

chat_history(chat_history)

Arguments

chat_history

A single string, a data.frame with 'role' and 'content' columns, or NULL. If a data.frame is provided, it should contain 'role' and 'content' columns, where 'role' is either 'user', 'assistant', or 'system', and 'content' is a character string representing a chat message

Value

A valid chat history data.frame (of class chat_history)

Examples

chat <- "Hi there!" |>
  chat_history()
chat
#>   role   content
#> 1 user Hi there!

chat_from_df <- data.frame(
  role = c("user", "assistant"),
  content = c("Hi there!", "Hello! How can I help you today?")
) |>
  chat_history()
chat_from_df
#>        role                          content
#> 1      user                        Hi there!
#> 2 assistant Hello! How can I help you today?