Text Generation
Transformers
Safetensors
English
Chinese
qwen3_5_text
Merge
ties
della
model-stock
geodella
qwen
qwen3.5
causal-lm
deltanet
linear-attention
agentic
reasoning
code
swe-bench
conversational
Instructions to use pragmaticcs/PentaCoder-9B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use pragmaticcs/PentaCoder-9B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="pragmaticcs/PentaCoder-9B") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("pragmaticcs/PentaCoder-9B") model = AutoModelForCausalLM.from_pretrained("pragmaticcs/PentaCoder-9B", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=256) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use pragmaticcs/PentaCoder-9B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "pragmaticcs/PentaCoder-9B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "pragmaticcs/PentaCoder-9B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/pragmaticcs/PentaCoder-9B
- SGLang
How to use pragmaticcs/PentaCoder-9B with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "pragmaticcs/PentaCoder-9B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "pragmaticcs/PentaCoder-9B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "pragmaticcs/PentaCoder-9B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "pragmaticcs/PentaCoder-9B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use pragmaticcs/PentaCoder-9B with Docker Model Runner:
docker model run hf.co/pragmaticcs/PentaCoder-9B
Download chat_template.jinja from pragmaticcs/PentaCoder-9B: direct link, hf CLI and curl.
- Browser
- Download file 33.1 kB
-
https://huggingface.co/pragmaticcs/PentaCoder-9B/resolve/main/chat_template.jinja
- Command line
-
hf download hf://pragmaticcs/PentaCoder-9B/chat_template.jinja
-
curl -L -o chat_template.jinja https://huggingface.co/pragmaticcs/PentaCoder-9B/resolve/main/chat_template.jinja
33.1 kB
| {%- set template_version = "qwen3.5-3.6-kat-qwopus-agentworld-ornith-merged-v3-agentic" %} | |
| {#- ========================================================================================= | |
| QWEN HYBRID AGENTIC CHAT TEMPLATE (KAT-Coder + Qwopus + AgentWorld + Ornith) | |
| Supported tool formats: | |
| - 'qwen3_coder' (default): Official XML syntax (<function=...><parameter=...>) | |
| - 'json': Official Qwen / Qwopus JSON syntax (<tool_call>{"name":...,"arguments":...}</tool_call>) | |
| Agentic Features: | |
| - Runtime protocol collision suppression (_suppress_tools / [TOOL_REQUEST]) | |
| - 3-tier consecutive failure warning escalation (anti-loop circuit breaker) | |
| - MCP / ToolCall argument normalization (.arguments, .parameters, .input) | |
| - Non-destructive JSON response truncation protection | |
| - Dynamic thinking pruning on multi-turn trajectories | |
| ========================================================================================= -#} | |
| {%- set _tool_format = tool_call_format if tool_call_format is defined else 'qwen3_coder' %} | |
| {%- set enable_thinking = (enable_thinking not in (false, 'false', 'False', 0, '0', 'off', 'none')) if enable_thinking is defined else true %} | |
| {%- set auto_disable_thinking_with_tools = (auto_disable_thinking_with_tools in (true, 'true', 'True', 1, '1', 'on')) if auto_disable_thinking_with_tools is defined else false %} | |
| {%- if preserve_reasoning is defined and preserve_reasoning is not none %} | |
| {%- set _preserve_thinking = (preserve_reasoning not in (false, 'false', 'False', 0, '0', 'off', 'none')) %} | |
| {%- elif preserve_thinking is defined and preserve_thinking is not none %} | |
| {%- set _preserve_thinking = (preserve_thinking not in (false, 'false', 'False', 0, '0', 'off', 'none')) %} | |
| {%- else %} | |
| {%- set _preserve_thinking = true %} | |
| {%- endif %} | |
| {%- set max_tool_arg_chars = (max_tool_arg_chars | int) if (max_tool_arg_chars is defined and max_tool_arg_chars is not none) else 0 %} | |
| {%- set max_tool_response_chars = (max_tool_response_chars | int) if (max_tool_response_chars is defined and max_tool_response_chars is not none) else 0 %} | |
| {%- set _has_tools = (tools is defined and tools and tools is iterable and tools is not mapping) %} | |
| {# --- Dynamic Reasoning Effort Mapping --- #} | |
| {%- set _effort_raw = (reasoning_effort | string | lower) if reasoning_effort is defined and reasoning_effort is not none else 'medium' %} | |
| {%- set _initial_thinking = enable_thinking %} | |
| {%- set _initial_effort = 'medium' %} | |
| {%- if _effort_raw in ('none', 'off') %} | |
| {%- set _initial_thinking = false %} | |
| {%- set _initial_effort = 'medium' %} | |
| {%- elif _effort_raw in ('minimal', 'low') %} | |
| {%- set _initial_effort = 'low' %} | |
| {%- elif _effort_raw in ('high', 'xhigh', 'max', 'ultracode', 'extreme', 'deep', 'long') %} | |
| {%- set _initial_thinking = true %} | |
| {%- set _initial_effort = 'xhigh' %} | |
| {%- else %} | |
| {%- set _initial_effort = 'medium' %} | |
| {%- endif %} | |
| {%- set ns_state = namespace(thinking=_initial_thinking, effort=_initial_effort) %} | |
| {%- if auto_disable_thinking_with_tools and _has_tools %} | |
| {%- set ns_state.thinking = false %} | |
| {%- endif %} | |
| {# --- In-Prompt Thinking Directive Scanning --- #} | |
| {%- for msg in messages %} | |
| {%- if msg.role == 'system' or msg.role == 'developer' or msg.role == 'user' %} | |
| {%- if msg.content is string %} | |
| {%- if '<|think_off|>' in msg.content %} | |
| {%- set ns_state.thinking = false %} | |
| {%- elif '<|think_on|>' in msg.content %} | |
| {%- set ns_state.thinking = true %} | |
| {%- elif '<|think_xhigh|>' in msg.content or '<|think_high|>' in msg.content or '<|think_ultracode|>' in msg.content or '<|think_extreme|>' in msg.content or '<|think_max|>' in msg.content or '<|think_deep|>' in msg.content or '<|think_long|>' in msg.content %} | |
| {%- set ns_state.thinking = true %} | |
| {%- set ns_state.effort = 'xhigh' %} | |
| {%- elif '<|think_low|>' in msg.content or '<|think_minimal|>' in msg.content %} | |
| {%- set ns_state.thinking = true %} | |
| {%- set ns_state.effort = 'low' %} | |
| {%- elif '<|think_medium|>' in msg.content %} | |
| {%- set ns_state.thinking = true %} | |
| {%- set ns_state.effort = 'medium' %} | |
| {%- endif %} | |
| {%- elif msg.content is iterable and msg.content is not mapping %} | |
| {%- for item in msg.content %} | |
| {%- if item is string %} | |
| {%- set _item_text = item %} | |
| {%- elif item is mapping and 'text' in item and item.text is string %} | |
| {%- set _item_text = item.text %} | |
| {%- elif item is mapping and 'content' in item and item.content is string %} | |
| {%- set _item_text = item.content %} | |
| {%- else %} | |
| {%- set _item_text = '' %} | |
| {%- endif %} | |
| {%- if _item_text %} | |
| {%- if '<|think_off|>' in _item_text %} | |
| {%- set ns_state.thinking = false %} | |
| {%- elif '<|think_on|>' in _item_text %} | |
| {%- set ns_state.thinking = true %} | |
| {%- elif '<|think_xhigh|>' in _item_text or '<|think_high|>' in _item_text or '<|think_ultracode|>' in _item_text or '<|think_extreme|>' in _item_text or '<|think_max|>' in _item_text or '<|think_deep|>' in _item_text or '<|think_long|>' in _item_text %} | |
| {%- set ns_state.thinking = true %} | |
| {%- set ns_state.effort = 'xhigh' %} | |
| {%- elif '<|think_low|>' in _item_text or '<|think_minimal|>' in _item_text %} | |
| {%- set ns_state.thinking = true %} | |
| {%- set ns_state.effort = 'low' %} | |
| {%- elif '<|think_medium|>' in _item_text %} | |
| {%- set ns_state.thinking = true %} | |
| {%- set ns_state.effort = 'medium' %} | |
| {%- endif %} | |
| {%- endif %} | |
| {%- endfor %} | |
| {%- endif %} | |
| {%- endif %} | |
| {%- endfor %} | |
| {%- set reasoning_instructions = '' %} | |
| {%- if ns_state.thinking %} | |
| {%- if ns_state.effort == 'xhigh' %} | |
| {%- set reasoning_instructions = 'Reasoning effort is set to xhigh. Please think rigorously through the task, explore alternative hypotheses, validate edge cases, and prioritize architectural soundness, consistency, and correctness in the final answer.' %} | |
| {%- elif ns_state.effort == 'low' %} | |
| {%- set reasoning_instructions = 'Reasoning effort is set to low. Keep your thinking concise, focused, and move directly to execution or tool calls without unnecessary elaboration.' %} | |
| {%- endif %} | |
| {%- endif %} | |
| {# --- Content Render Macro (Enforces Pure-Text CausalLM) --- #} | |
| {%- macro render_content(content, is_system_content=false) %} | |
| {%- if content is string %} | |
| {{- content }} | |
| {%- elif content is iterable and content is not mapping %} | |
| {%- for item in content %} | |
| {%- if item is mapping %} | |
| {%- if item.type == 'image' or 'image' in item or 'image_url' in item %} | |
| {{- raise_exception('This model is a pure text CausalLM (vision tower removed). Image inputs are not supported.') }} | |
| {%- elif item.type == 'video' or 'video' in item %} | |
| {{- raise_exception('This model is a pure text CausalLM (vision tower removed). Video inputs are not supported.') }} | |
| {%- elif 'text' in item %} | |
| {{- (item.text | string) if item.text is not none else '' }} | |
| {%- elif 'content' in item and item.content is string %} | |
| {{- item.content }} | |
| {%- else %} | |
| {{- item | tojson }} | |
| {%- endif %} | |
| {%- else %} | |
| {{- item | string }} | |
| {%- endif %} | |
| {%- endfor %} | |
| {%- elif content is none or content is undefined %} | |
| {{- '' }} | |
| {%- else %} | |
| {{- raise_exception('Unexpected content type.') }} | |
| {%- endif %} | |
| {%- endmacro %} | |
| {%- if not messages %} | |
| {{- raise_exception('No messages provided.') }} | |
| {%- endif %} | |
| {# --- Head Extraction (System/Developer Messages) --- #} | |
| {%- set head = namespace(count=0, seen_non_system=false) %} | |
| {%- for message in messages %} | |
| {%- set _is_sys = (message.role == 'system' or message.role == 'developer') %} | |
| {%- if _is_sys and not head.seen_non_system %} | |
| {%- set head.count = head.count + 1 %} | |
| {%- else %} | |
| {%- set head.seen_non_system = true %} | |
| {%- endif %} | |
| {%- endfor %} | |
| {%- set sys_state = namespace(content='') %} | |
| {%- for message in messages[:head.count] %} | |
| {%- set _part = render_content(message.content, true) | trim %} | |
| {%- if '<|think_off|>' in _part %}{%- set _part = _part.split('<|think_off|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_on|>' in _part %}{%- set _part = _part.split('<|think_on|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_xhigh|>' in _part %}{%- set _part = _part.split('<|think_xhigh|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_high|>' in _part %}{%- set _part = _part.split('<|think_high|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_ultracode|>' in _part %}{%- set _part = _part.split('<|think_ultracode|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_extreme|>' in _part %}{%- set _part = _part.split('<|think_extreme|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_max|>' in _part %}{%- set _part = _part.split('<|think_max|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_deep|>' in _part %}{%- set _part = _part.split('<|think_deep|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_long|>' in _part %}{%- set _part = _part.split('<|think_long|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_medium|>' in _part %}{%- set _part = _part.split('<|think_medium|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_low|>' in _part %}{%- set _part = _part.split('<|think_low|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_minimal|>' in _part %}{%- set _part = _part.split('<|think_minimal|>') | join('') | trim %}{%- endif %} | |
| {%- if _part %} | |
| {%- if sys_state.content %} | |
| {%- set sys_state.content = sys_state.content ~ '\n\n' ~ _part %} | |
| {%- else %} | |
| {%- set sys_state.content = _part %} | |
| {%- endif %} | |
| {%- endif %} | |
| {%- endfor %} | |
| {%- set _sc = sys_state.content %} | |
| {%- set _msgs = messages[head.count:] %} | |
| {# --- Optional Terse Directive --- #} | |
| {%- set _terse_on = (terse in (true, 'true', 'True', 1, '1', 'on')) if terse is defined else false %} | |
| {%- if _terse_on %} | |
| {%- if ns_state.thinking %} | |
| {%- set _terse_lead = 'Answer directly, after thinking. Lead with the answer, then only what it needs to be correct and usable.' %} | |
| {%- else %} | |
| {%- set _terse_lead = 'Answer directly and concisely. Give the answer with only what it needs to be correct and usable.' %} | |
| {%- endif %} | |
| {%- set _terse_core %} | |
| Never: open with preamble or pleasantries; restate the question; add filler transitions; hedge with niceties; or repeat a point you have already made. | |
| Always: keep essential steps, caveats, uncertainties, and specifics — never drop correctness or a needed warning for brevity. Keep the final answer lean. Use the least structure that conveys it (plain prose when short; lists or code only when they earn their place). If genuinely uncertain, say so and explain why — never omit uncertainty for the sake of brevity. | |
| If a user request is genuinely ambiguous, ask a sharp question, do not guess. | |
| {%- endset %} | |
| {%- set _terse = _terse_lead ~ '\n' ~ (_terse_core | trim) %} | |
| {%- if not _sc %} | |
| {%- set _sc = _terse | trim %} | |
| {%- else %} | |
| {%- set _sc = (_sc | trim) ~ '\n\n' ~ (_terse | trim) %} | |
| {%- endif %} | |
| {%- endif %} | |
| {# --- Frontend Runtime Protocol Collision Detection (Borrowed from Donor 1) --- #} | |
| {%- set _runtime_tool_protocol = (_sc is defined and _sc and ('[TOOL_REQUEST]' in _sc or '[AVAILABLE_TOOLS]' in _sc or '<tool_protocol>' in _sc)) %} | |
| {%- set _suppress_tools = (suppress_tool_instructions in (true, 'true', 'True', 1, '1', 'on')) if suppress_tool_instructions is defined else _runtime_tool_protocol %} | |
| {# --- Tool System Prompt Injection --- #} | |
| {%- if _has_tools and not _suppress_tools %} | |
| {{- '<|im_start|>system\n' }} | |
| {%- if reasoning_instructions %} | |
| {{- reasoning_instructions + '\n\n' }} | |
| {%- endif %} | |
| {{- '# Tools\n\nYou have access to the following functions:\n\n<tools>' }} | |
| {%- for tool in tools %} | |
| {{- '\n' }} | |
| {{- tool | tojson }} | |
| {%- endfor %} | |
| {{- '\n</tools>' }} | |
| {%- if _tool_format == 'json' %} | |
| {#- Canonical Qwen / Qwopus JSON invocation format (Borrowed from Donor 2) -#} | |
| {{- '\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n' }} | |
| {%- if ns_state.thinking %} | |
| {{- '<think>\nBrief explanation of next step or parameter rationale\n</think>\n' }} | |
| {%- endif %} | |
| {{- '<tool_call>\n{"name": "example_function_name", "arguments": {"example_parameter_1": "value_1", "example_parameter_2": "value_2"}}\n</tool_call>\n\n<IMPORTANT>\n' }} | |
| {%- if ns_state.thinking %} | |
| {{- '- All planning, hypothesis testing, and error reflection MUST be placed strictly inside the <think></think> block.\n' }} | |
| {{- '- Output the <tool_call> block IMMEDIATELY after thinking with NO conversational text before it.\n' }} | |
| {%- else %} | |
| {{- '- Output the <tool_call> block IMMEDIATELY with NO conversational text before it.\n' }} | |
| {%- endif %} | |
| {{- '- The <tool_call> tag MUST be at the very beginning of a new line, with NO indentation.\n' }} | |
| {{- '- For multiple calls in parallel, output a separate <tool_call></tool_call> block for EACH function. Do NOT nest calls.\n' }} | |
| {{- '- Keep tool calls purposeful: issue only the calls required for the immediate next step.\n' }} | |
| {{- '- Once all necessary data is gathered, provide your final answer directly without calling further tools.\n</IMPORTANT>' }} | |
| {%- else %} | |
| {#- Official Qwen3-Coder XML syntax (<function=...><parameter=...>) -#} | |
| {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }} | |
| {%- if ns_state.thinking %} | |
| {{- '<think>\nBrief explanation of next step or parameter rationale\n</think>\n' }} | |
| {%- endif %} | |
| {{- '<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nvalue_2\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\n' }} | |
| {%- if ns_state.thinking %} | |
| {{- '- All planning, hypothesis testing, and error reflection MUST be placed strictly inside the <think></think> block.\n' }} | |
| {{- '- Output the <tool_call> block IMMEDIATELY after thinking with NO conversational text before it.\n' }} | |
| {%- else %} | |
| {{- '- Output the <tool_call> block IMMEDIATELY with NO conversational text before it.\n' }} | |
| {%- endif %} | |
| {{- '- Function calls MUST follow the specified format: an inner <function=...></function> block nested within <tool_call></tool_call> tags.\n' }} | |
| {{- '- The <tool_call> and <function> tags MUST be at the very beginning of a new line, with NO spaces or indentation before them.\n' }} | |
| {{- '- For multiple calls, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest blocks.\n' }} | |
| {{- '- Keep tool calls purposeful: issue only the calls required for the immediate next step.\n' }} | |
| {{- '- Once all necessary data is gathered, provide your final answer directly without calling further tools.\n</IMPORTANT>' }} | |
| {%- endif %} | |
| {%- if _sc %} | |
| {{- '\n\n' + _sc }} | |
| {%- endif %} | |
| {{- '<|im_end|>\n' }} | |
| {%- else %} | |
| {%- if _sc %} | |
| {{- '<|im_start|>system\n' + (reasoning_instructions + '\n\n' if reasoning_instructions else '') + _sc + '<|im_end|>\n' }} | |
| {%- elif reasoning_instructions %} | |
| {{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' }} | |
| {%- endif %} | |
| {%- endif %} | |
| {# --- Multi-Turn Trajectory Analysis (Scanning for Root User Query) --- #} | |
| {%- set ns = namespace(found_query=false, last_query_index=0) %} | |
| {%- for message in _msgs[::-1] %} | |
| {%- set index = (_msgs | length - 1) - loop.index0 %} | |
| {%- if not ns.found_query and message.role == 'user' %} | |
| {%- set _rc = render_content(message.content) | trim %} | |
| {%- if not (_rc.startswith('<tool_response>') and _rc.endswith('</tool_response>')) %} | |
| {%- set ns.found_query = true %} | |
| {%- set ns.last_query_index = index %} | |
| {%- endif %} | |
| {%- endif %} | |
| {%- endfor %} | |
| {# Fallback when entire context consists of autonomous tool loops (Borrowed from Donor 1) #} | |
| {%- if not ns.found_query and _msgs | length > 0 %} | |
| {%- set ns.last_query_index = (_msgs | length - 1) %} | |
| {%- endif %} | |
| {# --- Conversation Render Loop --- #} | |
| {%- set ns2 = namespace(prev_role='', consecutive_failures=0) %} | |
| {%- for message in _msgs %} | |
| {%- set is_system = (message.role == "system" or message.role == "developer") %} | |
| {%- set content = render_content(message.content, is_system) | trim %} | |
| {%- if is_system or message.role == 'user' %} | |
| {%- if '<|think_off|>' in content %}{%- set content = content.split('<|think_off|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_on|>' in content %}{%- set content = content.split('<|think_on|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_xhigh|>' in content %}{%- set content = content.split('<|think_xhigh|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_high|>' in content %}{%- set content = content.split('<|think_high|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_ultracode|>' in content %}{%- set content = content.split('<|think_ultracode|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_extreme|>' in content %}{%- set content = content.split('<|think_extreme|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_max|>' in content %}{%- set content = content.split('<|think_max|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_deep|>' in content %}{%- set content = content.split('<|think_deep|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_long|>' in content %}{%- set content = content.split('<|think_long|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_medium|>' in content %}{%- set content = content.split('<|think_medium|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_low|>' in content %}{%- set content = content.split('<|think_low|>') | join('') | trim %}{%- endif %} | |
| {%- if '<|think_minimal|>' in content %}{%- set content = content.split('<|think_minimal|>') | join('') | trim %}{%- endif %} | |
| {%- endif %} | |
| {%- if is_system %} | |
| {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }} | |
| {%- elif message.role == 'user' %} | |
| {%- set ns2.consecutive_failures = 0 %} | |
| {{- '<|im_start|>user\n' + content + '<|im_end|>\n' }} | |
| {%- elif message.role == 'assistant' %} | |
| {%- set reasoning_content = '' %} | |
| {%- set _explicit_reasoning = '' %} | |
| {%- if message.reasoning_content is defined and message.reasoning_content is not none %} | |
| {%- if message.reasoning_content is string %} | |
| {%- set _explicit_reasoning = message.reasoning_content %} | |
| {%- elif message.reasoning_content is mapping and 'text' in message.reasoning_content %} | |
| {%- set _explicit_reasoning = message.reasoning_content.text | string %} | |
| {%- else %} | |
| {%- set _explicit_reasoning = message.reasoning_content | string %} | |
| {%- endif %} | |
| {%- elif message.thinking is defined and message.thinking is not none %} | |
| {%- if message.thinking is string %} | |
| {%- set _explicit_reasoning = message.thinking %} | |
| {%- elif message.thinking is mapping and 'text' in message.thinking %} | |
| {%- set _explicit_reasoning = message.thinking.text | string %} | |
| {%- else %} | |
| {%- set _explicit_reasoning = message.thinking | string %} | |
| {%- endif %} | |
| {%- elif message.reasoning is defined and message.reasoning is not none %} | |
| {%- if message.reasoning is string %} | |
| {%- set _explicit_reasoning = message.reasoning %} | |
| {%- elif message.reasoning is mapping and 'text' in message.reasoning %} | |
| {%- set _explicit_reasoning = message.reasoning.text | string %} | |
| {%- else %} | |
| {%- set _explicit_reasoning = message.reasoning | string %} | |
| {%- endif %} | |
| {%- endif %} | |
| {%- if _explicit_reasoning %} | |
| {%- set _lead_end = '' %} | |
| {%- if '</think>' in content %} | |
| {%- set _lead_end = '</think>' %} | |
| {%- elif '</thinking>' in content %} | |
| {%- set _lead_end = '</thinking>' %} | |
| {%- endif %} | |
| {%- if _lead_end %} | |
| {%- set _parts = content.split(_lead_end) %} | |
| {%- set content = (_parts[1:] | join(_lead_end)).lstrip('\n') %} | |
| {%- endif %} | |
| {%- set reasoning_content = _explicit_reasoning %} | |
| {%- else %} | |
| {%- set _think_end = '' %} | |
| {%- if '</think>' in content %} | |
| {%- set _think_end = '</think>' %} | |
| {%- elif '</thinking>' in content %} | |
| {%- set _think_end = '</thinking>' %} | |
| {%- endif %} | |
| {%- if _think_end %} | |
| {%- set _think_start = '<thinking>' if 'thinking' in _think_end else '<think>' %} | |
| {%- set _parts = content.split(_think_end) %} | |
| {%- set reasoning_content = _parts[0].rstrip('\n') %} | |
| {%- if _think_start in reasoning_content %} | |
| {%- set reasoning_content = reasoning_content.split(_think_start)[-1].lstrip('\n') %} | |
| {%- endif %} | |
| {%- set content = (_parts[1:] | join(_think_end)).lstrip('\n') %} | |
| {%- endif %} | |
| {%- endif %} | |
| {%- set reasoning_content = reasoning_content | trim %} | |
| {# Emit Assistant Thought and Content #} | |
| {%- if reasoning_content and (_preserve_thinking or loop.index0 >= ns.last_query_index) %} | |
| {{- '<|im_start|>assistant\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }} | |
| {%- else %} | |
| {{- '<|im_start|>assistant\n' + content }} | |
| {%- endif %} | |
| {# Emit Tool Calls (Guarded against double emission if <tool_call> is already in content) #} | |
| {%- if message.tool_calls is defined and message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping and '<tool_call>' not in content %} | |
| {%- for tool_call in message.tool_calls %} | |
| {%- if tool_call.function is defined and tool_call.function is not none %} | |
| {%- set tc = tool_call.function %} | |
| {%- else %} | |
| {%- set tc = tool_call %} | |
| {%- endif %} | |
| {%- set tc_name = tc.name if (tc.name is defined and tc.name is not none) else '' %} | |
| {# Resolve Arguments across OpenAI, Anthropic/Claude, and MCP schemas (.arguments, .parameters, .input) #} | |
| {%- set tc_args_raw = none %} | |
| {%- if tc.arguments is defined and tc.arguments is not none %} | |
| {%- set tc_args_raw = tc.arguments %} | |
| {%- elif tc.parameters is defined and tc.parameters is not none %} | |
| {%- set tc_args_raw = tc.parameters %} | |
| {%- elif tc.input is defined and tc.input is not none %} | |
| {%- set tc_args_raw = tc.input %} | |
| {%- endif %} | |
| {%- if _tool_format == 'json' %} | |
| {%- if loop.first %} | |
| {%- if content | trim %} | |
| {{- '\n\n' }} | |
| {%- endif %} | |
| {%- else %} | |
| {{- '\n' }} | |
| {%- endif %} | |
| {%- set _args = '{}' %} | |
| {%- if tc_args_raw is not none %} | |
| {%- if tc_args_raw is mapping %} | |
| {%- set _args = tc_args_raw | tojson %} | |
| {%- elif tc_args_raw is string %} | |
| {%- set _trimmed_args = tc_args_raw | trim %} | |
| {%- if _trimmed_args | length > 0 %} | |
| {%- if _trimmed_args | first == '{' and _trimmed_args | last == '}' %} | |
| {%- set _args = _trimmed_args %} | |
| {%- else %} | |
| {%- set _args = tc_args_raw | tojson %} | |
| {%- endif %} | |
| {%- endif %} | |
| {%- else %} | |
| {%- set _args = tc_args_raw | tojson %} | |
| {%- endif %} | |
| {%- endif %} | |
| {{- '<tool_call>\n{"name": ' }}{{- tc_name | tojson }}{{- ', "arguments": ' }}{{- _args }}{{- '}\n</tool_call>' }} | |
| {%- else %} | |
| {%- if loop.first %} | |
| {%- if content | trim %} | |
| {{- '\n\n<tool_call>\n<function=' + tc_name + '>\n' }} | |
| {%- else %} | |
| {{- '<tool_call>\n<function=' + tc_name + '>\n' }} | |
| {%- endif %} | |
| {%- else %} | |
| {{- '\n<tool_call>\n<function=' + tc_name + '>\n' }} | |
| {%- endif %} | |
| {%- if tc_args_raw is not none %} | |
| {%- if tc_args_raw is mapping %} | |
| {%- for args_name, args_value in tc_args_raw.items() %} | |
| {{- '<parameter=' + args_name + '>\n' }} | |
| {%- if args_value is string %} | |
| {%- set _av = args_value %} | |
| {%- else %} | |
| {%- set _av = args_value | tojson %} | |
| {%- endif %} | |
| {%- if max_tool_arg_chars > 0 and _av | length > max_tool_arg_chars %} | |
| {{- _av[:max_tool_arg_chars] + '\n[TRUNCATED - original length ' ~ (_av | length | string) ~ ' chars]' }} | |
| {%- else %} | |
| {{- _av }} | |
| {%- endif %} | |
| {{- '\n</parameter>\n' }} | |
| {%- endfor %} | |
| {%- else %} | |
| {%- if tc_args_raw is string %} | |
| {%- set _raw_args = tc_args_raw %} | |
| {%- else %} | |
| {%- set _raw_args = tc_args_raw | tojson %} | |
| {%- endif %} | |
| {%- if _raw_args %} | |
| {%- if max_tool_arg_chars > 0 and _raw_args | length > max_tool_arg_chars %} | |
| {{- _raw_args[:max_tool_arg_chars] + '\n[TRUNCATED - original length ' ~ (_raw_args | length | string) ~ ' chars]' }} | |
| {%- else %} | |
| {{- _raw_args }} | |
| {%- endif %} | |
| {%- endif %} | |
| {%- endif %} | |
| {%- endif %} | |
| {{- '</function>\n</tool_call>' }} | |
| {%- endif %} | |
| {%- endfor %} | |
| {%- endif %} | |
| {{- '<|im_end|>\n' }} | |
| {%- elif message.role == 'tool' %} | |
| {# --- Multi-Turn Tool Response & Escalating Circuit Breaker --- #} | |
| {%- set _content_lower = content | lower %} | |
| {%- set _content_head = _content_lower[:150] %} | |
| {%- set _is_code_or_grep = ('throw new ' in _content_lower or 'throw error' in _content_lower or 'console.error' in _content_lower or 'logger.error' in _content_lower or 'logging.error' in _content_lower or 'import ' in _content_head or 'def ' in _content_head or 'function ' in _content_head) %} | |
| {%- set _exit_code_zero = ('exit code: 0' in _content_head or 'process exited with code 0' in _content_head) %} | |
| {%- set _error_field_ok = ('"error": null' in _content_head or '"error":null' in _content_head or '"error": false' in _content_head or '"error":false' in _content_head or '"error": ""' in _content_head or '"error":""' in _content_head) %} | |
| {%- set _strong_error = (('"error":' in _content_head and not _error_field_ok) or '"status": "error"' in _content_head or '"status":"error"' in _content_head or '"success": false' in _content_head or '"success":false' in _content_head or 'traceback (most recent call last):' in _content_head or 'command not found' in _content_head or 'invalid syntax' in _content_head or 'fatal:' in _content_head or 'permission denied' in _content_head or 'no such file or directory' in _content_head or '404 not found' in _content_head or 'rate limit' in _content_head or 'connection refused' in _content_head or 'timed out' in _content_head or (('exit code: ' in _content_head or 'process exited with code' in _content_head) and not _exit_code_zero) or _content_head.startswith('exception:') or _content_head.startswith('failed to ')) %} | |
| {%- set _weak_error = ('error:' in _content_head or 'err!' in _content_head) %} | |
| {%- set _weak_suppressed = ('$ ' in _content_head or 'took ' in _content_head or content | length >= 600) %} | |
| {%- if not _is_code_or_grep and (_strong_error or (_weak_error and not _weak_suppressed)) %} | |
| {%- set ns2.consecutive_failures = ns2.consecutive_failures + 1 %} | |
| {%- else %} | |
| {%- set ns2.consecutive_failures = 0 %} | |
| {%- endif %} | |
| {%- if ns2.prev_role != 'tool' %} | |
| {{- '<|im_start|>user' }} | |
| {%- endif %} | |
| {# Truncate non-JSON outputs safely, alert if JSON was clipped (Borrowed from Donor 1) #} | |
| {%- set _is_json_payload = (content | trim | length > 0 and (content | trim)[:1] in ('{', '[')) %} | |
| {%- if max_tool_response_chars > 0 and content | length > max_tool_response_chars %} | |
| {%- if _is_json_payload %} | |
| {%- set content = content[:max_tool_response_chars] + '\n[TRUNCATED: Output exceeded ' ~ max_tool_response_chars ~ ' chars. Remaining JSON payload clipped]' %} | |
| {%- else %} | |
| {%- set content = content[:max_tool_response_chars] + '\n[TRUNCATED - original length ' ~ (content | length | string) ~ ' chars]' %} | |
| {%- endif %} | |
| {%- endif %} | |
| {{- '\n<tool_response>\n' + content }} | |
| {# 3-Tier Failure Escalation (Anti-Loop Circuit Breaker) #} | |
| {%- if ns2.consecutive_failures >= 3 %} | |
| {{- '\n\n⚠️ SYSTEM WARNING: ' ~ ns2.consecutive_failures ~ ' consecutive tool errors detected. CRITICAL AGENTIC LOOP. Do NOT repeat the failed command or variations of it. If a dependency, tool, or path is missing, immediately PIVOT to an alternative diagnostic tool or report the blocker directly.' }} | |
| {%- elif ns2.consecutive_failures == 2 %} | |
| {{- '\n\n⚠️ SYSTEM WARNING: 2 consecutive tool errors detected. Your previous technical approach is fundamentally incorrect. You MUST diagnose root cause and use a completely different approach or command.' }} | |
| {%- elif ns2.consecutive_failures == 1 %} | |
| {{- '\n\n⚠️ SYSTEM WARNING: The previous tool call returned an error. Diagnose the failure and retry with corrected arguments.' }} | |
| {%- endif %} | |
| {{- '\n</tool_response>' }} | |
| {%- if loop.last %} | |
| {{- '<|im_end|>\n' }} | |
| {%- else %} | |
| {%- set _next_role = _msgs[loop.index0 + 1].role %} | |
| {%- if _next_role != 'tool' %} | |
| {{- '<|im_end|>\n' }} | |
| {%- endif %} | |
| {%- endif %} | |
| {%- else %} | |
| {{- '<|im_start|>user\n[' + message.role + ']: ' + content + '<|im_end|>\n' }} | |
| {%- endif %} | |
| {%- set ns2.prev_role = message.role %} | |
| {%- endfor %} | |
| {# --- Generation Prompt --- #} | |
| {%- set _add_gen = (add_generation_prompt in (true, 'true', 'True', 1, '1', 'on')) if add_generation_prompt is defined else false %} | |
| {%- if _add_gen %} | |
| {{- '<|im_start|>assistant\n' }} | |
| {%- if not ns_state.thinking %} | |
| {{- '<think>\n\n</think>\n\n' }} | |
| {%- else %} | |
| {{- '<think>\n' }} | |
| {%- endif %} | |
| {%- endif %} |