Large Language Model Message Class
Large Language Model Message Class
message_history
List to store all message interactions.
system_prompt
The system prompt used for a conversation
new()
Initializes the LLMMessage object with an optional system prompt.
LLMMessage$new(system_prompt = "You are a helpful assistant")
system_prompt
A string that sets the initial system prompt.
A new LLMMessage object. Deep Clone of LLMMessage Object
This method creates a deep copy of the LLMMessage
object. It ensures that
all internal states, including message histories and settings, are copied
so that the original object remains unchanged when mutations are applied
to the copy. This is particularly useful for maintaining immutability in
a tidyverse-like functional programming context where functions should
not have side effects on their inputs.
clone_deep()
LLMMessage$clone_deep()
A new LLMMessage
object that is a deep copy of the original.
Add a message
Adds a message to the history. Optionally includes media.
add_message()
LLMMessage$add_message(role, content, media = NULL, json = FALSE)
role
The role of the message sender (e.g., "user", "assistant").
content
The textual content of the message.
media
Optional; media content to attach to the message.
json
Is the message a raw string that contains a json response? Convert to API format
Converts the message history to a format suitable for various API calls.
to_api_format()
LLMMessage$to_api_format(
api_type,
cgpt_image_detail = "auto",
no_system = FALSE
)
api_type
The type of API (e.g., "claude","groq","openai").
cgpt_image_detail
Specific option for ChatGPT API (imagedetail - set to auto)
no_system
Without system prompt (default: FALSE)
A message history in the target API format Simple helper function to determine whether the message history contains an image We check this function whenever we call models that do not support images so we can post a warning to the user that images were found but not sent to the model
has_image()
LLMMessage$has_image()
Returns TRUE if the message hisotry contains images Remove a Message by Index
Removes a message from the message history at the specified index.
remove_message()
LLMMessage$remove_message(index)
index
A positive integer indicating the position of the message to remove.
The LLMMessage
object, invisibly.
clone()
The objects of this class are cloneable with this method.
LLMMessage$clone(deep = FALSE)
deep
Whether to make a deep clone.
This class manages a history of messages and media interactions intended for use with large language models. It allows for adding messages, converting messages for API usage, and printing the history in a structured format.