I followed the book Hands-on Large Language Models to generate my first piece of text:
from transformers import AutoModelForCausalLM, AutoTokenizer
# Load model and tokenizer
= AutoModelForCausalLM.from_pretrained(
model "microsoft/Phi-3-mini-4k-instruct",
="cuda",
device_map="auto",
torch_dtype=False,
trust_remote_code
)= AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
tokenizer
from transformers import pipeline
# Create a pipeline
= pipeline(
generator "text-generation",
=model,
model=tokenizer,
tokenizer=False,
return_full_text=500,
max_new_tokens=False
do_sample
)
# The prompt (user input / query)
= [
messages "role": "user", "content": "tell a funny joke"}
{
]
# Generate output
= generator(messages)
output print(output[0]["generated_text"])
I asked it to tell a funny joke. The output was:
Why don't scientists trust atoms? Because they make up everything!
The sentence is fluent and understandable, but at first I didn’t get the punchline. When I asked the model what was funny about the sentence, it ran out of memory and couldn’t explain. So I turned to ChatGPT and finally got the humor: It was an English pun.