Microsoft から出た小型モデル Phi-2 を Colab で試す
Microsoft から出た小型モデル Phi-2 を使ってみたいと思います。
リリース記事: https://www.microsoft.com/en-us/research/blog/phi-2-the-surprising-power-of-small-language-models/
Satya さんがオープンソースにすると言っていたらしいですが、Huggingface のライセンスはまだリサーチのみ許可するライセンスでした。
MICROSOFT RESEARCH LICENSE TERMS
セットアップ
!pip install accelerate einops -Uqq
accelerate が必須だったかわかりませんが、 einops は必要でした。
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
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)
生成を楽にできるようテンプレートとなる関数を作ります。
# プロンプトの準備
def ask(
prompt: str,
system_prompt: str = None,
return_output: bool = False,
) -> str | None:
if system_prompt:
prompt = system_prompt + '\n\n' + prompt
inputs = tokenizer(
prompt,
return_tensors="pt"
).to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
pad_token_id=tokenizer.eos_token_id,
eos_token_id=tokenizer.eos_token_id,
max_new_tokens=256,
temperature=0.01,
repetition_penalty=1.1,
top_p=0.95,
do_sample=True,
streamer=TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=False),
)
if return_output:
return outputs
生成を試す
英語用のモデルなので、英語で試します。
ask("Give me a short answer. What are the first 10 numbers in the fibonacci sequence?")
ask("How did the first President of the United States become a President?")
回答は質問に近いですが、求めているのとは違う形で返ってきます。
日本語も試します。
ask("りんごが5つあります。そこから2つのりんごを取り除きました。残りのりんごの数は何個でしょう?")
全く日本語では答えられないようです。
算数の問題を試します。
ask("A bat and a ball costs $11 total. The bat is $10 more than the ball. How much is the ball")
すんなり正解。
状況把握の問題:
ask("""
### Question
There was a cookie on the table.
Tom entered the room.
The cookie disappeared.
What was likely to have happened?
""")
プログラミングの問題:
ask("Write me a Python function that parses text to extract multiple email addresses and returns them as a list of strings.")
```python
import re
def extract_emails(text):
pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
return re.findall(pattern, text)
# Example usage:
text = "Please contact us at info@example.com or support@example.org for assistance."
print(extract_emails(text)) # Output: ['info@example.com', support@example.org']
```
<|endoftext|>
試しに実行。
import re
def extract_emails(text):
pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
return re.findall(pattern, text)
text = "Please contact us at info.123@example.com or support+gmail@example.org for assistance."
print(extract_emails(text)) # Output: ['info@example.com', support@example.org']
ちゃんと動きました。
ちょっと突っ込んだ知識系の実用的な質問:
ask("My EC2 instance cannot git pull from my remote origin. What should I do?")
```
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._git_pull = None
@property
def git_pull(self):
return self._git_pull
@git_pull.setter
def git_pull(self, value):
if not isinstance(value, bool):
raise ValueError("GitPull must be a boolean")
self._git_pull = value
def _get_git_pull(self):
try:
subprocess.check_call(["git", "pull"])
except subprocess.CalledProcessError as e:
logger.error(f"Failed to git pull from {self.remote}")
raise GitPullError() from e
def run(self, command, *args, **kwargs):
"""Runs the given command on this EC2 instance."""
if self.git_pull and not self._git_pull:
self._get_git_pull()
return super().run(
```
全く求めていない回答となりました。こういうことは答えられないようです。
おわりに
以上、お読みいただきありがとうございます。少しでも参考になればと思います。
もし似たようなコンテンツに興味があれば、フォローしていただけると嬉しいです:
今回使った Colab:
この記事が気に入ったらサポートをしてみませんか?