char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
acecd5603
char/aliceai-t5-moe
1#include "parsers.h" 2 3common_chat_params common_chat_params_init_gigachat_v3 ( 4const common_chat_template & tmpl , 5const autoparser::generation_params & inputs ) { 6 7common_chat_params data ; 8 9data .prompt = common_chat_template_direct_apply_impl (tmpl ,inputs ); 10data .generation_prompt = common_chat_template_generation_prompt_impl (tmpl ,inputs ); 11data .format = COMMON_CHAT_FORMAT_PEG_NATIVE ; 12data .supports_thinking = false; 13data .preserved_tokens = { 14"<|message_sep|>\n\n" , 15"<|role_sep|>\n" , 16 }; 17 18if (inputs .has_continuation ()) { 19const auto & msg = inputs .continue_msg ; 20data .generation_prompt = "assistant<|role_sep|>\n" + msg .render_content (); 21data .prompt += data .generation_prompt ; 22 } 23 24auto has_tools = inputs .tools .is_array ()&& !inputs .tools .empty (); 25auto include_grammar = has_tools && inputs .tool_choice != COMMON_CHAT_TOOL_CHOICE_NONE ; 26const auto * tool_call_start_prefix = "<|message_sep|>\n\nfunction call<|role_sep|>\n" ; 27 28auto parser = build_chat_peg_parser ([& ](common_chat_peg_builder & p ) { 29auto ret = p .eps (); 30if (has_tools && inputs .tool_choice != COMMON_CHAT_TOOL_CHOICE_NONE ) { 31// Build a choice of all available tools 32auto tool_choice = p .choice (); 33for (const auto & tool :inputs .tools ) { 34const auto & function = tool .at ("function" ); 35 std::string name = function .at ("name" ); 36const auto schema = common_chat_tool_parameters (function ); 37 38auto tool_name = p .json_member ("name" ,"\"" + p .tool_name (p .literal (name ))+ "\"" ); 39auto tool_args = p .json_member ("arguments" ,p .tool_args (p .schema (p .json (),"tool-" + name + "-schema" ,schema ))); 40 41auto tool_open = p .tool_open (p .literal ("{" ) <<tool_name ); 42 43tool_choice |=p .rule ("tool-" + name ,tool_open <<"," <<tool_args <<"}" ); 44 } 45 46// Define the tool call structure 47auto min_calls = inputs .tool_choice == COMMON_CHAT_TOOL_CHOICE_REQUIRED ?1 :0 ; 48auto max_calls = 1 ;// parallel toolcalls are not supported 49auto tool_call = p .rule ("tool-call" ,p .literal (tool_call_start_prefix )+ tool_choice ); 50auto tool_calls = p .trigger_rule ("tool-call-root" ,p .repeat (tool_call ,/* min = */ min_calls ,/* max = */ max_calls )); 51 52ret = p .content (p .until ("<|message_sep|>\n\n" )) <<tool_calls ; 53 }else { 54// Content only parser 55include_grammar = false; 56ret = p .content (p .rest ()); 57 } 58 59return p .literal ("assistant<|role_sep|>\n" )+ ret ; 60 }); 61 62data .parser = parser .save (); 63 64if (include_grammar ) { 65data .grammar_lazy = has_tools && inputs .tool_choice == COMMON_CHAT_TOOL_CHOICE_AUTO ; 66 67data .grammar = build_grammar ([& ](const common_grammar_builder & builder ) { 68parser .build_grammar (builder ,data .grammar_lazy ); 69 }); 70 71data .grammar_triggers = { 72 {COMMON_GRAMMAR_TRIGGER_TYPE_WORD ,tool_call_start_prefix } 73 }; 74 } 75return data ; 76}