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
|
|
25 |
|
26 |
以上
|
27 |
|
28 |
+
以下、参考までに上記プログラムのコードを貼っておきます。
|
29 |
+
```
|
30 |
+
# -*- coding: utf-8 -*-
|
31 |
+
#"""LLM2024_Model_Inference_fewshot2_20241217-L4.ipynb#
|
32 |
+
#
|
33 |
+
#Automatically generated by Colab.
|
34 |
+
#
|
35 |
+
#Original file is located at
|
36 |
+
# https://colab.research.google.com/drive/1kEddvU3WIhShASF95yiua6iOpLDL04jV
|
37 |
+
#
|
38 |
+
#評価準備
|
39 |
+
#
|
40 |
+
### インストール
|
41 |
+
#
|
42 |
+
#* pip installは失敗することがあるので、失敗したらもう一度実行してください
|
43 |
+
#
|
44 |
+
#* githubから必要なファイルをコピーしてきます。gitが使える環境が必須です。
|
45 |
+
#"""
|
46 |
+
#
|
47 |
+
# Commented out IPython magic to ensure Python compatibility.
|
48 |
+
# %%capture
|
49 |
+
# # 1. ライブラリのインストール
|
50 |
+
# # 必要なライブラリをインストール
|
51 |
+
%%capture
|
52 |
+
!pip install unsloth
|
53 |
+
!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
54 |
+
!pip install -U torch
|
55 |
+
!pip install -U peft
|
56 |
+
|
57 |
+
!pip install openai
|
58 |
+
!pip install unsloth-zoo # Install or update unsloth-zoo package
|
59 |
+
!pip install --upgrade --no-cache-dir "unsloth-zoo @ git+https://github.com/unslothai/unsloth-zoo.git"
|
60 |
+
|
61 |
+
!pip install --upgrade openai
|
62 |
+
#!pip install --upgrade transformers[olmo2]
|
63 |
+
|
64 |
+
!pip install openai #==1.55.3 # This line installs openai==1.55.3
|
65 |
+
!pip install --upgrade transformers
|
66 |
+
!pip install --upgrade trl
|
67 |
+
|
68 |
+
# 推論&評価用ライブラリ h.godai
|
69 |
+
!pip install --upgrade git+https://github.com/h-godai/llm
|
70 |
+
|
71 |
+
# 必要なファイルをgithubから持ってきます。環境構築した直後に一度だけ実行してください。
|
72 |
+
|
73 |
+
!git clone https://github.com/h-godai/llm.git godai_temp
|
74 |
+
!cp -rv "godai_temp/LLM2024_env/" .
|
75 |
+
!rm -r godai_temp
|
76 |
+
|
77 |
+
#"""2回目以降はここから実行してください。"""
|
78 |
+
|
79 |
+
from google.colab import userdata
|
80 |
+
|
81 |
+
# 各APIキーの取得
|
82 |
+
OPENAI_API_KEY = userdata.get('OPENAI_API_KEY')
|
83 |
+
GEMINI_API_KEY = userdata.get('GEMINI_API_KEY')
|
84 |
+
HF_TOKEN = userdata.get('HF_TOKEN')
|
85 |
+
|
86 |
+
# 必要なライブラリを読み込み
|
87 |
+
from unsloth import FastLanguageModel
|
88 |
+
from peft import PeftModel
|
89 |
+
import torch
|
90 |
+
import json
|
91 |
+
from tqdm import tqdm
|
92 |
+
import re
|
93 |
+
|
94 |
+
import inference_evaluator.InferenceEvaluator as FireStorm
|
95 |
+
|
96 |
+
DataDir = "./LLM2024_env"
|
97 |
+
|
98 |
+
# モデルのロード
|
99 |
+
from huggingface_hub import login
|
100 |
+
from peft import LoraConfig
|
101 |
+
|
102 |
+
adapter_id = None
|
103 |
+
dpo_adapter_id = None
|
104 |
+
model_id = "llm-jp/llm-jp-3-13b"
|
105 |
+
adapter_id = "h-godai/llm-jp-3-13b-ft8-cm25k-dpo1.5x2_bad28_3.44_fs2_LoRA" # Release 15,19
|
106 |
+
|
107 |
+
model_name = model_id if adapter_id is None else adapter_id
|
108 |
+
|
109 |
+
|
110 |
+
# unslothのFastLanguageModelで元のモデルをロード。
|
111 |
+
dtype = None # Noneにしておけば自動で設定
|
112 |
+
load_in_4bit = True # 今回は13Bモデルを扱うためTrue
|
113 |
+
|
114 |
+
if adapter_id:
|
115 |
+
# LoRAと元のモデルを一緒にロード
|
116 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
117 |
+
model_name=adapter_id,
|
118 |
+
dtype=dtype,
|
119 |
+
load_in_4bit=load_in_4bit,
|
120 |
+
trust_remote_code=True,
|
121 |
+
)
|
122 |
+
else:
|
123 |
+
# 元モデルのみロード
|
124 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
125 |
+
model_name=model_id,
|
126 |
+
dtype=dtype,
|
127 |
+
load_in_4bit=load_in_4bit,
|
128 |
+
trust_remote_code=True,
|
129 |
+
)
|
130 |
+
|
131 |
+
model = FastLanguageModel.for_inference(model) # This line is added to fix the error
|
132 |
+
|
133 |
+
import json
|
134 |
+
|
135 |
+
class FewShotGenerator:
|
136 |
+
|
137 |
+
def __init__(self, fewshot_prompt = None):
|
138 |
+
self.fewshot_prompt = fewshot_prompt
|
139 |
+
|
140 |
+
def save(self, path):
|
141 |
+
with open(path, 'w', encoding='utf-8') as f:
|
142 |
+
json.dump(self.fewshot_prompt, f, indent=2, ensure_ascii=False)
|
143 |
+
|
144 |
+
def load(self, path):
|
145 |
+
with open(path, 'r', encoding='utf-8') as f:
|
146 |
+
self.fewshot_prompt = json.load(f)
|
147 |
+
|
148 |
+
# [{ "keywords": [<keyword[:Group]>,...], "fewshot1": <fewshot>, "endshot": <tailshot> }]
|
149 |
+
# Group指定がある場合、同一グループのいずれかがマッチすればOK
|
150 |
+
# それ以外は全マッチが必要
|
151 |
+
#
|
152 |
+
def input_prompt_hook(self, eval, prompt):
|
153 |
+
for fsp in self.fewshot_prompt:
|
154 |
+
kwlen = len(fsp["keywords"])
|
155 |
+
ok = True
|
156 |
+
group = {}
|
157 |
+
for keyword in fsp["keywords"]:
|
158 |
+
if ':' in keyword:
|
159 |
+
# group付、group内のいずれかでOK
|
160 |
+
words = keyword.split(':')
|
161 |
+
keyword = words[0]
|
162 |
+
gr = words[1]
|
163 |
+
hit = keyword in prompt
|
164 |
+
if gr not in group:
|
165 |
+
group[gr] = 0
|
166 |
+
if hit:
|
167 |
+
group[gr] += 1
|
168 |
+
else:
|
169 |
+
# groupなし。全て一致する必要あり
|
170 |
+
if keyword not in prompt:
|
171 |
+
ok = False; # 一つでもなければNG
|
172 |
+
pass
|
173 |
+
pass
|
174 |
+
pass # for keyword in
|
175 |
+
if ok:
|
176 |
+
# グループに0があればNG
|
177 |
+
for gr in group:
|
178 |
+
if group[gr] == 0:
|
179 |
+
ok = False;
|
180 |
+
break
|
181 |
+
pass
|
182 |
+
pass
|
183 |
+
pass
|
184 |
+
if ok and fsp["fewshot1"] is not None:
|
185 |
+
if 'endshot' in fsp:
|
186 |
+
return f"{fsp['fewshot1']}\n### ���示\n{prompt}\n\n{fsp['endshot']}\n### 回答\n"
|
187 |
+
else:
|
188 |
+
return f"{fsp['fewshot1']}\n### 指示\n{prompt}\n\n### 回答\n"
|
189 |
+
pass # for fsp in
|
190 |
+
# ない場合はデフォルト
|
191 |
+
return f"{eval.prefix_prompt_}\n### 指示\n{prompt}\n\n### 回答\n"
|
192 |
+
pass # def
|
193 |
+
pass # class
|
194 |
+
|
195 |
+
fsg = FewShotGenerator()
|
196 |
+
fsg.load(f"{DataDir}/fewshot_prompt.json")
|
197 |
+
|
198 |
+
|
199 |
+
# ここら↓は、LLM2024提出用のJsonl出力
|
200 |
+
|
201 |
+
# データセットの読み込み。
|
202 |
+
# omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。
|
203 |
+
datasets = []
|
204 |
+
with open(f"{DataDir}/elyza-tasks-100-TV_0.jsonl", "r") as f:
|
205 |
+
item = ""
|
206 |
+
for line in f:
|
207 |
+
line = line.strip()
|
208 |
+
item += line
|
209 |
+
if item.endswith("}"):
|
210 |
+
datasets.append(json.loads(item))
|
211 |
+
item = ""
|
212 |
+
|
213 |
+
# 推論開始
|
214 |
+
|
215 |
+
import inference_evaluator.InferenceEvaluator as FireStorm
|
216 |
+
|
217 |
+
FastLanguageModel.for_inference(model)
|
218 |
+
|
219 |
+
evaltask = FireStorm.InferenceEvaluator(model, None) # inference only
|
220 |
+
evaltask.tokenizer_ = tokenizer
|
221 |
+
evaltask.max_tokens_ = 1408 # 1024 # max_tokens
|
222 |
+
evaltask.temperature_ = 0.2 # temperature
|
223 |
+
evaltask.repetition_penalty_ = 1.2 # repetition_penalty
|
224 |
+
evaltask.do_sample_ = evaltask.temperature_ > 0
|
225 |
+
evaltask.top_p = 0.9 # top_p
|
226 |
+
|
227 |
+
evaltask.prefix_prompt_ = "" # "以下の指示に厳密に従って、正確に回答してください。\n"
|
228 |
+
evaltask.suffix_prompt_ = ""
|
229 |
+
evaltask.input_prompt_hook_ = fsg.input_prompt_hook # few shot hook
|
230 |
+
|
231 |
+
results = []
|
232 |
+
for data in datasets:
|
233 |
+
print(f"TaskId: {evaltask.eval_count_}")
|
234 |
+
input = data["input"]
|
235 |
+
output = evaltask.inference(input)
|
236 |
+
print(output)
|
237 |
+
results.append({"task_id": data["task_id"], "input": input, "output": output})
|
238 |
+
evaltask.eval_count_ += 1
|
239 |
+
|
240 |
+
# こちらで生成されたjsolを提出してください。
|
241 |
+
import re
|
242 |
+
import datetime
|
243 |
+
import pytz
|
244 |
+
now = datetime.datetime.now(pytz.timezone("Asia/Tokyo")).strftime('%Y%m%d_%H%M%S')
|
245 |
+
with open(f"./outputs-{now}.jsonl", 'w', encoding='utf-8') as f:
|
246 |
+
for result in results:
|
247 |
+
print(result)
|
248 |
+
json.dump(result, f, ensure_ascii=False)
|
249 |
+
f.write('\n')
|
250 |
+
```
|
251 |
+
|
252 |
|
253 |
# Uploaded model
|
254 |
|