Views
No views yet
| Filename | Quant type | File Size | Description |
|---|---|---|---|
| gemma-2-27B-it-function-calling-Q8_0.gguf | Q8_0 | 28.9GB | Extremely high quality, generally unneeded but max available quant. |
| gemma-2-27B-it-function-calling-Q6_K.gguf | Q6_K | 22.3GB | Very high quality, near perfect, recommended. |
pip install -U transformers1def get_weather(city: str):
2 """
3 A function that returns the weather in a given city.
4
5 Args:
6 city: The city to get the weather for.
7 """
8 import random
9
10 return "sunny" if random.random() > 0.5 else "rainy"
11def get_sunrise_sunset_times(city: str):
12 """
13 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].
14
15 Args:
16 city: The city to get the sunrise and sunset times for.
17 """
18
19 return ["6:00 AM", "6:00 PM"]1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3model = AutoModelForCausalLM.from_pretrained(
4 "DiTy/gemma-2-27b-it-function-calling-GGUF",
5 device_map="auto",
6 torch_dtype=torch.bfloat16, # use float16 or float32 if bfloat16 is not available to you.
7 cache_dir=PATH_TO_MODEL_DIR, # optional
8)
9tokenizer = AutoTokenizer.from_pretrained(
10 "DiTy/gemma-2-27b-it-function-calling-GGUF",
11 cache_dir=PATH_TO_MODEL_DIR, # optional
12)apply_chat_template. In order to take into account our written functions (tools),
we need to pass them as a list through the tools attribute and also use add_prompt_generation=True.1history_messages = [
2 {"role": "system", "content": "You are a helpful assistant with access to the following functions. Use them if required - "},
3 {"role": "user", "content": "Hi, can you tell me the time of sunrise in Los Angeles?"},
4]
5inputs = tokenizer.apply_chat_template(
6 history_messages,
7 tokenize=False,
8 add_generation_prompt=True, # adding prompt for generation
9 tools=[get_weather, get_sunrise_sunset_times], # our functions (tools)
10)
11print(inputs)inputs will look like this:<bos><start_of_turn>user
You are a helpful assistant with access to the following functions. Use them if required - {
"name": "get_weather",
"description": "A function that returns the weather in a given city.",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city to get the weather for."
}
},
"required": [
"city"
]
}
},
{
"name": "get_sunrise_sunset_times",
"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].",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city to get the sunrise and sunset times for."
}
},
"required": [
"city"
]
}
}
Hi, can you tell me the time of sunrise in Los Angeles?<end_of_turn>
<start_of_turn>modelapply_chat_template, there is no need to add special tokens during tokenization. So, use add_special_tokens=False:1terminator_ids = [
2 tokenizer.eos_token_id,
3 tokenizer.convert_tokens_to_ids("<end_of_turn>"),
4]
5prompt_ids = tokenizer.encode(inputs, add_special_tokens=False, return_tensors='pt').to(model.device)
6generated_ids = model.generate(
7 prompt_ids,
8 max_new_tokens=512,
9 eos_token_id=terminator_ids,
10 bos_token_id=tokenizer.bos_token_id,
11)
12generated_response = tokenizer.decode(generated_ids[0][prompt_ids.shape[-1]:], skip_special_tokens=False) # `skip_special_tokens=False` for debug
13print(generated_response)Function call: {"name": "get_sunrise_sunset_times", "arguments": {"city": "Los Angeles"}}<end_of_turn>1history_messages = [
2 {"role": "system", "content": "You are a helpful assistant with access to the following functions. Use them if required - "},
3 {"role": "user", "content": "Hi, can you tell me the time of sunrise in Los Angeles?"},
4 {"role": "function-call", "content": '{"name": "get_sunrise_sunset_times", "arguments": {"city": "Los Angeles"}}'},
5 {"role": "function-response", "content": '{"times_list": ["6:00 AM", "6:00 PM"]}'}, # a hypothetical response from our function
6]
7inputs = tokenizer.apply_chat_template(
8 history_messages,
9 tokenize=False,
10 add_generation_prompt=True, # adding prompt for generation
11 tools=[get_weather, get_sunrise_sunset_times], # our functions (tools)
12)
13print(inputs)inputs are correct:<bos><start_of_turn>user
You are a helpful assistant with access to the following functions. Use them if required - {
"name": "get_weather",
"description": "A function that returns the weather in a given city.",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city to get the weather for."
}
},
"required": [
"city"
]
}
},
{
"name": "get_sunrise_sunset_times",
"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].",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city to get the sunrise and sunset times for."
}
},
"required": [
"city"
]
}
}
Hi, can you tell me the time of sunrise in Los Angeles?<end_of_turn>
<start_of_turn>model
Function call: {"name": "get_sunrise_sunset_times", "arguments": {"city": "Los Angeles"}}<end_of_turn>
<start_of_turn>user
Function response: {"times_list": ["6:00 AM", "6:00 PM"]}<end_of_turn>
<start_of_turn>model1prompt_ids = tokenizer.encode(inputs, add_special_tokens=False, return_tensors='pt').to(model.device)
2generated_ids = model.generate(
3 prompt_ids,
4 max_new_tokens=512,
5 eos_token_id=terminator_ids,
6 bos_token_id=tokenizer.bos_token_id,
7)
8generated_response = tokenizer.decode(generated_ids[0][prompt_ids.shape[-1]:], skip_special_tokens=False) # `skip_special_tokens=False` for debug
9print(generated_response)The sunrise time in Los Angeles is 6:00 AM.<end_of_turn>pipeline1from transformers import pipeline
2generation_pipeline = pipeline(
3 "text-generation",
4 model="DiTy/gemma-2-27b-it-function-calling-GGUF",
5 model_kwargs={
6 "torch_dtype": torch.bfloat16, # use float16 or float32 if bfloat16 is not supported for you.
7 "cache_dir": PATH_TO_MODEL_DIR, # OPTIONAL
8 },
9 device_map="auto",
10)
11history_messages = [
12 {"role": "system", "content": "You are a helpful assistant with access to the following functions. Use them if required - "},
13 {"role": "user", "content": "Hi, can you tell me the time of sunrise in Los Angeles?"},
14 {"role": "function-call", "content": '{"name": "get_sunrise_sunset_times", "arguments": {"city": "Los Angeles"}}'},
15 {"role": "function-response", "content": '{"times_list": ["6:00 AM", "6:00 PM"]}'},
16]
17inputs = generation_pipeline.tokenizer.apply_chat_template(
18 history_messages,
19 tokenize=False,
20 add_generation_prompt=True,
21 tools=[get_weather, get_sunrise_sunset_times],
22)
23terminator_ids = [
24 generation_pipeline.tokenizer.eos_token_id,
25 generation_pipeline.tokenizer.convert_tokens_to_ids("<end_of_turn>")
26]
27outputs = generation_pipeline(
28 inputs,
29 max_new_tokens=512,
30 eos_token_id=terminator_ids,
31)
32print(outputs[0]["generated_text"][len(inputs):])apply_chat_template will be used.
It is necessary to transmit the message history in a certain format.1history_messages = [
2 {"role": "...", "content": "..."},
3 ...
4]system - an optional role, its content is always placed at the very beginning and before listing the functions available to the model (tools).
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 - "user - the user's request is transmitted through this role.function-call - The body of the function call is passed through this role.
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 "{...}"
to the "content" field, since using apply_chat_template, the postscript in the instructions is added automatically.function-response - in this role, we must pass the response of our function in the "content" field as a dictionary '{"name_returnable_value": value}'.model - the content under this role is considered to be the generated text of the model.[
{"role": "system", "content": "You are a helpful assistant with access to the following functions. Use them if required - "},
{"role": "user", "content": "Hi, can you tell me the time of sunrise in Los Angeles?"},
{"role": "function-call", "content": '{"name": "get_sunrise_sunset_times", "arguments": {"city": "Los Angeles"}}'},
{"role": "function-response", "content": '{"times_list": ["6:00 AM", "6:00 PM"]}'},
]<bos><start_of_turn>user
You are a helpful assistant with access to the following functions. Use them if required - {
"name": "get_weather",
"description": "A function that returns the weather in a given city.",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city to get the weather for."
}
},
"required": [
"city"
]
}
},
{
"name": "get_sunrise_sunset_times",
"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].",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city to get the sunrise and sunset times for."
}
},
"required": [
"city"
]
}
}
Hi, can you tell me the time of sunrise in Los Angeles?<end_of_turn>
<start_of_turn>model
Function call: {"name": "get_sunrise_sunset_times", "arguments": {"city": "Los Angeles"}}<end_of_turn>
<start_of_turn>user
Function response: {"times_list": ["6:00 AM", "6:00 PM"]}<end_of_turn>[
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "Tell me about California"},
]<bos><start_of_turn>user
You are a helpful assistant
Tell me about California<end_of_turn>| Model | Generation Language | Approximately Validation Loss |
|---|---|---|
| DiTy/gemma-2-27b-it-function-calling-GGUF | EN | 0.47 |
| DiTy/gemma-2-9b-it-russian-function-calling-GGUF | RU | 0.57 |
| DiTy/gemma-2-9b-it-function-calling-GGUF | EN | 0.5 |
| DiTy/gemma-2-2b-it-function-calling | EN | 0.66 |
1@article{gemma_2024,
2 title={Gemma},
3 url={https://www.kaggle.com/m/3301},
4 DOI={10.34740/KAGGLE/M/3301},
5 publisher={Kaggle},
6 author={Gemma Team},
7 year={2024}
8}