การใช้ LLM สำเร็จรูป

Python เบื้องต้นสำหรับ LLMs

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

การทำความเข้าใจภาษา

ภาพคนนั่งทำงานที่โต๊ะพร้อมงานต่างๆ ได้แก่ การจำแนกข้อความ การสรุปความ การวิเคราะห์อารมณ์ และการตอบคำถาม

Python เบื้องต้นสำหรับ LLMs

การสร้างภาษา

ภาพคนนั่งทำงานที่โต๊ะพร้อมงานต่างๆ ได้แก่ การสร้างข้อความและการแปลภาษา

Python เบื้องต้นสำหรับ LLMs

การสร้างข้อความ

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 ของ token ที่ใช้สิ้นสุดลำดับ
Python เบื้องต้นสำหรับ LLMs

การสร้างข้อความ

ภาพแสดงสองลำดับ: we should go และ i really like to travel พร้อม token ID และตำแหน่งของ padding และ EOS

  • pad_token_id: เติมพื้นที่ส่วนที่เหลือให้ครบตาม max_length
  • Padding: การเพิ่ม token
  • การกำหนดเป็น generator.tokenizer.eos_token_id ช่วยระบุจุดสิ้นสุดของข้อความ ซึ่งโมเดลเรียนรู้จากการฝึก
  • โมเดลสร้างข้อความจนถึง max_length หรือ pad_token_id
  • truncation = True
Python เบื้องต้นสำหรับ LLMs

การสร้างข้อความ

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.
  • ผลลัพธ์อาจไม่ดีหาก prompt ไม่ชัดเจน
Python เบื้องต้นสำหรับ LLMs

การชี้นำผลลัพธ์

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 เบื้องต้นสำหรับ LLMs

การแปลภาษา

  • 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 เบื้องต้นสำหรับ LLMs

มาฝึกกันเถอะ!

Python เบื้องต้นสำหรับ LLMs

Preparing Video For Download...