liblloyal 1.0.0
Composable primitives for llama.cpp inference
Loading...
Searching...
No Matches
chat_template.hpp
Go to the documentation of this file.
1#pragma once
2
3// SPDX-License-Identifier: Apache-2.0
4// Copyright 2026 Lloyal Labs
5
6#include "common.hpp"
7#include "helpers.hpp"
8#include <llama/llama.h>
9#include "nlohmann/json.hpp" // Relative path to vendored nlohmann/json.hpp
10#include <string>
11#include <vector>
12
28
34 std::string prompt; // Formatted prompt text
35 std::vector<std::string> additional_stops; // Stop tokens from template
36};
37
57inline FormatResult format(const llama_model *model,
58 const std::string &messages_json,
59 const std::string &template_override = "") {
60 FormatResult result;
61
62 try {
63 // Step 1: Call helpers.hpp function for template processing
64 // (This handles template selection, BOS/EOS tokens, and stop token
65 // extraction)
66 ChatTemplateResult helper_result =
67 format_chat_template_complete(model, messages_json, template_override);
68
69 // Step 2: Check if template processing succeeded
70 if (helper_result.prompt.empty()) {
72 "[chat_template::format] Template processing failed, using fallback");
73
74 // Step 3: Fallback to simple "role: content" format
75 try {
76 using json = nlohmann::ordered_json;
77 json messages = json::parse(messages_json);
78 std::string fallback;
79 for (const auto &msg : messages) {
80 if (msg.contains("role") && msg.contains("content")) {
81 fallback += msg["role"].get<std::string>() + ": " +
82 msg["content"].get<std::string>() + "\n";
83 }
84 }
85
86 result.prompt = fallback;
87 result.additional_stops = {}; // No stop tokens for fallback
88
90 "[chat_template::format] Using fallback format (%zu bytes)",
91 fallback.size());
92 return result;
93
94 } catch (const std::exception &e) {
96 "[chat_template::format] ERROR: Failed to parse messages JSON: %s",
97 e.what());
98 result.prompt = "";
99 result.additional_stops = {};
100 return result;
101 }
102 }
103
104 // Step 4: Success - return formatted result
105 result.prompt = helper_result.prompt;
106 result.additional_stops = helper_result.additional_stops;
107
109 "[chat_template::format] Successfully formatted with %zu stop tokens",
110 result.additional_stops.size());
111 return result;
112
113 } catch (const std::exception &e) {
114 LLOYAL_LOG_DEBUG("[chat_template::format] ERROR: %s", e.what());
115 result.prompt = "";
116 result.additional_stops = {};
117 return result;
118 }
119}
120
130inline bool validate(const std::string &template_str) {
131 try {
132 // Call helpers.hpp validation function
133 bool isValid = validate_chat_template_helper(template_str);
134 LLOYAL_LOG_DEBUG("[chat_template::validate] Template validation: %s",
135 isValid ? "valid" : "invalid");
136 return isValid;
137 } catch (const std::exception &e) {
138 LLOYAL_LOG_DEBUG("[chat_template::validate] ERROR: %s", e.what());
139 return false;
140 }
141}
142
143} // namespace lloyal::chat_template
#define LLOYAL_LOG_DEBUG(...)
liblloyal - Common definitions and logging
Definition common.hpp:47
Helper Utilities.
bool validate(const std::string &template_str)
Validate chat template syntax.
FormatResult format(const llama_model *model, const std::string &messages_json, const std::string &template_override="")
Format chat messages using model's chat template with fallback.
ChatTemplateResult format_chat_template_complete(const llama_model *model, const std::string &messages_json, const std::string &template_override="")
Complete chat template processing with stop token detection.
Definition helpers.hpp:282
bool validate_chat_template_helper(const std::string &template_str)
Validate chat template syntax.
Definition helpers.hpp:341
nlohmann::ordered_json json
Definition helpers.hpp:50
Result from complete chat template processing.
Definition helpers.hpp:113
std::string prompt
Formatted chat prompt ready for tokenization.
Definition helpers.hpp:114
std::vector< std::string > additional_stops
Template-specific stop tokens (e.g., "<|im_end|>", "<|eot_id|>")
Definition helpers.hpp:115
Result from chat template formatting NOTE: Named FormatResult, NOT ChatTemplateResult.
std::vector< std::string > additional_stops