QEUR23_PHI2SFT7: PromptEngineering~Reasoningを使ったLLM学習(Finetuning)
# ---
# プロンプト変換用の関数(Batch)
def formatting_prompts_func(examples):
output_text = []
for i in range(len(examples["instruction"])):
instruction = examples["instruction"][i]
input_text = examples["input"][i]
response = examples["output"][i]
if len(input_text) >= 2:
text = f'''Below is an Instruction that describes a task, paired with a Context that provides further information. Write a Response that appropriately completes the request.
### Instruction:
{instruction}
### Context:
{input_text}
### Response:
{response}
'''
else:
text = f'''Below is an Instruction that describes a task. Write a Response that appropriately completes the request.
### Instruction:
{instruction}
### Response:
{response}
'''
output_text.append(text)
return output_text
QEU:FOUNDER : “ちなみに、今回もMITモデルです。我々の今回のプロジェクトは「ぜ~んぶ」ね・・・。”
(重要:MITライセンスへのリンク:これからは、ず~っと使うよ)
D先生 : “あれ?このプログラムは前回のものと同じですよ?”
QEU:FOUNDER : “まずは、データセットが変わったことによる比較です。この実行結果(↑)と、つづく「Reasoningを使った」実行結果を比較しようという試みです。”
(これは、普通のプロンプトです。推論方式A)
# ---
# プロンプトを生成する
instruction = "日本でもっとも高い山はどこですか?"
input_text = ""
examples = [instruction, input_text]
prompt = create_prompts_func(examples)
print(prompt)
# <|system|>Wonder Woman is a warrior princess of the Amazons with a strong sense of justice and a mis-sion.
# ワンダーウーマンは、強い正義感と使命感を持ったアマゾン族の戦士プリンセスです。
#<|user|> What motivates you to fight for peace and love?
# 平和と愛のために戦う動機は何ですか?
#<|assistant|>
# ---
pipe = pipeline(task="text-generation", model=model, tokenizer=tokenizer, max_length=300)
result = pipe(prompt)
print(result[0]['generated_text'])
def generate_text(query):
inputs = tokenizer(query,
return_tensors="pt",
return_attention_mask=False).to('cuda')
outputs = model.generate(**inputs, max_length=200)
text = tokenizer.batch_decode(outputs)[0]
return text
generated_text = generate_text(prompt)
print(generated_text)
# ------
# プロンプト変換用の関数(Batch: For Reasoning)
def formatting_prompts_func(examples):
# ---
sys_message = "You are AI assistant who is good at multilingual support. Switch the answer lan-guage depending on the user."
#sys_message = "あなたは多言語対応が得意なAIアシスタントです。ユーザーに応じて回答する言語を切り替えます。"
# ---
output_text = []
for i in range(len(examples["instruction"])):
instruction = examples["instruction"][i]
input_text = examples["input"][i]
reasoning = examples["reasoning"][i]
response = examples["output"][i]
# ---
if len(input_text) >= 2:
text = f'''
<|system|>{sys_message}\n<|instruction|>{instruction}\n<|context|>{input_text}\n<|reasoning|>{reasoning}\n<|response|>{response}<|endoftext|>
'''
else:
text = f'''
<|system|>{sys_message}\n<|instruction|>{instruction}\n<|reasoning|>{reasoning}\n<|response|>{response}<|endoftext|>
'''
output_text.append(text)
return output_text
D先生 : “なるほど、System promptも明らかになって、今回のprompt engineeringの全貌が見えてきました。これでも、少し「安易な」感じが・・・。”
QEU:FOUNDER : “確かに、これでも「安易」なんだが、これでも効くとなればすごいでしょ?さて、学習損失は以下のようになりました。”
D先生 : “ほう・・・。前回よりも学習損失が減りましたね。”
QEU:FOUNDER : “学習損失とは参考にしか過ぎないが、前回より下がったということは「それなりに期待できる」でしょ?じゃあ、肝心の推論テストをやってみましょう。”
(推論のAタイプ)
(推論のBタイプ)
D先生 : “ついに回答から白山がでましたか・・・。これには驚きました。入力したプロンプトには、この単語はありません。少しだけ前に動いているように思います。。”
QEU:FOUNDER(設定年齢65歳) : “他の事例も見てみましょう。”
(推論のAタイプ)
(推論のBタイプ)
D先生 : “これも全然だめです。それでも、米国という言葉が出てきました。LLMの頭が動いている兆しがあります。Reasoningなしの場合と比較すると、「ヨチヨチ歩き」ながらも進歩がみられています。LLMがJ国語のデータベースを読み込んで解釈をしたいという意図が見えるんです。”
QEU:FOUNDER : “今回は面白かった?Phi-2のモデルって、おそらくJ国語の言語データをほとんど学習していないんです。だから、このままでは良い答えが出てこないんです。”
D先生 : “確かに今回のトライアルは面白いんですが。う~ん・・・。やりたいことがたくさんありすぎて困る・・・。”
QEU:FOUNDER : “小生もそうです。やりたいことがありすぎて、体がついていっていません。今回は、ゆる~いプロジェクトです。次に進めましょう。”
>寄付のお願い(click here)<
QEU:FOUNDER : “次はもうちょっとだけデータセットを改善し、かつ増量しましょう。”
~ まとめ ~
QEU:FOUNDER : “最近は、インターネットの情報が多くなって1つの事柄をいろいろな角度から見ることができるようになりました。”
C部長 : “とある地域のスーパースターの競演(↑)ですね。”
QEU:FOUNDER : “これを、「ものの見方」を変えればこうなる(↓)そうです。”
C部長 : “おお、こうなるのか・・・。”
QEU:FOUNDER : “さらに、他の角度からはこう(↓)見えるようです。。”
C部長 : “やっぱり、物事は立体的に見ないといけませんね。”
QEU:FOUNDER : “人生、一生勉強だなァ・・・。”
コメント
コメントを投稿