Views
No views yet
1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4tokenizer = AutoTokenizer.from_pretrained('hiieu/Vistral-7B-Chat-function-calling')
5model = AutoModelForCausalLM.from_pretrained(
6 'hiieu/Vistral-7B-Chat-function-calling',
7 torch_dtype=torch.bfloat16, # change to torch.float16 if you're using V100
8 device_map="auto",
9 use_cache=True,
10)
11
12functions_metadata = [
13 {
14 "type": "function",
15 "function": {
16 "name": "get_temperature",
17 "description": "get temperature of a city",
18 "parameters": {
19 "type": "object",
20 "properties": {
21 "city": {
22 "type": "string",
23 "description": "name"
24 }
25 },
26 "required": [
27 "city"
28 ]
29 }
30 }
31 }
32]
33
34conversation = [
35 {"role": "system", "content": f"""Bạn là một trợ lý hữu ích có quyền truy cập vào các chức năng sau. Sử dụng chúng nếu cần -\n{str(functions_metadata)} Để sử dụng các chức năng này, hãy phản hồi với:\n<functioncall> {{\\"name\\": \\"function_name\\", \\"arguments\\": {{\\"arg_1\\": \\"value_1\\", \\"arg_1\\": \\"value_1\\", ...}} }} </functioncall>\n\nTrường hợp đặc biệt bạn phải xử lý:\n - Nếu không có chức năng nào khớp với yêu cầu của người dùng, bạn sẽ phản hồi một cách lịch sự rằng bạn không thể giúp được.""" },
36 {"role": "user", "content": "Thời tiết ở Hà Nội đang là bao nhiêu độ"},
37 {"role": "assistant", "content": """<functioncall> {"name": "get_temperature", "arguments": '{"city": "Hà Nội"}'} </functioncall>"""},
38 {"role": "user", "content": """<function_response> {"temperature" : "20 C"} </function_response>"""},
39]
40
41input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
42
43out_ids = model.generate(
44 input_ids=input_ids,
45 max_new_tokens=768,
46 do_sample=True,
47 top_p=0.95,
48 top_k=40,
49 temperature=0.1,
50 repetition_penalty=1.05,
51)
52assistant = tokenizer.batch_decode(out_ids[:, input_ids.size(1): ], skip_special_tokens=True)[0].strip()
53print("Assistant: ", assistant)
54# >> Assistant: Thời tiết ở Hà Nội hiện tại là khoảng 20 độ C.