
llama.cpp 函数调用实战llama-server --jinja 下的 Native 工具格式、Generic 回退与 PEG 解析机制【免费下载链接】llama.cppLLM inference in C/C项目地址: https://gitcode.com/GitHub_Trending/ll/llama.cpp本文围绕 llama.cpp 官方的函数调用Function Calling文档展开讲解如何在llama-server上以 OpenAI 兼容接口实现工具调用哪些模型走原生Native格式、哪些走 Generic 回退、模板能力caps如何在源码层面被检测、--chat-template-file与parallel_tool_calls等关键参数如何配置并给出完整可复制的服务端启动命令与 curl 调用示例。读完本文你能够为自己的模型正确选择或覆盖聊天模板、跑通工具调用闭环并理解 llama.cpp 从模板渲染到输出解析的底层调用链。核心机制基于 chat.h 的工具调用支持llama.cpp 的工具调用支持由 common/chat.h 引入llama-server在启用--jinja标志时会走这条完整的工具感知路径。对模型而言工具调用支持分两类Native原生工具调用格式当前支持Llama 3.1 / 3.3含内置工具支持wolfram_alpha、web_search/brave_search、code_interpreter的工具名、Llama 3.2Functionary v3.1 / v3.2Hermes 2/3、Qwen 2.5Qwen 2.5 CoderMistral NemoFirefunction v2Command R7BDeepSeek R1WIP据文档描述该模型似乎不太愿意调用工具Generic通用工具调用格式当模板不被任何原生格式处理器识别时自动生效此时日志中会看到Chat format: Generic。必要时用--chat-template-file覆盖模板下文有完整示例Generic 支持可能比模型原生格式消耗更多 token效率更低。并行/多工具调用在部分模型上受支持但默认关闭需要在 completion 请求体中显式传parallel_tool_calls: true启用。源码层面的双路由Specialized 模板与 Differential Autoparser从源码结构看common/chat.cpp 中common_chat_templates_apply处理工具调用的核心逻辑分为两条路由Specialized 模板先尝试common_chat_try_specialized_template即用仓库内置的、逐模板手写的 C 处理逻辑Llama 3.x、Qwen 2.5 的 Hermes 2 Pro 风格、DeepSeek R1 等原生格式处理器都属于这一类Differential AutoparserGeneric 路径若 specialized 处理不匹配则回退到autoparser::autoparser::analyze_template分析 Jinja 模板动态生成 PEG 解析器来解析模型输出中的工具调用对应 docs/autoparser.md 所描述的自动解析机制。模板是否支持工具并不是猜测而是通过静态分析模板得出。common/jinja/caps.h 定义了模板能力位capsstruct caps { bool supports_tools true; bool supports_tool_calls true; bool supports_system_role true; bool supports_parallel_tool_calls true; // ... bool supports_object_arguments false; // for reporting on server std::mapstd::string, bool to_map() const; };这些能力位在 src/llama.cpp 的服务端/props接口中对外报告这也是官方文档建议你在http://localhost:8080/props检查chat_template/chat_template_tool_use属性的来源。此外common/chat.cpp 中还有几处与工具调用直接相关的防御性逻辑若模板支持工具调用但不原生描述工具caps.supports_tool_calls !caps.supports_tools会打印警告提示回退行为可能产生糟糕结果建议用--verbose检查 prompt 并考虑覆盖模板若同时指定了grammar与tools且tool_choice ! none会直接抛出Cannot specify grammar with tools错误针对一些要求 tool call 消息content字段非空的模板会执行workaround::requires_non_null_content等兼容性修补。消息结构上common/chat.h 的common_chat_msg携带tool_calls每条含name/arguments/id、tool_name、tool_call_id与reasoning_content并可转换为 OpenAI 兼容 JSONto_json_oaicompat——这就是/v1/chat/completions返回体中message.tool_calls字段的来源。常见模板与格式处理器对照表下表继承自官方文档列出常见 Jinja 模板所使用的格式处理器可用于快速判断你的模型走 Native 还是 Generic 路径TemplateFormatAlmawave-Velvet-14B.jinjaHermes 2 ProAtlaAI-Selene-1-Mini-Llama-3.1-8B.jinjaLlama 3.xCohereForAI-aya-expanse-8b.jinjaGenericCohereForAI-c4ai-command-r-plus-default.jinjaGenericCohereForAI-c4ai-command-r-plus-rag.jinjaGenericCohereForAI-c4ai-command-r-plus-tool_use.jinjaGenericCohereForAI-c4ai-command-r7b-12-2024-default.jinjaCommand R7B (extract reasoning)CohereForAI-c4ai-command-r7b-12-2024-rag.jinjaCommand R7B (extract reasoning)CohereForAI-c4ai-command-r7b-12-2024-tool_use.jinjaCommand R7B (extract reasoning)CohereForAI-c4ai-command-r7b-12-2024.jinjaGenericDavieLion-Llama-3.2-1B-SPIN-iter3.jinjaGenericDelta-Vector-Rei-12B.jinjaMistral NemoEpistemeAI-Mistral-Nemo-Instruct-12B-Philosophy-Math.jinjaMistral NemoFlofloB-83k_continued_pretraining_Qwen2.5-0.5B-Instruct_Unsloth_merged_16bit.jinjaHermes 2 ProFlofloB-test_continued_pretraining_Phi-3-mini-4k-instruct_Unsloth_merged_16bit.jinjaGenericHelpingAI-HAI-SER.jinjaGenericHuggingFaceTB-SmolLM2-1.7B-Instruct.jinjaGenericHuggingFaceTB-SmolLM2-135M-Instruct.jinjaGenericHuggingFaceTB-SmolLM2-360M-Instruct.jinjaGenericINSAIT-Institute-BgGPT-Gemma-2-27B-IT-v1.0.jinjaGenericIhor-Text2Graph-R1-Qwen2.5-0.5b.jinjaHermes 2 ProInfinigence-Megrez-3B-Instruct.jinjaGenericJosephgflowers-TinyLlama_v1.1_math_code-world-test-1.jinjaGenericLGAI-EXAONE-EXAONE-3.5-2.4B-Instruct.jinjaGenericLGAI-EXAONE-EXAONE-3.5-7.8B-Instruct.jinjaGenericLatitudeGames-Wayfarer-12B.jinjaGenericMagpie-Align-Llama-3-8B-Magpie-Align-v0.1.jinjaGenericMagpie-Align-Llama-3.1-8B-Magpie-Align-v0.1.jinjaGenericMaziyarPanahi-calme-3.2-instruct-78b.jinjaGenericMiniMaxAI-MiniMax-Text-01.jinjaGenericMiniMaxAI-MiniMax-VL-01.jinjaGenericNaniDAO-deepseek-r1-qwen-2.5-32B-ablated.jinjaDeepSeek R1 (extract reasoning)NexaAIDev-Octopus-v2.jinjaGenericNousResearch-Hermes-2-Pro-Llama-3-8B-default.jinjaGenericNousResearch-Hermes-2-Pro-Llama-3-8B-tool_use.jinjaHermes 2 ProNousResearch-Hermes-2-Pro-Mistral-7B-default.jinjaGenericNousResearch-Hermes-2-Pro-Mistral-7B-tool_use.jinjaHermes 2 ProNousResearch-Hermes-3-Llama-3.1-70B-default.jinjaGenericNousResearch-Hermes-3-Llama-3.1-70B-tool_use.jinjaHermes 2 ProNovaSky-AI-Sky-T1-32B-Flash.jinjaHermes 2 ProNovaSky-AI-Sky-T1-32B-Preview.jinjaHermes 2 ProOnlyCheeini-greesychat-turbo.jinjaGenericOrenguteng-Llama-3.1-8B-Lexi-Uncensored-V2.jinjaLlama 3.xOrionStarAI-Orion-14B-Chat.jinjaGenericPowerInfer-SmallThinker-3B-Preview.jinjaGenericPrimeIntellect-INTELLECT-1-Instruct.jinjaGenericQwen-QVQ-72B-Preview.jinjaGenericQwen-QwQ-32B-Preview.jinjaHermes 2 ProQwen-Qwen1.5-7B-Chat.jinjaGenericQwen-Qwen2-7B-Instruct.jinjaGenericQwen-Qwen2-VL-72B-Instruct.jinjaGenericQwen-Qwen2-VL-7B-Instruct.jinjaGenericQwen-Qwen2.5-0.5B.jinjaHermes 2 ProQwen-Qwen2.5-1.5B-Instruct.jinjaHermes 2 ProQwen-Qwen2.5-14B-Instruct-1M.jinjaHermes 2 ProQwen-Qwen2.5-14B.jinjaHermes 2 ProQwen-Qwen2.5-32B-Instruct.jinjaHermes 2 ProQwen-Qwen2.5-32B.jinjaHermes 2 ProQwen-Qwen2.5-3B-Instruct.jinjaHermes 2 ProQwen-Qwen2.5-72B-Instruct.jinjaHermes 2 ProQwen-Qwen2.5-7B-Instruct-1M.jinjaHermes 2 ProQwen-Qwen2.5-7B-Instruct.jinjaHermes 2 ProQwen-Qwen2.5-7B.jinjaHermes 2 ProQwen-Qwen2.5-Coder-32B-Instruct.jinjaHermes 2 ProQwen-Qwen2.5-Coder-7B-Instruct.jinjaHermes 2 ProQwen-Qwen2.5-Math-1.5B.jinjaHermes 2 ProQwen-Qwen2.5-Math-7B-Instruct.jinjaHermes 2 ProQwen-Qwen2.5-VL-3B-Instruct.jinjaHermes 2 ProQwen-Qwen2.5-VL-72B-Instruct.jinjaHermes 2 ProQwen-Qwen2.5-VL-7B-Instruct.jinjaHermes 2 ProRWKV-Red-Team-ARWKV-7B-Preview-0.1.jinjaHermes 2 ProSakanaAI-TinySwallow-1.5B-Instruct.jinjaHermes 2 ProSakanaAI-TinySwallow-1.5B.jinjaHermes 2 ProSao10K-70B-L3.3-Cirrus-x1.jinjaLlama 3.xSentientAGI-Dobby-Mini-Leashed-Llama-3.1-8B.jinjaLlama 3.xSentientAGI-Dobby-Mini-Unhinged-Llama-3.1-8B.jinjaLlama 3.xSteelskull-L3.3-Damascus-R1.jinjaLlama 3.xSteelskull-L3.3-MS-Nevoria-70b.jinjaLlama 3.xSteelskull-L3.3-Nevoria-R1-70b.jinjaLlama 3.xTHUDM-glm-4-9b-chat.jinjaGenericTHUDM-glm-edge-1.5b-chat.jinjaGenericTarek07-Progenitor-V1.1-LLaMa-70B.jinjaLlama 3.xTheBloke-FusionNet_34Bx2_MoE-AWQ.jinjaGenericTinyLlama-TinyLlama-1.1B-Chat-v1.0.jinjaGenericUCLA-AGI-Mistral7B-PairRM-SPPO-Iter3.jinjaGenericValiantLabs-Llama3.1-8B-Enigma.jinjaLlama 3.xabacusai-Fewshot-Metamath-OrcaVicuna-Mistral.jinjaGenericai21labs-AI21-Jamba-1.5-Large.jinjaGenericallenai-Llama-3.1-Tulu-3-405B-SFT.jinjaGenericallenai-Llama-3.1-Tulu-3-405B.jinjaGenericallenai-Llama-3.1-Tulu-3-8B.jinjaGenericarcee-ai-Virtuoso-Lite.jinjaHermes 2 Proarcee-ai-Virtuoso-Medium-v2.jinjaHermes 2 Proarcee-ai-Virtuoso-Small-v2.jinjaHermes 2 Proavemio-GRAG-NEMO-12B-ORPO-HESSIAN-AI.jinjaGenericbespokelabs-Bespoke-Stratos-7B.jinjaHermes 2 Probfuzzy1-acheron-m1a-llama.jinjaGenericbofenghuang-vigogne-2-70b-chat.jinjaGenericbytedance-research-UI-TARS-72B-DPO.jinjaGenericbytedance-research-UI-TARS-7B-DPO.jinjaGenericbytedance-research-UI-TARS-7B-SFT.jinjaGenericcarsenk-phi3.5_mini_exp_825_uncensored.jinjaGenericcyberagent-DeepSeek-R1-Distill-Qwen-14B-Japanese.jinjaDeepSeek R1 (extract reasoning)cyberagent-DeepSeek-R1-Distill-Qwen-32B-Japanese.jinjaDeepSeek R1 (extract reasoning)databricks-dbrx-instruct.jinjaGenericdeepseek-ai-DeepSeek-Coder-V2-Instruct.jinjaGenericdeepseek-ai-DeepSeek-Coder-V2-Lite-Base.jinjaGenericdeepseek-ai-DeepSeek-Coder-V2-Lite-Instruct.jinjaGenericdeepseek-ai-DeepSeek-R1-Distill-Llama-70B.jinjaDeepSeek R1 (extract reasoning)deepseek-ai-DeepSeek-R1-Distill-Llama-8B.jinjaDeepSeek R1 (extract reasoning)deepseek-ai-DeepSeek-R1-Distill-Qwen-1.5B.jinjaDeepSeek R1 (extract reasoning)deepseek-ai-DeepSeek-R1-Distill-Qwen-14B.jinjaDeepSeek R1 (extract reasoning)deepseek-ai-DeepSeek-R1-Distill-Qwen-32B.jinjaDeepSeek R1 (extract reasoning)deepseek-ai-DeepSeek-R1-Distill-Qwen-7B.jinjaDeepSeek R1 (extract reasoning)deepseek-ai-DeepSeek-R1-Zero.jinjaDeepSeek R1 (extract reasoning)deepseek-ai-DeepSeek-R1.jinjaDeepSeek R1 (extract reasoning)deepseek-ai-DeepSeek-V2-Lite.jinjaGenericdeepseek-ai-DeepSeek-V2.5.jinjaDeepSeek R1 (extract reasoning)deepseek-ai-DeepSeek-V3.jinjaDeepSeek R1 (extract reasoning)deepseek-ai-deepseek-coder-33b-instruct.jinjaGenericdeepseek-ai-deepseek-coder-6.7b-instruct.jinjaGenericdeepseek-ai-deepseek-coder-7b-instruct-v1.5.jinjaGenericdeepseek-ai-deepseek-llm-67b-chat.jinjaGenericdeepseek-ai-deepseek-llm-7b-chat.jinjaGenericdicta-il-dictalm2.0-instruct.jinjaGenericehristoforu-Falcon3-8B-Franken-Basestruct.jinjaHermes 2 Profireworks-ai-llama-3-firefunction-v2.jinjaFireFunction v2godlikehhd-alpaca_data_sampled_ifd_new_5200.jinjaHermes 2 Progodlikehhd-alpaca_data_score_max_0.7_2600.jinjaHermes 2 Progoogle-gemma-2-27b-it.jinjaGenericgoogle-gemma-2-2b-it.jinjaGenericgoogle-gemma-2-2b-jpn-it.jinjaGenericgoogle-gemma-7b-it.jinjaGenerichuihui-ai-DeepSeek-R1-Distill-Llama-70B-abliterated.jinjaDeepSeek R1 (extract reasoning)huihui-ai-DeepSeek-R1-Distill-Llama-8B-abliterated.jinjaDeepSeek R1 (extract reasoning)huihui-ai-DeepSeek-R1-Distill-Qwen-14B-abliterated-v2.jinjaDeepSeek R1 (extract reasoning)huihui-ai-DeepSeek-R1-Distill-Qwen-32B-abliterated.jinjaDeepSeek R1 (extract reasoning)huihui-ai-DeepSeek-R1-Distill-Qwen-7B-abliterated-v2.jinjaDeepSeek R1 (extract reasoning)huihui-ai-Qwen2.5-14B-Instruct-1M-abliterated.jinjaHermes 2 Proibm-granite-granite-3.1-8b-instruct.jinjaGenericindischepartij-MiniCPM-3B-OpenHermes-2.5-v2.jinjaGenericinflatebot-MN-12B-Mag-Mell-R1.jinjaGenericjinaai-ReaderLM-v2.jinjaGenerickms7530-chemeng_qwen-math-7b_24_1_100_1_nonmath.jinjaHermes 2 Proknifeayumu-Cydonia-v1.3-Magnum-v4-22B.jinjaMistral Nemolanggptai-qwen1.5-7b-chat-sa-v0.1.jinjaGenericlightblue-DeepSeek-R1-Distill-Qwen-7B-Japanese.jinjaDeepSeek R1 (extract reasoning)mattshumer-Reflection-Llama-3.1-70B.jinjaGenericmeetkai-functionary-medium-v3.1.jinjaFunctionary v3.1 Llama 3.1meetkai-functionary-medium-v3.2.jinjaFunctionary v3.2meta-llama-Llama-2-7b-chat-hf.jinjaGenericmeta-llama-Llama-3.1-8B-Instruct.jinjaLlama 3.xmeta-llama-Llama-3.2-11B-Vision-Instruct.jinjaLlama 3.xmeta-llama-Llama-3.2-1B-Instruct.jinjaLlama 3.xmeta-llama-Llama-3.2-3B-Instruct.jinjaLlama 3.xmeta-llama-Llama-3.3-70B-Instruct.jinjaLlama 3.xmeta-llama-Meta-Llama-3-8B-Instruct.jinjaGenericmeta-llama-Meta-Llama-3.1-8B-Instruct.jinjaLlama 3.xmicrosoft-Phi-3-medium-4k-instruct.jinjaGenericmicrosoft-Phi-3-mini-4k-instruct.jinjaGenericmicrosoft-Phi-3-small-8k-instruct.jinjaGenericmicrosoft-Phi-3.5-mini-instruct.jinjaGenericmicrosoft-Phi-3.5-vision-instruct.jinjaGenericmicrosoft-phi-4.jinjaGenericmigtissera-Tess-3-Mistral-Nemo-12B.jinjaGenericministral-Ministral-3b-instruct.jinjaGenericmistralai-Codestral-22B-v0.1.jinjaGenericmistralai-Mistral-7B-Instruct-v0.1.jinjaGenericmistralai-Mistral-7B-Instruct-v0.2.jinjaGenericmistralai-Mistral-7B-Instruct-v0.3.jinjaMistral Nemomistralai-Mistral-Large-Instruct-2407.jinjaMistral Nemomistralai-Mistral-Large-Instruct-2411.jinjaGenericmistralai-Mistral-Nemo-Instruct-2407.jinjaMistral Nemomistralai-Mistral-Small-24B-Instruct-2501.jinjaGenericmistralai-Mixtral-8x7B-Instruct-v0.1.jinjaGenericmkurman-Qwen2.5-14B-DeepSeek-R1-1M.jinjaHermes 2 Promlabonne-AlphaMonarch-7B.jinjaGenericmlx-community-Josiefied-Qwen2.5-0.5B-Instruct-abliterated-v1-float32.jinjaHermes 2 Promlx-community-Qwen2.5-VL-7B-Instruct-8bit.jinjaHermes 2 Promobiuslabsgmbh-DeepSeek-R1-ReDistill-Qwen-1.5B-v1.1.jinjaDeepSeek R1 (extract reasoning)netcat420-MFANNv0.20.jinjaGenericnetcat420-MFANNv0.24.jinjaGenericnetease-youdao-Confucius-o1-14B.jinjaHermes 2 Pronvidia-AceMath-7B-RM.jinjaHermes 2 Pronvidia-Eagle2-1B.jinjaHermes 2 Pronvidia-Eagle2-9B.jinjaHermes 2 Pronvidia-Llama-3.1-Nemotron-70B-Instruct-HF.jinjaLlama 3.xonnx-community-DeepSeek-R1-Distill-Qwen-1.5B-ONNX.jinjaDeepSeek R1 (extract reasoning)open-thoughts-OpenThinker-7B.jinjaHermes 2 Proopenchat-openchat-3.5-0106.jinjaGenericpankajmathur-orca_mini_v6_8b.jinjaGenericprinceton-nlp-Mistral-7B-Base-SFT-RDPO.jinjaGenericprinceton-nlp-Mistral-7B-Instruct-DPO.jinjaGenericprinceton-nlp-Mistral-7B-Instruct-RDPO.jinjaGenericprithivMLmods-Bellatrix-Tiny-1.5B-R1.jinjaHermes 2 ProprithivMLmods-Bellatrix-Tiny-1B-R1.jinjaLlama 3.xprithivMLmods-Bellatrix-Tiny-1B-v3.jinjaGenericprithivMLmods-Bellatrix-Tiny-3B-R1.jinjaLlama 3.xprithivMLmods-Blaze-14B-xElite.jinjaGenericprithivMLmods-Calcium-Opus-14B-Elite2-R1.jinjaHermes 2 ProprithivMLmods-Calme-Ties-78B.jinjaGenericprithivMLmods-Calme-Ties2-78B.jinjaGenericprithivMLmods-Calme-Ties3-78B.jinjaGenericprithivMLmods-ChemQwen2-vL.jinjaGenericprithivMLmods-GWQ2b.jinjaGenericprithivMLmods-LatexMind-2B-Codec.jinjaGenericprithivMLmods-Llama-3.2-6B-AlgoCode.jinjaLlama 3.xprithivMLmods-Megatron-Opus-14B-Exp.jinjaHermes 2 ProprithivMLmods-Megatron-Opus-14B-Stock.jinjaHermes 2 ProprithivMLmods-Megatron-Opus-7B-Exp.jinjaHermes 2 ProprithivMLmods-Omni-Reasoner-Merged.jinjaHermes 2 ProprithivMLmods-Omni-Reasoner4-Merged.jinjaHermes 2 ProprithivMLmods-Primal-Opus-14B-Optimus-v1.jinjaHermes 2 ProprithivMLmods-QwQ-Math-IO-500M.jinjaHermes 2 ProprithivMLmods-Qwen-7B-Distill-Reasoner.jinjaDeepSeek R1 (extract reasoning)prithivMLmods-Qwen2.5-1.5B-DeepSeek-R1-Instruct.jinjaHermes 2 ProprithivMLmods-Qwen2.5-14B-DeepSeek-R1-1M.jinjaHermes 2 ProprithivMLmods-Qwen2.5-32B-DeepSeek-R1-Instruct.jinjaHermes 2 ProprithivMLmods-Qwen2.5-7B-DeepSeek-R1-1M.jinjaHermes 2 ProprithivMLmods-Triangulum-v2-10B.jinjaHermes 2 Proqingy2024-Falcon3-2x10B-MoE-Instruct.jinjaHermes 2 Prorubenroy-Zurich-14B-GCv2-5m.jinjaHermes 2 Prorubenroy-Zurich-7B-GCv2-5m.jinjaHermes 2 Prosilma-ai-SILMA-Kashif-2B-Instruct-v1.0.jinjaGenericsimplescaling-s1-32B.jinjaHermes 2 Prosometimesanotion-Lamarck-14B-v0.7.jinjaHermes 2 Prosonthenguyen-zephyr-sft-bnb-4bit-DPO-mtbr-180steps.jinjaGenericsthenno-tempesthenno-icy-0130.jinjaGenericsumink-qwft.jinjaHermes 2 Proteknium-OpenHermes-2.5-Mistral-7B.jinjaGenericthirdeyeai-elevate360m.jinjaGenerictiiuae-Falcon3-10B-Instruct.jinjaHermes 2 Prounsloth-DeepSeek-R1-Distill-Llama-8B-unsloth-bnb-4bit.jinjaDeepSeek R1 (extract reasoning)unsloth-DeepSeek-R1-Distill-Llama-8B.jinjaDeepSeek R1 (extract reasoning)unsloth-DeepSeek-R1.jinjaDeepSeek R1 (extract reasoning)unsloth-Mistral-Small-24B-Instruct-2501-unsloth-bnb-4bit.jinjaGenericupstage-solar-pro-preview-instruct.jinjaGenericwhyhow-ai-PatientSeek.jinjaGenericxwen-team-Xwen-72B-Chat.jinjaHermes 2 Proxwen-team-Xwen-7B-Chat.jinjaHermes 2 Pro注意官方文档中该表标注了生成方式的 TODO早期依赖已移除的 minja原命令为./build/bin/test-chat ../minja/build/tests/*.jinja因此表内容可能未随最新模板完全同步实际以服务端日志中的Chat format:提示与/props输出为准。启动服务模型、模板与覆盖配置函数调用要求模型使用工具感知的 Jinja 模板。启动任意模型的服务后可先到http://localhost:8080/props检查chat_template或chat_template_tool_use属性来验证模型是否具备工具调用能力。以下是官方文档给出的、确认可用的启动命令可按需替换模型# 原生支持Native support: llama-server --jinja -fa -hf bartowski/Qwen2.5-7B-Instruct-GGUF:Q4_K_M llama-server --jinja -fa -hf bartowski/Mistral-Nemo-Instruct-2407-GGUF:Q6_K_L llama-server --jinja -fa -hf bartowski/Llama-3.3-70B-Instruct-GGUF:Q4_K_M llama-server --jinja -fa -hf ibm-granite/granite-4.1-3b-GGUF:Q4_K_M # DeepSeek R1 的原生支持配合官方模板覆盖效果最好官方模板有 bugllama.cpp 内部有绕过处理 llama-server --jinja -fa -hf bartowski/DeepSeek-R1-Distill-Qwen-7B-GGUF:Q6_K_L \ --chat-template-file models/templates/llama-cpp-deepseek-r1.jinja llama-server --jinja -fa -hf bartowski/DeepSeek-R1-Distill-Qwen-32B-GGUF:Q4_K_M \ --chat-template-file models/templates/llama-cpp-deepseek-r1.jinja # 这些 GGUF 需要正确的模板才能走原生支持 llama-server --jinja -fa -hf bartowski/functionary-small-v3.2-GGUF:Q4_K_M \ --chat-template-file models/templates/meetkai-functionary-medium-v3.2.jinja llama-server --jinja -fa -hf bartowski/Hermes-2-Pro-Llama-3-8B-GGUF:Q4_K_M \ --chat-template-file models/templates/NousResearch-Hermes-2-Pro-Llama-3-8B-tool_use.jinja llama-server --jinja -fa -hf bartowski/Hermes-3-Llama-3.1-8B-GGUF:Q4_K_M \ --chat-template-file models/templates/NousResearch-Hermes-3-Llama-3.1-8B-tool_use.jinja llama-server --jinja -fa -hf bartowski/firefunction-v2-GGUF -hff firefunction-v2-IQ1_M.gguf \ --chat-template-file models/templates/fireworks-ai-llama-3-firefunction-v2.jinja llama-server --jinja -fa -hf bartowski/c4ai-command-r7b-12-2024-GGUF:Q6_K_L \ --chat-template-file models/templates/CohereForAI-c4ai-command-r7b-12-2024-tool_use.jinja # Generic 格式支持 llama-server --jinja -fa -hf bartowski/phi-4-GGUF:Q4_0 llama-server --jinja -fa -hf bartowski/gemma-2-2b-it-GGUF:Q8_0 llama-server --jinja -fa -hf bartowski/c4ai-command-r-v01-GGUF:Q2_K要点解析--jinja启用 Jinja 模板渲染路径是工具调用支持的开关-fa启用 flash attention-hf repo:quant从 HuggingFace 自动下载指定量化的 GGUF--chat-template-file当 GGUF 自带的模板缺少工具感知逻辑或官方模板有 bug时用本地 Jinja 模板文件覆盖。仓库内置了一批常用模板位于 models/templates/如 llama-cpp-deepseek-r1.jinja、NousResearch-Hermes-2-Pro-Llama-3-8B-tool_use.jinja、fireworks-ai-llama-3-firefunction-v2.jinja、CohereForAI-c4ai-command-r7b-12-2024-tool_use.jinja更多模板可参考 models/templates/README.md。模板获取与兜底策略想从原始 HuggingFace 仓库获取官方模板可使用仓库提供的 scripts/get_chat_template.py用法示例见 models/templates/README.mdTIP若没有官方tool_useJinja 模板可以设置--chat-template chatml使用对许多模型都有效的默认模板效果因模型而异或者自己编写模板——例如仓库为 DeepSeek R1 蒸馏模型提供了自定义的 models/templates/llama-cpp-deepseek-r1.jinjaCAUTION警惕极端的 KV 量化例如-ctk q4_0它会显著劣化模型的工具调用表现。curl 实战调用工具并解析响应服务启动后可用 CLI 或任何支持 OpenAI 兼容后端的库/软件进行测试。下面两个示例分别覆盖代码执行类工具和天气查询类工具curl http://localhost:8080/v1/chat/completions -d { model: gpt-3.5-turbo, tools: [ { type:function, function:{ name:python, description:Runs code in an ipython interpreter and returns the result of the execution after 60 seconds., parameters:{ type:object, properties:{ code:{ type:string, description:The code to run in the ipython interpreter. } }, required:[code] } } } ], messages: [ { role: user, content: Print a hello world message with python. } ] }curl http://localhost:8080/v1/chat/completions -d { model: gpt-3.5-turbo, messages: [ {role: system, content: You are a chatbot that uses tools/functions. Dont overthink things.}, {role: user, content: What is the weather in Istanbul?} ], tools: [{ type:function, function:{ name:get_current_weather, description:Get the current weather in a given location, parameters:{ type:object, properties:{ location:{ type:string, description:The city and country/state, e.g. San Francisco, CA, or Paris, France } }, required:[location] } } }] }当模型决定调用工具时finish_reason为toolmessage.content为nulltool_calls数组携带工具名与 JSON 字符串形式的参数。文档给出的示例响应如下{ choices: [ { finish_reason: tool, index: 0, message: { content: null, tool_calls: [ { name: python, arguments: {\code\:\ \\nprint(\\\Hello, World!\\\)\} } ], role: assistant } } ], created: 1727287211, model: gpt-3.5-turbo, object: chat.completion, usage: { completion_tokens: 16, prompt_tokens: 44, total_tokens: 60 }, id: chatcmpl-Htbgh9feMmGM0LEH2hmQvwsCxq3c6Ni8 }得到tool_calls后标准闭环是客户端在本地执行工具将结果以role: tool的消息携带tool_call_id与输出追加回messages再次请求模型得到自然语言总结。这一闭环由 tools/server/tests/unit/test_tool_call.py 做自动化验证——其中包含工具调用/不调用的参数化用例、tool_choiceauto/required/none 等组合与流式stream模式检查可作为集成测试的直接参照。parallel_tool_calls并行工具调用的开关与实现多/并行工具调用仅在部分模型模板上受支持且默认关闭。启用方式是在 completion 请求体中传parallel_tool_calls: true。从源码看这一开关的传递链是模板能力位caps.supports_parallel_tool_calls定义于 common/jinja/caps.h在模板渲染时被静态分析得出决定默认值服务端在 tools/server/server-common.cpp 中从请求体读取该字段模板能力为真时以请求值覆盖默认值该值经 common/chat.h 中common_chat_templates_inputs.parallel_tool_calls默认false传入模板渲染部分 Native 模板如 Llama 3.x 内置工具会在渲染 system 段时据此决定是否为多个工具生成占位调用。因此实际行为可以概括为请求值 模板能力若模板本身不支持并行工具调用请求中的true也不会生效。关键文件索引文件作用docs/function-calling.md本文主体依据的官方函数调用文档common/chat.h工具调用数据结构、模板输入/输出、解析参数定义common/chat.cppSpecialized 模板与 Differential Autoparser 双路由、caps 兼容处理common/jinja/caps.h模板能力位supports_tools / supports_parallel_tool_calls 等common/jinja/parser.cpp、common/jinja/runtime.cppJinja 模板词法分析、解析与渲染tools/server/server-common.cpp/v1/chat/completions请求体解析含parallel_tool_callstools/server/tests/unit/test_tool_call.py服务端工具调用端到端测试scripts/get_chat_template.py从 HuggingFace 仓库提取官方聊天模板models/templates/仓库内置的 Jinja 工具感知模板小结llama.cpp 的函数调用以llama-server --jinja为入口通过模板能力静态分析caps与Specialized 原生格式 Generic 自动解析双路由实现了对所有模型的 OpenAI 兼容工具调用原生格式Llama 3.x、Qwen 2.5、Mistral Nemo、Functionary、Hermes 2/3、Command R7B、Firefunction v2、DeepSeek R1 等更省 token、更稳定Generic 回退则保证任意带模板的模型都能调用工具。实践中建议优先核对/props中的chat_template_tool_use用--chat-template-file补齐工具感知模板避免激进 KV 量化并按需通过parallel_tool_calls开启并行调用。【免费下载链接】llama.cppLLM inference in C/C项目地址: https://gitcode.com/GitHub_Trending/ll/llama.cpp创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考