sanjay920 commited on
Commit
c5e0670
·
verified ·
1 Parent(s): 0251a12

Update README.md

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