Here we use 4 functions for a simple math chaining question:
python
1functions =[2{3'type':'function',4'function':{5'name':'addition',6'description':"Adds two numbers together",7'parameters':{8'type':'object',9'properties':{10'a':{11'description':'First number to add',12'type':'string'13},14'b':{15'description':'Second number to add',16'type':'string'17}18},19'required':[]20}21}22},23{24'type':'function',25'function':{26'name':'subtraction',27'description':"Subtracts two numbers",28'parameters':{29'type':'object',30'properties':{31'a':{32'description':'First number to be subtracted from',33'type':'string'34},35'b':{36'description':'Number to subtract',37'type':'string'38}39},40'required':[]41}42}43},44{45'type':'function',46'function':{47'name':'multiplication',48'description':"Multiply two numbers together",49'parameters':{50'type':'object',51'properties':{52'a':{53'description':'First number to multiply',54'type':'string'55},56'b':{57'description':'Second number to multiply',58'type':'string'59}60},61'required':[]62}63}64},65{66'type':'function',67'function':{68'name':'division',69'description':"Divide two numbers",70'parameters':{71'type':'object',72'properties':{73'a':{74'description':'First number to use as the dividend',75'type':'string'76},77'b':{78'description':'Second number to use as the divisor',79'type':'string'80}81},82'required':[]83}84}85},86]
3. Start the conversation
python
1messages =[2{"role":"system","content":"You are a helpful assistant."},3{"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"},4]56defrun_model(messages, functions):7## Format messages in Rubra's format8 formatted_msgs = preprocess_input(msgs=messages, tools=functions)910 text = tokenizer.apply_chat_template(11 formatted_msgs,12 tokenize=False,13 add_generation_prompt=True14)15 model_inputs = tokenizer([text], return_tensors="pt").to(model.device)1617 generated_ids = model.generate(18 model_inputs.input_ids,19 max_new_tokens=51220)21 generated_ids =[22 output_ids[len(input_ids):]for input_ids, output_ids inzip(model_inputs.input_ids, generated_ids)23]2425 response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]26return response
272829raw_output = run_model(messages, functions)30# Check if there's a function call31function_call = postprocess_output(raw_output)32if function_call:33print(function_call)34else:35print(raw_output)
You should see this output, which is a function call made by the AI assistant:
4. Add Executed Tool Result to Message History & Continue the Conversation
python
1if function_call:2# append the assistant tool call msg3 messages.append({"role":"assistant","tool_calls": function_call})4# append the result of the tool call in openai format, in this case, the value of add 6 to 4 is 10.5 messages.append({'role':'tool','tool_call_id': function_call[0]["id"],'name': function_call[0]["function"]["name"],'content':'10'})6 raw_output1 = run_model(messages, functions)7# Check if there's a function call89 function_call = postprocess_output(raw_output1)10if function_call:11print(function_call)12else:13print(raw_output)
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.
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 }
}