事前学習済みLLMの利用

Python で学ぶ LLM の入門

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

言語理解

机に座り、テキストの分類、要約、感情分析、質問と回答を示す複数のタスクを抱えている人物

Python で学ぶ LLM の入門

言語生成

机に座り、周囲に複数のタスクがある人物がテキスト生成と翻訳を表現している

Python で学ぶ LLM の入門

テキスト生成

generator = pipeline(task="text-generation", model="distilgpt2")

prompt = "The Gion neighborhood in Kyoto is famous for"

output = generator(prompt, max_length=100, pad_token_id=generator.tokenizer.eos_token_id)
  • 一貫性がある
  • 意味のある
  • 人間らしいテキスト
  • eos_token_id: 終了シーケンストークンID
Python で学ぶ LLM の入門

テキスト生成

二つのシーケンスのイラスト: 行こう、私は旅行が本当に好き。 トークンIDを含み、パディングとEOSの位置を表示します

  • pad_token_id:最大でmax_length{{1}}までの余分なスペースを埋める
  • パディング: トークンの追加
  • generator.tokenizer.eos_token_idに設定すると、意味のあるテキストの終わりを示します
  • モデルは最大 max_length または pad_token_id {{4}} を生成します
  • truncation = True
Python で学ぶ LLM の入門

テキスト生成

generator = pipeline(task="text-generation", model="distilgpt2")

prompt = "The Gion neighborhood in Kyoto is famous for"

output = generator(prompt, max_length=100, pad_token_id=generator.tokenizer.eos_token_id)

print(output[0]["generated_text"])
The Gion neighborhood in Kyoto is famous for its many colorful green forests, such as the 
Red Hill, the Red River and the Red River. The Gion neighborhood is home to the world's 
tallest trees.
  • プロンプトが曖昧な場合、出力は最適以下になる場合があります
Python で学ぶ LLM の入門

出力を誘導する

generator = pipeline(task="text-generation", model="distilgpt2")


review = "This book was great. I enjoyed the plot twist in Chapter 10." response = "Dear reader, thank you for your review." prompt = f"Book review:\n{review}\n\nBook shop response to the review:\n{response}"
output = generator(prompt, max_length=100, pad_token_id=generator.tokenizer.eos_token_id) print(output[0]["generated_text"])
Dear reader, thank you for your review. We'd like to thank you for your reading!
Python で学ぶ LLM の入門

言語翻訳

  • Hugging Faceには翻訳タスクとモデルの完全なリストがあります
translator = pipeline(task="translation_en_to_es", model="Helsinki-NLP/opus-mt-en-es")

text = "Walking amid Gion's Machiya wooden houses was a mesmerizing experience."
output = translator(text, clean_up_tokenization_spaces=True)
print(output[0]["translation_text"])
Caminar entre las casas de madera Machiya de Gion fue una experiencia fascinante.
Python で学ぶ LLM の入門

練習しましょう!

Python で学ぶ LLM の入門

Preparing Video For Download...