char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aldehir Rojascommon : implement common_schema internal representation for JSON schemas (#28736)acecd5603

char/aliceai-t5-moe
4.9 KiB110 linesraw
1#include "parsers.h"
2
3// LFM2 format detection: template uses <|tool_list_start|>[...]<|tool_list_end|> around the tool list
4// and <|tool_call_start|>[...]<|tool_call_end|> around each tool call
5bool is_lfm2_template(const std::string & src) {
6    return src.find("<|tool_list_start|>") != std::string::npos &&
7           src.find("<|tool_list_end|>")   != std::string::npos;
8}
9
10// LFM2/LFM2.5 parser. Tool calls are almost Python-style and parallel-capable
11// (except dotted names and JSON literals true/false/null).
12// Always wrapped in <|tool_call_start|>[name(args)]<|tool_call_end|> with optional <think> reasoning.
13// tool_list_tokens preserves LFM2 system tool-list markers.
14common_chat_params common_chat_params_init_lfm2(const common_chat_template &          tmpl,
15                                                       const autoparser::generation_params & inputs,
16                                                       bool tool_list_tokens) {
17    common_chat_params data;
18
19    const std::string TOOL_CALL_START = "<|tool_call_start|>";
20    const std::string TOOL_CALL_END   = "<|tool_call_end|>";
21    const std::string TOOL_LIST_START = "<|tool_list_start|>";
22    const std::string TOOL_LIST_END   = "<|tool_list_end|>";
23    const std::string THINK_START     = "<think>";
24    const std::string THINK_END       = "</think>";
25    const std::string GEN_PROMPT      = "<|im_start|>assistant\n";
26
27    // Copy reasoning to the "thinking" field the template expects
28    auto adjusted_messages = json::array();
29    for (auto msg : inputs.messages) {
30        if (msg.contains("reasoning_content") && msg.at("reasoning_content").is_string()) {
31            msg["thinking"] = msg.at("reasoning_content");
32        }
33        adjusted_messages.push_back(msg);
34    }
35
36    data.prompt            = common_chat_template_direct_apply_impl(tmpl, inputs, adjusted_messages);
37    data.generation_prompt = common_chat_template_generation_prompt_impl(tmpl, inputs, adjusted_messages);
38    data.format            = COMMON_CHAT_FORMAT_PEG_NATIVE;
39    data.supports_thinking = true;
40    data.preserved_tokens  = { TOOL_CALL_START, TOOL_CALL_END, THINK_START, THINK_END };
41    if (tool_list_tokens) {
42        data.preserved_tokens.push_back(TOOL_LIST_START);
43        data.preserved_tokens.push_back(TOOL_LIST_END);
44    }
45
46    data.thinking_start_tag = THINK_START;
47    data.thinking_end_tags  = {THINK_END};
48
49    auto has_tools           = inputs.tools.is_array() && !inputs.tools.empty();
50    auto has_response_format = !inputs.json_schema.is_null() && inputs.json_schema.is_object();
51    // Gate by reasoning format and whether the template supports <think>
52    auto extract_reasoning = inputs.reasoning_format != COMMON_REASONING_FORMAT_NONE &&
53                             tmpl.source().find(THINK_START) != std::string::npos;
54    auto include_grammar   = has_response_format || (has_tools && inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_NONE);
55
56    if (inputs.has_continuation()) {
57        const auto & msg = inputs.continue_msg;
58
59        data.generation_prompt = GEN_PROMPT + THINK_START + msg.reasoning_content;
60        if (inputs.continue_final_message == COMMON_CHAT_CONTINUATION_CONTENT) {
61            data.generation_prompt += THINK_END + msg.render_content();
62        }
63
64        data.prompt += data.generation_prompt;
65    }
66
67    auto parser = build_chat_peg_parser([&](common_chat_peg_builder & p) {
68        auto generation_prompt = p.literal(GEN_PROMPT);
69        auto end = p.end();
70
71        auto reasoning = p.eps();
72        if (extract_reasoning) {
73            reasoning = p.optional(THINK_START + p.reasoning(p.until(THINK_END)) + THINK_END);
74        }
75
76        if (!has_tools || inputs.tool_choice == COMMON_CHAT_TOOL_CHOICE_NONE) {
77            if (has_response_format) {
78                auto response_format = p.content(p.schema(p.json(), "response-format-schema", inputs.json_schema));
79                return generation_prompt + reasoning + response_format + end;
80            }
81            return generation_prompt + reasoning + p.content(p.rest()) + end;
82        }
83        auto tool_calls = p.rule("tool-calls",
84            p.trigger_rule("tool-call",
85                p.literal(TOOL_CALL_START) +
86                p.python_style_tool_calls(inputs.tools, inputs.parallel_tool_calls, /* allow_json_literals = */ true) +
87                p.literal(TOOL_CALL_END)
88            )
89        );
90
91        auto content = p.content(p.until(TOOL_CALL_START));
92
93        return generation_prompt + reasoning + content + tool_calls + end;
94    });
95
96    data.parser = parser.save();
97
98    if (include_grammar) {
99        data.grammar_lazy = !(has_response_format || (has_tools && inputs.tool_choice == COMMON_CHAT_TOOL_CHOICE_REQUIRED));
100        data.grammar      = build_grammar([&](const common_grammar_builder & builder) {
101            parser.build_grammar(builder, data.grammar_lazy);
102        });
103
104        data.grammar_triggers = {
105            { COMMON_GRAMMAR_TRIGGER_TYPE_WORD, TOOL_CALL_START }
106        };
107    }
108
109    return data;
110}