Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ggml org Llama cpp Chat Types

From Leeroopedia
Knowledge Sources
Domains Chat, API
Last Updated 2026-02-15 00:00 GMT

Overview

Declares the chat template enumeration and functions for detecting and applying model-specific chat prompt formats.

Description

This header defines the `llm_chat_template` enum with entries for 40+ model families (ChatML, Llama2-4, Mistral, DeepSeek, Gemma, Phi, etc.). It exposes three functions: `llm_chat_template_from_str` for name-to-enum lookup, `llm_chat_detect_template` for heuristic template detection from template strings, and `llm_chat_apply_template` for formatting a vector of chat messages into a properly formatted prompt string.

Usage

Include this header when implementing chat functionality. It serves as the core interface that standardizes chat template handling across the codebase, allowing higher-level components (server, CLI) to format conversations correctly for any supported model.

Code Reference

Source Location

Signature

enum llm_chat_template {
    LLM_CHAT_TEMPLATE_CHATML,
    LLM_CHAT_TEMPLATE_LLAMA_2,
    // ... 40+ model family entries ...
    LLM_CHAT_TEMPLATE_UNKNOWN,
};

llm_chat_template llm_chat_template_from_str(const std::string & name);
llm_chat_template llm_chat_detect_template(const std::string & tmpl);
int32_t llm_chat_apply_template(
    llm_chat_template tmpl,
    const std::vector<const llama_chat_message *> & chat,
    std::string & dest, bool add_ass);

Import

#include "llama-chat.h"
// Dependencies:
#include <string>
#include <vector>
#include <cstdint>

I/O Contract

Inputs

Name Type Required Description
name const std::string & Yes Template name string for llm_chat_template_from_str
tmpl const std::string & Yes Template content string for heuristic detection
tmpl llm_chat_template Yes Template enum value for llm_chat_apply_template
chat const std::vector<const llama_chat_message *> & Yes Vector of chat messages to format
dest std::string & Yes Output string buffer for the formatted prompt
add_ass bool Yes Whether to add the assistant prompt prefix at the end

Outputs

Name Type Description
llm_chat_template_from_str return llm_chat_template Enum value corresponding to the template name
llm_chat_detect_template return llm_chat_template Detected template enum from heuristic analysis
llm_chat_apply_template return int32_t Number of characters written to dest, or negative on error

Usage Examples

#include "llama-chat.h"

// Detect template from model metadata
llm_chat_template tmpl = llm_chat_detect_template(template_str);

// Format chat messages
std::vector<const llama_chat_message *> messages = { &msg1, &msg2 };
std::string formatted;
int32_t len = llm_chat_apply_template(tmpl, messages, formatted, true);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment