File size: 8,718 Bytes
bed0be1
659aebf
5780cdd
 
659aebf
8f295c5
659aebf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5780cdd
659aebf
 
 
bed0be1
 
2f0b1c1
 
 
8f295c5
2f0b1c1
 
 
 
 
 
 
 
 
 
4ddae95
2f0b1c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d32f93
2f0b1c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53537bd
2f0b1c1
53537bd
 
 
 
2f0b1c1
53537bd
 
 
 
 
 
2f0b1c1
 
53537bd
 
 
2f0b1c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53537bd
2f0b1c1
53537bd
 
2f0b1c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e926725
2f0b1c1
 
 
 
 
 
 
 
 
4d84409
 
 
 
 
 
7ff5b5d
4d84409
 
 
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
---
license: apache-2.0
library_name: transformers
pipeline_tag: text-generation
model-index:
- name: Rubra-Qwen2-7B-Instruct
  results:
  - task:
      type: text-generation
    dataset:
      type: MMLU
      name: MMLU
    metrics:
    - type: 5-shot
      value: 68.88
      verified: false
  - task:
      type: text-generation
    dataset:
      type: GPQA
      name: GPQA
    metrics:
    - type: 0-shot
      value: 30.36
      verified: false
  - task:
      type: text-generation
    dataset:
      type: GSM-8K
      name: GSM-8K
    metrics:
    - type: 8-shot, CoT
      value: 75.82
      verified: false
  - task:
      type: text-generation
    dataset:
      type: MATH
      name: MATH
    metrics:
    - type: 4-shot, CoT
      value: 28.72
      verified: false
  - task:
      type: text-generation
    dataset:
      type: MT-bench
      name: MT-bench
    metrics:
    - type: GPT-4 as Judge
      value: 8.08
      verified: false
tags:
- function-calling
- tool-calling
- agentic
- rubra
- conversational
language:
- en
- zh
---

# Qwen2 7B Instruct

## Model description
The model is the result of further post-training [Qwen/Qwen2-7B-Instruct](https://huggingface.co/Qwen/Qwen2-7B-Instruct). It is capable of complex multi-turn tool/function calling.

## Training

The model was post-trained (freeze tuned & DPO) on a proprietary dataset consisting of diverse function calling, chat, and instruct data.

## How to use

You can use the model with the Hugging Face `transformers` and the rubra library [rubra-tools](https://github.com/rubra-ai/rubra-tools) as follows:

```
pip install rubra_tools torch==2.3.0 transformers accelerate
```

You also need Node.js and npm installed. Once you do, install the `jsonrepair` package - it's used to fix some rare hallucinations by the model.

```
npm install jsonrepair
```

### 1. Load the Model
```python
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
from rubra_tools import preprocess_input, postprocess_output

model_id = "rubra-ai/Qwen2-7B-Instruct"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype="auto",
    device_map="auto",
)
```

### 2. Define Functions

Here we use 4 functions for a simple math chaining question:
```python
functions = [
    {
            'type': 'function',
            'function': {
                'name': 'addition',
                'description': "Adds two numbers together",
                'parameters': {
                    'type': 'object',
                    'properties': {
                        'a': {
                            'description': 'First number to add',
                            'type': 'string'
                        },
                        'b': {
                            'description': 'Second number to add',
                            'type': 'string'
                        }
                    },
                    'required': []
                }
            }
        },
        {
            'type': 'function',
            'function': {
                'name': 'subtraction',
                'description': "Subtracts two numbers",
                'parameters': {
                    'type': 'object',
                    'properties': {
                        'a': {
                            'description': 'First number to be subtracted from',
                            'type': 'string'
                        },
                        'b': {
                            'description': 'Number to subtract',
                            'type': 'string'
                        }
                    },
                    'required': []
                }
            }
        },
        {
            'type': 'function',
            'function': {
                'name': 'multiplication',
                'description': "Multiply two numbers together",
                'parameters': {
                    'type': 'object',
                    'properties': {
                        'a': {
                            'description': 'First number to multiply',
                            'type': 'string'
                        },
                        'b': {
                            'description': 'Second number to multiply',
                            'type': 'string'
                        }
                    },
                    'required': []
                }
            }
        },
        {
            'type': 'function',
            'function': {
                'name': 'division',
                'description': "Divide two numbers",
                'parameters': {
                    'type': 'object',
                    'properties': {
                        'a': {
                            'description': 'First number to use as the dividend',
                            'type': 'string'
                        },
                        'b': {
                            'description': 'Second number to use as the divisor',
                            'type': 'string'
                        }
                    },
                    'required': []
                }
            }
        },
]
```

### 3. Start the conversation
```python
messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "What is the result of four plus six? Take the result and add 2? Then multiply by 5 and then divide by two"},
]

def run_model(messages, functions):
    ## Format messages in Rubra's format
    formatted_msgs = preprocess_input(msgs=messages, tools=functions)

    text = tokenizer.apply_chat_template(
        formatted_msgs,
        tokenize=False,
        add_generation_prompt=True
    )
    model_inputs = tokenizer([text], return_tensors="pt").to(model.device)

    generated_ids = model.generate(
        model_inputs.input_ids,
        max_new_tokens=512
    )
    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)[0]
    return response


raw_output = run_model(messages, functions)
# Check if there's a function call
function_call = postprocess_output(raw_output)
if function_call:
    print(function_call)
else:
    print(raw_output)
```

You should see this output, which is a function call made by the AI assistant:
```
[{'id': 'fc65a533', 'function': {'name': 'addition', 'arguments': '{"a": "4", "b": "6"}'}, 'type': 'function'}]
```

### 4. Add Executed Tool Result to Message History & Continue the Conversation

```python
if function_call:
    # append the assistant tool call msg
    messages.append({"role": "assistant", "tool_calls": function_call})
    # append the result of the tool call in openai format, in this case, the value of add 6 to 4 is 10.
    messages.append({'role': 'tool', 'tool_call_id': function_call[0]["id"], 'name': function_call[0]["function"]["name"], 'content': '10'})
    raw_output1 = run_model(messages, functions)
    # Check if there's a function call

    function_call = postprocess_output(raw_output1)
    if function_call:
        print(function_call)
    else:
        print(raw_output)
```

The LLM will make another call
```
[{'id': '2ffc3de4', 'function': {'name': 'addition', 'arguments': '{"a": "10", "b": "2"}'}, 'type': 'function'}]
```

## Framework Versions

- Transformers 4.41.2
- Pytorch 2.3.1+cu121
- Datasets 2.19.2
- Tokenizers 0.19.1

## Limitations and Bias

While the model performs well on a wide range of tasks, it may still produce biased or incorrect outputs. Users should exercise caution and critical judgment when using the model in sensitive or high-stakes applications. The model's outputs are influenced by the data it was trained on, which may contain inherent biases.

## Ethical Considerations

Users should ensure that the deployment of this model adheres to ethical guidelines and consider the potential societal impact of the generated text. Misuse of the model for generating harmful or misleading content is strongly discouraged.

## Acknowledgements

We would like to thank Alibaba Cloud for the model.

## Contact Information

For questions or comments about the model, please reach out to [the rubra team](mailto:rubra@acorn.io).

## Citation

If you use this work, please cite it as:

```
@misc {rubra_ai_2024,
	author       = { Sanjay Nadhavajhala and Yingbei Tong },
	title        = { Rubra-Qwen2-7B-Instruct },
	year         = 2024,
	url          = { https://huggingface.co/rubra-ai/Qwen2-7B-Instruct },
	doi          = { 10.57967/hf/2683 },
	publisher    = { Hugging Face }
}
```