sanjay920 commited on
Commit
2f0b1c1
·
verified ·
1 Parent(s): 659aebf

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +230 -1
README.md CHANGED
@@ -58,4 +58,233 @@ language:
58
  - zh
59
  ---
60
 
61
- # Qwen2 7B Instruct
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  - zh
59
  ---
60
 
61
+ # Qwen2 7B Instruct
62
+
63
+ ## Model description
64
+ The model is the result of further post-training [meta-llama/Meta-Llama-3-8B-Instruct](https://huggingface.co/meta-llama/Meta-Llama-3-8B-Instruct). It is capable of complex tool/function calling.
65
+
66
+ ## Training
67
+
68
+ The model was post-trained (freeze tuned & DPO) on a proprietary dataset consisting of diverse function calling, chat, and instruct data.
69
+
70
+ ## How to use
71
+
72
+ 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:
73
+
74
+ ```
75
+ pip install rubra_tools torch==2.3.0 transformers
76
+ ```
77
+
78
+ 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.
79
+
80
+ ```
81
+ npm install jsonrepair
82
+ ```
83
+
84
+ ### 1. Load the Model
85
+ ```python
86
+ from transformers import AutoTokenizer, AutoModelForCausalLM
87
+ import torch
88
+ from rubra_tools import preprocess_input, postprocess_output
89
+
90
+ model_id = "rubra-ai/Qwen2-7B-Instruct"
91
+
92
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
93
+ model = AutoModelForCausalLM.from_pretrained(
94
+ model_id,
95
+ torch_dtype=torch.bfloat16,
96
+ device_map="auto",
97
+ )
98
+ ```
99
+
100
+ ### 2. Define Functions
101
+
102
+ Here we use 4 functions for a simple math chaining question:
103
+ ```python
104
+ functions = [
105
+ {
106
+ 'type': 'function',
107
+ 'function': {
108
+ 'name': 'addition',
109
+ 'description': "Adds two numbers together",
110
+ 'parameters': {
111
+ 'type': 'object',
112
+ 'properties': {
113
+ 'a': {
114
+ 'description': 'First number to add',
115
+ 'type': 'string'
116
+ },
117
+ 'b': {
118
+ 'description': 'Second number to add',
119
+ 'type': 'string'
120
+ }
121
+ },
122
+ 'required': []
123
+ }
124
+ }
125
+ },
126
+ {
127
+ 'type': 'function',
128
+ 'function': {
129
+ 'name': 'subtraction',
130
+ 'description': "Subtracts two numbers",
131
+ 'parameters': {
132
+ 'type': 'object',
133
+ 'properties': {
134
+ 'a': {
135
+ 'description': 'First number to be subtracted from',
136
+ 'type': 'string'
137
+ },
138
+ 'b': {
139
+ 'description': 'Number to subtract',
140
+ 'type': 'string'
141
+ }
142
+ },
143
+ 'required': []
144
+ }
145
+ }
146
+ },
147
+ {
148
+ 'type': 'function',
149
+ 'function': {
150
+ 'name': 'multiplication',
151
+ 'description': "Multiply two numbers together",
152
+ 'parameters': {
153
+ 'type': 'object',
154
+ 'properties': {
155
+ 'a': {
156
+ 'description': 'First number to multiply',
157
+ 'type': 'string'
158
+ },
159
+ 'b': {
160
+ 'description': 'Second number to multiply',
161
+ 'type': 'string'
162
+ }
163
+ },
164
+ 'required': []
165
+ }
166
+ }
167
+ },
168
+ {
169
+ 'type': 'function',
170
+ 'function': {
171
+ 'name': 'division',
172
+ 'description': "Divide two numbers",
173
+ 'parameters': {
174
+ 'type': 'object',
175
+ 'properties': {
176
+ 'a': {
177
+ 'description': 'First number to use as the dividend',
178
+ 'type': 'string'
179
+ },
180
+ 'b': {
181
+ 'description': 'Second number to use as the divisor',
182
+ 'type': 'string'
183
+ }
184
+ },
185
+ 'required': []
186
+ }
187
+ }
188
+ },
189
+ ]
190
+ ```
191
+
192
+ ### 3. Start the conversation
193
+ ```python
194
+ messages = [
195
+ {"role": "system", "content": "You are a helpful assistant."},
196
+ {"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"},
197
+ ]
198
+
199
+ def run_model(messages, functions):
200
+ ## Format messages in Rubra's format
201
+ formatted_msgs = preprocess_input(msgs=messages, tools=functions)
202
+
203
+ input_ids = tokenizer.apply_chat_template(
204
+ formatted_msgs,
205
+ add_generation_prompt=True,
206
+ return_tensors="pt"
207
+ ).to(model.device)
208
+
209
+ terminators = [
210
+ tokenizer.eos_token_id,
211
+ tokenizer.convert_tokens_to_ids("")
212
+ ]
213
+
214
+ outputs = model.generate(
215
+ input_ids,
216
+ max_new_tokens=1000,
217
+ eos_token_id=terminators,
218
+ do_sample=True,
219
+ temperature=0.1,
220
+ top_p=0.9,
221
+ )
222
+ response = outputs[0][input_ids.shape[-1]:]
223
+ raw_output = tokenizer.decode(response, skip_special_tokens=True)
224
+ return raw_output
225
+
226
+ raw_output = run_model(messages, functions)
227
+ # Check if there's a function call
228
+ function_call = postprocess_output(raw_output)
229
+ if function_call:
230
+ print(function_call)
231
+ else:
232
+ print(raw_output)
233
+ ```
234
+
235
+ You should see this output, which is a function call made by the AI assistant:
236
+ ```
237
+ [{'id': 'fc65a533', 'function': {'name': 'addition', 'arguments': '{"a": "4", "b": "6"}'}, 'type': 'function'}]
238
+ ```
239
+
240
+ ### 4. Add Executed Tool Result to Message History & Continue the Conversation
241
+
242
+ ```python
243
+ if function_call:
244
+ # append the assistant tool call msg
245
+ messages.append({"role": "assistant", "tool_calls": function_call})
246
+ # append the result of the tool call in openai format, in this case, the value of add 6 to 4 is 10.
247
+ messages.append({'role': 'tool', 'tool_call_id': function_call[0]["id"], 'name': function_call[0]["function"]["name"], 'content': '10'})
248
+ raw_output = run_model(messages, functions)
249
+ # Check if there's a function call
250
+ function_call = postprocess_output(raw_output)
251
+ if function_call:
252
+ print(function_call)
253
+ else:
254
+ print(raw_output)
255
+ ```
256
+
257
+ The LLM will make another call
258
+ ```
259
+ [{'id': '2ffc3de4', 'function': {'name': 'addition', 'arguments': '{"a": "10", "b": "2"}'}, 'type': 'function'}]
260
+ ```
261
+
262
+ ## Framework Versions
263
+
264
+ - Transformers 4.41.2
265
+ - Pytorch 2.3.1+cu121
266
+ - Datasets 2.19.2
267
+ - Tokenizers 0.19.1
268
+
269
+ ## Limitations and Bias
270
+
271
+ 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.
272
+
273
+ ## Ethical Considerations
274
+
275
+ 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.
276
+
277
+ ## Acknowledgements
278
+
279
+ We would like to thank Meta for the model and LLaMA-Factory for training utilities.
280
+
281
+ ## Contact Information
282
+
283
+ For questions or comments about the model, please reach out to [the rubra team](mailto:[email protected]).
284
+
285
+ ## Citation
286
+
287
+ If you use this work, please cite it as:
288
+
289
+
290
+