Views
No views yet
1from datasets import load_dataset
2
3# データセットの読み込み
4dataset = load_dataset("json", data_files="ichikara-instruction-003-001-1.json")
5
6# プロンプトフォーマットの定義
7prompt = """### 指示
8{}
9### 回答
10{}"""
11
12# フォーマット変換関数
13def formatting_prompts_func(examples):
14 input = examples["text"]
15 output = examples["output"]
16 text = prompt.format(input, output) + tokenizer.eos_token
17 return {"formatted_text": text}
18
19# データの変換
20dataset = dataset.map(
21 formatting_prompts_func,
22 num_proc=4,
23)1import os
2import torch
3import json
4from tqdm import tqdm
5from unsloth import FastLanguageModel
6
7# 必要なパッケージのインストール
8!pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
9!pip install --upgrade torch xformers1# GPU設定の確認
2device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
3
4# モデルとトークナイザーの読み込み
5model_id = "llm-jp/llm-jp-3-13b"
6max_seq_length = 512
7model, tokenizer = FastLanguageModel.from_pretrained(
8 model_name=model_id,
9 dtype=None,
10 load_in_4bit=True,
11 trust_remote_code=True,
12)
13
14# 推論モードに設定
15FastLanguageModel.for_inference(model)1# データセットの読み込み
2datasets = []
3with open("elyza-tasks-100-TV_0.jsonl", "r") as f:
4 item = ""
5 for line in f:
6 line = line.strip()
7 item += line
8 if item.endswith("}"):
9 datasets.append(json.loads(item))
10 item = ""
11
12# 推論実行
13results = []
14for dt in tqdm(datasets):
15 input = dt["input"]
16 prompt = f"""### 指示\n{input}\n### 回答\n"""
17
18 inputs = tokenizer([prompt], return_tensors="pt").to(device)
19 outputs = model.generate(
20 **inputs,
21 max_new_tokens=512,
22 use_cache=True,
23 do_sample=False,
24 repetition_penalty=1.2
25 )
26 prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
27
28 results.append({
29 "task_id": dt["task_id"],
30 "input": input,
31 "output": prediction
32 })
33
34# 結果をjsonl形式で保存
35with open("llm-jp-3-13b-it-v7_output.jsonl", 'w', encoding='utf-8') as f:
36 for result in results:
37 json.dump(result, f, ensure_ascii=False)
38 f.write('\n')1@misc{llm-jp-3-13b-it-v7,
2 author = {[YOUR_NAME]},
3 title = {llm-jp-3-13b-it-v7: Instruction-tuned LLM-JP-3-13B with QLoRA},
4 year = {2024},
5 publisher = {Hugging Face},
6 journal = {Hugging Face Hub},
7 howpublished = {\url{https://huggingface.co/[YOUR_USERNAME]/llm-jp-3-13b-it-v7}},
8}
9
10@inproceedings{sekine-etal-2024-ichikara,
11 title = {ichikara-instruction: LLMのための日本語インストラクションデータの構築},
12 author = {関根 聡 and 安藤 まや and 後藤 美知子 and 鈴木 久美 and 河原 大輔 and 井之上 直也 and 乾 健太郎},
13 booktitle = {言語処理学会第30回年次大会},
14 year = {2024}
15}