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.


Method clone_deep()

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.

Usage

LLMMessage$clone_deep()

Returns

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


Method add_message()

Add a message

Adds a message to the history. Optionally includes media.

Usage

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

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?

meta

Optional, metadata returned by an API


Method to_api_format()

Convert to API format

Converts the message history to a format suitable for various API calls.

Usage

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

Arguments

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)

Returns

A message history in the target API format


Method has_image()

Simple helper method to determine whether the message history contains an image.

Usage

LLMMessage$has_image()

Returns

Returns TRUE if the message hisotry contains images


Method remove_message()

Remove a Message by Index

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

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.

By default, this function respects the tidyllm_print_metadata option. If the option is set to TRUE, metadata for each message (if available) will be printed.

Users can override the default behavior by explicitly passing a value to the .meta parameter.

Usage

LLMMessage$print(.meta = getOption("tidyllm_print_metadata", FALSE))

Arguments

.meta

Logical; if TRUE, prints metadata for each message. Defaults to the value of getOption("tidyllm_print_metadata", FALSE).


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.