Update README.md
Browse files
README.md
CHANGED
@@ -11,6 +11,112 @@ license: apache-2.0
|
|
11 |
language:
|
12 |
- en
|
13 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Uploaded model
|
16 |
|
|
|
11 |
language:
|
12 |
- en
|
13 |
---
|
14 |
+
Function calling requires two step inferences, below is the example:
|
15 |
+
# Step 1:
|
16 |
+
```python
|
17 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
18 |
+
import torch
|
19 |
+
import json
|
20 |
+
|
21 |
+
model_id = "R1_tool_call_Distill-Qwen-1.5B"
|
22 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
23 |
+
model = AutoModelForCausalLM.from_pretrained(
|
24 |
+
model_id,
|
25 |
+
torch_dtype=torch.bfloat16,
|
26 |
+
device_map="auto",
|
27 |
+
)
|
28 |
+
tools = [
|
29 |
+
{
|
30 |
+
"name": "create_contact",
|
31 |
+
"description": "Create a new contact",
|
32 |
+
"parameters": {
|
33 |
+
"type": "object",
|
34 |
+
"properties": {
|
35 |
+
"name": {
|
36 |
+
"type": "string",
|
37 |
+
"description": "The name of the contact"
|
38 |
+
},
|
39 |
+
"email": {
|
40 |
+
"type": "string",
|
41 |
+
"description": "The email address of the contact"
|
42 |
+
}
|
43 |
+
},
|
44 |
+
"required": ["name", "email"]
|
45 |
+
}
|
46 |
+
}
|
47 |
+
]
|
48 |
+
|
49 |
+
|
50 |
+
messages = [
|
51 |
+
{ "role": "user", "content": f"""You have access to these tools, use them if necessary: {tools}
|
52 |
+
I need to create a new contact for my friend John Doe. His email is [email protected]."""
|
53 |
+
}
|
54 |
+
]
|
55 |
+
|
56 |
+
input_ids = tokenizer.apply_chat_template(
|
57 |
+
messages,
|
58 |
+
add_generation_prompt=True,
|
59 |
+
return_tensors="pt"
|
60 |
+
).to(model.device)
|
61 |
+
|
62 |
+
|
63 |
+
outputs = model.generate(
|
64 |
+
input_ids,
|
65 |
+
max_new_tokens=256,
|
66 |
+
do_sample=True,
|
67 |
+
temperature=0.6,
|
68 |
+
top_p=0.9,
|
69 |
+
)
|
70 |
+
response = outputs[0][input_ids.shape[-1]:]
|
71 |
+
print(tokenizer.decode(response, skip_special_tokens=True))
|
72 |
+
# >>
|
73 |
+
# >> <|tool▁calls▁begin|><|tool▁call▁begin|>function<|tool▁sep|>create_contact
|
74 |
+
# >> ```json
|
75 |
+
# >> {"name": "John Doe", "email": "[email protected]"}
|
76 |
+
# >> ```<|tool▁call▁end|><|tool▁calls▁end|>
|
77 |
+
|
78 |
+
# Above is a response from assistant, you need to parse it and execute a tool on your own.
|
79 |
+
```
|
80 |
+
|
81 |
+
# Step 2:
|
82 |
+
```python
|
83 |
+
messages = [
|
84 |
+
{"role": "user", "content": """You have access to these tools, use them if necessary: {tools}\n\nI need to create a new contact for my friend John Doe. His email is [email protected]."""},
|
85 |
+
{"role": "assistant", "content": None, "tool_calls": [
|
86 |
+
{
|
87 |
+
"type": "function",
|
88 |
+
"function": {
|
89 |
+
"name": "create_contact",
|
90 |
+
"arguments": json.dumps({"name": "John Doe", "email": "[email protected]"})
|
91 |
+
}
|
92 |
+
},
|
93 |
+
]},
|
94 |
+
{"role": "tool", "name": "create_contact", "content": """{"status": "success", "message": "Contact for John Doe with email [email protected] has been created successfully."}"""},
|
95 |
+
]
|
96 |
+
|
97 |
+
input_ids = tokenizer.apply_chat_template(
|
98 |
+
messages,
|
99 |
+
add_generation_prompt=True,
|
100 |
+
return_tensors="pt"
|
101 |
+
).to(model.device)
|
102 |
+
|
103 |
+
outputs = model.generate(
|
104 |
+
input_ids,
|
105 |
+
max_new_tokens=256,
|
106 |
+
do_sample=True,
|
107 |
+
temperature=0.6,
|
108 |
+
top_p=0.9,
|
109 |
+
)
|
110 |
+
response = outputs[0][input_ids.shape[-1]:]
|
111 |
+
print(tokenizer.decode(response, skip_special_tokens=True))
|
112 |
+
# >>
|
113 |
+
# >> <think>Based on the user's request, I have created a contact for John Doe with his email address. The tool has successfully created the contact. I will now provide the contact information to the user.</think>
|
114 |
+
|
115 |
+
# >> The contact for John Doe has been successfully created with the email address [email protected]. Please feel free to reach out to him if needed.
|
116 |
+
```
|
117 |
+
|
118 |
+
# Limitations:
|
119 |
+
- The model sometimes refused not to think
|
120 |
|
121 |
# Uploaded model
|
122 |
|