Skip to contents

This function appends messages to a chat log stored in the package's private chat environment. The messages are given either as a data.frame with the columns role and content, or as another chatlog, whose messages are appended in order.

Usage

add_to_chatlog(msgs, chatlog_id = NULL)

Arguments

msgs

A data.frame with the columns role and content holding the messages to be added, or a chatlog object whose messages are added.

chatlog_id

The id of the stored chatlog to add msgs to. Required: a chatlog in msgs cannot be added to itself.

Value

The updated chatlog object with the newly added messages.

Details

Only the role and content columns are kept, as character: any other column (e.g. refusal or annotations from an API response) is dropped, so it is never sent back to the API. The log stored under chatlog_id keeps its id; a chatlog given in msgs is not changed.

Author

Ulrich Matter umatter@protonmail.com

Examples

# Start a chat log, then add a user message and a reply to it
start_chat(chatlog_id = "demo")
#> Chatlog ID: demo
#>     role                      content
#> 1 system You are a helpful assistant.
new_msgs <- data.frame(
  role = c("user", "assistant"),
  content = c("Hello!", "Hi! How can I help?")
)
updated <- add_to_chatlog(new_msgs, chatlog_id = "demo")
updated@messages
#>        role                      content
#> 1    system You are a helpful assistant.
#> 2      user                       Hello!
#> 3 assistant          Hi! How can I help?

# Append one log's messages to another
start_chat(initial_content = "You are terse.", chatlog_id = "demo2")
#> Chatlog ID: demo2
#>     role        content
#> 1 system You are terse.
add_to_chatlog(get_chatlog("demo"), chatlog_id = "demo2")
#> Chatlog ID: demo2
#>        role                      content
#> 1    system               You are terse.
#> 2    system You are a helpful assistant.
#> 3      user                       Hello!
#> 4 assistant          Hi! How can I help?