File size: 2,779 Bytes
d2dcd4b c9c1150 cdbb555 d390ee2 cdbb555 d390ee2 cdbb555 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
---
datasets:
- glaiveai/glaive-function-calling-v2
---
## Tool Information
Define the tools and their functionalities as a list of dictionaries.
```python
tools_info = [
{
"name": "cancel_reservation",
"description": "cancel a reservation",
"parameters": {
"type": "object",
"properties": {
"reservation_number": {
"type": "integer",
"description": "Reservation number"
}
},
"required": ["reservation_number"]
}
},
{
"name": "get_reservations",
"description": "get reservation numbers",
"parameters": {
"type": "object",
"properties": {
"user_id": {
"type": "integer",
"description": "User id"
}
},
"required": [
"user_id"
]
}
},
]
```
## System Initialization
Initialize the system's interactive capabilities using the defined tools.
```python
system = f"You are a helpful assistant with access to the following functions: \n {json.dumps(tools_info, indent=2)}."
```
## Conversation Flow
Simulate a conversation flow where the user requests to cancel a reservation.
```python
messages = [
{"role": "system", "content": system},
{"role": "user", "content": "Help me to cancel a reservation"},
{"role": "assistant", "content": "I can help with that. Could you please provide me with the reservation number?"},
{"role": "user", "content": "the reservation number is 1011"}
]
```
Or the user requests to display its reservations, note the use of "tool" role.
```python
messages=[
{"role":"system","content": system},
{"role": "user","content": "Help me to find my reservations, my user id is 110"},
{"role": "assistant","content":'<func_call> {"name": "get_reservations", "arguments": {"user_id": 110}}'},
{"role": "tool","content":'["AB001","CD002","GG100"]'}
]
```
## Model Loading
Load the causal language model and tokenizer.
```python
model_id = "caldana/function_calling_llama3_8b_instruct"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.bfloat16,
device_map="auto",
)
```
## Generating Response
Generate a response from the model based on the conversation context.
```python
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt"
).to(model.device)
terminators = [
tokenizer.eos_token_id,
tokenizer.convert_tokens_to_ids("")
]
outputs = model.generate(
input_ids,
max_new_tokens=256,
eos_token_id=terminators,
do_sample=True,
temperature=0.6,
top_p=0.9,
)
response = outputs[0][input_ids.shape[-1]:]
print(tokenizer.decode(response, skip_special_tokens=True))
```
|