from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "minpeter/Llama-3.2-1B-chatml-tool-v3"

model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

input_text = """<|im_start|>system
You are a function calling AI model. You are provided with function signatures within <tools> </tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions.
<tools>
[{"function": {"description": "Get the current weather in a given location", "name": "get_current_weather", "parameters": {"properties": {"location": {"description": "The city and state, e.g. San Francisco, CA", "type": "string"}, "unit": {"enum": ["celsius", "fahrenheit"], "type": "string"}}, "required": ["location"], "type": "object"}}, "type": "function"}]
</tools>
For each function call return a json object with function name and arguments within <tool_call> </tool_call> tags with the following schema:
<tool_call>
{'arguments': <args-dict>, 'name': <function-name>}
</tool_call><|im_end|>
<|im_start|>user
What is the weather like in Boston?<|im_end|>
<|im_start|>assistant
"""

input_length = len(tokenizer.tokenize(input_text))

input_ids = tokenizer.encode(input_text, return_tensors="pt")

output = model.generate(input_ids, max_new_tokens=600)
generated_ids = output[0][input_length:]

generated_text = tokenizer.decode(generated_ids, skip_special_tokens=True)

tokens = tokenizer.tokenize(generated_text)
token_ids = tokenizer.convert_tokens_to_ids(tokens)

for token, id in zip(tokens, token_ids):
    print(f"Token: {token:20} ID: {id}")

print(f"\nGenerated text:\n{generated_text}")

Output:

Token: Ċ                    ID: 198
Token: <tool_call>          ID: 128013
Token: Ċ                    ID: 198
Token: {'                   ID: 13922
Token: arguments            ID: 16774
Token: ':                   ID: 1232
Token: Ġ{'                  ID: 5473
Token: location             ID: 2588
Token: ':                   ID: 1232
Token: Ġ'                   ID: 364
Token: Boston               ID: 65432
Token: ',                   ID: 518
Token: Ġ'                   ID: 364
Token: unit                 ID: 3928
Token: ':                   ID: 1232
Token: Ġ'                   ID: 364
Token: f                    ID: 69
Token: ahrenheit            ID: 49010
Token: '},                  ID: 25762
Token: Ġ'                   ID: 364
Token: name                 ID: 609
Token: ':                   ID: 1232
Token: Ġ'                   ID: 364
Token: get                  ID: 456
Token: _current             ID: 11327
Token: _weather             ID: 70464
Token: 'Ċ                   ID: 1270
Token: </tool_call>         ID: 128014

Generated text:

<tool_call>
{'arguments': {'location': 'Boston', 'unit': 'fahrenheit'}, 'name': 'get_current_weather'
</tool_call>
Downloads last month
0
Safetensors
Model size
1.5B params
Tensor type
BF16
·
Inference Providers NEW
This model is not currently available via any of the supported third-party Inference Providers, and the model is not deployed on the HF Inference API.

Model tree for minpeter/Llama-3.2-1B-chatml-tool-v3