Skip to contents

Large Language Model Message Class

Large Language Model Message Class

Details

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.

Public fields

message_history

List to store all message interactions.

system_prompt

The system prompt used for a conversation

Methods


Method new()

Initializes the LLMMessage object with an optional system prompt.

Usage

LLMMessage$new(system_prompt = "You are a helpful assistant")

Arguments

system_prompt

A string that sets the initial system prompt.

Returns

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.


Method clone_deep()

Usage

LLMMessage$clone_deep()

Returns

A new LLMMessage object that is a deep copy of the original. Add a message

Adds a message to the history. Optionally includes media.


Method add_message()

Usage

LLMMessage$add_message(role, content, media = NULL, json = FALSE)

Arguments

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.


Method to_api_format()

Usage

LLMMessage$to_api_format(api_type, cgpt_image_detail = "auto")

Arguments

api_type

The type of API (e.g., "claude","groq","chatgpt").

cgpt_image_detail

Specific option for ChatGPT API (imagedetail - set to auto)

Returns

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


Method has_image()

Usage

LLMMessage$has_image()

Returns

Returns TRUE if the message hisotry contains images Remove a Message by Index

Removes a message from the message history at the specified index.


Method remove_message()

Usage

LLMMessage$remove_message(index)

Arguments

index

A positive integer indicating the position of the message to remove.

Returns

The LLMMessage object, invisibly.


Method print()

Prints the current message history in a structured format.

Usage

LLMMessage$print()


Method clone()

The objects of this class are cloneable with this method.

Usage

LLMMessage$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.