| apply_chat_template | R Documentation |
Formats conversation messages using the model's built-in chat template or a custom template. This is essential for chat models that expect specific formatting for multi-turn conversations.
apply_chat_template(model, messages, template = NULL,
add_assistant = TRUE)
model |
A model object created with |
messages |
List of chat messages, each with 'role' and 'content' fields. Role should be 'user', 'assistant', or 'system' |
template |
Optional custom template string (default: NULL, uses model's built-in template) |
add_assistant |
Whether to add assistant prompt suffix for response generation (default: TRUE) |
Formatted prompt string ready for text generation
model_load, quick_llama, generate
## Not run:
# Load a chat model
model <- model_load("path/to/chat_model.gguf")
# Format a conversation
messages <- list(
list(role = "system", content = "You are a helpful assistant."),
list(role = "user", content = "What is machine learning?"),
list(role = "assistant", content = "Machine learning is..."),
list(role = "user", content = "Give me an example.")
)
# Apply chat template
formatted_prompt <- apply_chat_template(model, messages)
# Generate response
response <- quick_llama(formatted_prompt)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.