char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
acecd5603
char/aliceai-t5-moe
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 , 5const autoparser::generation_params & inputs ) { 6common_chat_params data ; 7 8data .prompt = common_chat_template_direct_apply_impl (tmpl ,inputs ); 9data .generation_prompt = common_chat_template_generation_prompt_impl (tmpl ,inputs ); 10data .format = COMMON_CHAT_FORMAT_PEG_NATIVE ; 11data .preserved_tokens = { 12">>>all" , 13 }; 14 15auto has_tools = inputs .tools .is_array ()&& !inputs .tools .empty (); 16auto include_grammar = has_tools && inputs .tool_choice != COMMON_CHAT_TOOL_CHOICE_NONE ; 17 18if (inputs .has_continuation ()) { 19const auto & msg = inputs .continue_msg ; 20data .generation_prompt = "<|start_header_id|>assistant<|end_header_id|>\n\n>>>all\n" + msg .render_content (); 21data .prompt += data .generation_prompt ; 22 } 23 24auto 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 33auto content_until_tool = p .literal ("all\n" )+ p .content (p .until (">>>" )); 34auto content_until_end = p .literal ("all\n" )+ p .content (p .rest ()); 35auto 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 38if (!has_tools || inputs .tool_choice == COMMON_CHAT_TOOL_CHOICE_NONE ) { 39// When no tools, just match the prefix and capture everything after 40return generation_prompt + content_until_end + p .end (); 41 } 42 43// Build tool call parsers for each available function 44auto tool_choice = p .choice (); 45foreach_function (inputs .tools , [& ](const json & tool ) { 46const auto & function = tool .at ("function" ); 47 std::string name = function .at ("name" ); 48const auto schema = common_chat_tool_parameters (function ); 49 50// Tool format: >>>function_name\n{json_args} 51auto tool_parser = p .tool ( 52p .tool_open (p .tool_name (p .literal (name ))+ p .literal ("\n" ))+ 53p .tool_args (p .schema (p .json (),"tool-" + name + "-schema" ,schema )) 54 ); 55 56tool_choice |=p .rule ("tool-" + name ,tool_parser ); 57 }); 58 59auto content_only = content_until_end ; 60auto tools_only = p .trigger_rule ("tools" ,p .one_or_more (tool_choice )); 61auto content_and_tools = content_until_tool + tools_only ; 62 63auto ret = p .eps (); 64if (inputs .tool_choice == COMMON_CHAT_TOOL_CHOICE_REQUIRED ) { 65if (inputs .parallel_tool_calls ) { 66ret = p .choice ({content_and_tools ,tools_only })+ p .end (); 67 }else { 68ret = p .choice ({content_until_tool + tool_choice ,tools_only })+ p .end (); 69 } 70 }else if (inputs .parallel_tool_calls ) { 71ret = p .choice ({content_and_tools ,content_only ,tools_only })+ p .end (); 72 }else { 73auto content_and_tool = content_until_tool + tool_choice ; 74ret = p .choice ({content_and_tool ,content_only ,tool_choice })+ p .end (); 75 } 76return generation_prompt + ret ; 77 }); 78 79data .parser = parser .save (); 80 81if (include_grammar ) { 82data .grammar_lazy = inputs .tool_choice == COMMON_CHAT_TOOL_CHOICE_AUTO ; 83 84data .grammar = build_grammar ([& ](const common_grammar_builder & builder ) { 85parser .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") 90data .grammar_triggers = { 91 {COMMON_GRAMMAR_TRIGGER_TYPE_PATTERN ,">>>(?!all)" } 92 }; 93 } 94 95return data ; 96}