Salesforceの70億パラメータのLLMのxgen-7b-8k-inst の使い方
今回は、Saolesforceの70億パラメータのxgen-7b-8k-instを試してみます。前回、xgen-7b-8k-baseを実行しましたが、それをベースにdatabricks-dolly-15k, oasst1, BaizeとGPT関係のデータセットで訓練したそうです。
今回使用したコードは、以下を参考にしています。
今回、Google Colabで実行したコードとなります。
!pip install tiktoken transformers
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("Salesforce/xgen-7b-8k-inst", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("Salesforce/xgen-7b-8k-inst", torch_dtype=torch.bfloat16)
header = (
"A chat between a curious human and an artificial intelligence assistant. "
"The assistant gives helpful, detailed, and polite answers to the human's questions.\n\n"
)
article = """
Advantages:
ChatGPT, developed by OpenAI, is a state-of-the-art language model that excels in generating human-like text. Its primary strength lies in its ability to understand context and
generate relevant responses, making it ideal for tasks such as drafting emails, writing articles, or engaging in conversation. It's trained on a diverse range of internet text,
enabling it to handle a wide array of topics. Furthermore, it can be fine-tuned to specific tasks, increasing its versatility.
Lastly, it supports multiple languages, broadening its applicability.
Disadvantages:
Despite its strengths, ChatGPT has limitations. It doesn't have access to real-time information or personal data unless explicitly provided during the conversation,
limiting its ability to provide personalized or up-to-date responses. It can sometimes generate incorrect or nonsensical responses,
as it's based on pattern recognition rather than understanding. It can also be excessively verbose and tends to overuse certain phrases.
Ethical concerns arise as it can be used to generate misleading information or deepfake text.
Lastly, it requires careful handling to avoid generating inappropriate content.
""" # insert a document here
prompt = f"### Human: Please summarize the following article.\n\n{article}.\n###"
inputs = tokenizer(header + prompt, return_tensors="pt")
sample = model.generate(**inputs, do_sample=True, max_new_tokens=2048, top_k=100, eos_token_id=50256)
output = tokenizer.decode(sample[0])
print(output.strip().replace("Assistant:", ""))
このプログラムは、articleに書いた記事を要約するコードとなります。
では、同じ内容の日本語の場合は、どうなるか見てみます。
article = """
利点:
ChatGPTはOpenAIによって開発された最先端の言語モデルで、人間らしいテキストを生成することに優れています。その主な強みは、文脈を理解し関連する応答を生成する能力にあり、メールの作成、記事の執筆、会話の進行などのタスクに理想的です。インターネット上の多様なテキストを学習しているため、幅広いトピックを扱うことができます。さらに、特定のタスクに微調整することができ、その汎用性を高めています。最後に、複数の言語をサポートしているため、その適用範囲が広がります。
欠点:
その強みにもかかわらず、ChatGPTには制限があります。リアルタイムの情報や個人データにはアクセスできず、会話中に明示的に提供されない限り、パーソナライズされたまたは最新の応答を提供する能力が制限されます。理解ではなくパターン認識に基づいているため、時折、誤ったまたは無意味な応答を生成することがあります。また、過度に冗長で、特定のフレーズを過度に使用する傾向があります。誤解を招く情報やディープフェイクテキストを生成するために使用される可能性があるため、倫理的な懸念が生じます。最後に、不適切なコンテンツを生成しないように注意深く取り扱う必要があります。
""" # insert a document here
prompt = f"### Human: Please summarize the following article.\n\n{article}.\n###"
inputs = tokenizer(header + prompt, return_tensors="pt")
sample = model.generate(**inputs, do_sample=True, max_new_tokens=2048, top_k=100, eos_token_id=50256)
output = tokenizer.decode(sample[0])
print(output.strip().replace("Assistant:", ""))
今回のxgen-7b-8k-instは、精巧に作られているのではないでしょうか。せっかくですので、xgen-7b-8k-baseで聞いた質問も聞いてみます。
inputs = tokenizer("What is VisionOS?", return_tensors="pt")
sample = model.generate(**inputs, max_length=128)
print(tokenizer.decode(sample[0]))
他にも色々と聞いてみます。
inputs = tokenizer("Do you introduce yourself?", return_tensors="pt")
sample = model.generate(**inputs, max_length=128)
print(tokenizer.decode(sample[0]))
inputs = tokenizer("What is ChatGPT?", return_tensors="pt")
sample = model.generate(**inputs, max_length=128)
print(tokenizer.decode(sample[0]))
inputs = tokenizer("日本の総理大臣は誰ですか?", return_tensors="pt")
sample = model.generate(**inputs, max_length=128)
print(tokenizer.decode(sample[0]))
この記事が気に入ったらサポートをしてみませんか?