Views
No views yet
func_calling_singleturn subset.1from unsloth import FastLanguageModel
2import torch
3from vllm import SamplingParams
4
5max_seq_length = 4096 # Can increase for longer reasoning traces
6lora_rank = 32 # Larger rank = smarter, but slower
7
8model, tokenizer = FastLanguageModel.from_pretrained(
9 model_name = "Bharatdeep-H/xml_cot_fm_1",
10 #model_name = "unsloth/Qwen2.5-1.5B-Instruct",
11 max_seq_length = max_seq_length,
12 load_in_4bit = True, # False for LoRA 16bit
13 fast_inference = True, # Enable vLLM fast inference
14 max_lora_rank = lora_rank,
15 gpu_memory_utilization = 0.6, # Reduce if out of memory
16)
17
18model = FastLanguageModel.get_peft_model(
19 model,
20 r = lora_rank, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
21 target_modules = [
22 "q_proj", "k_proj", "v_proj", "o_proj",
23 "gate_proj", "up_proj", "down_proj",
24 ], # Remove QKVO if out of memory
25 lora_alpha = lora_rank,
26 use_gradient_checkpointing = "unsloth", # Enable long context finetuning
27 random_state = 3407,
28)
29
30# Format definitions
31FORMAT_PROMPT = """
32Respond in the following format:
33<chain_of_thought>
34...
35</chain_of_thought>
36<tool_call>
37...
38</tool_call>
39"""
40
41SYSTEM_MIX_USER_PROMPT = "You are a function calling AI model. You are provided with function signatures within <tools> </tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions.\n\n<tools>[[{'type': 'function', 'function': {'name': 'book_appointment', 'description': 'Books an appointment for a patient with a specific dentist at a given date and time.', 'parameters': {'type': 'object', 'properties': {'patient_id': {'type': 'string', 'description': 'The unique identifier for the patient.'}, 'dentist_id': {'type': 'string', 'description': 'The unique identifier for the dentist.'}, 'preferred_date': {'type': 'string', 'description': 'The preferred date for the appointment.'}, 'time_slot': {'type': 'string', 'description': 'The preferred time slot for the appointment.'}}, 'required': ['patient_id', 'dentist_id', 'preferred_date', 'time_slot']}}}, {'type': 'function', 'function': {'name': 'reschedule_appointment', 'description': 'Reschedules an existing appointment to a new date and time.', 'parameters': {'type': 'object', 'properties': {'appointment_id': {'type': 'string', 'description': 'The unique identifier for the existing appointment.'}, 'new_date': {'type': 'string', 'description': 'The new date for the rescheduled appointment.'}, 'new_time_slot': {'type': 'string', 'description': 'The new time slot for the rescheduled appointment.'}}, 'required': ['appointment_id', 'new_date', 'new_time_slot']}}}, {'type': 'function', 'function': {'name': 'cancel_appointment', 'description': 'Cancels an existing appointment.', 'parameters': {'type': 'object', 'properties': {'appointment_id': {'type': 'string', 'description': 'The unique identifier for the appointment to be canceled.'}}, 'required': ['appointment_id']}}}, {'type': 'function', 'function': {'name': 'find_available_time_slots', 'description': 'Finds available time slots for a dentist on a given date.', 'parameters': {'type': 'object', 'properties': {'dentist_id': {'type': 'string', 'description': 'The unique identifier for the dentist.'}, 'date': {'type': 'string', 'description': 'The date to check for available time slots.'}}, 'required': ['dentist_id', 'date']}}}, {'type': 'function', 'function': {'name': 'send_appointment_reminder', 'description': 'Sends an automated reminder to the patient for an upcoming appointment.', 'parameters': {'type': 'object', 'properties': {'appointment_id': {'type': 'string', 'description': 'The unique identifier for the appointment.'}, 'reminder_time': {'type': 'string', 'description': 'The time before the appointment when the reminder should be sent.'}}, 'required': ['appointment_id', 'reminder_time']}}}]]</tools>\n\nFor each user query, you must:\n\n1. First, generate your reasoning within <chain_of_thought> </chain_of_thought> tags. This should explain your analysis of the user's request and how you determined which function(s) to call, or why no appropriate function is available.\n\n2. Then, call the appropriate function(s) by returning a JSON object within <tool_call> </tool_call> tags using the following schema:\n<tool_call>\n{'arguments': <args-dict>, 'name': <function-name>}\n</tool_call>\n\n3. If you determine that none of the provided tools can appropriately resolve the user's query based on the tools' descriptions, you must still provide your reasoning in <chain_of_thought> tags, followed by:\n<tool_call>NO_CALL_AVAILABLE</tool_call>\n\nRemember that your <chain_of_thought> analysis must ALWAYS precede any <tool_call> tags, regardless of whether a suitable function is available."
42USER_QUERY = "As the manager of a dental practice, I'm looking to streamline our booking process. I need to schedule an appointment for our patient, John Doe with ID 'p123', with Dr. Sarah Smith, whose dentist ID is 'd456'. Please book this appointment for May 15, 2023, at 2:00 PM. Additionally, I would like to set up an automated reminder for John Doe to ensure he remembers his appointment. Can you book this appointment and arrange for the reminder to be sent out in advance?"
43
44text = tokenizer.apply_chat_template([
45 {'role': 'system', 'content': FORMAT_PROMPT},
46 {'role': 'user', 'content': SYSTEM_MIX_USER_PROMPT + "\n\nUSER QUERY: " + USER_QUERY}
47], tokenize = False, add_generation_prompt = True)
48
49sampling_params = SamplingParams(
50 temperature = 0.8,
51 top_p = 0.95,
52 max_tokens = 1024,
53)
54
55
56output = model.fast_generate(
57 text,
58 sampling_params = sampling_params,
59 #lora_request = model.load_lora("grpo_saved_lora"),
60)[0].outputs[0].text
61
62print(output)