Utilizarea LLM-urilor pre-antrenate

Introducere în LLM-uri cu Python

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

Înțelegerea limbajului

Persoană la birou cu mai multe sarcini în jur, ilustrând clasificarea textului, rezumarea, analiza sentimentelor și întrebări și răspunsuri

Introducere în LLM-uri cu Python

Generarea limbajului

Persoană la birou cu mai multe sarcini în jur, ilustrând generarea de text și traducerea

Introducere în LLM-uri cu Python

Generare de text

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)
  • Coerent
  • Semnificativ
  • Text similar cu cel uman
  • eos_token_id: ID-ul tokenului de sfârșit de secvență
Introducere în LLM-uri cu Python

Generare de text

Ilustrație a două secvențe: we should go, i really like to travel. Include ID-uri de token și indică unde se află padding-ul și EOS

  • pad_token_id: completează spațiul rămas până la max_length
  • Padding: adăugare de tokeni
  • Setat la generator.tokenizer.eos_token_id marchează sfârșitul textului semnificativ, învățat prin antrenare
  • Modelul generează până la max_length sau pad_token_id
  • truncation = True
Introducere în LLM-uri cu Python

Generare de text

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.
  • Rezultatul poate fi slab dacă promptul este vag
Introducere în LLM-uri cu Python

Ghidarea rezultatului

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!
Introducere în LLM-uri cu Python

Traducere automată

  • Hugging Face oferă o listă completă de sarcini și modele de traducere
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.
Introducere în LLM-uri cu Python

Să exersăm!

Introducere în LLM-uri cu Python

Preparing Video For Download...