Skip to contents

Inserts a message at a specified position in a list of messages. The role and content are converted to a list and inserted into the input list at the given position.

Usage

insert_message(content, role = "user", x = NULL, position = -1, ...)

Arguments

content

The content of the message.

role

The role of the message. Can be "user", "system", "assistant". Default is "user".

x

A list of messages. Default is NULL.

position

The position at which to insert the new message. Default is -1 (end of list).

...

Additional arguments such as images.

Value

A list of messages with the new message inserted at the specified position.

Examples

messages <- list(
    list(role = "system", content = "Be friendly"),
    list(role = "user", content = "How are you?")
)
insert_message("INSERT MESSAGE AT THE END", "user", messages)
#> [[1]]
#> [[1]]$role
#> [1] "system"
#> 
#> [[1]]$content
#> [1] "Be friendly"
#> 
#> 
#> [[2]]
#> [[2]]$role
#> [1] "user"
#> 
#> [[2]]$content
#> [1] "How are you?"
#> 
#> 
#> [[3]]
#> [[3]]$role
#> [1] "user"
#> 
#> [[3]]$content
#> [1] "INSERT MESSAGE AT THE END"
#> 
#> 
insert_message("INSERT MESSAGE AT THE BEGINNING", "user", messages, 2)
#> [[1]]
#> [[1]]$role
#> [1] "system"
#> 
#> [[1]]$content
#> [1] "Be friendly"
#> 
#> 
#> [[2]]
#> [[2]]$role
#> [1] "user"
#> 
#> [[2]]$content
#> [1] "INSERT MESSAGE AT THE BEGINNING"
#> 
#> 
#> [[3]]
#> [[3]]$role
#> [1] "user"
#> 
#> [[3]]$content
#> [1] "How are you?"
#> 
#>