File size: 5,582 Bytes
905da01
 
 
2c9ac07
 
 
 
 
5f8ec52
2c9ac07
5f8ec52
 
 
 
2c9ac07
 
5f8ec52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c9ac07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f8ec52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c9ac07
 
 
5f8ec52
 
 
 
 
 
 
 
 
 
 
 
2c9ac07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f8ec52
 
 
 
 
 
 
2c9ac07
 
 
5f8ec52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c3fedd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c9ac07
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
---
base_model:
- codellama/CodeLlama-7b-hf
language:
- en
license: apache-2.0
library_name: transformers
pipeline_tag: text-generation
---

# **TL-CodeLLaMA-2**

TL-CodeLLaMA-2 is a model designed for tool use, built upon CodeLLaMA-7b. It is trained on 1,217 data samples using the *TL-Training* framework and demonstrates effective performance across a variety of tool use tasks. More information can be found in the paper "[TL-Training: A Task-Feature-Based Framework for Training Large Language Models in Tool Use](https://www.arxiv.org/abs/2412.15495)".

Code: https://github.com/Junjie-Ye/TL-Training

# Model Use

## Requirements

To use this model, please make sure to install transformers:
```bash
pip install transformers
```

## Data Orgnization

The data needs to be organized in the following format:

```json
[
    {
        "role": "System",
        "content": "Function:
def random_advice():
    \"\"\"
    Returns a random advice slip as a slip object.
    \"\"\"

Function:
def advice_by_id(slip_id:str):
    \"\"\"
    If an advice slip is found with the corresponding {slip_id}, a slip object is returned.

    Args:
        slip_id (string): The unique ID of this advice slip.
    \"\"\"

Function:
def search_advice(query:str):
    \"\"\"
    If an advice slip is found, containing the corresponding search term in {query}, an array of slip objects is returned inside a search object.

    Args:
        query (string): The search query provided.
    \"\"\"

Function:
def ask_to_user(question:str):
    \"\"\"
    You can ask user for guidance when you think you need more information to handle the task, but you should use this tool as less as you can.

    Args:
        question (string): The question you want to ask to user.
    \"\"\"

Function:
def finish(answer:str):
    \"\"\"
    Finish the task and give your answer.

    Args:
        answer (string): Your answer for the task.
    \"\"\"

"
    },
    {
        "role": "User",
        "content": "Could you give me some advice about 'love'?"
    },
    {
        "role": "Assistant",
        "content": "search_advice(query = 'love') "
    },
    {
        "role": "Output",
        "content": "..."
    }
]
```

## Chat Template

The chat template is:

```jinja
{% for message in messages %}{{message['role'] + ': ' + message['content']}}{% if loop.last %}{% if add_generation_prompt %}{{ '
Assistant:' }}{% else %}{{ '</s>'}}{% endif %}{% else %}{{ '
' }}{% endif %}{% endfor %}
```

## Inference

```python
from transformers import AutoModelForCausalLM, AutoTokenizer

model_path = "Junjie-Ye/TL-CodeLLaMA-2"

data = [
    {
        "role": "System",
        "content": "Function:
def random_advice():
    \"\"\"
    Returns a random advice slip as a slip object.
    \"\"\"

Function:
def advice_by_id(slip_id:str):
    \"\"\"
    If an advice slip is found with the corresponding {slip_id}, a slip object is returned.

    Args:
        slip_id (string): The unique ID of this advice slip.
    \"\"\"

Function:
def search_advice(query:str):
    \"\"\"
    If an advice slip is found, containing the corresponding search term in {query}, an array of slip objects is returned inside a search object.

    Args:
        query (string): The search query provided.
    \"\"\"

Function:
def ask_to_user(question:str):
    \"\"\"
    You can ask user for guidance when you think you need more information to handle the task, but you should use this tool as less as you can.

    Args:
        question (string): The question you want to ask to user.
    \"\"\"

Function:
def finish(answer:str):
    \"\"\"
    Finish the task and give your answer.

    Args:
        answer (string): Your answer for the task.
    \"\"\"

"
    },
    {
        "role": "User",
        "content": "Could you give me some advice about 'love'?"
    }
]

chat_template = "{% for message in messages %}{{message['role'] + ': ' + message['content']}}{% if loop.last %}{% if add_generation_prompt %}{{ '
Assistant:' }}{% else %}{{ '</s>'}}{% endif %}{% else %}{{ '
' }}{% endif %}{% endfor %}"

model = AutoModelForCausalLM.from_pretrained(
    model_path,
    torch_dtype="auto",
    device_map="auto",
    trust_remote_code=True
).eval()

tokenizer = AutoTokenizer.from_pretrained(model_path,
                                            padding_side="left",
                                            trust_remote_code=True)
if tokenizer.pad_token_id is None:
    tokenizer.pad_token_id = tokenizer.eos_token_id

text = tokenizer.apply_chat_template(
        data,
        tokenize=False,
        chat_template=chat_template,
        add_generation_prompt=add_generation_prompt
    )
model_inputs = tokenizer(
    [text], return_tensors="pt", padding=True).to("cuda")

generated_ids = model.generate(
    max_new_tokens=1024,
    **model_inputs,
)
generated_ids = [
    output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)

print(response)
```

# Citation

If you find this model useful in your research, please cite:

```bibtex
@misc{TL-Training,
      title={TL-Training: A Task-Feature-Based Framework for Training Large Language Models in Tool Use}, 
      author={Junjie Ye and Yilong Wu and Sixian Li and Yuming Yang and Tao Gui and Qi Zhang and Xuanjing Huang and Peng Wang and Zhongchao Shi and Jianping Fan and Zhengyin Du},
      year={2024},
      eprint={2412.15495},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2412.15495}, 
}
```