Add files using upload-large-folder tool
Browse files- .gitattributes +1 -0
- README.md +73 -0
- chat_template.jinja +137 -0
- config.json +34 -0
- generation_config.json +7 -0
- model.safetensors +3 -0
- tokenizer.json +3 -0
- tokenizer_config.json +16 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: en
|
| 3 |
+
license: apache-2.0
|
| 4 |
+
base_model: Nanbeige/Nanbeige4.1-3B
|
| 5 |
+
tags:
|
| 6 |
+
- tool-use
|
| 7 |
+
- gmail
|
| 8 |
+
- function-calling
|
| 9 |
+
- sft
|
| 10 |
+
- dpo
|
| 11 |
+
pipeline_tag: text-generation
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# Nanbeige4.1-3B — Gmail Tool-Use (SFT + DPO)
|
| 15 |
+
|
| 16 |
+
Fine-tuned version of [Nanbeige/Nanbeige4.1-3B](https://huggingface.co/Nanbeige/Nanbeige4.1-3B)
|
| 17 |
+
for Gmail tool-calling tasks using a two-stage training pipeline.
|
| 18 |
+
|
| 19 |
+
## Training Pipeline
|
| 20 |
+
|
| 21 |
+
### Stage 1 — Supervised Fine-Tuning (SFT)
|
| 22 |
+
- **Dataset:** Multi-turn Gmail agent traces generated via LangGraph + Claude emulation
|
| 23 |
+
- **Format:** ChatML with tool_calls (OpenAI function-calling schema)
|
| 24 |
+
- **Method:** LoRA r=16, α=32, 7 target modules
|
| 25 |
+
- **Result:** loss 0.8464 → 0.1888 · PPL 2.33 → 1.21
|
| 26 |
+
|
| 27 |
+
### Stage 2 — Direct Preference Optimization (DPO)
|
| 28 |
+
- **Dataset:** 3223 preference pairs from SFT traces (3 rejection strategies)
|
| 29 |
+
- `wrong_tool` — incorrect tool selected
|
| 30 |
+
- `missing_args` — required arguments omitted
|
| 31 |
+
- `bad_answer` — poor final response
|
| 32 |
+
- **Method:** DPO β=0.1, sigmoid loss, LoRA r=16, `ref_model=None` (PEFT implicit ref)
|
| 33 |
+
- **Result:** val_loss=0.000765 · reward accuracy=100% · normalized margin=+0.52
|
| 34 |
+
|
| 35 |
+
## Supported Tools
|
| 36 |
+
|
| 37 |
+
| Tool | Description |
|
| 38 |
+
|---|---|
|
| 39 |
+
| `search_emails` | Search Gmail inbox with filters |
|
| 40 |
+
| `read_email` | Read full email content by ID |
|
| 41 |
+
| `send_email` | Send a new email |
|
| 42 |
+
| `draft_email` | Create a draft |
|
| 43 |
+
| `modify_email` | Add/remove labels, mark read/unread |
|
| 44 |
+
| `download_attachment` | Download email attachment |
|
| 45 |
+
|
| 46 |
+
## Usage
|
| 47 |
+
|
| 48 |
+
```python
|
| 49 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 50 |
+
import torch
|
| 51 |
+
|
| 52 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 53 |
+
"TurkishCodeMan/Nanbeige4.1-3B-Gmail-Tool-Use",
|
| 54 |
+
torch_dtype=torch.bfloat16,
|
| 55 |
+
trust_remote_code=True,
|
| 56 |
+
)
|
| 57 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 58 |
+
"TurkishCodeMan/Nanbeige4.1-3B-Gmail-Tool-Use",
|
| 59 |
+
trust_remote_code=True,
|
| 60 |
+
)
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
## Training Details
|
| 64 |
+
|
| 65 |
+
| Parameter | Value |
|
| 66 |
+
|---|---|
|
| 67 |
+
| Base model | Nanbeige/Nanbeige4.1-3B |
|
| 68 |
+
| SFT LoRA rank | 16 |
|
| 69 |
+
| DPO LoRA rank | 16 |
|
| 70 |
+
| DPO β | 0.1 |
|
| 71 |
+
| Max length | 2682 tokens |
|
| 72 |
+
| GPU | 1× RTX 4090 24GB |
|
| 73 |
+
| Framework | TRL 0.22 · Transformers 4.57 · PEFT 0.18 |
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
{%- if tools %}
|
| 3 |
+
{{- '<|im_start|>system
|
| 4 |
+
' }}
|
| 5 |
+
{%- if messages[0].role == 'system' %}
|
| 6 |
+
{{- messages[0].content + '
|
| 7 |
+
|
| 8 |
+
' }}
|
| 9 |
+
{%- else %}
|
| 10 |
+
{{- '你是一位工具函数调用专家,你会得到一个问题和一组可能的工具函数。根据问题,你需要进行一个或多个函数/工具调用以实现目的,请尽量尝试探索通过工具解决问题。
|
| 11 |
+
如果没有一个函数可以使用,请直接使用自然语言回复用户。
|
| 12 |
+
如果给定的问题缺少函数所需的参数,请使用自然语言进行提问,向用户询问必要信息。
|
| 13 |
+
如果调用结果已经足够回答用户问题,请对历史结果进行总结,使用自然语言回复用户。' }}
|
| 14 |
+
{%- endif %}
|
| 15 |
+
{{- "# Tools
|
| 16 |
+
|
| 17 |
+
You may call one or more functions to assist with the user query.
|
| 18 |
+
|
| 19 |
+
You are provided with function signatures within <tools></tools> XML tags:
|
| 20 |
+
<tools>" }}
|
| 21 |
+
{%- for tool in tools %}
|
| 22 |
+
{{- "
|
| 23 |
+
" }}
|
| 24 |
+
{{- tool | tojson }}
|
| 25 |
+
{%- endfor %}
|
| 26 |
+
{{- "
|
| 27 |
+
</tools>
|
| 28 |
+
|
| 29 |
+
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
|
| 30 |
+
<tool_call>
|
| 31 |
+
{\"name\": <function-name>, \"arguments\": <args-json-object>}
|
| 32 |
+
</tool_call><|im_end|>
|
| 33 |
+
" }}
|
| 34 |
+
{%- else %}
|
| 35 |
+
{%- if messages[0].role == 'system' %}
|
| 36 |
+
{{- '<|im_start|>system
|
| 37 |
+
' + messages[0].content + '<|im_end|>
|
| 38 |
+
' }}
|
| 39 |
+
{%- else %}
|
| 40 |
+
{{- '<|im_start|>system
|
| 41 |
+
你是南北阁,一款由BOSS直聘自主研发并训练的专业大语言模型。<|im_end|>
|
| 42 |
+
' }}
|
| 43 |
+
{%- endif %}
|
| 44 |
+
{%- endif %}
|
| 45 |
+
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
|
| 46 |
+
{%- for message in messages[::-1] %}
|
| 47 |
+
{%- set index = (messages|length - 1) - loop.index0 %}
|
| 48 |
+
{%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}
|
| 49 |
+
{%- set ns.multi_step_tool = false %}
|
| 50 |
+
{%- set ns.last_query_index = index %}
|
| 51 |
+
{%- endif %}
|
| 52 |
+
{%- endfor %}
|
| 53 |
+
{%- for message in messages %}
|
| 54 |
+
{%- if message.content is string %}
|
| 55 |
+
{%- set content = message.content %}
|
| 56 |
+
{%- else %}
|
| 57 |
+
{%- set content = '' %}
|
| 58 |
+
{%- endif %}
|
| 59 |
+
{%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
|
| 60 |
+
{{- '<|im_start|>' + message.role + '
|
| 61 |
+
' + content + '<|im_end|>' + '
|
| 62 |
+
' }}
|
| 63 |
+
{%- elif message.role == "assistant" %}
|
| 64 |
+
{%- set reasoning_content = '' %}
|
| 65 |
+
{%- if message.reasoning_content is string %}
|
| 66 |
+
{%- set reasoning_content = message.reasoning_content %}
|
| 67 |
+
{%- else %}
|
| 68 |
+
{%- if '</think>' in content %}
|
| 69 |
+
{%- set reasoning_content = content.split('</think>')[0].rstrip('
|
| 70 |
+
').split('<think>')[-1].lstrip('
|
| 71 |
+
') %}
|
| 72 |
+
{%- set content = content.split('</think>')[-1].lstrip('
|
| 73 |
+
') %}
|
| 74 |
+
{%- endif %}
|
| 75 |
+
{%- endif %}
|
| 76 |
+
{%- if loop.index0 > ns.last_query_index or keep_all_think or (extra_body is defined and extra_body.keep_all_think) %}
|
| 77 |
+
{%- if loop.last or (not loop.last and reasoning_content) %}
|
| 78 |
+
{{- '<|im_start|>' + message.role + '
|
| 79 |
+
<think>
|
| 80 |
+
' + reasoning_content.strip('
|
| 81 |
+
') + '
|
| 82 |
+
</think>
|
| 83 |
+
|
| 84 |
+
' + content.lstrip('
|
| 85 |
+
') }}
|
| 86 |
+
{%- else %}
|
| 87 |
+
{{- '<|im_start|>' + message.role + '
|
| 88 |
+
' + content }}
|
| 89 |
+
{%- endif %}
|
| 90 |
+
{%- else %}
|
| 91 |
+
{{- '<|im_start|>' + message.role + '
|
| 92 |
+
' + content }}
|
| 93 |
+
{%- endif %}
|
| 94 |
+
{%- if message.tool_calls %}
|
| 95 |
+
{%- for tool_call in message.tool_calls %}
|
| 96 |
+
{%- if (loop.first and content) or (not loop.first) %}
|
| 97 |
+
{{- '
|
| 98 |
+
' }}
|
| 99 |
+
{%- endif %}
|
| 100 |
+
{%- if tool_call.function %}
|
| 101 |
+
{%- set tool_call = tool_call.function %}
|
| 102 |
+
{%- endif %}
|
| 103 |
+
{{- '<tool_call>
|
| 104 |
+
{"name": "' }}
|
| 105 |
+
{{- tool_call.name }}
|
| 106 |
+
{{- '", "arguments": ' }}
|
| 107 |
+
{%- if tool_call.arguments is string %}
|
| 108 |
+
{{- tool_call.arguments }}
|
| 109 |
+
{%- else %}
|
| 110 |
+
{{- tool_call.arguments | tojson }}
|
| 111 |
+
{%- endif %}
|
| 112 |
+
{{- '}
|
| 113 |
+
</tool_call>' }}
|
| 114 |
+
{%- endfor %}
|
| 115 |
+
{%- endif %}
|
| 116 |
+
{{- '<|im_end|>
|
| 117 |
+
' }}
|
| 118 |
+
{%- elif message.role == "tool" %}
|
| 119 |
+
{%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
|
| 120 |
+
{{- '<|im_start|>user' }}
|
| 121 |
+
{%- endif %}
|
| 122 |
+
{{- '
|
| 123 |
+
<tool_response>
|
| 124 |
+
' }}
|
| 125 |
+
{{- content }}
|
| 126 |
+
{{- '
|
| 127 |
+
</tool_response>' }}
|
| 128 |
+
{%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
|
| 129 |
+
{{- '<|im_end|>
|
| 130 |
+
' }}
|
| 131 |
+
{%- endif %}
|
| 132 |
+
{%- endif %}
|
| 133 |
+
{%- endfor %}
|
| 134 |
+
{%- if add_generation_prompt %}
|
| 135 |
+
{{- '<|im_start|>assistant
|
| 136 |
+
' }}
|
| 137 |
+
{%- endif %}
|
config.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"LlamaForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_bias": false,
|
| 6 |
+
"attention_dropout": 0.0,
|
| 7 |
+
"bos_token_id": 166100,
|
| 8 |
+
"dtype": "bfloat16",
|
| 9 |
+
"embd_pdrop": 0.0,
|
| 10 |
+
"eos_token_id": 166101,
|
| 11 |
+
"head_dim": 128,
|
| 12 |
+
"hidden_act": "silu",
|
| 13 |
+
"hidden_size": 2560,
|
| 14 |
+
"initializer_range": 0.02,
|
| 15 |
+
"intermediate_size": 10496,
|
| 16 |
+
"max_position_embeddings": 262144,
|
| 17 |
+
"mlp_bias": false,
|
| 18 |
+
"model_type": "llama",
|
| 19 |
+
"num_attention_heads": 20,
|
| 20 |
+
"num_hidden_layers": 32,
|
| 21 |
+
"num_key_value_heads": 4,
|
| 22 |
+
"pad_token_id": 0,
|
| 23 |
+
"pretraining_tp": 1,
|
| 24 |
+
"resid_pdrop": 0.0,
|
| 25 |
+
"rms_norm_eps": 1e-05,
|
| 26 |
+
"rope_parameters": {
|
| 27 |
+
"rope_theta": 70000000,
|
| 28 |
+
"rope_type": "default"
|
| 29 |
+
},
|
| 30 |
+
"tie_word_embeddings": false,
|
| 31 |
+
"transformers_version": "5.2.0",
|
| 32 |
+
"use_cache": true,
|
| 33 |
+
"vocab_size": 166144
|
| 34 |
+
}
|
generation_config.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 166100,
|
| 4 |
+
"eos_token_id": 166101,
|
| 5 |
+
"pad_token_id": 0,
|
| 6 |
+
"transformers_version": "5.2.0"
|
| 7 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9f90f6b5dddadb2ee8bcf9adce4777d0510229d5431034be7d8d51a3cfb82073
|
| 3 |
+
size 7867308040
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5ae505b5935b6c55b15a2707a01e6c6b65bf8d8f36bb5d4d4a4e53f094b96f34
|
| 3 |
+
size 18451221
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": true,
|
| 3 |
+
"backend": "tokenizers",
|
| 4 |
+
"bos_token": "<|im_start|>",
|
| 5 |
+
"clean_up_tokenization_spaces": false,
|
| 6 |
+
"eos_token": "<|im_end|>",
|
| 7 |
+
"is_local": true,
|
| 8 |
+
"legacy": true,
|
| 9 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 10 |
+
"pad_token": "<|im_end|>",
|
| 11 |
+
"sp_model_kwargs": {},
|
| 12 |
+
"spaces_between_special_tokens": false,
|
| 13 |
+
"tokenizer_class": "TokenizersBackend",
|
| 14 |
+
"unk_token": "<unk>",
|
| 15 |
+
"use_default_system_prompt": false
|
| 16 |
+
}
|