Views
No views yet
1from unsloth import FastLanguageModel
2from transformers import TextStreamer
3import torch
4from unsloth.chat_templates import get_chat_template
5max_seq_length = 4096
6
7
8model, tokenizer = FastLanguageModel.from_pretrained(
9 model_name = "kesimeg/function-calling-llama-3.1-8B",
10 max_seq_length = max_seq_length,
11)
12tokenizer = get_chat_template(
13 tokenizer,
14 chat_template = "llama-3.1",
15)
16
17FastLanguageModel.for_inference(model)
18
19#For instruction prompts use the code below
20instruction = "What is the integral of cos(x)"
21convos = [{"role":"user","content":instruction}]
22texts = tokenizer.apply_chat_template(convos,tokenize = False, add_generation_prompt = True)
23
24inputs = tokenizer(
25[ texts
26], return_tensors = "pt").to("cuda")
27
28
29text_streamer = TextStreamer(tokenizer, skip_prompt = False)
30outputs = model.generate(**inputs, max_new_tokens = 4096, streamer = text_streamer)
31
32
33#For function calling use the following code
34query = """Make an approprite function call according to user query:I'm trying to get a
35 specific number of products from the catalog, let's say 15, but I don't want
36 to start from the beginning. I want to skip the first 200 products. Can you
37 help me with that?"""
38
39tool_object = """[{"name": "get_products", "description":\
40 "Fetches a list of products from an external API with optional query\
41 parameters for limiting and skipping items in the response.", "parameters":\
42 {"limit": {"description": "The number of products to return.", "type":\
43 "int", "default": ""}, "skip": {"description": "The number of products to\
44 skip in the response.", "type": "int", "default": ""}}}]"""
45
46texts = tokenizer.apply_chat_template(convos,tools=tool_object,tokenize = False, add_generation_prompt = False)
47texts = texts.replace('"parameters": d','"arguments": d') # original tool use function uses parameters our dataset uses arguments
48
49convos = [{"role":"user","content":texts}]
50texts = tokenizer.apply_chat_template(convos,tokenize = False, add_generation_prompt = True)
51
52inputs = tokenizer(
53[ texts
54], return_tensors = "pt").to("cuda")
55
56text_streamer = TextStreamer(tokenizer, skip_prompt = True)
57outputs = model.generate(**inputs, max_new_tokens = 4096, streamer = text_streamer)