llama_chat_apply_template: Apply chat template to messages

View source: R/llama.R

llama_chat_apply_templateR Documentation

Apply chat template to messages

Description

Formats a conversation using the specified chat template. This is essential for instruct/chat models to work correctly.

Usage

llama_chat_apply_template(
  messages,
  template = NULL,
  add_generation_prompt = TRUE
)

Arguments

messages

List of messages, each with 'role' and 'content' elements. Roles are typically "system", "user", "assistant".

template

Template string (from [llama_chat_template]) or NULL to use default

add_generation_prompt

Whether to add the assistant prompt prefix at the end

Value

A character scalar containing the formatted prompt string, ready to be passed to llama_generate.

Examples

## Not run: 
model <- llama_load_model("llama-3.2-instruct.gguf")
tmpl <- llama_chat_template(model)

messages <- list(
  list(role = "system", content = "You are a helpful assistant."),
  list(role = "user", content = "What is R?")
)

prompt <- llama_chat_apply_template(messages, template = tmpl)
cat(prompt)

ctx <- llama_new_context(model)
response <- llama_generate(ctx, prompt)

## End(Not run)

llamaR documentation built on May 28, 2026, 1:06 a.m.