Gemma 2 9b It Russian Function Calling GGUF
该模型是基于google/gemma-2-9b-it针对函数调用任务进行微调的版本,训练数据完全由人工标注,使用了俄语版本的DiTy/function-calling数据集。
下载量 509
发布时间 : 4/25/2025
模型简介
该模型是一个针对函数调用任务优化的俄语语言模型,能够理解和生成函数调用请求,并处理函数响应。
模型特点
函数调用优化
专门针对函数调用任务进行微调,能够生成准确的函数调用请求和处理响应。
俄语支持
支持俄语,适用于俄语环境下的函数调用任务。
高效推理
基于Gemma-2-9b-it架构,推理速度快,适合实时应用。
模型能力
函数调用生成
俄语文本生成
函数响应处理
使用案例
智能助手
天气查询
用户询问天气时,模型生成函数调用请求并返回天气信息。
提供准确的天气信息
日出日落时间查询
用户询问日出日落时间时,模型生成函数调用请求并返回时间信息。
提供准确的日出日落时间
🚀 DiTy/gemma-2-9b-it-russian-function-calling-GGUF
本模型是 google/gemma-2-9b-it 的微调版本,用于在非合成数据上执行 函数调用 任务。这些数据仅由人工进行了完整注释,来自俄语版的 DiTy/function-calling 数据集。
除了 safetensors 格式外,该模型还提供 GGUF 格式(在这种情况下,您只需下载单个文件(如何推理 GGUF 模型)):
文件名 | 量化类型 | 文件大小 | 描述 |
---|---|---|---|
gemma-2-9B-it-russian-function-calling-F16.gguf | F16 | 18.5GB | 采用 float16 的基础模型 |
📚 模型卡片章节
🚀 快速开始
📦 安装指南
首先,您需要安装 transformers
库:
pip install -U transformers
💻 使用示例
基础用法
如何为 函数调用 准备您的函数(工具)
您需要用 Python 代码 编写模型使用的函数(工具),并务必添加 Python 文档字符串,如下例所示:
def get_weather(city: str):
"""
该函数用于返回指定城市的天气情况。
参数:
city: 需要查询天气的城市。
"""
import random
return "sunny" if random.random() > 0.5 else "rainy"
def get_sunrise_sunset_times(city: str):
"""
该函数用于返回指定城市当前日期的日出和日落时间(无需用户提供日期),格式为列表:[日出时间, 日落时间]。
参数:
city: 可以查询日出和日落时间的城市。
"""
return ["6:00", "18:00"]
直接使用聊天模板进行生成
接下来,您需要加载模型和分词器:
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(
"DiTy/gemma-2-9b-it-russian-function-calling-GGUF",
device_map="auto",
torch_dtype=torch.bfloat16, # 如果您的环境不支持 bfloat16,可以使用 float16 或 float32。
cache_dir=PATH_TO_MODEL_DIR, # 可选
)
tokenizer = AutoTokenizer.from_pretrained(
"DiTy/gemma-2-9b-it-russian-function-calling-GGUF",
cache_dir=PATH_TO_MODEL_DIR, # 可选
)
要获得生成结果,只需使用 apply_chat_template
。为了考虑我们编写的函数(工具),我们需要通过 tools
属性将它们作为列表传递,并使用 add_prompt_generation=True
:
history_messages = [
{"role": "system", "content": "你是一个有用的助手,可以使用以下函数。如有需要,请使用它们 - "},
{"role": "user", "content": "你好,你能告诉我克拉斯诺达尔的日出时间吗?"}
]
inputs = tokenizer.apply_chat_template(
history_messages,
tokenize=False,
add_generation_prompt=True, # 添加生成提示
tools=[get_weather, get_sunrise_sunset_times], # 我们的函数(工具)
)
print(inputs)
此时,我们的 inputs
将如下所示:
<bos><start_of_turn>user
你是一个有用的助手,可以使用以下函数。如有需要,请使用它们 - {
"name": "get_weather",
"description": "该函数用于返回指定城市的天气情况。",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "需要查询天气的城市。"
}
},
"required": [
"city"
]
}
},
{
"name": "get_sunrise_sunset_times",
"description": "该函数用于返回指定城市当前日期的日出和日落时间(无需用户提供日期),格式为列表:[日出时间, 日落时间]。",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "可以查询日出和日落时间的城市。"
}
},
"required": [
"city"
]
}
}
你好,你能告诉我克拉斯诺达尔的日出时间吗?<end_of_turn>
<start_of_turn>model
现在,我们可以生成模型的响应。请注意,在使用 apply_chat_template
之后,在分词时不需要 添加特殊标记。因此,我们使用 add_special_tokens=False
:
terminator_ids = [
tokenizer.eos_token_id,
tokenizer.convert_tokens_to_ids("<end_of_turn>"),
]
prompt_ids = tokenizer.encode(inputs, add_special_tokens=False, return_tensors='pt').to(model.device)
generated_ids = model.generate(
prompt_ids,
max_new_tokens=512,
eos_token_id=terminator_ids,
bos_token_id=tokenizer.bos_token_id,
)
generated_response = tokenizer.decode(generated_ids[0][prompt_ids.shape[-1]:], skip_special_tokens=False) # `skip_special_tokens=False` 用于调试
print(generated_response)
我们得到的生成结果是一个函数调用:
函数调用: {"name": "get_sunrise_sunset_times", "arguments": {"city": "克拉斯诺达尔"}}<end_of_turn>
很好,现在我们可以使用我们的 可调用函数 获取并处理结果,然后向模型提供 函数 的响应:
history_messages = [
{"role": "system", "content": "你是一个有用的助手,可以使用以下函数。如有需要,请使用它们 - "},
{"role": "user", "content": "你好,你能告诉我克拉斯诺达尔的日出时间吗?"},
{"role": "function-call", "content": '{"name": "get_sunrise_sunset_times", "arguments": {"city": "Los Angeles"}}'},
{"role": "function-response", "content": '{"times_list": ["6:00", "18:00"]}'}, # 假设我们的函数返回的响应
]
inputs = tokenizer.apply_chat_template(
history_messages,
tokenize=False,
add_generation_prompt=True, # 添加生成提示
tools=[get_weather, get_sunrise_sunset_times], # 我们的函数(工具)
)
print(inputs)
让我们确保 inputs
是正确的:
<bos><start_of_turn>user
你是一个有用的助手,可以使用以下函数。如有需要,请使用它们 - {
"name": "get_weather",
"description": "该函数用于返回指定城市的天气情况。",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "需要查询天气的城市。"
}
},
"required": [
"city"
]
}
},
{
"name": "get_sunrise_sunset_times",
"description": "该函数用于返回指定城市当前日期的日出和日落时间(无需用户提供日期),格式为列表:[日出时间, 日落时间]。",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "可以查询日出和日落时间的城市。"
}
},
"required": [
"city"
]
}
}
你好,你能告诉我克拉斯诺达尔的日出时间吗?<end_of_turn>
<start_of_turn>model
函数调用: {"name": "get_sunrise_sunset_times", "arguments": {"city": "克拉斯诺达尔"}}<end_of_turn>
<start_of_turn>user
函数响应: {"times_list": ["6:00", "18:00"]}<end_of_turn>
<start_of_turn>model
同样,我们生成模型的响应:
prompt_ids = tokenizer.encode(inputs, add_special_tokens=False, return_tensors='pt').to(model.device)
generated_ids = model.generate(
prompt_ids,
max_new_tokens=512,
eos_token_id=terminator_ids,
bos_token_id=tokenizer.bos_token_id,
)
generated_response = tokenizer.decode(generated_ids[0][prompt_ids.shape[-1]:], skip_special_tokens=False) # `skip_special_tokens=False` 用于调试
print(generated_response)
结果,我们得到了模型的响应:
克拉斯诺达尔的日出时间是早上 6:00,日落时间是晚上 18:00。<end_of_turn>
高级用法
使用 transformers
的 pipeline
进行生成
使用 pipeline 进行生成
from transformers import pipeline
generation_pipeline = pipeline(
"text-generation",
model="DiTy/gemma-2-9b-it-russian-function-calling-GGUF",
model_kwargs={
"torch_dtype": torch.bfloat16, # 如果您的环境不支持 bfloat16,可以使用 float16 或 float32。
"cache_dir": PATH_TO_MODEL_DIR, # 可选
},
device_map="auto",
)
history_messages = [
{"role": "system", "content": "你是一个有用的助手,可以使用以下函数。如有需要,请使用它们 - "},
{"role": "user", "content": "你好,你能告诉我克拉斯诺达尔的日出时间吗?"},
{"role": "function-call", "content": '{"name": "get_sunrise_sunset_times", "arguments": {"city": "克拉斯诺达尔"}}'},
{"role": "function-response", "content": '{"times_list": ["6:00", "18:00"]}'}
]
inputs = generation_pipeline.tokenizer.apply_chat_template(
history_messages,
tokenize=False,
add_generation_prompt=True,
tools=[get_weather, get_sunrise_sunset_times],
)
terminator_ids = [
generation_pipeline.tokenizer.eos_token_id,
generation_pipeline.tokenizer.convert_tokens_to_ids("<end_of_turn>")
]
outputs = generation_pipeline(
inputs,
max_new_tokens=512,
eos_token_id=terminator_ids,
)
print(outputs[0]["generated_text"][len(inputs):])
📚 详细文档
提示结构和预期内容
为了使模型正常工作,建议使用 apply_chat_template
。您需要以特定格式传递消息历史记录:
history_messages = [
{"role": "...", "content": "..."},
...
]
可用的角色如下:
system
- 这是一个可选角色,其内容始终位于最前面,并在列出模型可用的函数(工具)之前。您可以始终使用训练期间使用的标准模板:"你是一个有用的助手,可以使用以下函数。如有需要,请使用它们 - "user
- 用户的请求通过此角色传递。function-call
- 函数调用的主体通过此角色传递。尽管模型经过训练以生成 "函数调用: {...}<end_of_turn>" 形式的函数调用,但您仍然只需在 "content" 字段中传递主体 "{...}",因为使用apply_chat_template
时,指令中的后缀会自动添加。function-response
- 在这个角色中,我们需要以字典 '{"name_returnable_value": value}' 的形式在 "content" 字段中传递函数的响应。model
- 与此角色相关的内容被视为模型生成的文本。
函数调用的聊天历史结构
[
{"role": "system", "content": "你是一个有用的助手,可以使用以下函数。如有需要,请使用它们 - "},
{"role": "user", "content": "你好,你能告诉我克拉斯诺达尔的日出时间吗?"},
{"role": "function-call", "content": '{"name": "get_sunrise_sunset_times", "arguments": {"city": "克拉斯诺达尔"}}'},
{"role": "function-response", "content": '{"times_list": ["6:00", "18:00"]}'}
]
这看起来像:
<bos><start_of_turn>user
你是一个有用的助手,可以使用以下函数。如有需要,请使用它们 - {
"name": "get_weather",
"description": "该函数用于返回指定城市的天气情况。",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "需要查询天气的城市。"
}
},
"required": [
"city"
]
}
},
{
"name": "get_sunrise_sunset_times",
"description": "该函数用于返回指定城市当前日期的日出和日落时间(无需用户提供日期),格式为列表:[日出时间, 日落时间]。",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "可以查询日出和日落时间的城市。"
}
},
"required": [
"city"
]
}
}
你好,你能告诉我克拉斯诺达尔的日出时间吗?<end_of_turn>
<start_of_turn>model
函数调用: {"name": "get_sunrise_sunset_times", "arguments": {"city": "克拉斯诺达尔"}}<end_of_turn>
<start_of_turn>user
函数响应: {"times_list": ["6:00", "18:00"]}<end_of_turn>
普通用户 - 模型模板的聊天历史结构
[
{"role": "system", "content": "你是一个友好的助手"},
{"role": "user", "content": "给我讲讲莫斯科"}
]
这看起来像:
<bos><start_of_turn>user
你是一个友好的助手
给我讲讲莫斯科<end_of_turn>
🔧 技术细节
模型评估
在训练过程中,验证损失接近以下值:
模型 | 生成语言 | 近似验证损失 |
---|---|---|
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 |
📄 许可证
本模型采用 apache-2.0
许可证。
📖 引用
@article{gemma_2024,
title={Gemma},
url={https://www.kaggle.com/m/3301},
DOI={10.34740/KAGGLE/M/3301},
publisher={Kaggle},
author={Gemma Team},
year={2024}
}
Phi 2 GGUF
其他
Phi-2是微软开发的一个小型但强大的语言模型,具有27亿参数,专注于高效推理和高质量文本生成。
大型语言模型 支持多种语言
P
TheBloke
41.5M
205
Roberta Large
MIT
基于掩码语言建模目标预训练的大型英语语言模型,采用改进的BERT训练方法
大型语言模型 英语
R
FacebookAI
19.4M
212
Distilbert Base Uncased
Apache-2.0
DistilBERT是BERT基础模型的蒸馏版本,在保持相近性能的同时更轻量高效,适用于序列分类、标记分类等自然语言处理任务。
大型语言模型 英语
D
distilbert
11.1M
669
Llama 3.1 8B Instruct GGUF
Meta Llama 3.1 8B Instruct 是一个多语言大语言模型,针对多语言对话用例进行了优化,在常见的行业基准测试中表现优异。
大型语言模型 英语
L
modularai
9.7M
4
Xlm Roberta Base
MIT
XLM-RoBERTa是基于100种语言的2.5TB过滤CommonCrawl数据预训练的多语言模型,采用掩码语言建模目标进行训练。
大型语言模型 支持多种语言
X
FacebookAI
9.6M
664
Roberta Base
MIT
基于Transformer架构的英语预训练模型,通过掩码语言建模目标在海量文本上训练,支持文本特征提取和下游任务微调
大型语言模型 英语
R
FacebookAI
9.3M
488
Opt 125m
其他
OPT是由Meta AI发布的开放预训练Transformer语言模型套件,参数量从1.25亿到1750亿,旨在对标GPT-3系列性能,同时促进大规模语言模型的开放研究。
大型语言模型 英语
O
facebook
6.3M
198
1
基于transformers库的预训练模型,适用于多种NLP任务
大型语言模型
Transformers

1
unslothai
6.2M
1
Llama 3.1 8B Instruct
Llama 3.1是Meta推出的多语言大语言模型系列,包含8B、70B和405B参数规模,支持8种语言和代码生成,优化了多语言对话场景。
大型语言模型
Transformers 支持多种语言

L
meta-llama
5.7M
3,898
T5 Base
Apache-2.0
T5基础版是由Google开发的文本到文本转换Transformer模型,参数规模2.2亿,支持多语言NLP任务。
大型语言模型 支持多种语言
T
google-t5
5.4M
702
精选推荐AI模型
Llama 3 Typhoon V1.5x 8b Instruct
专为泰语设计的80亿参数指令模型,性能媲美GPT-3.5-turbo,优化了应用场景、检索增强生成、受限生成和推理任务
大型语言模型
Transformers 支持多种语言

L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一个基于SODA数据集训练的超小型对话模型,专为边缘设备推理设计,体积仅为Cosmo-3B模型的2%左右。
对话系统
Transformers 英语

C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基于RoBERTa架构的中文抽取式问答模型,适用于从给定文本中提取答案的任务。
问答系统 中文
R
uer
2,694
98
智启未来,您的人工智能解决方案智库
简体中文