add_msg_to_chat_history | R Documentation |
This function appends a message to a chat_history()
object.
The function can automatically determine the role of the message to be
added based on the last message in the chat history. The role can also be
manually specified.
add_msg_to_chat_history(
chat_history,
message,
role = c("auto", "user", "assistant", "system", "tool"),
tool_result = NULL
)
chat_history |
A single string, a data.frame which is a valid chat history
(see A |
message |
A character string representing the message to add |
role |
A character string representing the role of the message sender. One of:
|
tool_result |
A logical indicating whether the message is a tool result (e.g., the result of a function call) |
The chat_history object may be of different types:
A single string: The function will create a new chat history object with the string as the first message; the role of that first message will be "user"
A data.frame: The function will append the message to the data.frame.
The data.frame must be a valid chat history; see chat_history()
A list: The function will extract the chat history from the list.
The list must contain a valid chat history under the key 'chat_history'.
This may typically be the result from send_prompt()
when using
'return_mode = "full"'
A Tidyprompt object (tidyprompt): The function will extract the chat history from the object. This will be done by concatenating the 'system_prompt', 'chat_history', and 'base_prompt' into a chat history data.frame. Note that the other properties of the tidyprompt object will be lost
NULL: The function will create a new chat history object with no messages; the message will be the first message
A chat_history()
object with the message added as the last row
chat <- "Hi there!" |>
chat_history()
chat
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
# `add_msg_to_chat_history()` may be used to add messages to a chat history
chat_from_df <- chat_from_df |>
add_msg_to_chat_history("Calculate 2+2 for me, please!")
chat_from_df
# You can also continue conversations which originate from `send_prompt()`:
## Not run:
result <- "Hi there!" |>
send_prompt(return_mode = "full")
# --- Sending request to LLM provider (llama3.1:8b): ---
# Hi there!
# --- Receiving response from LLM provider: ---
# It's nice to meet you. Is there something I can help you with, or would you
# like to chat?
# Access the chat history from the result:
chat_from_send_prompt <- result$chat_history
# Add a message to the chat history:
chat_history_with_new_message <- chat_from_send_prompt |>
add_msg_to_chat_history("Let's chat!")
# The new chat history can be input for a new tidyprompt:
prompt <- tidyprompt(chat_history_with_new_message)
# You can also take an existing tidyprompt and add the new chat history to it;
# this way, you can continue a conversation using the same prompt wraps
prompt$set_chat_history(chat_history_with_new_message)
# send_prompt() also accepts a chat history as input:
new_result <- chat_history_with_new_message |>
send_prompt(return_mode = "full")
# You can also create a persistent chat history object from
# a chat history data frame; see ?`persistent_chat-class`
chat <- `persistent_chat-class`$new(llm_provider_ollama(), chat_from_send_prompt)
chat$chat("Let's chat!")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.