初始化项目,由ModelHub XC社区提供模型

Model: unsloth/granite-4.0-micro-GGUF
Source: Original Platform
This commit is contained in:
ModelHub XC
2026-05-07 10:37:46 +08:00
commit a4e1041448
31 changed files with 958 additions and 0 deletions

62
.gitattributes vendored Normal file
View File

@@ -0,0 +1,62 @@
*.7z filter=lfs diff=lfs merge=lfs -text
*.arrow filter=lfs diff=lfs merge=lfs -text
*.bin filter=lfs diff=lfs merge=lfs -text
*.bz2 filter=lfs diff=lfs merge=lfs -text
*.ckpt filter=lfs diff=lfs merge=lfs -text
*.ftz filter=lfs diff=lfs merge=lfs -text
*.gz filter=lfs diff=lfs merge=lfs -text
*.h5 filter=lfs diff=lfs merge=lfs -text
*.joblib filter=lfs diff=lfs merge=lfs -text
*.lfs.* filter=lfs diff=lfs merge=lfs -text
*.mlmodel filter=lfs diff=lfs merge=lfs -text
*.model filter=lfs diff=lfs merge=lfs -text
*.msgpack filter=lfs diff=lfs merge=lfs -text
*.npy filter=lfs diff=lfs merge=lfs -text
*.npz filter=lfs diff=lfs merge=lfs -text
*.onnx filter=lfs diff=lfs merge=lfs -text
*.ot filter=lfs diff=lfs merge=lfs -text
*.parquet filter=lfs diff=lfs merge=lfs -text
*.pb filter=lfs diff=lfs merge=lfs -text
*.pickle filter=lfs diff=lfs merge=lfs -text
*.pkl filter=lfs diff=lfs merge=lfs -text
*.pt filter=lfs diff=lfs merge=lfs -text
*.pth filter=lfs diff=lfs merge=lfs -text
*.rar filter=lfs diff=lfs merge=lfs -text
*.safetensors filter=lfs diff=lfs merge=lfs -text
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
*.tar.* filter=lfs diff=lfs merge=lfs -text
*.tar filter=lfs diff=lfs merge=lfs -text
*.tflite filter=lfs diff=lfs merge=lfs -text
*.tgz filter=lfs diff=lfs merge=lfs -text
*.wasm filter=lfs diff=lfs merge=lfs -text
*.xz filter=lfs diff=lfs merge=lfs -text
*.zip filter=lfs diff=lfs merge=lfs -text
*.zst filter=lfs diff=lfs merge=lfs -text
*tfevents* filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-UD-IQ1_S.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-UD-Q3_K_XL.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-UD-Q2_K_XL.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-UD-IQ1_M.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-Q4_K_M.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-UD-IQ2_XXS.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-Q3_K_M.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-UD-IQ3_XXS.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-Q2_K.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-Q6_K.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-Q2_K_L.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-Q5_K_M.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-Q4_0.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-Q4_1.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-IQ4_NL.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-Q3_K_S.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-Q4_K_S.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-UD-Q6_K_XL.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-Q5_K_S.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-Q8_0.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-UD-Q8_K_XL.gguf filter=lfs diff=lfs merge=lfs -text
imatrix_unsloth.gguf_file filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-BF16.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-UD-Q5_K_XL.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-UD-Q4_K_XL.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-UD-IQ2_M.gguf filter=lfs diff=lfs merge=lfs -text
granite-4.0-micro-IQ4_XS.gguf filter=lfs diff=lfs merge=lfs -text

650
README.md Normal file
View File

@@ -0,0 +1,650 @@
---
base_model:
- ibm-granite/granite-4.0-micro
license: apache-2.0
library_name: transformers
tags:
- language
- unsloth
- granite-4.0
---
<div>
<p style="margin-bottom: 0; margin-top: 0;">
<strong>See <a href="https://huggingface.co/collections/unsloth/granite-40-68ddf64b4a8717dc22a9322d">our collection</a> for all versions of Granite-4.0 including GGUF, 4-bit & 16-bit formats.</strong>
</p>
<p style="margin-bottom: 0;">
<em>Learn to run Granite 4.0 correctly - <a href="https://docs.unsloth.ai/new/ibm-granite-4.0">Read our Guide</a>.</em>
</p>
<p style="margin-top: 0;margin-bottom: 0;">
<em>See <a href="https://docs.unsloth.ai/basics/unsloth-dynamic-v2.0-gguf">Unsloth Dynamic 2.0 GGUFs</a> for our quantization benchmarks.</em>
</p>
<div style="display: flex; gap: 5px; align-items: center; ">
<a href="https://github.com/unslothai/unsloth/">
<img src="https://github.com/unslothai/unsloth/raw/main/images/unsloth%20new%20logo.png" width="133">
</a>
<a href="https://discord.gg/unsloth">
<img src="https://github.com/unslothai/unsloth/raw/main/images/Discord%20button.png" width="173">
</a>
<a href="https://docs.unsloth.ai/new/ibm-granite-4.0">
<img src="https://raw.githubusercontent.com/unslothai/unsloth/refs/heads/main/images/documentation%20green%20button.png" width="143">
</a>
</div>
<h1 style="margin-top: 0rem;">✨ Read our Granite-4.0 Guide <a href="https://docs.unsloth.ai/new/ibm-granite-4.0">here</a>!</h1>
</div>
- Fine-tune Granite-4.0 for free using our [Google Colab notebook](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Granite4.0.ipynb)
- Read our Blog about Granite-4.0 support: https://docs.unsloth.ai/new/ibm-granite-4.0
- View the rest of our notebooks in our [docs here](https://docs.unsloth.ai/get-started/unsloth-notebooks).
# Granite-4.0-Micro
**Model Summary:**
Granite-4.0-Micro is a 3B parameter long-context instruct model finetuned from *Granite-4.0-Micro-Base* using a combination of open source instruction datasets with permissive license and internally collected synthetic datasets. This model is developed using a diverse set of techniques with a structured chat format, including supervised finetuning, model alignment using reinforcement learning, and model merging. Granite 4.0 instruct models feature improved *instruction following (IF)* and *tool-calling* capabilities, making them more effective in enterprise applications.
- **Developers:** Granite Team, IBM
- **HF Collection:** [Granite 4.0 Language Models HF Collection](https://huggingface.co/collections/ibm-granite/granite-40-language-models-6811a18b820ef362d9e5a82c)
- **GitHub Repository:** [ibm-granite/granite-4.0-language-models](https://github.com/ibm-granite/granite-4.0-language-models)
- **Website**: [Granite Docs](https://www.ibm.com/granite/docs/)
- **Release Date**: October 2nd, 2025
- **License:** [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)
**Supported Languages:**
English, German, Spanish, French, Japanese, Portuguese, Arabic, Czech, Italian, Korean, Dutch, and Chinese. Users may finetune Granite 4.0 models for languages beyond these languages.
**Intended use:**
The model is designed to follow general instructions and can serve as the foundation for AI assistants across diverse domains, including business applications, as well as for LLM agents equipped with tool-use capabilities.
*Capabilities*
* Summarization
* Text classification
* Text extraction
* Question-answering
* Retrieval Augmented Generation (RAG)
* Code related tasks
* Function-calling tasks
* Multilingual dialog use cases
* Fill-In-the-Middle (FIM) code completions
<!-- <todo>Need to test the examples. (especially the tool calling and RAG ones)</todo>
-->
**Generation:**
This is a simple example of how to use Granite-4.0-Micro model.
Install the following libraries:
```shell
pip install torch torchvision torchaudio
pip install accelerate
pip install transformers
```
Then, copy the snippet from the section that is relevant for your use case.
```python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
device = "cuda"
model_path = "ibm-granite/granite-4.0-micro"
tokenizer = AutoTokenizer.from_pretrained(model_path)
# drop device_map if running on CPU
model = AutoModelForCausalLM.from_pretrained(model_path, device_map=device)
model.eval()
# change input text as desired
chat = [
{ "role": "user", "content": "Please list one IBM Research laboratory located in the United States. You should only output its name and location." },
]
chat = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
# tokenize the text
input_tokens = tokenizer(chat, return_tensors="pt").to(device)
# generate output tokens
output = model.generate(**input_tokens,
max_new_tokens=100)
# decode output tokens into text
output = tokenizer.batch_decode(output)
# print output
print(output[0])
```
Expected output:
```shell
<|start_of_role|>user<|end_of_role|>Please list one IBM Research laboratory located in the United States. You should only output its name and location.<|end_of_text|>
<|start_of_role|>assistant<|end_of_role|>Almaden Research Center, San Jose, California<|end_of_text|>
```
**Tool-calling:**
Granite-4.0-Micro comes with enhanced tool calling capabilities, enabling seamless integration with external functions and APIs. To define a list of tools please follow OpenAI's function [definition schema](https://platform.openai.com/docs/guides/function-calling?api-mode=responses#defining-functions).
This is an example of how to use Granite-4.0-Micro model tool-calling ability:
```python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
device = "cuda"
model_path = "ibm-granite/granite-4.0-micro"
tokenizer = AutoTokenizer.from_pretrained(model_path)
# drop device_map if running on CPU
model = AutoModelForCausalLM.from_pretrained(model_path, device_map=device)
model.eval()
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather for a specified city.",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "Name of the city"
}
},
"required": ["city"]
}
}
}
]
# change input text as desired
chat = [
{ "role": "user", "content": "What's the weather like in Boston right now?" },
]
chat = tokenizer.apply_chat_template(chat, \
tokenize=False, \
tools=tools, \
add_generation_prompt=True)
# tokenize the text
input_tokens = tokenizer(chat, return_tensors="pt").to(device)
# generate output tokens
output = model.generate(**input_tokens,
max_new_tokens=100)
# decode output tokens into text
output = tokenizer.batch_decode(output)
# print output
print(output[0])
```
Expected output:
```shell
<|start_of_role|>system<|end_of_role|>You are a helpful assistant with access to the following tools. You may call one or more tools to assist with the user query.
You are provided with function signatures within <tools></tools> XML tags:
- <tools>
- unsloth
{"type": "function", "function": {"name": "get_current_weather", "description": "Get the current weather for a specified city.", "parameters": {"type": "object", "properties": {"city": {"type": "string", "description": "Name of the city"}}, "required": ["city"]}}}
</tools>
For each tool call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
- <tool_call>
- unsloth
{"name": <function-name>, "arguments": <args-json-object>}
</tool_call>. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request.<|end_of_text|>
<|start_of_role|>user<|end_of_role|>What's the weather like in Boston right now?<|end_of_text|>
<|start_of_role|>assistant<|end_of_role|><tool_call>
{"name": "get_current_weather", "arguments": {"city": "Boston"}}
</tool_call><|end_of_text|>
```
<!-- **Retrieval Augmented Generation:**
*Coming soon* -->
**Evaluation Results:**
<table>
<!-- <caption><b> All Results</b></caption> -->
<thead>
<tr>
<th style="text-align:left; background-color: #001d6c; color: white;">Benchmarks</th>
<th style="text-align:left; background-color: #001d6c; color: white;">Metric</th>
<th style="text-align:center; background-color: #001d6c; color: white;">Micro Dense</th>
<th style="text-align:center; background-color: #001d6c; color: white;">H Micro Dense</th>
<th style="text-align:center; background-color: #001d6c; color: white;">H Tiny MoE</th>
<th style="text-align:center; background-color: #001d6c; color: white;">H Small MoE</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="6" style="text-align:center; background-color: #FFFFFF; color: #2D2D2D; font-style:italic;">
General Tasks
</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">MMLU</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">5-shot</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">65.98</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">67.43</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">68.65</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">78.44</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">MMLU-Pro</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">5-shot, CoT</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">44.5</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">43.48</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">44.94</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">55.47</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">BBH</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">3-shot, CoT</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">72.48</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">69.36</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">66.34</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">81.62</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">AGI EVAL</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">0-shot, CoT</td>
<td style="text-align:right; background-color:#DAE8FF; color: #2D2D2D;">64.29</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">59</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">62.15</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">70.63</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">GPQA</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">0-shot, CoT</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">30.14</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">32.15</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">32.59</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">40.63</td>
</tr>
<tr>
<td colspan="6" style="text-align:center; background-color: #FFFFFF; color: #2D2D2D; font-style:italic;">
Alignment Tasks
</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">AlpacaEval 2.0</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;"></td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">29.49</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">31.49</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">30.61</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">42.48</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">IFEval</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">Instruct, Strict</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">85.5</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">86.94</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">84.78</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">89.87</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">IFEval</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">Prompt, Strict</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">79.12</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">81.71</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">78.1</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">85.22</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">IFEval</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">Average</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">82.31</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">84.32</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">81.44</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">87.55</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">ArenaHard</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;"></td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">25.84</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">36.15</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">35.75</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">46.48</td>
</tr>
<tr>
<td colspan="6" style="text-align:center; background-color: #FFFFFF; color: #2D2D2D; font-style:italic;">
Math Tasks
</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">GSM8K</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">8-shot</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">85.45</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">81.35</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">84.69</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">87.27</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">GSM8K Symbolic</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">8-shot</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">79.82</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">77.5</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">81.1</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">87.38</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">Minerva Math</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">0-shot, CoT</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">62.06</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">66.44</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">69.64</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">74</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">DeepMind Math</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">0-shot, CoT</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">44.56</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">43.83</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">49.92</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">59.33</td>
</tr>
<tr>
<td colspan="6" style="text-align:center; background-color: #FFFFFF; color: #2D2D2D; font-style:italic;">
Code Tasks
</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">HumanEval</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">pass@1</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">80</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">81</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">83</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">88</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">HumanEval+</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">pass@1</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">72</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">75</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">76</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">83</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">MBPP</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">pass@1</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">72</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">73</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">80</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">84</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">MBPP+</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">pass@1</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">64</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">64</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">69</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">71</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">CRUXEval-O</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">pass@1</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">41.5</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">41.25</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">39.63</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">50.25</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">BigCodeBench</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">pass@1</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">39.21</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">37.9</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">41.06</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">46.23</td>
</tr>
<tr>
<td colspan="6" style="text-align:center; background-color: #FFFFFF; color: #2D2D2D; font-style:italic;">
Tool Calling Tasks
</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">BFCL v3</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;"></td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">59.98</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">57.56</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">57.65</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">64.69</td>
</tr>
<tr>
<td colspan="6" style="text-align:center; background-color: #FFFFFF; color: #2D2D2D; font-style:italic;">
Multilingual Tasks
</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">MULTIPLE</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">pass@1</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">49.21</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">49.46</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">55.83</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">57.37</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">MMMLU</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">5-shot</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">55.14</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">55.19</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">61.87</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">69.69</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">INCLUDE</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">5-shot</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">51.62</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">50.51</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">53.12</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">63.97</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">MGSM</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">8-shot</td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">28.56</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">44.48</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">45.36</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">38.72</td>
</tr>
<tr>
<td colspan="6" style="text-align:center; background-color: #FFFFFF; color: #2D2D2D; font-style:italic;">
Safety
</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">SALAD-Bench</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;"></td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">97.06</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">96.28</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">97.77</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">97.3</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">AttaQ</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;"></td>
<td style="text-align:right; background-color: #DAE8FF; color: #2D2D2D;">86.05</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">84.44</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">86.61</td>
<td style="text-align:right; background-color: #FFFFFF; color: #2D2D2D;">86.64</td>
</tr>
</tbody></table>
<table>
<caption><b>Multilingual Benchmarks and thr included languages:</b></caption>
<thead>
<tr>
<th style="text-align:left; background-color: #001d6c; color: white;">Benchmarks</th>
<th style="text-align:left; background-color: #001d6c; color: white;"># Langs</th>
<th style="text-align:center; background-color: #001d6c; color: white;">Languages</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">MMMLU</td>
<td style="text-align:center; background-color: #FFFFFF; color: #2D2D2D;">11</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">ar, de, en, es, fr, ja, ko, pt, zh, bn, hi</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">INCLUDE</td>
<td style="text-align:center; background-color: #FFFFFF; color: #2D2D2D;">14</td>
<!-- <td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">hindi, bengali, tamil, telugu, arabic, german, spanish, french, italian, japanese, korean, dutch, portuguese, chinese</td> -->
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">hi, bn, ta, te, ar, de, es, fr, it, ja, ko, nl, pt, zh</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">MGSM</td>
<td style="text-align:center; background-color: #FFFFFF; color: #2D2D2D;">5</td>
<td style="text-align:left; background-color: #FFFFFF; color: #2D2D2D;">en, es, fr, ja, zh</td>
</tr>
</tbody>
</table>
**Model Architecture:**
Granite-4.0-Micro baseline is built on a decoder-only dense transformer architecture. Core components of this architecture are: GQA, RoPE, MLP with SwiGLU, RMSNorm, and shared input/output embeddings.
<table>
<thead>
<tr>
<th style="text-align:left; background-color: #001d6c; color: white;">Model</th>
<th style="text-align:center; background-color: #001d6c; color: white;">Micro Dense</th>
<th style="text-align:center; background-color: #001d6c; color: white;">H Micro Dense</th>
<th style="text-align:center; background-color: #001d6c; color: white;">H Tiny MoE</th>
<th style="text-align:center; background-color: #001d6c; color: white;">H Small MoE</th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;">Embedding size</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">2560</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">2048</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">1536</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">4096</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;">Number of layers</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">40 attention</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">4 attention / 36 Mamba2</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">4 attention / 36 Mamba2</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">4 attention / 36 Mamba2</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;">Attention head size</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">64</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">64</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">128</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">128</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;">Number of attention heads</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">40</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">32</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">12</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">32</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;">Number of KV heads</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">8</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">8</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">4</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">8</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;">Mamba2 state size</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">-</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">128</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">128</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">128</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;">Number of Mamba2 heads</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">-</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">64</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">48</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">128</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;">MLP / Shared expert hidden size</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">8192</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">8192</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">1024</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">1536</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;">Num. Experts</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">-</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">-</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">64</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">72</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;">Num. active Experts</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">-</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">-</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">6</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">10</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;">Expert hidden size</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">-</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">-</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">512</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">768</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;">MLP activation</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">SwiGLU</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">SwiGLU</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">SwiGLU</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">SwiGLU</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;">Sequence length</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">128K</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">128K</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">128K</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">128K</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;">Position embedding</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">RoPE</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">NoPE</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">NoPE</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">NoPE</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;"># Parameters</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">3B</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">3B</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">7B</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">32B</td>
</tr>
<tr>
<td style="text-align:left; background-color: #FFFFFF; color: black;"># Active parameters</td>
<td style="text-align:center; background-color: #DAE8FF; color: black;">3B</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">3B</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">1B</td>
<td style="text-align:center; background-color: #FFFFFF; color: black;">9B</td>
</tr>
</tbody></table>
**Training Data:**
Overall, our SFT data is largely comprised of three key sources: (1) publicly available datasets with permissive license, (2) internal synthetic data targeting specific capabilities, and (3) a select set of human-curated data.
**Infrastructure:**
We trained the Granite 4.0 Language Models utilizing an NVIDIA GB200 NVL72 cluster hosted in CoreWeave. Intra-rack communication occurs via the 72-GPU NVLink domain, and a non-blocking, full Fat-Tree NDR 400 Gb/s InfiniBand network provides inter-rack communication. This cluster provides a scalable and efficient infrastructure for training our models over thousands of GPUs.
**Ethical Considerations and Limitations:**
Granite 4.0 Instruction Models are primarily finetuned using instruction-response pairs mostly in English, but also multilingual data covering multiple languages. Although this model can handle multilingual dialog use cases, its performance might not be similar to English tasks. In such case, introducing a small number of examples (few-shot) can help the model in generating more accurate outputs. While this model has been aligned by keeping safety in consideration, the model may in some cases produce inaccurate, biased, or unsafe responses to user prompts. So we urge the community to use this model with proper safety testing and tuning tailored for their specific tasks.
**Resources**
- ⭐️ Learn about the latest updates with Granite: https://www.ibm.com/granite
- 📄 Get started with tutorials, best practices, and prompt engineering advice: https://www.ibm.com/granite/docs/
- 💡 Learn about the latest Granite learning resources: https://ibm.biz/granite-learning-resources
<!-- ## Citation
```
@misc{granite-models,
author = {author 1, author2, ...},
title = {},
journal = {},
volume = {},
year = {2024},
url = {https://arxiv.org/abs/0000.00000},
}
``` -->

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b6b27fbc5243e4294afcc527a6bfbe6809df1b665af0179952e4377e2ec278e5
size 6809656800

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:8faf8f8813bf9ea1c62b4284bb70e8ac0a4d687249c6932b660fc75956aa06a1
size 1991163648

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:cb48b3f1cfd4b3500b7007ec4fb9729e28224ea3f94aaec7892f3a101d040a32
size 1894498048

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:c1fdfcae12679ac41cf55875de3647804643552457bdffacee0d4311f226e609
size 1371438848

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:c1fdfcae12679ac41cf55875de3647804643552457bdffacee0d4311f226e609
size 1371438848

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b167c94cefd31b763f00c16d0c5ff74c76d4b612caac27e65607a1d4f36b010a
size 1725579008

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:1168270175756e39de329c311bc70e90a508390eb64271e83dc9dd874bd666a7
size 1566818048

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:c4763a66780871dc656a5b579cd23fe8a77efbe2d253b03a693efa713de2c371
size 1991163648

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:93bec266d1e2dd8eaec18f8e2115d39276b7210bdf6e0c49ab1b2ee85bdacc9f
size 2181218048

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:3ed0a9f397bab63eb93231449086ce5dc3f309598d007825fc01cfd32c4b93e1
size 2099502848

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:bc154bb604fc12af4f1682ab94e33afb2aeaa68b8df6afbd32a537cf02cc89e3
size 1998372608

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:7f77222fa5fb8445b2fbcac13577f1f4d76896a304a88d27d6d5baa382a8970c
size 2437013248

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:373157397937a931d139a484aa0326ebba0392127636aa669666058e8406e813
size 2377826048

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:f55d3414e2c8650c1a1d718d5e6cbf0b06b78f17668d047d28b69ab82445ba0d
size 2795618048

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:9ece7079edab674f2192e5339e9198e5844ae348fb289982dc0da1ec9469b830
size 3619692288

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:c66f9f1bc3e31e319ebf42eb870440b606374c7d7ff163ff07868d9b943e9ea1
size 938655488

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:bc0e541d4a5b16ecee62a75c1c9882912c84f74435e5bc85d96c236320f0808e
size 882294528

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:40932e3a07b51848a355f4631c6057cbed24a198c377236ab3871eb1baa3011b
size 1265680128

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:f225d235e05c000e4b767664a481158e7161bac3158626451d83c8406525980e
size 1035525888

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:0a84765d194736dc489da43e4e77ed8c01dbb06e898a32f1b55b5945109d022f
size 1404083968

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:7af1ced1d091f14b029d57fe6b9709ba8686911e4cd6f980c884a1ef83e8d979
size 1402363648

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d924a560af878eacba0e8340b25159e18e066500d026518fe3e50e7ff8918828
size 1768689408

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:90849f9762f9a19f0d49b7822a01588dfef7218fc111bcbe291fee81a8431e8d
size 2137308928

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:2f6ade9828221e21a3d3149eaee1bd3a90c6a19dae415b990d74d7d3d72673ba
size 2449813248

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:fbcb699f488d2b557aee6cb9003c76b7693a26905349f34d3b7139224c11f733
size 3048300288

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b2ef94881daf3f3e27801394964eab9b0e7e2c92765101b3d85de7164103e8f8
size 4241465088

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:2d90b81af88d25953d25417df32c44f7579f1fee5a3195260f8361c33ad4a17e
size 3811424

7
params Normal file
View File

@@ -0,0 +1,7 @@
{
"stop": [
"<|end_of_text|>"
],
"temperature": 0.0,
"min_p" : 0.00
}

158
template Normal file
View File

@@ -0,0 +1,158 @@
{{- /*
------ MESSAGE PARSING ------
*/}}
{{- /*
Declare the system prompt chunks used for different features
*/}}
{{- $tools_system_message_prefix := "You are a helpful assistant with access to the following tools. You may call one or more tools to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
{{- $tools_system_message_suffix := "\n</tools>\n\nFor each tool call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call>. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request." }}
{{- $documents_system_message_prefix := "You are a helpful assistant with access to the following documents. You may use one or more documents to assist with the user query.\n\nYou are given a list of documents within <documents></documents> XML tags:\n<documents>" }}
{{- $documents_system_message_suffix := "\n</documents>\n\nWrite the response to the user's input by strictly aligning with the facts in the provided documents. If the information needed to answer the question is not available in the documents, inform the user that the question cannot be answered based on the available data." }}
{{- $default_system_message := "You are a helpful assistant. Please ensure responses are professional, accurate, and safe." }}
{{- /*
Declare the prompt structure variables to be filled in from messages
*/}}
{{- $tools_system_message := "" }}
{{- $documents_system_message := "" }}
{{- $system_message := "" }}
{{- $document_counter := 0 }}
{{- $last_query_index := 0 }}
{{- /*
Output parsing heuristic
Ollama has very specific heuristics for parsing the tags for thinking and tool
calling. Rather than contorting the actual message expansion to match the
expectations, we explicitly put them here behind an unreachable condition.
*/}}
{{- if false }}
{{- /* tool calls: https://github.com/ollama/ollama/blob/main/tools/template.go#L17 */}}
{{- if .ToolCalls }}<tool_call>{{- end }}
{{- end }}
{{- /*
Create tools system message chunk
*/}}
{{- if .Tools }}
{{- $tools_system_message = print $tools_system_message_prefix }}
{{- range $_, $tool_body := .Tools }}
{{- $tools_system_message = print $tools_system_message "\n" (json $tool_body) }}
{{- end }}
{{- $tools_system_message = print $tools_system_message $tools_system_message_suffix }}
{{- end }}
{{- /*
Loop over messages to parse variables:
- User provided documents in the "document" role
- Last user query index
- Initial system message
NOTE: Since Ollama collates consecutive roles, for documents, we work around
this by allowing the role to contain a qualifier after the role string. This
is also then used as the title of the document.
*/ -}}
{{- range $index, $_ := .Messages }}
{{- if (and (eq .Role "system") (eq $index 0)) }}
{{- if ne $system_message "" }}
{{- $system_message = print $system_message "\n" }}
{{- end }}
{{- $system_message = print $system_message .Content }}
{{- else if eq .Role "user" }}
{{- /*
NOTE: 31 == len '<tool_response></tool_response>'. We only check the
prefix match since go template doesn't support negative indexing.
*/}}
{{- if or (lt (len .Content) 31) (ne (slice .Content 0 15) "<tool_response>") }}
{{- $last_query_index = $index }}
{{- end }}
{{- else if (and (ge (len .Role) 8) (eq (slice .Role 0 8) "document")) }}
{{- if (eq $document_counter 0)}}
{{- $documents_system_message = print $documents_system_message_prefix}}
{{- end }}
{{- $identifier := ""}}
{{- if (ge (len .Role) 9) }}
{{- $identifier = slice .Role 9}}
{{- end }}
{{- if (eq $identifier "") }}
{{- $identifier := print $document_counter}}
{{- end }}
{{- $documents_system_message = print $documents_system_message "\n{\"doc_id\": " $document_counter ", \"title\": \"" $identifier "\", \"text\": \"" .Content "\"}"}}
{{- $document_counter = len (printf "a%*s" $document_counter "")}}
{{- end }}
{{- end }}
{{- if (ne $document_counter 0) }}
{{- $documents_system_message = print $documents_system_message $documents_system_message_suffix}}
{{- end }}
{{- /*
Construct the full system message
*/}}
{{- if ne $tools_system_message "" }}
{{- if ne $system_message "" }}
{{- $system_message = print $system_message "\n\n" }}
{{- end }}
{{- $system_message = print $system_message $tools_system_message }}
{{- end }}
{{- if ne $documents_system_message "" }}
{{- if ne $system_message "" }}
{{- $system_message = print $system_message "\n\n" }}
{{- end }}
{{- $system_message = print $system_message $documents_system_message }}
{{- end }}
{{- /*
------ TEMPLATE EXPANSION ------
*/}}
{{- if ne $system_message "" -}}
<|start_of_role|>system<|end_of_role|>{{ $system_message }}<|end_of_text|>{{ "\n" }}
{{- else -}}
<|start_of_role|>system<|end_of_role|>{{ $default_system_message }}<|end_of_text|>{{ "\n" }}
{{- end }}
{{- $prev_role := "" }}
{{- range $message_index, $_ := .Messages }}
{{- $next_message_index := len (printf "a%*s" $message_index "") }}
{{- if or (eq .Role "user") (and (eq .Role "system") (ne $message_index 0)) }}
{{- "" }}<|start_of_role|>{{- .Role }}<|end_of_role|>{{- .Content }}<|end_of_text|>{{ "\n" }}
{{- else if eq .Role "assistant" -}}
{{- "" }}<|start_of_role|>{{ .Role }}<|end_of_role|>{{ .Content }}
{{- /* Expand tool calls */}}
{{- $content := .Content }}
{{- if .ToolCalls }}
{{- range $tool_idx, $tool_call := .ToolCalls }}
{{- if or (ne $content "") (ne $tool_idx 0) }}
{{- "\n" }}
{{- end }}
{{- print "<tool_call>\n{\"name\": \"" $tool_call.Function.Name "\", \"arguments\": " (json $tool_call.Function.Arguments) "}\n</tool_call>" }}
{{- end }}
{{- end }}
{{- /* End assistant block */}}<|end_of_text|>{{ "\n" }}
{{- else if eq .Role "tool" }}
{{- if (ne $prev_role "tool") -}}
<|start_of_role>user<|end_of_role|>
{{- end }}
{{- "\n" }}<tool_response>{{ print "\n" .Content "\n" }}</tool_response>
{{- if ne $next_message_index (len $.Messages) }}
{{- $next_element := index $.Messages $next_message_index }}
{{- if ne $next_element.Role "tool" -}}
<|end_of_text|>{{ "\n" }}
{{- end }}
{{- else -}}
<|end_of_text|>{{ "\n" }}
{{- end }}
{{- end }}
{{- /* If not an assistant message at the end, add generation prompt */}}
{{- if and (eq $next_message_index (len $.Messages)) (ne .Role "assistant") }}
{{- "<|start_of_role|>assistant<|end_of_role|>" }}
{{- end }}
{{- end }}