Views
No views yet
1from peft import PeftModel
2from transformers import AutoModelForCausalLM
3from peft import AutoPeftModelForCausalLM
4from transformers import AutoTokenizer
5import torch
6
7peft_model_id = "Abdulvajid/Llama_Qlora_Reasoning_ToolCalling_Finetuned_4Bit"
8
9model = AutoPeftModelForCausalLM.from_pretrained(peft_model_id,
10 torch_dtype=torch.float16,
11 device_map='cuda',
12 load_in_4bit=True)
13
14tokenizer = AutoTokenizer.from_pretrained(peft_model_id)
15
16tools = [
17 {
18 "type": "function",
19 "function": {
20 "name": "search_restaurants",
21 "description": "Search for restaurants in a specific location",
22 "parameters": {
23 "type": "object",
24 "properties": {
25 "location": {
26 "type": "string",
27 "description": "The location to search for restaurants"
28 },
29 "cuisine": {
30 "type": "string",
31 "description": "The cuisine type to filter the restaurants"
32 },
33 "price_range": {
34 "type": "integer",
35 "description": "The price range of the restaurants (1 = cheap to 4 = very expensive)"
36 }
37 },
38 "required": ["location"]
39 }
40 }
41 }
42]
43
44messages=[
45 {"role": "user", "content": "I'm in Malappuram, can you find a restaurant for me?"}
46 ]
47
48prompt = tokenizer.apply_chat_template(
49 messages,
50 tools=tools,
51 add_generation_prompt=True,
52 tokenize=True,
53 return_tensors="pt"
54).to('cuda')
55
56output = model.generate(prompt, max_new_tokens=500)
57
58print(''.join(tokenizer.batch_decode(output[0][len(prompt[0]):])))1@misc{vonwerra2022trl,
2 title = {{TRL: Transformer Reinforcement Learning}},
3 author = {Leandro von Werra and Younes Belkada and Lewis Tunstall and Edward Beeching and Tristan Thrush and Nathan Lambert and Shengyi Huang and Kashif Rasul and Quentin Gallou{\'e}dec},
4 year = 2020,
5 journal = {GitHub repository},
6 publisher = {GitHub},
7 howpublished = {\url{https://github.com/huggingface/trl}}
8}