base_model: ytu-ce-cosmos/Turkish-Llama-8b-DPO-v0.1
tags:
- 文本生成推理
- 转换器
- unsloth
- llama
- trl
- sft
license: apache-2.0
language:
- 英语
- 土耳其语
datasets:
- atasoglu/turkish-function-calling-20k
pipeline_tag: 文本生成
上传的模型
此模型基于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": "城市和国家,例如:Bogotá, Colombia",
}
},
"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":"Paris, France"}