Dùng LLM đã huấn luyện sẵn

Nhập môn LLMs trong Python

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

Hiểu ngôn ngữ

Người ngồi tại bàn với nhiều tác vụ xung quanh: phân loại văn bản, tóm tắt, phân tích cảm xúc, hỏi đáp

Nhập môn LLMs trong Python

Tạo ngôn ngữ

Người ngồi tại bàn với các tác vụ xung quanh: sinh văn bản và dịch

Nhập môn LLMs trong Python

Sinh văn bản

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)
  • Mạch lạc
  • Có ý nghĩa
  • Giống văn bản con người
  • eos_token_id: ID token kết thúc chuỗi
Nhập môn LLMs trong Python

Sinh văn bản

Minh họa hai chuỗi: we should go, i really like to travel. Gồm ID token và vị trí padding và EOS

  • pad_token_id: lấp đầy đến max_length
  • Padding: thêm token
  • Đặt generator.tokenizer.eos_token_id để đánh dấu hết nội dung có ý nghĩa, mô hình học được trong huấn luyện
  • Mô hình sinh tối đa đến max_length hoặc pad_token_id
  • truncation = True
Nhập môn LLMs trong Python

Sinh văn bản

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.
  • Kết quả có thể kém nếu prompt mơ hồ
Nhập môn LLMs trong Python

Dẫn hướng đầu ra

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!
Nhập môn LLMs trong Python

Dịch ngôn ngữ

  • Hugging Face có danh sách đầy đủ tác vụ và mô hình dịch
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.
Nhập môn LLMs trong Python

Hãy luyện tập!

Nhập môn LLMs trong Python

Preparing Video For Download...