モデル概要
モデル特徴
モデル能力
使用事例
🚀 Qwen2 7B Instruct
このモデルは、複雑な多ターンのツール/関数呼び出しが可能な言語モデルです。Qwen/Qwen2-7B-Instruct をさらに事後学習した結果です。
🚀 クイックスタート
モデルの説明
このモデルは、Qwen/Qwen2-7B-Instruct をさらに事後学習したもので、複雑な多ターンのツール/関数呼び出しが可能です。
学習
このモデルは、多様な関数呼び出し、チャット、命令データからなる独自のデータセットで事後学習(凍結微調整とDPO)されました。
使用方法
Hugging Faceの transformers
と rubra ライブラリ rubra-tools を使用して、このモデルを次のように使用できます。
まず、必要なパッケージをインストールします。
pip install rubra_tools torch==2.3.0 transformers accelerate
また、Node.jsとnpmをインストールする必要があります。インストール後、jsonrepair
パッケージをインストールします。これは、モデルのまれな幻覚を修正するために使用されます。
npm install jsonrepair
1. モデルの読み込み
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
from rubra_tools import preprocess_input, postprocess_output
model_id = "rubra-ai/Qwen2-7B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype="auto",
device_map="auto",
)
2. 関数の定義
ここでは、簡単な数学の連鎖問題に対して4つの関数を使用します。
functions = [
{
'type': 'function',
'function': {
'name': 'addition',
'description': "Adds two numbers together",
'parameters': {
'type': 'object',
'properties': {
'a': {
'description': 'First number to add',
'type': 'string'
},
'b': {
'description': 'Second number to add',
'type': 'string'
}
},
'required': []
}
}
},
{
'type': 'function',
'function': {
'name': 'subtraction',
'description': "Subtracts two numbers",
'parameters': {
'type': 'object',
'properties': {
'a': {
'description': 'First number to be subtracted from',
'type': 'string'
},
'b': {
'description': 'Number to subtract',
'type': 'string'
}
},
'required': []
}
}
},
{
'type': 'function',
'function': {
'name': 'multiplication',
'description': "Multiply two numbers together",
'parameters': {
'type': 'object',
'properties': {
'a': {
'description': 'First number to multiply',
'type': 'string'
},
'b': {
'description': 'Second number to multiply',
'type': 'string'
}
},
'required': []
}
}
},
{
'type': 'function',
'function': {
'name': 'division',
'description': "Divide two numbers",
'parameters': {
'type': 'object',
'properties': {
'a': {
'description': 'First number to use as the dividend',
'type': 'string'
},
'b': {
'description': 'Second number to use as the divisor',
'type': 'string'
}
},
'required': []
}
}
},
]
3. 会話の開始
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the result of four plus six? Take the result and add 2? Then multiply by 5 and then divide by two"},
]
def run_model(messages, functions):
## Format messages in Rubra's format
formatted_msgs = preprocess_input(msgs=messages, tools=functions)
text = tokenizer.apply_chat_template(
formatted_msgs,
tokenize=False,
add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(
model_inputs.input_ids,
max_new_tokens=512
)
generated_ids = [
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
return response
raw_output = run_model(messages, functions)
# Check if there's a function call
function_call = postprocess_output(raw_output)
if function_call:
print(function_call)
else:
print(raw_output)
AIアシスタントによる関数呼び出しである次の出力が表示されるはずです。
[{'id': 'fc65a533', 'function': {'name': 'addition', 'arguments': '{"a": "4", "b": "6"}'}, 'type': 'function'}]
4. 実行されたツールの結果をメッセージ履歴に追加し、会話を続ける
if function_call:
# append the assistant tool call msg
messages.append({"role": "assistant", "tool_calls": function_call})
# append the result of the tool call in openai format, in this case, the value of add 6 to 4 is 10.
messages.append({'role': 'tool', 'tool_call_id': function_call[0]["id"], 'name': function_call[0]["function"]["name"], 'content': '10'})
raw_output1 = run_model(messages, functions)
# Check if there's a function call
function_call = postprocess_output(raw_output1)
if function_call:
print(function_call)
else:
print(raw_output)
LLMは別の呼び出しを行います。
[{'id': '2ffc3de4', 'function': {'name': 'addition', 'arguments': '{"a": "10", "b": "2"}'}, 'type': 'function'}]
🔧 技術詳細
フレームワークのバージョン
- Transformers 4.41.2
- Pytorch 2.3.1+cu121
- Datasets 2.19.2
- Tokenizers 0.19.1
制限事項とバイアス
このモデルは幅広いタスクで良好な性能を発揮しますが、依然として偏ったまたは誤った出力を生成する可能性があります。ユーザーは、このモデルを敏感なまたは重要なアプリケーションで使用する際には、注意と批判的な判断を行使する必要があります。モデルの出力は、学習データに内在するバイアスの影響を受ける可能性があります。
倫理的な考慮事項
ユーザーは、このモデルの展開が倫理的なガイドラインに準拠していることを確認し、生成されたテキストの潜在的な社会的影響を考慮する必要があります。このモデルを有害または誤解を招く内容の生成に悪用することは強く推奨されません。
📄 ライセンス
このモデルは、Apache-2.0ライセンスの下で提供されています。
🙏 謝辞
このモデルについて、Alibaba Cloudに感謝します。
📞 連絡先
このモデルに関する質問やコメントは、rubraチームにお問い合わせください。
📚 引用
このモデルを使用する場合は、次のように引用してください。
@misc {rubra_ai_2024,
author = { Sanjay Nadhavajhala and Yingbei Tong },
title = { Rubra-Qwen2-7B-Instruct },
year = 2024,
url = { https://huggingface.co/rubra-ai/Qwen2-7B-Instruct },
doi = { 10.57967/hf/2683 },
publisher = { Hugging Face }
}



