QEUR23_PHI2SFT2: PHI2を使って推論してみる(その2-より使いやすく改造する)
~ 価値の源泉は「質問力」!!(中) ~
D先生(設定年齢65歳) : “我々、現在の社会の主流である老人様による「老人によるLLM開発」をつづけましょう。これは我々老人が最先端のLLM(自然言語モデル)をゆるーく使って遊ぶというイベントです。前回は、単純な推論のプログラムでしたが、もうちょっとレベルアップをさせたいですね。”
(重要:MITライセンスへのリンク:これからは、ず~っと使うよ)
QEU:FOUNDER(設定年齢65歳) : “前回の話題でためになったのは、「MITライセンス」だけですね。何しろ、前回に紹介したpythonコードはとても使いづらいし・・・。”
D先生 : “じゃあ、改造していきます?”
QEU:FOUNDER : “まずは「一問一答」方式に改造していきたいね。それは「prompt engineering」だけでやれるよ。プロンプトエンジニアリングの威力を理解するためにも、ちょっと動かしてみましょう。第一弾、プログラムをドン!”
# Inference Microsoft-Phi2(INSTRUCTION)
# ---
!pip install -U transformers
!pip install -U peft
!pip install -U accelerate
# ----
import os
from huggingface_hub import login
login("hf_XXXX ")
# -----
# 普通の推論(1)
# -----
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
torch.set_default_device("cuda")
model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2", torch_dtype="auto", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", trust_remote_code=True)
# -----
# INSTRUCTION FUNCTION
# -----
def generate_llm_response(model, tokenizer, prompt, max_length):
output_termination = "\nOutput:"
total_input = f"Instruct:{prompt}{output_termination}"
inputs = tokenizer(total_input, return_tensors="pt", return_attention_mask=False)
#inputs = inputs.to(device)
eos_token_id = tokenizer.eos_token_id
outputs = model.generate(**inputs, max_length=max_length, eos_token_id=eos_token_id)
# Find the position of "Output:" and extract the text after it
generated_text = tokenizer.batch_decode(outputs)[0]
# Split the text at "Output:" and take the second part
split_text = generated_text.split("Output:", 1)
assistant_response = split_text[1].strip() if len(split_text) > 1 else ""
assistant_response = assistant_response.replace("<|endoftext|>", "").strip()
return assistant_response
# -----
# 普通の推論(2)
# -----
prompt = '''How do you see the current economic climate impacting investment strategies in the upcoming year?'''
max_length = 500
outputs = generate_llm_response(model, tokenizer, prompt, max_length)
print(outputs)
QEU:FOUNDER : “さて、「ゆる~く」やってみた結果が以下の通りです。Instruction方式を使えば、それなりによくなるでしょう?”
(Q&A)
Inputted prompt::Instruct:Please list three industrial fields where LLM (Large-Scale Language Models) can have a large impact on business, their application examples, and the scale of the impact such as personnel reductions. please reply more than 300 and less than 500 words.
Output: LLM (Large-Scale Language Models) have the potential to revolutionize various industrial fields, including healthcare, finance, and manufacturing. Here are three examples of how LLM can have a large impact on business:
- Healthcare: LLM can be used to analyze medical records and identify patterns that can help doctors diagnose diseases more accurately. For example, LLM can be used to analyze patient data and identify risk factors for heart disease. This can help doctors develop personalized treatment plans for patients, reducing the risk of complications and improving patient outcomes.
- Finance: LLM can be used to analyze financial data and identify patterns that can help investors make better invest-ment decisions. For example, LLM can be used to analyze stock market data and identify trends that can help investors predict future market movements. This can help investors make more informed investment decisions, reducing the risk of losses and improving returns.
- Manufacturing: LLM can be used to analyze manufacturing data and identify patterns that can help manufacturers improve their production processes. For example, LLM can be used to analyze data from sensors on manufacturing equipment and identify patterns that can help manufacturers predict equipment failures before they occur. This can help manufacturers reduce downtime and improve productivity, reducing the need for personnel reductions. Overall, LLM has the potential to have a large impact on business by improving decision-making processes and reducing costs. As the technology continues to evolve, we can expect to see even more applications of LLM in various industrial fields.
入力したプロンプト::指示:LLM (Large-Scale Language Model) がビジネスに大きな影響を与える可能性のある産業分野を 3 つ挙げてください。適用例と人員削減などの影響の規模を教えてください。 300 ワード以上 500 ワード未満で返信してください。
出力: LLM (Large-Scale Language Model) は、医療、金融、製造などのさまざまな産業分野に革命を起こす可能性を秘めています。 LLM がビジネスに大きな影響を与える可能性があることを示す 3 つの例を次に示します。
- ヘルスケア: LLM を使用すると、医療記録を分析し、医師が病気をより正確に診断するのに役立つパターンを特定できます。 たとえば、LLM を使用して患者データを分析し、心臓病の危険因子を特定できます。 これは、医師が患者に合わせた治療計画を策定し、合併症のリスクを軽減し、患者の転帰を改善するのに役立ちます。
- 財務: LLM を使用すると、財務データを分析し、投資家がより適切な投資決定を行うのに役立つパターンを特定できます。 たとえば、LLM を使用して株式市場データを分析し、投資家が将来の市場の動きを予測するのに役立つ傾向を特定できます。 これにより、投資家はより多くの情報に基づいた投資決定を行うことができ、損失のリスクが軽減され、収益が向上します。
- 製造: LLM を使用すると、製造データを分析し、メーカーが生産プロセスを改善するのに役立つパターンを特定できます。 たとえば、LLM を使用すると、製造装置のセンサーからのデータを分析し、メーカーが装置の故障を発生前に予測するのに役立つパターンを特定できます。 これにより、メーカーはダウンタイムを削減し、生産性を向上させ、人員削減の必要性を減らすことができます。 全体として、LLM は意思決定プロセスを改善し、コストを削減することにより、ビジネスに大きな影響を与える可能性があります。 テクノロジーが進化し続けるにつれて、LLM はさまざまな産業分野でさらに多くの応用が見られることが期待されます。
D先生 : “本当に、大規模言語モデル(LLM)はというのはすごいですよね。それにしても、回答の品質がいいねえ・・・。”
QEU:FOUNDER : “推論結果を見れば見るほど、Phi-2モデルをJ国語でファインチューニングして、推論をやってみたいと思わん?さてと・・・。今回は、さらにもう一歩レベルアップさせますよ。Webアプリを自動的に作成してくれる便利ツールであるGradioを使います。とても入力が楽になるよ。ちなみに、なんと「Huggingface」に事例コードが載っていたわ・・・。”
D先生 : “なんか、すばらしいタイトルですな・・・。”
QEU:FOUNDER : “じゃあ、つづけて第二弾プログラムをドン!ちなみに、これはMITライセンスなのですが、MITリンクは、先に貼ってありますよ。皆さま、お読みになってください。”
# GRADIO VERSION(HUGGINGFACE)
#######################
'''
License: MIT
'''
#######################
##### Dependencies
""" IMPORTANT: Uncomment the following line if you are in a Colab/Notebook environment """
#!pip install gradio einops accelerate bitsandbytes transformers
#####
import gradio as gr
import transformers
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import random
import re
def cut_text_after_last_token(text, token):
last_occurrence = text.rfind(token)
if last_occurrence != -1:
result = text[last_occurrence + len(token):].strip()
return result
else:
return None
class _SentinelTokenStoppingCriteria(transformers.StoppingCriteria):
def __init__(self, sentinel_token_ids: torch.LongTensor,
starting_idx: int):
transformers.StoppingCriteria.__init__(self)
self.sentinel_token_ids = sentinel_token_ids
self.starting_idx = starting_idx
def __call__(self, input_ids: torch.LongTensor,
_scores: torch.FloatTensor) -> bool:
for sample in input_ids:
trimmed_sample = sample[self.starting_idx:]
if trimmed_sample.shape[-1] < self.sentinel_token_ids.shape[-1]:
continue
for window in trimmed_sample.unfold(
0, self.sentinel_token_ids.shape[-1], 1):
if torch.all(torch.eq(self.sentinel_token_ids, window)):
return True
return False
model_path = 'freecs/ArtificialThinker-Phi2'
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True, load_in_4bit=False, torch_dtype=torch.float16).to(device) #remove .to() if load_in_4/8bit = True
# -----
def phine(message, history, temperature, top_p, top_k, repetition_penalty, sys_message):
n = 0
context = ""
if history and len(history) > 0:
for x in history:
for h in x:
if n%2 == 0:
context+=f"""\n<|prompt|>{h}\n"""
else:
pattern = re.compile(r'<details>.*?</details>')
result = re.sub(pattern, '', h)
context+=f"""<|response|>{result}"""
n+=1
else:
context = ""
prompt = f"""\n<|system|>{sys_message}"""+context+"\n<|prompt|>"+message+"<|endoftext|>\n<|reasoning|>"
tokenized = tokenizer(prompt, return_tensors="pt").to(device)
stopping_criteria_list = transformers.StoppingCriteriaList([
_SentinelTokenStoppingCriteria(
sentinel_token_ids=tokenizer(
"<|endoftext|>",
add_special_tokens=False,
return_tensors="pt",
).input_ids.to(device),
starting_idx=tokenized.input_ids.shape[-1])
])
token = model.generate(**tokenized,
stopping_criteria=stopping_criteria_list,
do_sample=True,
max_length=2048, temperature=temperature, top_p=top_p, top_k = top_k, repeti-tion_penalty = repetition_penalty
)
completion = tokenizer.decode(token[0], skip_special_tokens=True)
token = "<|reasoning|>"
reasoning = cut_text_after_last_token(completion, token)
prompt = f"""\n<|system|>{sys_message}"""+context+"\n<|prompt|>"+message+"\n<|reasoning|>"+reasoning+"\n<|response|>"
tokenized = tokenizer(prompt, return_tensors="pt").to(device)
token = model.generate(**tokenized,
stopping_criteria=stopping_criteria_list,
do_sample=True,
max_length=2048, temperature=temperature, top_p=top_p, top_k = top_k, repeti-tion_penalty = repetition_penalty
)
completion = tokenizer.decode(token[0], skip_special_tokens=True)
token = "<|response|>"
response = cut_text_after_last_token(completion, token)
res = f"""<details><summary>Reasoning (Click Me)</summary>{reasoning}</details>\n\n{response}"""
return res
demo = gr.ChatInterface(phine,
additional_inputs=[
gr.Slider(0.1, 2.0, label="temperature", value=0.3),
gr.Slider(0.1, 2.0, label="Top P", value=0.9),
gr.Slider(1, 500, label="Top K", value=50),
gr.Slider(0.1, 2.0, label="Repetition Penalty", value=1.1),
gr.Textbox(label="System Prompt",max_lines=1,interactive=True, value="You are an AI assistant named Phine developed by FreeCS.org. You are polite and smart.")
]
)
if __name__ == "__main__":
demo.queue().launch(share=True, debug=True) #If debug=True causes problems you can set it to False
QEU:FOUNDER : “このように、他人のプログラムを堂々と貼ることができるのは気持ちのいいことですな(笑)。さて、推論の結果はこんな感じ(↓)です。”
D先生 : “あれ?「Reasoning is all you need」って?”
QEU:FOUNDER : “さあ・・・。現在、調査中です。これ以上の説明は差し控える・・・。”
>寄付のお願い(click here)<
QEU:FOUNDER : “今回は、本当にゆっくりやりたい。「老人によるLLM」だからね・・・。”
~ まとめ ~
・・・ 前回のつづきです ・・・
C部長 : “これらのGURUたちと、「爺さん」といわんのですね。”
QEU:FOUNDER : “わしが爺さんならば、彼らは「ひいじいさん」でしょ?彼らの議論は、もう時代に合わなくなっているんですよ。今の時代ではね、もう全然あわない。もし、爺さんがいうように「品質とはバラツキである」なら、それはすでに買えるのです。ロボットもあるし、我々も提案している外観検査自動機もある。問題は、(ものの)価値をどのように得るかなんです。見てみて、コレ(↓)・・・。”
C部長 : “あれ、F山の写真じゃない?キレイですね。”
QEU:FOUNDER : “これを見ていて、「21世紀の価値のあり方」に気づいたんですよ。この場所には、世界中から観光客が押し寄せて来てごった返しているそうです。・・・でも、人々はなぜここで写真を撮りたいの?”
C部長 : “自分のSNSアカウントにアップしたい。もしくは、他の人に見せつけて自慢したい。”
QEU:FOUNDER : “それは半分くらいは当たっています。彼らは、この写真を撮ることによって、「語る資格」を得ることができたんです。例えば、Cさんが他の人から、「ねえ?あそこの風景ってどうだった?」って聞かれるとするでしょ?どう答える?”
C部長 : “そりゃ、向こう(旅行先)に行って、体験したことを話しますよ。”
QEU:FOUNDER : “小生ならば、体験していないこともバンバン話ますよ。自分がやっていなかったこともお構いなしに・・・。・・・だって、相手は自分が体験していないということは知らないじゃない?今だったら、旅行から帰って、関連したYoutubeを見直して自分の「架空旅行記」をでっちあげることなんかは、とっても簡単!!”
C部長 : “可能ではありますが、ひどいなあ。・・・まあ、見方を変えれば写真を撮ることには、それほどの価値があるということです。”
QEU:FOUNDER : “今、ものの価値というのは大きく変わろうとしているんです。現在において、価値というものは「シェアすること」によって得られるんです。”
C部長 : “それ(価値を得ること)が、DMAICSサイクルの本質ですね。”
QEU:FOUNDER : “一旦、(ものの)価値を得たら、品質を得ることは簡単ですよ。”
コメント
コメントを投稿