Guiding unstructured responses

Lavorare con Llama 3

Imtihan Ahmed

Machine Learning Engineer

Controlling model output

  • ✅ Parameters
  • ✅ Roles

How to refine model

Lavorare con Llama 3

Controlling model output

  • ✅ Parameters
  • ✅ Roles

How to refine model - refine prompts

Lavorare con Llama 3

Controlling model output

  • ✅ Parameters
  • ✅ Roles

How to refine model - zero-shot/few-shot prompting

Lavorare con Llama 3

Controlling model output

  • ✅ Parameters
  • ✅ Roles

How to refine model - use stop words

Lavorare con Llama 3

Refining prompts

  • Example: summarization
text_choice1 = "Summarize key trends in the aviation industry from the last year, 
                focusing on fuel efficiency innovations."

text_choice2 = "Tell me about the aviation industry."
output = llm(text_choice1) # More specific prompt is more effective print(output['choices'][0]['text'])
The aviation industry has made significant strides in fuel efficiency innovations 
over the last year, driven by the need to reduce greenhouse gas emissions and 
operating costs. Sustainable Aviation Fuels (SAFs) have emerged as a key trend...
Lavorare con Llama 3

Components of effective prompting

Components of effective prompting

Lavorare con Llama 3

Components of effective prompting

Components of effective prompting - Precision

Lavorare con Llama 3

Components of effective prompting

Components of effective prompting - ambiguity

Lavorare con Llama 3

Components of effective prompting

Components of effective prompting - keywords

Lavorare con Llama 3

Components of effective prompting

Components of effective prompting - action words

Lavorare con Llama 3

Components of effective prompting

Components of effective prompting - examples

Lavorare con Llama 3

Zero-shot prompting

  • Zero-shot prompting: a single instruction
text = "Summarize recent mergers in the airline industry."

output = llm(text) print(output['choices'][0]['text'])
Recent mergers in the airline industry include Alaska Air Group's acquisition of 
Hawaiian Airlines in 2024, with both airlines continuing to operate as separate 
brands. In 2022, Delta Air Lines purchased 20% of LATAM Airlines Group...
Lavorare con Llama 3

Refining zero-shot prompts

  • Need to distinguish task, expected output, additional context
  • Zero-shot prompting with labels
text = """
       INSTRUCTION: Write concisely and in 2-3 sentences that cover only key points.
       QUESTION: Summarize recent mergers in the airline industry.
       ANSWER:
       """
Lavorare con Llama 3

Few-shot prompting

  • Few-shot prompting: use of multiple examples

Prompt

Lavorare con Llama 3

Few-shot prompting

  • Few-shot prompting: use of multiple examples

Prompt with 'aircraft model' highlighted

Lavorare con Llama 3

Few-shot prompting

  • Few-shot prompting: use of multiple examples

Prompt with 'passenger capacity' highlighted

Lavorare con Llama 3

Few-shot prompting

  • Few-shot prompting: use of multiple examples

Prompt with 'fuel consumption' highlighted

Lavorare con Llama 3

Few-shot prompting

$$

output = llm(f"Continue the entries: {text}") 

print(output['choices'][0]['text'])
Aircraft Model: Airbus A350-900
Passenger Capacity: 350
Fuel Consumption: 2.39 liters per seat per 100 km
Lavorare con Llama 3

Using stop words

  • Need concise insights
  • Use stop words to end the response at a specific point
  • Example: question-answering application
text = "Which airlines operate direct flights from London to Singapore?"


output = llm(text, stop=["Q:"]) # Stop responses at "Q:"
print(output['choices'][0]['text'])
You can fly direct from London to Singapore with Singapore Airlines and 
British Airways.
Lavorare con Llama 3

Let's practice!

Lavorare con Llama 3

Preparing Video For Download...