Uso di LLM pre-addestrati

Introduzione agli LLM in Python

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

Comprensione del linguaggio

Persona seduta alla scrivania con vari task attorno: classificazione testo, sintesi, analisi del sentiment, domanda-risposta

Introduzione agli LLM in Python

Generazione del linguaggio

Persona alla scrivania con vari task attorno: generazione di testo e traduzione

Introduzione agli LLM in Python

Generazione di testo

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)
  • Coerente
  • Significativo
  • Testo simile a quello umano
  • eos_token_id: ID del token di fine sequenza
Introduzione agli LLM in Python

Generazione di testo

Illustrazione di due sequenze: we should go, i really like to travel. Include ID dei token e mostra dove ci sono padding ed EOS

  • pad_token_id: riempie lo spazio extra fino a max_length
  • Padding: aggiunta di token
  • Impostarlo a generator.tokenizer.eos_token_id segna la fine del testo utile, appresa in training
  • Il modello genera fino a max_length o pad_token_id
  • truncation = True
Introduzione agli LLM in Python

Generazione di testo

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.
  • L'output può essere subottimale se il prompt è vago
Introduzione agli LLM in Python

Guidare l'output

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!
Introduzione agli LLM in Python

Traduzione linguistica

  • Hugging Face ha un elenco completo di task e modelli di traduzione
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.
Introduzione agli LLM in Python

Passiamo alla pratica !

Introduzione agli LLM in Python

Preparing Video For Download...