Skip to contents

This function replaces the last response in a chatlog with a newly generated one. The output can be displayed as a message in the console, returned as a message, or returned as a response object.

Usage

regenerate(
  chatlog_id = ".__CURRENTCHAT__",
  output = c("message_to_console", "message", "response_object"),
  ...
)

Arguments

chatlog_id

A character string specifying the ID of the chatlog (default: ".__CURRENTCHAT__")

output

A character string specifying the output format. Options are 'message_to_console', 'message', or 'response_object' (default: 'message_to_console')

...

Additional arguments to pass to the chat_completion() function. As in chat, n other than 1 stops with an error.

Value

If output is 'message_to_console', the function prints the reply to the console as chat() does (text verbatim, code blocks as code) and returns NULL invisibly. If output is 'message', the function returns a character string containing the reply. If output is 'response_object', the function returns the full response object.

Details

The last message of the chatlog must be an assistant reply; otherwise regenerate() stops. The new reply is requested without that last reply, and the stored chatlog is only changed once a valid new reply has arrived: if the request fails, the old reply is kept.

Examples

if (FALSE) { # \dontrun{
# Start a chat under the id "demo"
chat("Hello, how are you?", chatlog_id = "demo")

# Regenerate the last reply and display it in the console
regenerate("demo")

# Regenerate the last reply and return it as a character string
reply <- regenerate("demo", output = "message")

# Regenerate the last reply and return the full response object
response_object <- regenerate("demo", output = "response_object")

# Without an id, regenerate() works on the conversation of chat()
chat("Tell me a joke.")
regenerate()
} # }