Text Generation
Transformers
Safetensors
PyTorch
English
nemotron_labs_sigma
feature-extraction
nvidia
nemotron-labs
conversational
custom_code
Instructions to use nvidia/Nemotron-Labs-Sigma-3B-instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use nvidia/Nemotron-Labs-Sigma-3B-instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="nvidia/Nemotron-Labs-Sigma-3B-instruct", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("nvidia/Nemotron-Labs-Sigma-3B-instruct", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use nvidia/Nemotron-Labs-Sigma-3B-instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "nvidia/Nemotron-Labs-Sigma-3B-instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "nvidia/Nemotron-Labs-Sigma-3B-instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/nvidia/Nemotron-Labs-Sigma-3B-instruct
- SGLang
How to use nvidia/Nemotron-Labs-Sigma-3B-instruct 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 "nvidia/Nemotron-Labs-Sigma-3B-instruct" \ --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": "nvidia/Nemotron-Labs-Sigma-3B-instruct", "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 "nvidia/Nemotron-Labs-Sigma-3B-instruct" \ --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": "nvidia/Nemotron-Labs-Sigma-3B-instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use nvidia/Nemotron-Labs-Sigma-3B-instruct with Docker Model Runner:
docker model run hf.co/nvidia/Nemotron-Labs-Sigma-3B-instruct
Fix classifier-free guidance: isolate the unconditional branch, carry the raw conditional mean as self-conditioning, keep the readout unguided; ship the SFT tokenizer (chat control tokens, EOS <|im_end|>)
Browse files- generation_config.json +2 -1
- modeling_nemotron_labs_sigma.py +37 -14
- special_tokens_map.json +1 -1
- tokenizer.json +2 -2
- tokenizer_config.json +19 -19
generation_config.json
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
{
|
| 2 |
"bos_token_id": 1,
|
| 3 |
-
"eos_token_id":
|
|
|
|
| 4 |
}
|
|
|
|
| 1 |
{
|
| 2 |
"bos_token_id": 1,
|
| 3 |
+
"eos_token_id": 11,
|
| 4 |
+
"_note": "SFT tokenizer: ids 10-17 are the chat control tokens; EOS is <|im_end|> (11), not </s> (2)."
|
| 5 |
}
|
modeling_nemotron_labs_sigma.py
CHANGED
|
@@ -444,7 +444,20 @@ class NemotronLabsSigmaModel(PreTrainedModel):
|
|
| 444 |
x_selfcond,
|
| 445 |
guidance_scale,
|
| 446 |
):
|
| 447 |
-
"""One denoiser evaluation.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
cfg = self.config
|
| 449 |
b, block, ed = z.shape
|
| 450 |
device = z.device
|
|
@@ -458,15 +471,19 @@ class NemotronLabsSigmaModel(PreTrainedModel):
|
|
| 458 |
z_var = alpha_sq / ed + sigma_sq
|
| 459 |
x_in = (z / z_var.sqrt().unsqueeze(-1)).float()
|
| 460 |
h = F.linear(x_in, self.replaid_input_linear.weight.float())
|
|
|
|
| 461 |
if cfg.replaid_selfcond and x_selfcond is not None:
|
| 462 |
h = h + F.linear(
|
| 463 |
x_selfcond.float() * math.sqrt(ed), self.replaid_selfcond_linear.weight.float()
|
| 464 |
)
|
| 465 |
if not cfg.replaid_disable_time_conditioning:
|
| 466 |
-
|
| 467 |
_gamma_embed(gamma_tok.float(), 64), self.replaid_gamma_linear.weight.float()
|
| 468 |
)
|
|
|
|
|
|
|
| 469 |
h = h.to(self._enc_dtype)
|
|
|
|
| 470 |
storage_positions = context_len + torch.arange(block, device=device)
|
| 471 |
|
| 472 |
def head(hidden, z_local):
|
|
@@ -492,12 +509,15 @@ class NemotronLabsSigmaModel(PreTrainedModel):
|
|
| 492 |
logits = head(hidden, z)
|
| 493 |
nfe = 1
|
| 494 |
|
|
|
|
|
|
|
| 495 |
w = float(guidance_scale)
|
| 496 |
if w != 1.0:
|
| 497 |
-
# Unconditional pass:
|
| 498 |
-
#
|
|
|
|
| 499 |
hidden_u = self.encoder(
|
| 500 |
-
|
| 501 |
position_ids=block_position_ids,
|
| 502 |
storage_positions=storage_positions,
|
| 503 |
attn_mask=None,
|
|
@@ -506,13 +526,11 @@ class NemotronLabsSigmaModel(PreTrainedModel):
|
|
| 506 |
)
|
| 507 |
logits_u = head(hidden_u, z)
|
| 508 |
nfe += 1
|
| 509 |
-
x_c = F.softmax(logits, dim=-1) @ embedding_matrix
|
| 510 |
x_u = F.softmax(logits_u, dim=-1) @ embedding_matrix
|
| 511 |
-
|
| 512 |
-
logits = logits_u + w * (logits - logits_u)
|
| 513 |
else:
|
| 514 |
-
|
| 515 |
-
return logits,
|
| 516 |
|
| 517 |
@staticmethod
|
| 518 |
def _edm_sigma_gamma_grid(steps, gamma_0, gamma_1, rho, device):
|
|
@@ -529,8 +547,11 @@ class NemotronLabsSigmaModel(PreTrainedModel):
|
|
| 529 |
def _log_snr_gamma_grid(steps, gamma_0, gamma_1, device):
|
| 530 |
# Linear interpolation in VP gamma (negative log-SNR), from gamma_1
|
| 531 |
# (noisy) to gamma_0 (clean); matches the internal 'log_snr' schedule.
|
|
|
|
|
|
|
| 532 |
return torch.linspace(
|
| 533 |
-
gamma_1.double(), gamma_0.double(), steps + 1,
|
|
|
|
| 534 |
)
|
| 535 |
|
| 536 |
def forward(self, input_ids=None, **kwargs):
|
|
@@ -683,7 +704,7 @@ class NemotronLabsSigmaModel(PreTrainedModel):
|
|
| 683 |
|
| 684 |
for i in range(steps + 1):
|
| 685 |
gamma_t = gamma_grid[i : i + 1]
|
| 686 |
-
logits, x_reconst, used = self._denoise_block(
|
| 687 |
z, gamma_t, cache, key_valid, block_position_ids,
|
| 688 |
embedding_matrix, x_selfcond, guidance_scale,
|
| 689 |
)
|
|
@@ -701,7 +722,9 @@ class NemotronLabsSigmaModel(PreTrainedModel):
|
|
| 701 |
generated += block
|
| 702 |
break
|
| 703 |
|
| 704 |
-
|
|
|
|
|
|
|
| 705 |
|
| 706 |
alpha_sq_t = torch.sigmoid(-gamma_t)
|
| 707 |
sigma_t = torch.sigmoid(gamma_t).sqrt()
|
|
@@ -848,7 +871,7 @@ class NemotronLabsSigmaModel(PreTrainedModel):
|
|
| 848 |
dtype=torch.float64, generator=generator)
|
| 849 |
x_selfcond = None
|
| 850 |
for i in range(diffusion_calls_per_round):
|
| 851 |
-
logits, x_reconst, _ = self._denoise_block(
|
| 852 |
z, grid[i : i + 1], cache, key_valid, block_position_ids,
|
| 853 |
embedding_matrix, x_selfcond, 1.0,
|
| 854 |
)
|
|
|
|
| 444 |
x_selfcond,
|
| 445 |
guidance_scale,
|
| 446 |
):
|
| 447 |
+
"""One denoiser evaluation.
|
| 448 |
+
|
| 449 |
+
Returns ``(cond_logits, x_guided, cond_mean, nfe)`` in fp32:
|
| 450 |
+
|
| 451 |
+
* ``cond_logits`` - the CONDITIONAL logits. Guidance shapes the embedding
|
| 452 |
+
estimate that drives the transition, never the discrete readout.
|
| 453 |
+
* ``x_guided`` - the guided embedding estimate, for the transition only.
|
| 454 |
+
* ``cond_mean`` - the raw conditional mean, which is what gets carried
|
| 455 |
+
as self-conditioning. Guidance and score temperature are extrapolations
|
| 456 |
+
and must never be fed back.
|
| 457 |
+
|
| 458 |
+
The unconditional branch sees neither the clean context nor the
|
| 459 |
+
self-conditioning signal.
|
| 460 |
+
"""
|
| 461 |
cfg = self.config
|
| 462 |
b, block, ed = z.shape
|
| 463 |
device = z.device
|
|
|
|
| 471 |
z_var = alpha_sq / ed + sigma_sq
|
| 472 |
x_in = (z / z_var.sqrt().unsqueeze(-1)).float()
|
| 473 |
h = F.linear(x_in, self.replaid_input_linear.weight.float())
|
| 474 |
+
h_uncond = h # captured BEFORE self-conditioning
|
| 475 |
if cfg.replaid_selfcond and x_selfcond is not None:
|
| 476 |
h = h + F.linear(
|
| 477 |
x_selfcond.float() * math.sqrt(ed), self.replaid_selfcond_linear.weight.float()
|
| 478 |
)
|
| 479 |
if not cfg.replaid_disable_time_conditioning:
|
| 480 |
+
t_emb = F.linear(
|
| 481 |
_gamma_embed(gamma_tok.float(), 64), self.replaid_gamma_linear.weight.float()
|
| 482 |
)
|
| 483 |
+
h = h + t_emb
|
| 484 |
+
h_uncond = h_uncond + t_emb
|
| 485 |
h = h.to(self._enc_dtype)
|
| 486 |
+
h_uncond = h_uncond.to(self._enc_dtype)
|
| 487 |
storage_positions = context_len + torch.arange(block, device=device)
|
| 488 |
|
| 489 |
def head(hidden, z_local):
|
|
|
|
| 509 |
logits = head(hidden, z)
|
| 510 |
nfe = 1
|
| 511 |
|
| 512 |
+
cond_mean = F.softmax(logits, dim=-1) @ embedding_matrix
|
| 513 |
+
|
| 514 |
w = float(guidance_scale)
|
| 515 |
if w != 1.0:
|
| 516 |
+
# Unconditional pass: no clean context (block-only, bidirectional)
|
| 517 |
+
# and no self-conditioning. RoPE is relative, so absolute offsets
|
| 518 |
+
# cancel.
|
| 519 |
hidden_u = self.encoder(
|
| 520 |
+
h_uncond,
|
| 521 |
position_ids=block_position_ids,
|
| 522 |
storage_positions=storage_positions,
|
| 523 |
attn_mask=None,
|
|
|
|
| 526 |
)
|
| 527 |
logits_u = head(hidden_u, z)
|
| 528 |
nfe += 1
|
|
|
|
| 529 |
x_u = F.softmax(logits_u, dim=-1) @ embedding_matrix
|
| 530 |
+
x_guided = x_u + w * (cond_mean - x_u)
|
|
|
|
| 531 |
else:
|
| 532 |
+
x_guided = cond_mean
|
| 533 |
+
return logits, x_guided, cond_mean, nfe
|
| 534 |
|
| 535 |
@staticmethod
|
| 536 |
def _edm_sigma_gamma_grid(steps, gamma_0, gamma_1, rho, device):
|
|
|
|
| 547 |
def _log_snr_gamma_grid(steps, gamma_0, gamma_1, device):
|
| 548 |
# Linear interpolation in VP gamma (negative log-SNR), from gamma_1
|
| 549 |
# (noisy) to gamma_0 (clean); matches the internal 'log_snr' schedule.
|
| 550 |
+
# The grid is built in fp64: linspace otherwise falls back to the default
|
| 551 |
+
# dtype and the step endpoints drift from the evaluation sampler's.
|
| 552 |
return torch.linspace(
|
| 553 |
+
gamma_1.double(), gamma_0.double(), steps + 1,
|
| 554 |
+
device=device, dtype=torch.float64,
|
| 555 |
)
|
| 556 |
|
| 557 |
def forward(self, input_ids=None, **kwargs):
|
|
|
|
| 704 |
|
| 705 |
for i in range(steps + 1):
|
| 706 |
gamma_t = gamma_grid[i : i + 1]
|
| 707 |
+
logits, x_reconst, cond_mean, used = self._denoise_block(
|
| 708 |
z, gamma_t, cache, key_valid, block_position_ids,
|
| 709 |
embedding_matrix, x_selfcond, guidance_scale,
|
| 710 |
)
|
|
|
|
| 722 |
generated += block
|
| 723 |
break
|
| 724 |
|
| 725 |
+
# Guidance and score temperature are extrapolations; the
|
| 726 |
+
# self-conditioning signal is the raw conditional mean.
|
| 727 |
+
x_selfcond = cond_mean.detach().float()
|
| 728 |
|
| 729 |
alpha_sq_t = torch.sigmoid(-gamma_t)
|
| 730 |
sigma_t = torch.sigmoid(gamma_t).sqrt()
|
|
|
|
| 871 |
dtype=torch.float64, generator=generator)
|
| 872 |
x_selfcond = None
|
| 873 |
for i in range(diffusion_calls_per_round):
|
| 874 |
+
logits, x_reconst, _cond_mean, _ = self._denoise_block(
|
| 875 |
z, grid[i : i + 1], cache, key_valid, block_position_ids,
|
| 876 |
embedding_matrix, x_selfcond, 1.0,
|
| 877 |
)
|
special_tokens_map.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
| 7 |
"single_word": false
|
| 8 |
},
|
| 9 |
"eos_token": {
|
| 10 |
-
"content": "<
|
| 11 |
"lstrip": false,
|
| 12 |
"normalized": false,
|
| 13 |
"rstrip": false,
|
|
|
|
| 7 |
"single_word": false
|
| 8 |
},
|
| 9 |
"eos_token": {
|
| 10 |
+
"content": "<|im_end|>",
|
| 11 |
"lstrip": false,
|
| 12 |
"normalized": false,
|
| 13 |
"rstrip": false,
|
tokenizer.json
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c6021eb6847e682f89aa52d5eb6e8c7d902a23acfc8137e25211cf84828f1592
|
| 3 |
+
size 17077485
|
tokenizer_config.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
{
|
| 2 |
-
"add_bos_token":
|
| 3 |
"add_eos_token": false,
|
| 4 |
"add_prefix_space": false,
|
| 5 |
"added_tokens_decoder": {
|
|
@@ -84,7 +84,7 @@
|
|
| 84 |
"special": true
|
| 85 |
},
|
| 86 |
"10": {
|
| 87 |
-
"content": "<
|
| 88 |
"lstrip": false,
|
| 89 |
"normalized": false,
|
| 90 |
"rstrip": false,
|
|
@@ -92,7 +92,7 @@
|
|
| 92 |
"special": true
|
| 93 |
},
|
| 94 |
"11": {
|
| 95 |
-
"content": "<
|
| 96 |
"lstrip": false,
|
| 97 |
"normalized": false,
|
| 98 |
"rstrip": false,
|
|
@@ -100,52 +100,52 @@
|
|
| 100 |
"special": true
|
| 101 |
},
|
| 102 |
"12": {
|
| 103 |
-
"content": "<
|
| 104 |
"lstrip": false,
|
| 105 |
"normalized": false,
|
| 106 |
"rstrip": false,
|
| 107 |
"single_word": false,
|
| 108 |
-
"special":
|
| 109 |
},
|
| 110 |
"13": {
|
| 111 |
-
"content": "<
|
| 112 |
"lstrip": false,
|
| 113 |
"normalized": false,
|
| 114 |
"rstrip": false,
|
| 115 |
"single_word": false,
|
| 116 |
-
"special":
|
| 117 |
},
|
| 118 |
"14": {
|
| 119 |
-
"content": "<
|
| 120 |
"lstrip": false,
|
| 121 |
"normalized": false,
|
| 122 |
"rstrip": false,
|
| 123 |
"single_word": false,
|
| 124 |
-
"special":
|
| 125 |
},
|
| 126 |
"15": {
|
| 127 |
-
"content": "<
|
| 128 |
"lstrip": false,
|
| 129 |
"normalized": false,
|
| 130 |
"rstrip": false,
|
| 131 |
"single_word": false,
|
| 132 |
-
"special":
|
| 133 |
},
|
| 134 |
"16": {
|
| 135 |
-
"content": "<
|
| 136 |
"lstrip": false,
|
| 137 |
"normalized": false,
|
| 138 |
"rstrip": false,
|
| 139 |
"single_word": false,
|
| 140 |
-
"special":
|
| 141 |
},
|
| 142 |
"17": {
|
| 143 |
-
"content": "<
|
| 144 |
"lstrip": false,
|
| 145 |
"normalized": false,
|
| 146 |
"rstrip": false,
|
| 147 |
"single_word": false,
|
| 148 |
-
"special":
|
| 149 |
},
|
| 150 |
"18": {
|
| 151 |
"content": "<SPECIAL_18>",
|
|
@@ -8005,15 +8005,15 @@
|
|
| 8005 |
}
|
| 8006 |
},
|
| 8007 |
"bos_token": "<s>",
|
| 8008 |
-
"chat_template": "{{'<SPECIAL_10>System'}}{% for message in messages %}{% if message['role'] == 'system' %}{{'\n' + message['content'].strip()}}{% endif %}{% endfor %}{{'\n'}}{% for message in messages %}{% if message['role'] == 'user' %}{{ '\n<SPECIAL_11>User\n' + message['content'].strip() + '\n<SPECIAL_11>Assistant\n' }}{% elif message['role'] == 'assistant' %}{{ message['content'].strip() }}{% endif %}{% endfor %}",
|
| 8009 |
"clean_up_tokenization_spaces": false,
|
| 8010 |
-
"eos_token": "<
|
| 8011 |
"extra_special_tokens": {},
|
| 8012 |
"model_input_names": [
|
| 8013 |
"input_ids",
|
| 8014 |
"attention_mask"
|
| 8015 |
],
|
| 8016 |
-
"model_max_length":
|
| 8017 |
"tokenizer_class": "PreTrainedTokenizerFast",
|
| 8018 |
-
"unk_token": "<unk>"
|
|
|
|
| 8019 |
}
|
|
|
|
| 1 |
{
|
| 2 |
+
"add_bos_token": false,
|
| 3 |
"add_eos_token": false,
|
| 4 |
"add_prefix_space": false,
|
| 5 |
"added_tokens_decoder": {
|
|
|
|
| 84 |
"special": true
|
| 85 |
},
|
| 86 |
"10": {
|
| 87 |
+
"content": "<|im_start|>",
|
| 88 |
"lstrip": false,
|
| 89 |
"normalized": false,
|
| 90 |
"rstrip": false,
|
|
|
|
| 92 |
"special": true
|
| 93 |
},
|
| 94 |
"11": {
|
| 95 |
+
"content": "<|im_end|>",
|
| 96 |
"lstrip": false,
|
| 97 |
"normalized": false,
|
| 98 |
"rstrip": false,
|
|
|
|
| 100 |
"special": true
|
| 101 |
},
|
| 102 |
"12": {
|
| 103 |
+
"content": "<think>",
|
| 104 |
"lstrip": false,
|
| 105 |
"normalized": false,
|
| 106 |
"rstrip": false,
|
| 107 |
"single_word": false,
|
| 108 |
+
"special": false
|
| 109 |
},
|
| 110 |
"13": {
|
| 111 |
+
"content": "</think>",
|
| 112 |
"lstrip": false,
|
| 113 |
"normalized": false,
|
| 114 |
"rstrip": false,
|
| 115 |
"single_word": false,
|
| 116 |
+
"special": false
|
| 117 |
},
|
| 118 |
"14": {
|
| 119 |
+
"content": "<tool_call>",
|
| 120 |
"lstrip": false,
|
| 121 |
"normalized": false,
|
| 122 |
"rstrip": false,
|
| 123 |
"single_word": false,
|
| 124 |
+
"special": false
|
| 125 |
},
|
| 126 |
"15": {
|
| 127 |
+
"content": "</tool_call>",
|
| 128 |
"lstrip": false,
|
| 129 |
"normalized": false,
|
| 130 |
"rstrip": false,
|
| 131 |
"single_word": false,
|
| 132 |
+
"special": false
|
| 133 |
},
|
| 134 |
"16": {
|
| 135 |
+
"content": "<tool_response>",
|
| 136 |
"lstrip": false,
|
| 137 |
"normalized": false,
|
| 138 |
"rstrip": false,
|
| 139 |
"single_word": false,
|
| 140 |
+
"special": false
|
| 141 |
},
|
| 142 |
"17": {
|
| 143 |
+
"content": "</tool_response>",
|
| 144 |
"lstrip": false,
|
| 145 |
"normalized": false,
|
| 146 |
"rstrip": false,
|
| 147 |
"single_word": false,
|
| 148 |
+
"special": false
|
| 149 |
},
|
| 150 |
"18": {
|
| 151 |
"content": "<SPECIAL_18>",
|
|
|
|
| 8005 |
}
|
| 8006 |
},
|
| 8007 |
"bos_token": "<s>",
|
|
|
|
| 8008 |
"clean_up_tokenization_spaces": false,
|
| 8009 |
+
"eos_token": "<|im_end|>",
|
| 8010 |
"extra_special_tokens": {},
|
| 8011 |
"model_input_names": [
|
| 8012 |
"input_ids",
|
| 8013 |
"attention_mask"
|
| 8014 |
],
|
| 8015 |
+
"model_max_length": 262144,
|
| 8016 |
"tokenizer_class": "PreTrainedTokenizerFast",
|
| 8017 |
+
"unk_token": "<unk>",
|
| 8018 |
+
"chat_template": "{% macro render_extra_keys(json_dict, handled_keys) %}\n {%- if json_dict is mapping %}\n {%- for json_key in json_dict if json_key not in handled_keys %}\n {%- if json_dict[json_key] is mapping or (json_dict[json_key] is sequence and json_dict[json_key] is not string) %}\n {{- '\\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | tojson | safe) ~ '</' ~ json_key ~ '>' }}\n {%- else %}\n {{-'\\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | string) ~ '</' ~ json_key ~ '>' }}\n {%- endif %}\n {%- endfor %}\n {%- endif %}\n{% endmacro %}\n{%- set enable_thinking = enable_thinking if enable_thinking is defined else True %}\n{%- set truncate_history_thinking = truncate_history_thinking if truncate_history_thinking is defined else True %}\n\n{%- set ns = namespace(last_user_idx = -1) %}\n{%- set loop_messages = messages %}\n{%- for m in loop_messages %}\n {%- if m[\"role\"] == \"user\" %}\n {%- set ns.last_user_idx = loop.index0 %}\n {%- endif %}\n{%- endfor %}\n\n{%- if messages[0][\"role\"] == \"system\" %}\n {%- set system_message = messages[0][\"content\"] %}\n {%- set loop_messages = messages[1:] %}\n{%- else %}\n {%- set system_message = \"\" %}\n {%- set loop_messages = messages %}\n{%- endif %}\n{%- if not tools is defined %}\n {%- set tools = [] %}\n{%- endif %}\n{# Recompute last_user_idx relative to loop_messages after handling system #}\n{%- set ns = namespace(last_user_idx = -1) %}\n{%- for m in loop_messages %}\n {%- if m[\"role\"] == \"user\" %}\n {%- set ns.last_user_idx = loop.index0 %}\n {%- endif %}\n{%- endfor %}\n{%- if system_message is defined %}\n {{- \"<|im_start|>system\\n\" + system_message }}\n{%- else %}\n {%- if tools is iterable and tools | length > 0 %}\n {{- \"<|im_start|>system\\n\" }}\n {%- endif %}\n{%- endif %}\n{%- if tools is iterable and tools | length > 0 %}\n {%- if system_message is defined and system_message | length > 0 %}\n {{- \"\\n\\n\" }}\n {%- endif %}\n {{- \"# Tools\\n\\nYou have access to the following functions:\\n\\n\" }}\n {{- \"<tools>\" }}\n {%- for tool in tools %}\n {%- if tool.function is defined %}\n {%- set tool = tool.function %}\n {%- endif %}\n {{- \"\\n<function>\\n<name>\" ~ tool.name ~ \"</name>\" }}\n {%- if tool.description is defined %}\n {{- '\\n<description>' ~ (tool.description | trim) ~ '</description>' }}\n {%- endif %}\n {{- '\\n<parameters>' }}\n {%- if tool.parameters is defined and tool.parameters is mapping and tool.parameters.properties is defined and tool.parameters.properties is mapping %}\n {%- for param_name, param_fields in tool.parameters.properties|items %}\n {{- '\\n<parameter>' }}\n {{- '\\n<name>' ~ param_name ~ '</name>' }}\n {%- if param_fields.type is defined %}\n {{- '\\n<type>' ~ (param_fields.type | string) ~ '</type>' }}\n {%- endif %}\n {%- if param_fields.description is defined %}\n {{- '\\n<description>' ~ (param_fields.description | trim) ~ '</description>' }}\n {%- endif %}\n {%- if param_fields.enum is defined %}\n {{- '\\n<enum>' ~ (param_fields.enum | tojson | safe) ~ '</enum>' }}\n {%- endif %}\n {%- set handled_keys = ['name', 'type', 'description', 'enum'] %}\n {{- render_extra_keys(param_fields, handled_keys) }}\n {{- '\\n</parameter>' }}\n {%- endfor %}\n {%- endif %}\n {% set handled_keys = ['type', 'properties', 'required'] %}\n {{- render_extra_keys(tool.parameters, handled_keys) }}\n {%- if tool.parameters is defined and tool.parameters.required is defined %}\n {{- '\\n<required>' ~ (tool.parameters.required | tojson | safe) ~ '</required>' }}\n {%- endif %}\n {{- '\\n</parameters>' }}\n {%- set handled_keys = ['type', 'name', 'description', 'parameters'] %}\n {{- render_extra_keys(tool, handled_keys) }}\n {{- '\\n</function>' }}\n {%- endfor %}\n {{- \"\\n</tools>\" }}\n\n {{- '\\n\\nIf you choose to call a function ONLY reply in the following format with NO suffix:\\n\\n<tool_call>\\n<function=example_function_name>\\n<parameter=example_parameter_1>\\nvalue_1\\n</parameter>\\n<parameter=example_parameter_2>\\nThis is the value for the second parameter\\nthat can span\\nmultiple lines\\n</parameter>\\n</function>\\n</tool_call>\\n\\n<IMPORTANT>\\nReminder:\\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\\n- Required parameters MUST be specified\\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\\n</IMPORTANT>' }}\n{%- endif %}\n\n\n{%- if system_message is defined %}\n {{- '<|im_end|>\\n' }}\n{%- else %}\n {%- if tools is iterable and tools | length > 0 %}\n {{- '<|im_end|>\\n' }}\n {%- endif %}\n{%- endif %}\n\n{%- for message in loop_messages %}\n {%- if message.role == \"assistant\" %}\n {# Add reasoning content in to content field for unified processing below. #}\n {%- if message.reasoning_content is defined and message.reasoning_content is string and message.reasoning_content | trim | length > 0 %}\n {%- set content = \"<think>\\n\" ~ message.reasoning_content ~ \"\\n</think>\\n\" ~ (message.content | default('', true)) %}\n {%- else %}\n {%- set content = message.content | default('', true) %}\n {%- if content is string -%}\n {# Allow downstream logic to to take care of broken thought, only handle coherent reasoning here. #}\n {%- if '<think>' not in content and '</think>' not in content -%}\n {%- set content = \"<think></think>\" ~ content -%}\n {%- endif -%}\n {%- else -%}\n {%- set content = content -%}\n {%- endif -%}\n {%- endif %}\n {%- if message.tool_calls is defined and message.tool_calls is iterable and message.tool_calls | length > 0 %}\n {# Assistant message has tool calls. #}\n {{- '<|im_start|>assistant\\n' }}\n {%- set include_content = not (truncate_history_thinking and loop.index0 < ns.last_user_idx) %}\n {%- if content is string and content | trim | length > 0 %}\n {%- if include_content %}\n {{- (content | trim) ~ '\\n' -}}\n {%- else %}\n {%- set c = (content | string) %}\n {%- if '</think>' in c %}\n {# Keep only content after the last closing think. Also generation prompt causes this. #}\n {%- set c = c.split('</think>')[-1] %}\n {%- elif '<think>' in c %}\n {# If <think> was opened but never closed, drop the trailing think segment #}\n {%- set c = c.split('<think>')[0] %}\n {%- endif %}\n {%- set c = \"<think></think>\" ~ c | trim %}\n {%- if c | length > 0 %}\n {{- c ~ '\\n' -}}\n {%- endif %}\n {%- endif %}\n {%- else %}\n {{- \"<think></think>\" -}}\n {%- endif %}\n {%- for tool_call in message.tool_calls %}\n {%- if tool_call.function is defined %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {{- '<tool_call>\\n<function=' ~ tool_call.name ~ '>\\n' -}}\n {%- if tool_call.arguments is defined %}\n {%- for args_name, args_value in tool_call.arguments|items %}\n {{- '<parameter=' ~ args_name ~ '>\\n' -}}\n {%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}\n {{- args_value ~ '\\n</parameter>\\n' -}}\n {%- endfor %}\n {%- endif %}\n {{- '</function>\\n</tool_call>\\n' -}}\n {%- endfor %}\n {{- '<|im_end|>\\n' }}\n {%- else %}\n {# Assistant message doesn't have tool calls. #}\n {%- if not (truncate_history_thinking and loop.index0 < ns.last_user_idx) %}\n {{- '<|im_start|>assistant\\n' ~ (content | default('', true) | string | trim) ~ '<|im_end|>\\n' }}\n {%- else %}\n {%- set c = (content | default('', true) | string) %}\n {%- if '<think>' in c and '</think>' in c %}\n {%- set c = \"<think></think>\" ~ c.split('</think>')[-1] %}\n {%- endif %}\n {%- set c = c | trim %}\n {%- if c | length > 0 %}\n {{- '<|im_start|>assistant\\n' ~ c ~ '<|im_end|>\\n' }}\n {%- else %}\n {{- '<|im_start|>assistant\\n<|im_end|>\\n' }}\n {%- endif %}\n {%- endif %}\n {%- endif %}\n {%- elif message.role == \"user\" or message.role == \"system\" %}\n {{- '<|im_start|>' + message.role + '\\n' }}\n {%- set content = message.content | string %}\n {{- content }}\n {{- '<|im_end|>\\n' }}\n {%- elif message.role == \"tool\" %}\n {%- if loop.previtem and loop.previtem.role != \"tool\" %}\n {{- '<|im_start|>user\\n' }}\n {%- endif %}\n {{- '<tool_response>\\n' }}\n {{- message.content }}\n {{- '\\n</tool_response>\\n' }}\n {%- if not loop.last and loop.nextitem.role != \"tool\" %}\n {{- '<|im_end|>\\n' }}\n {%- elif loop.last %}\n {{- '<|im_end|>\\n' }}\n {%- endif %}\n {%- else %}\n {{- '<|im_start|>' + message.role + '\\n' + message.content + '<|im_end|>\\n' }}\n {%- endif %}\n{%- endfor %}\n\n{%- if add_generation_prompt %}\n {%- if enable_thinking %}\n {{- '<|im_start|>assistant\\n<think>\\n' }}\n {%- else %}\n {{- '<|im_start|>assistant\\n<think></think>' }}\n {%- endif %}\n{%- endif %}\n"
|
| 8019 |
}
|