qwen3 1.7b tools调用代码示例
·
最近需要用到qwen3的tools的功能,然后需要写个示例看一下具体的实现,需要的自行复制并修改,仅供参考:
from transformers import AutoModelForCausalLM, AutoTokenizer
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
},
}
]
if __name__=="__main__":
model_name = "/workspace/models/Qwen3-1.7B"
# load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto"
)
# Define the user's message
messages = [
{"role": "user", "content": "What's the weather like in Boston today?"},
]
prompt = tokenizer.apply_chat_template(
messages,
tools=tools,
# tokenize=False,
add_generation_prompt=True,
return_dict=True,
return_tensors="pt"
)
print(prompt)
inputs = {k: v.to(model.device) for k, v in prompt.items()}
out = model.generate(**inputs, max_new_tokens=256)
print(tokenizer.decode(out[0][len(inputs["input_ids"][0]):]))
输出结果为:
{'input_ids': tensor([[151644, 8948, 198, 2, 13852, 271, 2610, 1231, 1618,
825, 476, 803, 5746, 311, 7789, 448, 279, 1196,
3239, 382, 2610, 525, 3897, 448, 729, 32628, 2878,
366, 15918, 1472, 15918, 29, 11874, 9492, 510, 27,
15918, 397, 4913, 1313, 788, 330, 1688, 497, 330,
1688, 788, 5212, 606, 788, 330, 455, 11080, 69364,
497, 330, 4684, 788, 330, 1949, 279, 1482, 9104,
304, 264, 2661, 3728, 497, 330, 13786, 788, 5212,
1313, 788, 330, 1700, 497, 330, 13193, 788, 5212,
2527, 788, 5212, 1313, 788, 330, 917, 497, 330,
4684, 788, 330, 785, 3283, 323, 1584, 11, 384,
1302, 13, 5836, 12879, 11, 9183, 14345, 330, 3843,
788, 5212, 1313, 788, 330, 917, 497, 330, 9018,
788, 4383, 66, 40247, 497, 330, 69, 47910, 1341,
38154, 330, 6279, 788, 4383, 2527, 1341, 3417, 532,
522, 15918, 1339, 2461, 1817, 729, 1618, 11, 470,
264, 2951, 1633, 448, 729, 829, 323, 5977, 2878,
220, 151657, 151658, 11874, 9492, 510, 151657, 198, 4913,
606, 788, 366, 1688, 11494, 8066, 330, 16370, 788,
366, 2116, 56080, 40432, 31296, 151658, 151645, 198, 151644,
872, 198, 3838, 594, 279, 9104, 1075, 304, 10196,
3351, 30, 151645, 198, 151644, 77091, 198]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1]])}
<think>
Okay, the user is asking about the weather in Boston today. Let me check the tools available. There's a function called get_current_weather that takes location and unit parameters. The required parameter is location, which they provided as Boston. The unit is optional, so I can choose either celsius or fahrenheit. Since the user didn't specify, maybe I should default to Fahrenheit, which is commonly used in the US. So I'll call the function with location set to "Boston" and unit as "fahrenheit". That should get the current weather details for Boston.
</think>
<tool_call>
{"name": "get_current_weather", "arguments": {"location": "Boston", "unit": "fahrenheit"}}
</tool_call><|im_end|>
从输出结果可以看出,有thinking过程,还有tool call的参数。
更多推荐


所有评论(0)