We will focus on text
based prompting
What’s a prompt?
- “A prompt is an input to a Generative AI model, that is used to
guide its output”
- “Prompts may consist of text, image, sound, or other media”
Prompting is for in-context learning (ICL)
What’s a prompt template?
- “A prompt template is a function that contains one or more variables
which will be replaced by some media (usually text) to create a
prompt.”
- “a prompt template becomes a prompt when input is inserted into
it.”
What’s a prompt template?
Can have multiple variables
- “Write a {{POEM FORM}} about {{TOPIC}}”
- Write a sonnet about HCAI
What’s a prompt template?
Prompt chain
- “two or more prompt templates used in succession”
- “The output of the prompt generated by the first prompt template is
used to parameterize the second template, continuing until all templates
are exhausted”
Prompt engineering
- “the iterative process of developing a prompt by modifying or
changing the prompting technique that you are using”
Prompt components
- Directives
- Examples/Shots/Exemplars
- Output Formatting
- Style
- Role
- Context
Directives — what’s to be
done?
- Explicit
- Implicit
- “Good morning: buen día
- Good night: ”
Demonstrations that guide the GenAI to accomplish a task
1-shot
- “Good morning: buen día
- Good night: ”
Few-shot
2+2: four
4+5: nine
8+0:
How many shots?
System prompt from Anthropic
Text based prompting
techniques
Text based
prompting techniques — lots of
options!
Text based prompting
techniques
- Zero-shot
- Few-shot
- Chain of thought (CoT)
Zero-shot prompting
techniques
- Role prompting
- Style prompting
- Emotion prompting
- S2A (System 2 Attention)
- Rephrase and Respond (RaR)
- Self-Ask
Role prompting — assign a
role
- “You are a great poet”
- “You are an expert financial advisor”
Style prompting — desired
style
- “Be concise”
- “Respond in songs”
Emotion
prompting — using phrases of “psychological relevance to human”
- “This is very important to my career”
Emotion
prompting — using phrases of “psychological relevance to human”
- “Are you sure that’s your final answer? Believe in your abilities
and strive for excellence. Your hard work will yield remarkable results”
S2A (System 2 Attention)
- Two steps
- Ask to rewrite the prompt to remove any unnecessary information
- Use the rewritten prompt for the answer
Rephrase and Respond (RaR)
- Clarify ambiguous information in the prompt
- “Rephrase and expand the question”
Self-Ask
- Decide if follow-up questions are required
- generate the follow-up questions
- answer the follow-up questions
- answer the main query
Text based prompting
techniques
- Zero-shot
- Few-shot
- Chain of thought
(CoT)
Chain of thought (CoT)
- Explain the reasoning process before answering question
- CoT can be Zero-shot or few-shot
Zero-shot CoT
- Step-back prompting
- Analogical prompting
Step-back prompting
- Abstraction
- Ask the LLM a generic, higher-level concept
- Reasoning
- “Ask the LLM the original question, given its answer to the abstract
question”
- Example
Analogical prompting
- “generate examples that are relevant to the problem”
- Example
Few-shot CoT
- Contrastive few-shot CoT
- Exemplars with both incorrect and correct for the CoT prompt
- How to and how not to reason
- Example
Todo
- Create a meta prompt and run it in GPT or Claude
We often want structured output
When you are using LLMs as a
pipeline or infrastructure for further processing, you
want consistency
Free form text output
is not fun to parse
We want structured
output (using Instructor)
import instructor
class Response(BaseModel):
message: str
r = client.responses.create(
input="Write a haiku about IST 597 (HCAI)",
response_model=Response,
)
Define the structure
of response from LLMs
import instructor
class Response(BaseModel):
message: str
r = client.responses.create(
input="Write a haiku about IST 597 (HCAI)",
response_model=Response,
)
response_model: expected structure
Different data types
class User(BaseModel):
name: str
age: int
Todo: Using different data
types
- Create a new response model that only includes integers
(
int)
- Use it in a prompt that returns an integer
- What happens if your prompt does not return an integer?
Todo: Anthropic prompt
library