Skip to contents

Extracts the content of an assistant's reply from an LLMMessage object at a specific index. This function can handle replies that are expected to be in JSON format by attempting to parse them. If parsing fails or if the user opts for raw text, the function will gracefully return the original content.

Usage

get_reply(.llm, .index = NULL, .raw = FALSE)

Arguments

.llm

A LLMMessage object containing the history of messages exchanged with the assistant. This parameter must be a valid LLMMessage object; otherwise, the function will throw an error.

.index

A positive integer indicating which assistant reply to retrieve. Defaults to NULL, which retrieves the last reply.

.raw

A logical value indicating whether to return the raw text even if the message is marked as JSON. Defaults to FALSE, meaning the function will attempt to parse the JSON.

Value

Returns the content of the assistant's reply at the specified index, based on the following conditions:

  • If there are no assistant replies, NULL is returned.

  • If the reply is marked as JSON and parsing is successful, a list containing:

    • parsed_content: The parsed JSON content.

    • raw_response: The original raw content.

    • json: A flag indicating successful JSON parsing (TRUE).

  • If JSON parsing fails, a list containing:

    • parsed_content: NULL.

    • raw_response: The original raw content.

    • json: FALSE.

  • If .raw is TRUE or the message is not marked as JSON, returns the raw text content directly.