Views
No views yet
1from transformers import AutoTokenizer, AutoModelForCausalLM
2import torch
3
4model_id = "hiieu/Meta-Llama-3-8B-Instruct-function-calling-json-mode"
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6model = AutoModelForCausalLM.from_pretrained(
7 model_id,
8 torch_dtype=torch.bfloat16,
9 device_map="auto",
10)
11
12messages = [
13 {"role": "system", "content": "You are a helpful assistant, answer in JSON with key \"message\""},
14 {"role": "user", "content": "Who are you?"},
15]
16
17input_ids = tokenizer.apply_chat_template(
18 messages,
19 add_generation_prompt=True,
20 return_tensors="pt"
21).to(model.device)
22
23terminators = [
24 tokenizer.eos_token_id,
25 tokenizer.convert_tokens_to_ids("<|eot_id|>")
26]
27
28outputs = model.generate(
29 input_ids,
30 max_new_tokens=256,
31 eos_token_id=terminators,
32 do_sample=True,
33 temperature=0.6,
34 top_p=0.9,
35)
36response = outputs[0][input_ids.shape[-1]:]
37print(tokenizer.decode(response, skip_special_tokens=True))
38# >> {"message": "I am a helpful assistant, with access to a vast amount of information. I can help you with tasks such as answering questions, providing definitions, translating text, and more. Feel free to ask me anything!"}1functions_metadata = [
2 {
3 "type": "function",
4 "function": {
5 "name": "get_temperature",
6 "description": "get temperature of a city",
7 "parameters": {
8 "type": "object",
9 "properties": {
10 "city": {
11 "type": "string",
12 "description": "name"
13 }
14 },
15 "required": [
16 "city"
17 ]
18 }
19 }
20 }
21]
22
23messages = [
24 { "role": "system", "content": f"""You are a helpful assistant with access to the following functions: \n {str(functions_metadata)}\n\nTo use these functions respond with:\n<functioncall> {{ "name": "function_name", "arguments": {{ "arg_1": "value_1", "arg_1": "value_1", ... }} }} </functioncall>\n\nEdge cases you must handle:\n - If there are no functions that match the user request, you will respond politely that you cannot help."""},
25 { "role": "user", "content": "What is the temperature in Tokyo right now?"}
26]
27
28input_ids = tokenizer.apply_chat_template(
29 messages,
30 add_generation_prompt=True,
31 return_tensors="pt"
32).to(model.device)
33
34terminators = [
35 tokenizer.eos_token_id,
36 tokenizer.convert_tokens_to_ids("<|eot_id|>")
37]
38
39outputs = model.generate(
40 input_ids,
41 max_new_tokens=256,
42 eos_token_id=terminators,
43 do_sample=True,
44 temperature=0.6,
45 top_p=0.9,
46)
47response = outputs[0][input_ids.shape[-1]:]
48print(tokenizer.decode(response, skip_special_tokens=True))
49# >> <functioncall> {"name": "get_temperature", "arguments": '{"city": "Tokyo"}'} </functioncall>"""}1messages = [
2 { "role": "system", "content": f"""You are a helpful assistant with access to the following functions: \n {str(functions_metadata)}\n\nTo use these functions respond with:\n<functioncall> {{ "name": "function_name", "arguments": {{ "arg_1": "value_1", "arg_1": "value_1", ... }} }} </functioncall>\n\nEdge cases you must handle:\n - If there are no functions that match the user request, you will respond politely that you cannot help."""},
3 { "role": "user", "content": "What is the temperature in Tokyo right now?"},
4 # You will get the previous prediction, extract it will the tag <functioncall>
5 # execute the function and append it to the messages like below:
6 { "role": "assistant", "content": """<functioncall> {"name": "get_temperature", "arguments": '{"city": "Tokyo"}'} </functioncall>"""},
7 { "role": "user", "content": """<function_response> {"temperature":30 C} </function_response>"""}
8]
9
10input_ids = tokenizer.apply_chat_template(
11 messages,
12 add_generation_prompt=True,
13 return_tensors="pt"
14).to(model.device)
15
16terminators = [
17 tokenizer.eos_token_id,
18 tokenizer.convert_tokens_to_ids("<|eot_id|>")
19]
20
21outputs = model.generate(
22 input_ids,
23 max_new_tokens=256,
24 eos_token_id=terminators,
25 do_sample=True,
26 temperature=0.6,
27 top_p=0.9,
28)
29response = outputs[0][input_ids.shape[-1]:]
30print(tokenizer.decode(response, skip_special_tokens=True))
31# >> The current temperature in Tokyo is 30 degrees Celsius.