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.2 KiB96 linesraw
1#include "parsers.h"
2
3// Functionary v3.2 - uses recipient-based format: >>>recipient\n{content}
4common_chat_params common_chat_params_init_functionary_v3_2(const common_chat_template &    tmpl,
5                                                                   const autoparser::generation_params & inputs) {
6    common_chat_params data;
7
8    data.prompt            = common_chat_template_direct_apply_impl(tmpl, inputs);
9    data.generation_prompt = common_chat_template_generation_prompt_impl(tmpl, inputs);
10    data.format            = COMMON_CHAT_FORMAT_PEG_NATIVE;
11    data.preserved_tokens  = {
12        ">>>all",
13    };
14
15    auto has_tools         = inputs.tools.is_array() && !inputs.tools.empty();
16    auto include_grammar   = has_tools && inputs.tool_choice != COMMON_CHAT_TOOL_CHOICE_NONE;
17
18    if (inputs.has_continuation()) {
19        const auto & msg = inputs.continue_msg;
20        data.generation_prompt = "<|start_header_id|>assistant<|end_header_id|>\n\n>>>all\n" + msg.render_content();
21        data.prompt += data.generation_prompt;
22    }
23
24    auto parser = build_chat_peg_parser([&](common_chat_peg_builder & p) {
25        // Functionary v3.2 format:
26        // - Normal content: >>>all\n{content}
27        // - Tool calls: >>>function_name\n{json_args}
28        // Generation prompt ends with ">>>" so model outputs recipient immediately
29
30        // Build content parser for >>>all\n{content}
31        // When tools are present, content stops before the next ">>>" (tool call)
32        // When no tools, content goes until end
33        auto content_until_tool = p.literal("all\n") + p.content(p.until(">>>"));
34        auto content_until_end  = p.literal("all\n") + p.content(p.rest());
35        auto generation_prompt  = p.literal("<|start_header_id|>assistant<|end_header_id|>\n\n>>>");
36
37        // If no tools or tool_choice is NONE, just parse content
38        if (!has_tools || inputs.tool_choice == COMMON_CHAT_TOOL_CHOICE_NONE) {
39            // When no tools, just match the prefix and capture everything after
40            return generation_prompt + content_until_end + p.end();
41        }
42
43        // Build tool call parsers for each available function
44        auto tool_choice = p.choice();
45        foreach_function(inputs.tools, [&](const json & tool) {
46            const auto & function = tool.at("function");
47            std::string  name     = function.at("name");
48            const auto   schema   = common_chat_tool_parameters(function);
49
50            // Tool format: >>>function_name\n{json_args}
51            auto tool_parser = p.tool(
52                p.tool_open(p.tool_name(p.literal(name)) + p.literal("\n")) +
53                p.tool_args(p.schema(p.json(), "tool-" + name + "-schema", schema))
54            );
55
56            tool_choice |= p.rule("tool-" + name, tool_parser);
57        });
58
59        auto content_only = content_until_end;
60        auto tools_only = p.trigger_rule("tools", p.one_or_more(tool_choice));
61        auto content_and_tools = content_until_tool + tools_only;
62
63        auto ret = p.eps();
64        if (inputs.tool_choice == COMMON_CHAT_TOOL_CHOICE_REQUIRED) {
65            if (inputs.parallel_tool_calls) {
66                ret = p.choice({ content_and_tools, tools_only }) + p.end();
67            } else {
68                ret = p.choice({ content_until_tool + tool_choice, tools_only }) + p.end();
69            }
70        } else if (inputs.parallel_tool_calls) {
71            ret = p.choice({ content_and_tools, content_only, tools_only }) + p.end();
72        } else {
73            auto content_and_tool = content_until_tool + tool_choice;
74            ret = p.choice({ content_and_tool, content_only, tool_choice }) + p.end();
75        }
76        return generation_prompt + ret;
77    });
78
79    data.parser = parser.save();
80
81    if (include_grammar) {
82        data.grammar_lazy = inputs.tool_choice == COMMON_CHAT_TOOL_CHOICE_AUTO;
83
84        data.grammar = build_grammar([&](const common_grammar_builder & builder) {
85            parser.build_grammar(builder, data.grammar_lazy);
86        });
87
88        // Grammar trigger for when the model starts outputting a tool call
89        // (after the initial ">>>" in the generation prompt but recipient other than "all")
90        data.grammar_triggers = {
91            { COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN, ">>>(?!all)" }
92        };
93    }
94
95    return data;
96}