Downloads · 30 days
7
25% of all-time downloads
tomhao/yi_6b_chat_tool_use
yi_6b_chat_tool_use is a machine learning model from tomhao. Use it for the machine learning task on the model card, and read the license before you ship it in a product. The card lists the license as mit.
这是一个基于 01-ai/Yi-6b-chat 模型进行微调的版本,专门用于实现工具调用(Tool Calling / Function Calling)功能。模型能够理解用户意图,并根据预先定义的工具集,生成一个结构化的 JSON 对象来调用相应的工具,从而与外部 API 或本地函数进行交互。
Downloads · 30 days
7
25% of all-time downloads
All-time downloads
28
Public
Parameters
6.1B
8.1 GB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors8.1 GB · 100%
From the Hugging Face model README
这是一个基于 01-ai/Yi-6b-chat 模型进行微调的版本,专门用于实现工具调用(Tool Calling / Function Calling)功能。模型能够理解用户意图,并根据预先定义的工具集,生成一个结构化的 JSON 对象来调用相应的工具,从而与外部 API 或本地函数进行交互。
此模型在 Yi-6B-Chat 的强大语言理解能力基础上,通过特定格式的指令微调,学会了“思考”何时需要借助外部工具来更好地完成用户的请求。当检测到需要使用工具时,模型不会直接回答,而是会生成一个包含工具名称和所需参数的 JSON 字符串。
开发者可以捕获这个 JSON 输出,执行相应的函数或 API 调用,然后将结果返回给模型,以生成最终的、更准确和丰富的回答。
主要特性:
01-ai/Yi-6b-chat你可以使用 transformers 库轻松地加载和使用此模型。以下是一个简单的示例,展示如何定义工具并让模型生成调用。
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import json
# 1. 加载模型和分词器
model_path = "your_model_path" # 替换为你的模型路径
tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.bfloat16, # 根据你的硬件调整
device_map="auto"
)
# 2. 定义你的工具集 (Tools)
tools = [
{
"name": "get_current_weather",
"description": "获取指定城市的实时天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称,例如:北京、上海"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "温度单位"
}
},
"required": ["city"]
}
},
{
"name": "send_email",
"description": "发送一封电子邮件",
"parameters": {
"type": "object",
"properties": {
"recipient": {
"type": "string",
"description": "收件人邮箱地址"
},
"subject": {
"type": "string",
"description": "邮件主题"
},
"body": {
"type": "string",
"description": "邮件正文内容"
}
},
"required": ["recipient", "subject", "body"]
}
}
]
# 3. 构建 Prompt
query = "帮我查一下北京今天的天气,用摄氏度显示"
system_prompt = f"You are a helpful assistant with access to the following tools. Use them if required to answer the user's query.\n{json.dumps(tools, indent=2)}"
# 使用 Yi-Chat 模型的对话模板
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": query}
]
# 将 messages 转换为模型期望的输入格式
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
# 4. 模型推理
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs,
max_new_tokens=256,
eos_token_id=tokenizer.eos_token_id, # 根据你的 tokenizer 设置
pad_token_id=tokenizer.pad_token_id if tokenizer.pad_token_id is not None else tokenizer.eos_token_id,
do_sample=True,
top_p=0.8,
temperature=0.7
)
response_text = tokenizer.decode(outputs[0][len(inputs.input_ids[0]):], skip_special_tokens=True)
print("--- Model Output ---")
print(response_text)
# 5. 解析并执行工具调用
# !!! 警告:绝不要直接执行模型生成的代码或字符串。始终先进行解析和验证。
try:
tool_call_json = json.loads(response_text)
tool_name = tool_call_json.get("name")
tool_args = tool_call_json.get("arguments", {})
print(f"\n--- Tool Call Parsed ---")
print(f"Tool Name: {tool_name}")
print(f"Arguments: {tool_args}")
# 在这里添加你的工具执行逻辑
# if tool_name == "get_current_weather":
# result = get_current_weather(**tool_args)
# ...
except json.JSONDecodeError:
print("\n--- Final Answer (No Tool Call) ---")
print(response_text)
为了触发工具调用,模型期望的输入遵循特定的格式。在微调期间,我们使用了包含系统指令的对话模板。
模板示例:
<|im_start|>system
You are a helpful assistant with access to the following tools. Use them if required to answer the user's query.
[
{
"name": "get_current_weather",
"description": "获取指定城市的实时天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称,例如:北京、上海"
}
},
"required": ["city"]
}
}
]
<|im_end|>
<|im_start|>user
上海今天天气怎么样?<|im_end|>
<|im_start|>assistant
当模型检测到需要调用工具时,它将在 assistant 部分生成如下的 JSON:
{
"name": "get_current_weather",
"arguments": {
"city": "上海"
}
}
yi-6b-chat 可能存在的所有偏见。模型生成的输出是文本,永远不要在没有严格审查和安全沙箱的情况下,使用 eval() 或 exec() 等函数直接执行模型生成的任何代码或命令。工具调用的 JSON 输出也应经过白名单和参数类型验证,以防止潜在的注入攻击。
此模型旨在作为后端 AI 系统的一部分,用于: