Update README.md
Browse files
README.md
CHANGED
@@ -1,199 +1,408 @@
|
|
1 |
---
|
2 |
library_name: transformers
|
3 |
-
tags:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
---
|
5 |
|
6 |
-
#
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
## Model Details
|
13 |
-
|
14 |
-
### Model Description
|
15 |
-
|
16 |
-
<!-- Provide a longer summary of what this model is. -->
|
17 |
-
|
18 |
-
This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
|
19 |
-
|
20 |
-
- **Developed by:** [More Information Needed]
|
21 |
-
- **Funded by [optional]:** [More Information Needed]
|
22 |
-
- **Shared by [optional]:** [More Information Needed]
|
23 |
-
- **Model type:** [More Information Needed]
|
24 |
-
- **Language(s) (NLP):** [More Information Needed]
|
25 |
-
- **License:** [More Information Needed]
|
26 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
27 |
-
|
28 |
-
### Model Sources [optional]
|
29 |
-
|
30 |
-
<!-- Provide the basic links for the model. -->
|
31 |
-
|
32 |
-
- **Repository:** [More Information Needed]
|
33 |
-
- **Paper [optional]:** [More Information Needed]
|
34 |
-
- **Demo [optional]:** [More Information Needed]
|
35 |
-
|
36 |
-
## Uses
|
37 |
-
|
38 |
-
<!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
|
39 |
-
|
40 |
-
### Direct Use
|
41 |
-
|
42 |
-
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
43 |
-
|
44 |
-
[More Information Needed]
|
45 |
-
|
46 |
-
### Downstream Use [optional]
|
47 |
-
|
48 |
-
<!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
|
49 |
-
|
50 |
-
[More Information Needed]
|
51 |
-
|
52 |
-
### Out-of-Scope Use
|
53 |
-
|
54 |
-
<!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
|
55 |
-
|
56 |
-
[More Information Needed]
|
57 |
-
|
58 |
-
## Bias, Risks, and Limitations
|
59 |
-
|
60 |
-
<!-- This section is meant to convey both technical and sociotechnical limitations. -->
|
61 |
-
|
62 |
-
[More Information Needed]
|
63 |
-
|
64 |
-
### Recommendations
|
65 |
-
|
66 |
-
<!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
|
67 |
-
|
68 |
-
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
|
69 |
-
|
70 |
-
## How to Get Started with the Model
|
71 |
-
|
72 |
-
Use the code below to get started with the model.
|
73 |
-
|
74 |
-
[More Information Needed]
|
75 |
-
|
76 |
-
## Training Details
|
77 |
-
|
78 |
-
### Training Data
|
79 |
-
|
80 |
-
<!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
|
81 |
-
|
82 |
-
[More Information Needed]
|
83 |
-
|
84 |
-
### Training Procedure
|
85 |
-
|
86 |
-
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
87 |
-
|
88 |
-
#### Preprocessing [optional]
|
89 |
-
|
90 |
-
[More Information Needed]
|
91 |
-
|
92 |
-
|
93 |
-
#### Training Hyperparameters
|
94 |
-
|
95 |
-
- **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
|
96 |
-
|
97 |
-
#### Speeds, Sizes, Times [optional]
|
98 |
-
|
99 |
-
<!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
|
100 |
-
|
101 |
-
[More Information Needed]
|
102 |
-
|
103 |
-
## Evaluation
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
#### Testing Data
|
110 |
-
|
111 |
-
<!-- This should link to a Dataset Card if possible. -->
|
112 |
-
|
113 |
-
[More Information Needed]
|
114 |
-
|
115 |
-
#### Factors
|
116 |
-
|
117 |
-
<!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
|
118 |
-
|
119 |
-
[More Information Needed]
|
120 |
-
|
121 |
-
#### Metrics
|
122 |
-
|
123 |
-
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
124 |
-
|
125 |
-
[More Information Needed]
|
126 |
-
|
127 |
-
### Results
|
128 |
-
|
129 |
-
[More Information Needed]
|
130 |
-
|
131 |
-
#### Summary
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
## Model Examination [optional]
|
136 |
-
|
137 |
-
<!-- Relevant interpretability work for the model goes here -->
|
138 |
-
|
139 |
-
[More Information Needed]
|
140 |
-
|
141 |
-
## Environmental Impact
|
142 |
-
|
143 |
-
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
144 |
-
|
145 |
-
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
146 |
-
|
147 |
-
- **Hardware Type:** [More Information Needed]
|
148 |
-
- **Hours used:** [More Information Needed]
|
149 |
-
- **Cloud Provider:** [More Information Needed]
|
150 |
-
- **Compute Region:** [More Information Needed]
|
151 |
-
- **Carbon Emitted:** [More Information Needed]
|
152 |
-
|
153 |
-
## Technical Specifications [optional]
|
154 |
-
|
155 |
-
### Model Architecture and Objective
|
156 |
-
|
157 |
-
[More Information Needed]
|
158 |
-
|
159 |
-
### Compute Infrastructure
|
160 |
-
|
161 |
-
[More Information Needed]
|
162 |
-
|
163 |
-
#### Hardware
|
164 |
-
|
165 |
-
[More Information Needed]
|
166 |
-
|
167 |
-
#### Software
|
168 |
-
|
169 |
-
[More Information Needed]
|
170 |
-
|
171 |
-
## Citation [optional]
|
172 |
-
|
173 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
174 |
-
|
175 |
-
**BibTeX:**
|
176 |
-
|
177 |
-
[More Information Needed]
|
178 |
-
|
179 |
-
**APA:**
|
180 |
-
|
181 |
-
[More Information Needed]
|
182 |
-
|
183 |
-
## Glossary [optional]
|
184 |
-
|
185 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
186 |
-
|
187 |
-
[More Information Needed]
|
188 |
-
|
189 |
-
## More Information [optional]
|
190 |
-
|
191 |
-
[More Information Needed]
|
192 |
-
|
193 |
-
## Model Card Authors [optional]
|
194 |
-
|
195 |
-
[More Information Needed]
|
196 |
-
|
197 |
-
## Model Card Contact
|
198 |
|
199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
library_name: transformers
|
3 |
+
tags:
|
4 |
+
- conversational
|
5 |
+
- gemma2
|
6 |
+
- function-calling
|
7 |
+
- trl
|
8 |
+
license: apache-2.0
|
9 |
+
datasets:
|
10 |
+
- DiTy/function-calling
|
11 |
+
language:
|
12 |
+
- en
|
13 |
+
pipeline_tag: text-generation
|
14 |
---
|
15 |
|
16 |
+
# DiTy/gemma-2-2b-it-function-calling
|
17 |
|
18 |
+
> NB: If you want to use the model to call functions in complex, long and confusing dialogues, it is better to use a larger model [DiTy/gemma-2-9b-it-function-calling](https://huggingface.co/DiTy/gemma-2-9b-it-function-calling).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
This model is a fine-tuned version of [google/gemma-2-2b-it](https://huggingface.co/google/gemma-2-2b-it) for the **Function Calling** task on non-synthetic data,
|
21 |
+
fully annotated by humans only, on the English version of the <ins>*DiTy/function-calling*</ins> dataset.
|
22 |
+
<!-- Provide a quick summary of what the model is/does. -->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
## Model card tree
|
25 |
+
|
26 |
+
* [How prepare your functions (tools) for *Function Calling*](#prepare_func_call)
|
27 |
+
* [Just use chat template for generation](#just_chat_template)
|
28 |
+
* [Prompt structure and expected content](#roles)
|
29 |
+
* [Evaluation of function calling models](#eval)
|
30 |
+
|
31 |
+
## Usage (HuggingFace Transformers)
|
32 |
+
|
33 |
+
Below we share some code snippets on how to get quickly started with running the model. First, install the Transformers library with:
|
34 |
+
```bash
|
35 |
+
pip install -U transformers
|
36 |
+
```
|
37 |
+
|
38 |
+
### <a name="prepare_func_call"></a>Prepare your functions for *Function Calling*
|
39 |
+
|
40 |
+
You should write the functions (tools) used by the model in *Python code* and make sure to add *Python docstrings* as in the example below:
|
41 |
+
```python
|
42 |
+
def get_weather(city: str):
|
43 |
+
"""
|
44 |
+
A function that returns the weather in a given city.
|
45 |
+
|
46 |
+
Args:
|
47 |
+
city: The city to get the weather for.
|
48 |
+
"""
|
49 |
+
import random
|
50 |
+
|
51 |
+
return "sunny" if random.random() > 0.5 else "rainy"
|
52 |
+
|
53 |
+
|
54 |
+
def get_sunrise_sunset_times(city: str):
|
55 |
+
"""
|
56 |
+
A function that returns the time of sunrise and sunset at the present moment, for a given city, in the form of a list: [sunrise_time, sunset_time].
|
57 |
+
|
58 |
+
Args:
|
59 |
+
city: The city to get the sunrise and sunset times for.
|
60 |
+
"""
|
61 |
+
|
62 |
+
return ["6:00 AM", "6:00 PM"]
|
63 |
+
```
|
64 |
+
|
65 |
+
### <a name="just_chat_template"></a>Just use chat template
|
66 |
+
|
67 |
+
Next, you need to download the model and tokenizer:
|
68 |
+
```python
|
69 |
+
import torch
|
70 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
71 |
+
|
72 |
+
model = AutoModelForCausalLM.from_pretrained(
|
73 |
+
"DiTy/gemma-2-2b-it-function-calling",
|
74 |
+
device_map="auto",
|
75 |
+
torch_dtype=torch.bfloat16, # use float16 or float32 if bfloat16 is not available to you.
|
76 |
+
cache_dir=PATH_TO_MODEL_DIR, # optional
|
77 |
+
)
|
78 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
79 |
+
"DiTy/gemma-2-2b-it-function-calling",
|
80 |
+
cache_dir=PATH_TO_MODEL_DIR, # optional
|
81 |
+
)
|
82 |
+
```
|
83 |
+
|
84 |
+
To get the result of generation, just use `apply_chat_template`. In order to take into account our written functions (tools),
|
85 |
+
we need to pass them as a list through the `tools` attribute and also use `add_prompt_generation=True`.
|
86 |
+
```python
|
87 |
+
history_messages = [
|
88 |
+
{"role": "system", "content": "You are a helpful assistant with access to the following functions. Use them if required - "},
|
89 |
+
{"role": "user", "content": "Hi, can you tell me the time of sunrise in Los Angeles?"},
|
90 |
+
]
|
91 |
+
|
92 |
+
inputs = tokenizer.apply_chat_template(
|
93 |
+
history_messages,
|
94 |
+
tokenize=False,
|
95 |
+
add_generation_prompt=True, # adding prompt for generation
|
96 |
+
tools=[get_weather, get_sunrise_sunset_times], # our functions (tools)
|
97 |
+
)
|
98 |
+
```
|
99 |
+
|
100 |
+
Then our `inputs` will look like this:
|
101 |
+
```
|
102 |
+
<bos><start_of_turn>user
|
103 |
+
You are a helpful assistant with access to the following functions. Use them if required - {
|
104 |
+
"name": "get_weather",
|
105 |
+
"description": "A function that returns the weather in a given city.",
|
106 |
+
"parameters": {
|
107 |
+
"type": "object",
|
108 |
+
"properties": {
|
109 |
+
"city": {
|
110 |
+
"type": "string",
|
111 |
+
"description": "The city to get the weather for."
|
112 |
+
}
|
113 |
+
},
|
114 |
+
"required": [
|
115 |
+
"city"
|
116 |
+
]
|
117 |
+
}
|
118 |
+
},
|
119 |
+
{
|
120 |
+
"name": "get_sunrise_sunset_times",
|
121 |
+
"description": "A function that returns the time of sunrise and sunset at the present moment, for a given city, in the form of a list: [sunrise_time, sunset_time].",
|
122 |
+
"parameters": {
|
123 |
+
"type": "object",
|
124 |
+
"properties": {
|
125 |
+
"city": {
|
126 |
+
"type": "string",
|
127 |
+
"description": "The city to get the sunrise and sunset times for."
|
128 |
+
}
|
129 |
+
},
|
130 |
+
"required": [
|
131 |
+
"city"
|
132 |
+
]
|
133 |
+
}
|
134 |
+
}
|
135 |
+
|
136 |
+
Hi, can you tell me the time of sunrise in Los Angeles?<end_of_turn>
|
137 |
+
<start_of_turn>model
|
138 |
+
|
139 |
+
```
|
140 |
+
|
141 |
+
Now we can generate a model's response.
|
142 |
+
Be careful because, after `apply_chat_template`, there is no need to *add special tokens* during tokenization. So, use `add_special_tokens=False`:
|
143 |
+
```python
|
144 |
+
terminator_ids = [
|
145 |
+
tokenizer.eos_token_id,
|
146 |
+
tokenizer.convert_tokens_to_ids("<end_of_turn>"),
|
147 |
+
]
|
148 |
+
|
149 |
+
prompt_ids = tokenizer.encode(inputs, add_special_tokens=False, return_tensors='pt').to(model.device)
|
150 |
+
generated_ids = model.generate(
|
151 |
+
prompt_ids,
|
152 |
+
max_new_tokens=512,
|
153 |
+
eos_token_id=terminator_ids,
|
154 |
+
bos_token_id=tokenizer.bos_token_id,
|
155 |
+
)
|
156 |
+
generated_response = tokenizer.decode(generated_ids[0][prompt_ids.shape[-1]:], skip_special_tokens=False) # `skip_special_tokens=False` for debug
|
157 |
+
```
|
158 |
+
|
159 |
+
We get the generation as a function call:
|
160 |
+
```
|
161 |
+
Function call: {"name": "get_sunrise_sunset_times", "arguments": {"city": "Los Angeles"}}<end_of_turn>
|
162 |
+
```
|
163 |
+
|
164 |
+
Great, now we can pick up and process the results with our *called function*, and then provide the model with the *function's response*:
|
165 |
+
```python
|
166 |
+
history_messages = [
|
167 |
+
{"role": "system", "content": "You are a helpful assistant with access to the following functions. Use them if required - "},
|
168 |
+
{"role": "user", "content": "Hi, can you tell me the time of sunrise in Los Angeles?"},
|
169 |
+
{"role": "function-call", "content": '{"name": "get_sunrise_sunset_times", "arguments": {"city": "Los Angeles"}}'},
|
170 |
+
{"role": "function-response", "content": '{"times_list": ["6:00 AM", "6:00 PM"]}'}, # a hypothetical response from our function
|
171 |
+
]
|
172 |
+
|
173 |
+
inputs = tokenizer.apply_chat_template(
|
174 |
+
history_messages,
|
175 |
+
tokenize=False,
|
176 |
+
add_generation_prompt=True, # adding prompt for generation
|
177 |
+
tools=[get_weather, get_sunrise_sunset_times], # our functions (tools)
|
178 |
+
)
|
179 |
+
```
|
180 |
+
|
181 |
+
Let's make sure the `inputs` are correct:
|
182 |
+
```
|
183 |
+
<bos><start_of_turn>user
|
184 |
+
You are a helpful assistant with access to the following functions. Use them if required - {
|
185 |
+
"name": "get_weather",
|
186 |
+
"description": "A function that returns the weather in a given city.",
|
187 |
+
"parameters": {
|
188 |
+
"type": "object",
|
189 |
+
"properties": {
|
190 |
+
"city": {
|
191 |
+
"type": "string",
|
192 |
+
"description": "The city to get the weather for."
|
193 |
+
}
|
194 |
+
},
|
195 |
+
"required": [
|
196 |
+
"city"
|
197 |
+
]
|
198 |
+
}
|
199 |
+
},
|
200 |
+
{
|
201 |
+
"name": "get_sunrise_sunset_times",
|
202 |
+
"description": "A function that returns the time of sunrise and sunset at the present moment, for a given city, in the form of a list: [sunrise_time, sunset_time].",
|
203 |
+
"parameters": {
|
204 |
+
"type": "object",
|
205 |
+
"properties": {
|
206 |
+
"city": {
|
207 |
+
"type": "string",
|
208 |
+
"description": "The city to get the sunrise and sunset times for."
|
209 |
+
}
|
210 |
+
},
|
211 |
+
"required": [
|
212 |
+
"city"
|
213 |
+
]
|
214 |
+
}
|
215 |
+
}
|
216 |
+
|
217 |
+
Hi, can you tell me the time of sunrise in Los Angeles?<end_of_turn>
|
218 |
+
<start_of_turn>model
|
219 |
+
Function call: {"name": "get_sunrise_sunset_times", "arguments": {"city": "Los Angeles"}}<end_of_turn>
|
220 |
+
<start_of_turn>user
|
221 |
+
Function response: {"times_list": ["6:00 AM", "6:00 PM"]}<end_of_turn>
|
222 |
+
<start_of_turn>model
|
223 |
+
|
224 |
+
```
|
225 |
+
|
226 |
+
Similarly, we generate a response from the model:
|
227 |
+
```python
|
228 |
+
prompt_ids = tokenizer.encode(inputs, add_special_tokens=False, return_tensors='pt').to(model.device)
|
229 |
+
generated_ids = model.generate(
|
230 |
+
prompt_ids,
|
231 |
+
max_new_tokens=512,
|
232 |
+
eos_token_id=terminator_ids,
|
233 |
+
bos_token_id=tokenizer.bos_token_id,
|
234 |
+
)
|
235 |
+
generated_response = tokenizer.decode(generated_ids[0][prompt_ids.shape[-1]:], skip_special_tokens=False) # `skip_special_tokens=False` for debug
|
236 |
+
```
|
237 |
+
|
238 |
+
As a result, we get the model's response:
|
239 |
+
```
|
240 |
+
The sunrise time in Los Angeles is 6:00 AM.<end_of_turn>
|
241 |
+
```
|
242 |
+
|
243 |
+
## Usage via transformers `pipeline`
|
244 |
+
|
245 |
+
<details>
|
246 |
+
<summary>
|
247 |
+
Generation via pipeline
|
248 |
+
</summary>
|
249 |
+
|
250 |
+
```python
|
251 |
+
from transformers import pipeline
|
252 |
+
|
253 |
+
|
254 |
+
generation_pipeline = pipeline(
|
255 |
+
"text-generation",
|
256 |
+
model="DiTy/gemma-2-2b-it-function-calling",
|
257 |
+
model_kwargs={
|
258 |
+
"torch_dtype": torch.bfloat16, # use float16 or float32 if bfloat16 is not supported for you.
|
259 |
+
"cache_dir": PATH_TO_MODEL_DIR, # OPTIONAL
|
260 |
+
},
|
261 |
+
device_map="auto",
|
262 |
+
)
|
263 |
+
|
264 |
+
history_messages = [
|
265 |
+
{"role": "system", "content": "You are a helpful assistant with access to the following functions. Use them if required - "},
|
266 |
+
{"role": "user", "content": "Hi, can you tell me the time of sunrise in Los Angeles?"},
|
267 |
+
{"role": "function-call", "content": '{"name": "get_sunrise_sunset_times", "arguments": {"city": "Los Angeles"}}'},
|
268 |
+
{"role": "function-response", "content": '{"times_list": ["6:00 AM", "6:00 PM"]}'},
|
269 |
+
]
|
270 |
+
|
271 |
+
inputs = generation_pipeline.tokenizer.apply_chat_template(
|
272 |
+
history_messages,
|
273 |
+
tokenize=False,
|
274 |
+
add_generation_prompt=True,
|
275 |
+
tools=[get_weather, get_sunrise_sunset_times],
|
276 |
+
)
|
277 |
+
|
278 |
+
terminator_ids = [
|
279 |
+
generation_pipeline.tokenizer.eos_token_id,
|
280 |
+
generation_pipeline.tokenizer.convert_tokens_to_ids("<end_of_turn>")
|
281 |
+
]
|
282 |
+
|
283 |
+
outputs = generation_pipeline(
|
284 |
+
inputs,
|
285 |
+
max_new_tokens=512,
|
286 |
+
eos_token_id=terminator_ids,
|
287 |
+
)
|
288 |
+
|
289 |
+
print(outputs[0]["generated_text"][len(inputs):])
|
290 |
+
```
|
291 |
+
|
292 |
+
</details>
|
293 |
+
|
294 |
+
## <a name="roles"></a>Prompt structure and expected content
|
295 |
+
|
296 |
+
For the most correct operation of the model, it is assumed that `apply_chat_template` will be used.
|
297 |
+
It is necessary to transmit the message history in a certain format.
|
298 |
+
```python
|
299 |
+
history_messages = [
|
300 |
+
{"role": "...", "content": "..."},
|
301 |
+
...
|
302 |
+
]
|
303 |
+
```
|
304 |
+
|
305 |
+
The following roles are available for use:
|
306 |
+
|
307 |
+
* `system` - an optional role, its content is always placed at the very beginning and before listing the functions available to the model (tools).
|
308 |
+
You can always use the standard option that was used during the training: ***"You are a helpful assistant with access to the following functions. Use them if required - "***
|
309 |
+
* `user` - the user's request is transmitted through this role.
|
310 |
+
* `function-call` - The body of the function call is passed through this role.
|
311 |
+
Although the model is trained to generate a function call in the form of ***"Function call: {...}\<end_of_turn\>"***, you should still pass only the body ***"{...}"***
|
312 |
+
to the *"content"* field, since using `apply_chat_template`, the postscript in the instructions is added automatically.
|
313 |
+
* `function-response` - in this role, we must pass the response of our function in the *"content"* field as a dictionary ***'{"name_returnable_value": value}'***.
|
314 |
+
* `model` - the content under this role is considered to be the generated text of the model.
|
315 |
+
|
316 |
+
### Chat history with *Function Calling*
|
317 |
+
|
318 |
+
```
|
319 |
+
[
|
320 |
+
{"role": "system", "content": "You are a helpful assistant with access to the following functions. Use them if required - "},
|
321 |
+
{"role": "user", "content": "Hi, can you tell me the time of sunrise in Los Angeles?"},
|
322 |
+
{"role": "function-call", "content": '{"name": "get_sunrise_sunset_times", "arguments": {"city": "Los Angeles"}}'},
|
323 |
+
{"role": "function-response", "content": '{"times_list": ["6:00 AM", "6:00 PM"]}'},
|
324 |
+
]
|
325 |
+
```
|
326 |
+
|
327 |
+
It looks like:
|
328 |
+
```
|
329 |
+
<bos><start_of_turn>user
|
330 |
+
You are a helpful assistant with access to the following functions. Use them if required - {
|
331 |
+
"name": "get_weather",
|
332 |
+
"description": "A function that returns the weather in a given city.",
|
333 |
+
"parameters": {
|
334 |
+
"type": "object",
|
335 |
+
"properties": {
|
336 |
+
"city": {
|
337 |
+
"type": "string",
|
338 |
+
"description": "The city to get the weather for."
|
339 |
+
}
|
340 |
+
},
|
341 |
+
"required": [
|
342 |
+
"city"
|
343 |
+
]
|
344 |
+
}
|
345 |
+
},
|
346 |
+
{
|
347 |
+
"name": "get_sunrise_sunset_times",
|
348 |
+
"description": "A function that returns the time of sunrise and sunset at the present moment, for a given city, in the form of a list: [sunrise_time, sunset_time].",
|
349 |
+
"parameters": {
|
350 |
+
"type": "object",
|
351 |
+
"properties": {
|
352 |
+
"city": {
|
353 |
+
"type": "string",
|
354 |
+
"description": "The city to get the sunrise and sunset times for."
|
355 |
+
}
|
356 |
+
},
|
357 |
+
"required": [
|
358 |
+
"city"
|
359 |
+
]
|
360 |
+
}
|
361 |
+
}
|
362 |
+
|
363 |
+
Hi, can you tell me the time of sunrise in Los Angeles?<end_of_turn>
|
364 |
+
<start_of_turn>model
|
365 |
+
Function call: {"name": "get_sunrise_sunset_times", "arguments": {"city": "Los Angeles"}}<end_of_turn>
|
366 |
+
<start_of_turn>user
|
367 |
+
Function response: {"times_list": ["6:00 AM", "6:00 PM"]}<end_of_turn>
|
368 |
+
```
|
369 |
+
|
370 |
+
|
371 |
+
### Chat history with a standard user-model template
|
372 |
+
|
373 |
+
```
|
374 |
+
[
|
375 |
+
{"role": "system", "content": "You are a helpful assistant"},
|
376 |
+
{"role": "user", "content": "Tell me about California"},
|
377 |
+
]
|
378 |
+
```
|
379 |
+
|
380 |
+
It looks like:
|
381 |
+
```
|
382 |
+
<bos><start_of_turn>user
|
383 |
+
You are a helpful assistant
|
384 |
+
|
385 |
+
Tell me about California<end_of_turn>
|
386 |
+
```
|
387 |
+
|
388 |
+
## <a name="eval"></a>Evaluation
|
389 |
+
|
390 |
+
During the learning process, the validation error was approximated to the following values:
|
391 |
+
|
392 |
+
| **Model** | **Generation Language** | **Approximately Validation Loss** |
|
393 |
+
| :-----: | :-----: | :-----: |
|
394 |
+
| [DiTy/gemma-2-9b-it-function-calling](https://huggingface.co/DiTy/gemma-2-9b-it-function-calling) | EN | 0.5 |
|
395 |
+
| **[DiTy/gemma-2-2b-it-function-calling](https://huggingface.co/DiTy/gemma-2-2b-it-function-calling)** | EN | 0.66 |
|
396 |
+
|
397 |
+
## Citation
|
398 |
+
|
399 |
+
```none
|
400 |
+
@article{gemma_2024,
|
401 |
+
title={Gemma},
|
402 |
+
url={https://www.kaggle.com/m/3301},
|
403 |
+
DOI={10.34740/KAGGLE/M/3301},
|
404 |
+
publisher={Kaggle},
|
405 |
+
author={Gemma Team},
|
406 |
+
year={2024}
|
407 |
+
}
|
408 |
+
```
|