基础模型: ytu-ce-cosmos/Turkish-Llama-8b-DPO-v0.1
标签:
- 文本生成推理
- 转换器
- unsloth
- llama
- trl
- sft
许可证: apache-2.0
语言:
- 英语
- 土耳其语
数据集:
- atasoglu/turkish-function-calling-20k
管道标签: 文本生成
上传的模型
此模型基于ytu-ce-cosmos/Turkish-Llama-8b-DPO-v0.1进行适配,并在atasoglu/turkish-function-calling-20k数据集上进行了微调,以执行土耳其语的函数调用任务。
- 开发者: atasoglu
- 许可证: apache-2.0
- 微调自模型: ytu-ce-cosmos/Turkish-Llama-8b-DPO-v0.1
此llama模型使用Unsloth和Huggingface的TRL库进行了2倍速的训练。

使用方法
首先,加载模型:
import json
from unsloth import FastLanguageModel
model, tokenizer = FastLanguageModel.from_pretrained(
model_name="atasoglu/Turkish-Llama-3-8B-function-calling",
load_in_4bit=True,
)
FastLanguageModel.for_inference(model)
设置工具和消息:
system_prompt = """你是一个乐于助人、聪明且能够进行函数调用的助手。
我希望你能够使用下面JSON片段中提供的函数来适当地回答用户的问题。
进行函数调用时需要遵循的指令:
* 函数以JSON模式表示。
* 如果用户的问题可以通过这些函数中的至少一个来回答,则在JSON片段中生成合适的函数调用。
* 切勿为函数参数编造信息,仅使用用户提供的信息。
* 如果用户的问题无法通过任何函数回答,仅返回"无法通过给定函数回答"的文本,不要做其他解释。
请按照这些指令回答问题。"""
user_prompt = """### 函数
'''json
{tools}
'''
### 问题
{query}"""
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取给定地点的当前温度。",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市和国家,例如:波哥大, 哥伦比亚",
}
},
"required": ["location"],
"additionalProperties": False,
},
"strict": True,
},
}
]
query = "巴黎现在的天气如何?"
messages = [
{
"role": "system",
"content": system_prompt,
},
{
"role": "user",
"content": user_prompt.format(
tools=json.dumps(tools, ensure_ascii=False),
query=query,
),
},
]
注意: 在运行前将用户提示中的单引号字符更改为反引号以指定JSON片段。
然后,生成并评估输出:
import re
def eval_function_calling(text):
match_ = re.search(r"```json(.*)```", text, re.DOTALL)
if match_ is None:
return False, text
return True, json.loads(match_.group(1).strip())
inputs = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_dict=True,
return_tensors="pt",
).to("cuda")
generation_kwargs = dict(
do_sample=True,
use_cache=True,
max_new_tokens=500,
temperature=0.3,
top_p=0.9,
top_k=40,
)
outputs = model.generate(**inputs, **generation_kwargs)
output_ids = outputs[:, inputs["input_ids"].shape[1] :]
generated_texts = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
has_function_calling, results = eval_function_calling(generated_texts[0])
if has_function_calling:
for result in results:
fn = result["function"]
name, args = fn["name"], fn["arguments"]
print(f"调用函数 {name!r} 并使用以下参数: {args}")
else:
print(f"无函数调用: {results!r}")
输出:
调用函数 'get_weather' 并使用以下参数: {"location":"巴黎, 法国"}