🚀 Granite-20B-FunctionCalling
Granite-20B-FunctionCalling 是一个基于 IBM 的 granite-20b-code-instruct 模型微调得到的模型,旨在为 Granite 模型家族引入函数调用能力。该模型采用多任务训练方法,针对函数调用中包含的七项基本任务进行训练,包括嵌套函数调用、函数链、并行函数、函数名称检测、参数值对检测、最优下一个函数和响应生成。
🚀 快速开始
模型概述
预期用途
该模型旨在响应与函数调用相关的指令。
💻 使用示例
基础用法
以下是一个如何使用 Granite-20B-Code-FunctionCalling 模型的简单示例:
import json
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
device = "cuda"
model_path = "ibm-granite/granite-20b-functioncalling"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path, device_map=device)
model.eval()
query = "What's the current weather in New York?"
functions = [
{
"name": "get_current_weather",
"description": "Get the current weather",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
},
{
"name": "get_stock_price",
"description": "Retrieves the current stock price for a given ticker symbol. The ticker symbol must be a valid symbol for a publicly traded company on a major US stock exchange like NYSE or NASDAQ. The tool will return the latest trade price in USD. It should be used when the user asks about the current or most recent price of a specific stock. It will not provide any other information about the stock or company.",
"parameters": {
"type": "object",
"properties": {
"ticker": {
"type": "string",
"description": "The stock ticker symbol, e.g. AAPL for Apple Inc."
}
},
"required": ["ticker"]
}
}
]
payload = {
"functions_str": [json.dumps(x) for x in functions],
"query": query,
}
instruction = tokenizer.apply_chat_template(payload, tokenize=False, add_generation_prompt=True)
input_tokens = tokenizer(instruction, return_tensors="pt").to(device)
outputs = model.generate(**input_tokens, max_new_tokens=100)
outputs = tokenizer.batch_decode(outputs)
for output in outputs:
print(output)
📄 许可证
本项目采用 Apache 2.0 许可证。