Update README.md
Browse files
README.md
CHANGED
|
@@ -72,8 +72,182 @@ You can use the model with the Hugging Face `transformers` and the rubra library
|
|
| 72 |
pip install rubra_tools torch==2.3.0 transformers
|
| 73 |
```
|
| 74 |
|
|
|
|
| 75 |
```python
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
```
|
| 78 |
|
| 79 |
## Training Hyperparameters
|
|
|
|
| 72 |
pip install rubra_tools torch==2.3.0 transformers
|
| 73 |
```
|
| 74 |
|
| 75 |
+
### 1. Load the Model
|
| 76 |
```python
|
| 77 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 78 |
+
import torch
|
| 79 |
+
from rubra_tools import preprocess_input, postprocess_output
|
| 80 |
+
|
| 81 |
+
model_id = "rubra-ai/Meta-Llama-3-8B-Instruct"
|
| 82 |
+
|
| 83 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 84 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 85 |
+
model_id,
|
| 86 |
+
torch_dtype=torch.bfloat16,
|
| 87 |
+
device_map="auto",
|
| 88 |
+
)
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
### 2. Define Functions
|
| 92 |
+
|
| 93 |
+
Here we use 4 functions for a simple math chaining question:
|
| 94 |
+
```python
|
| 95 |
+
functions = [
|
| 96 |
+
{
|
| 97 |
+
'type': 'function',
|
| 98 |
+
'function': {
|
| 99 |
+
'name': 'addition',
|
| 100 |
+
'description': "Adds two numbers together",
|
| 101 |
+
'parameters': {
|
| 102 |
+
'type': 'object',
|
| 103 |
+
'properties': {
|
| 104 |
+
'a': {
|
| 105 |
+
'description': 'First number to add',
|
| 106 |
+
'type': 'string'
|
| 107 |
+
},
|
| 108 |
+
'b': {
|
| 109 |
+
'description': 'Second number to add',
|
| 110 |
+
'type': 'string'
|
| 111 |
+
}
|
| 112 |
+
},
|
| 113 |
+
'required': []
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
},
|
| 117 |
+
{
|
| 118 |
+
'type': 'function',
|
| 119 |
+
'function': {
|
| 120 |
+
'name': 'subtraction',
|
| 121 |
+
'description': "Subtracts two numbers",
|
| 122 |
+
'parameters': {
|
| 123 |
+
'type': 'object',
|
| 124 |
+
'properties': {
|
| 125 |
+
'a': {
|
| 126 |
+
'description': 'First number to be subtracted from',
|
| 127 |
+
'type': 'string'
|
| 128 |
+
},
|
| 129 |
+
'b': {
|
| 130 |
+
'description': 'Number to subtract',
|
| 131 |
+
'type': 'string'
|
| 132 |
+
}
|
| 133 |
+
},
|
| 134 |
+
'required': []
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
},
|
| 138 |
+
{
|
| 139 |
+
'type': 'function',
|
| 140 |
+
'function': {
|
| 141 |
+
'name': 'multiplication',
|
| 142 |
+
'description': "Multiply two numbers together",
|
| 143 |
+
'parameters': {
|
| 144 |
+
'type': 'object',
|
| 145 |
+
'properties': {
|
| 146 |
+
'a': {
|
| 147 |
+
'description': 'First number to multiply',
|
| 148 |
+
'type': 'string'
|
| 149 |
+
},
|
| 150 |
+
'b': {
|
| 151 |
+
'description': 'Second number to multiply',
|
| 152 |
+
'type': 'string'
|
| 153 |
+
}
|
| 154 |
+
},
|
| 155 |
+
'required': []
|
| 156 |
+
}
|
| 157 |
+
}
|
| 158 |
+
},
|
| 159 |
+
{
|
| 160 |
+
'type': 'function',
|
| 161 |
+
'function': {
|
| 162 |
+
'name': 'division',
|
| 163 |
+
'description': "Divide two numbers",
|
| 164 |
+
'parameters': {
|
| 165 |
+
'type': 'object',
|
| 166 |
+
'properties': {
|
| 167 |
+
'a': {
|
| 168 |
+
'description': 'First number to use as the dividend',
|
| 169 |
+
'type': 'string'
|
| 170 |
+
},
|
| 171 |
+
'b': {
|
| 172 |
+
'description': 'Second number to use as the divisor',
|
| 173 |
+
'type': 'string'
|
| 174 |
+
}
|
| 175 |
+
},
|
| 176 |
+
'required': []
|
| 177 |
+
}
|
| 178 |
+
}
|
| 179 |
+
},
|
| 180 |
+
]
|
| 181 |
+
```
|
| 182 |
+
|
| 183 |
+
### 3. Start the conversation
|
| 184 |
+
```python
|
| 185 |
+
messages = [
|
| 186 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 187 |
+
{"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"},
|
| 188 |
+
]
|
| 189 |
+
|
| 190 |
+
def run_model(messages, functions):
|
| 191 |
+
## Format messages in Rubra's format
|
| 192 |
+
formatted_msgs = preprocess_input(msgs=messages, tools=functions)
|
| 193 |
+
|
| 194 |
+
input_ids = tokenizer.apply_chat_template(
|
| 195 |
+
formatted_msgs,
|
| 196 |
+
add_generation_prompt=True,
|
| 197 |
+
return_tensors="pt"
|
| 198 |
+
).to(model.device)
|
| 199 |
+
|
| 200 |
+
terminators = [
|
| 201 |
+
tokenizer.eos_token_id,
|
| 202 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
| 203 |
+
]
|
| 204 |
+
|
| 205 |
+
outputs = model.generate(
|
| 206 |
+
input_ids,
|
| 207 |
+
max_new_tokens=1000,
|
| 208 |
+
eos_token_id=terminators,
|
| 209 |
+
do_sample=True,
|
| 210 |
+
temperature=0.1,
|
| 211 |
+
top_p=0.9,
|
| 212 |
+
)
|
| 213 |
+
response = outputs[0][input_ids.shape[-1]:]
|
| 214 |
+
raw_output = tokenizer.decode(response, skip_special_tokens=True)
|
| 215 |
+
return raw_output
|
| 216 |
+
|
| 217 |
+
raw_output = run_model(messages, functions)
|
| 218 |
+
# Check if there's a function call
|
| 219 |
+
function_call = postprocess_output(raw_output)
|
| 220 |
+
if function_call:
|
| 221 |
+
print(function_call)
|
| 222 |
+
else:
|
| 223 |
+
print(raw_output)
|
| 224 |
+
```
|
| 225 |
+
|
| 226 |
+
You should see this output, which is a function call made by the AI assistant:
|
| 227 |
+
```
|
| 228 |
+
[{'id': 'fc65a533', 'function': {'name': 'addition', 'arguments': '{"a": "4", "b": "6"}'}, 'type': 'function'}]
|
| 229 |
+
```
|
| 230 |
+
|
| 231 |
+
### 4. Add Executed Tool Result to Message History & Continue the Conversation
|
| 232 |
+
|
| 233 |
+
```python
|
| 234 |
+
if function_call:
|
| 235 |
+
# append the assistant tool call msg
|
| 236 |
+
messages.append({"role": "assistant", "tool_calls": function_call})
|
| 237 |
+
# append the result of the tool call in openai format, in this case, the value of add 6 to 4 is 10.
|
| 238 |
+
messages.append({'role': 'tool', 'tool_call_id': function_call[0]["id"], 'name': function_call[0]["function"]["name"], 'content': '10'})
|
| 239 |
+
raw_output = run_model(messages, functions)
|
| 240 |
+
# Check if there's a function call
|
| 241 |
+
function_call = postprocess_output(raw_output)
|
| 242 |
+
if function_call:
|
| 243 |
+
print(function_call)
|
| 244 |
+
else:
|
| 245 |
+
print(raw_output)
|
| 246 |
+
```
|
| 247 |
+
|
| 248 |
+
The LLM will make another call
|
| 249 |
+
```
|
| 250 |
+
[{'id': '2ffc3de4', 'function': {'name': 'addition', 'arguments': '{"a": "10", "b": "2"}'}, 'type': 'function'}]
|
| 251 |
```
|
| 252 |
|
| 253 |
## Training Hyperparameters
|