Session 3 Prompt Engineering For Generative AI
Session 3 Prompt Engineering For Generative AI
Ram N Sangwan
Prompt – LLMs as Next Word Predictors
Prompt Completion
Four score and seven years ago Fathers brought forth on this continent a new nation ..
our
Prompt & Prompt Engineering
Prompt
• The input or initial text provided
to the model.
Prompt Engineering
• The process of iteratively refining
a prompt for the purpose of
eliciting a particular style of
response.
Aligning LLMs to Follow Instructions
Completion LLMs are trained to predict the next word on a large dataset of
internet text, rather than to safely perform the language task that the user
wants.
Can not give instructions or ask questions to a completion LLM.
• Ensure that the language model understands the user’s intent correctly and provides accurate
responses.
In-Context Learning and K-Shot Prompting
In-context learning – Conditioning (Prompting) an LLM with instructions and/or demonstrations
of the task it is meant to complete.
K-Shot Prompting – Explicitly providing k examples of the intended task in the prompt.
• No example
Zero Shot • The goal is for the Model to make predictions about
new classes by using prior knowledge.
• One example
One Shot • The goal is to make predictions for the new classes
based on this single example
• Some examples
Few Shot • The goal is to make predictions for new classes
based on few examples of labeled data.
Zero Shot Example
• Let's say you want to use an LLM for translation without any fine-tuning or training.
• You can provide the model with a zero-shot prompt like this:
Prompt: "Translate the following English text to French: 'Hello, how are you?’”
• The model understands the structure and semantics of languages and can generate a reasonable
translation in French:
• Even without specific example, it can use the structure of the provided example to generate a new recipe:
Give Examples
Give Examples for completing the task for a desirable outcome
Tokens
Temperature of 0 makes the model deterministic (limits the model to use the word with the highest probability).
When temperature is increased, the distribution is flattened over all words.
With increased temperature, model uses words with lower probabilities
Top k
Top k tells the model to pick the next token from top ”k” tokens in its list, sorted by
probability.
Top p is similar to “Top k” but picks from the top tokens based on the sum of their
probabilities.
These are useful if you want to get rid of repetition in your outputs.
Q: A juggler can juggle 16 balls. Half of the balls are golf balls, and Q: A juggler can juggle 16 balls. Half of the balls are golf balls, and
half of the golf balls are blue. How many blue golf balls are there? half of the golf balls are blue. How many blue golf balls are there?
A: The answer (arabic numerals) is A: Let's think step by step.
(Output) 8 X (Output) There are 16 balls in total. Half of the balls are golf balls.
That means that there are 8 golf balls. Half of the golf balls are
blue. That means that there are 4 blue golf balls.
Chain of Thought – Use and Not to Use Cases
Use Cases Not to Use Cases
Complex Problem Solving: Simple Queries:
• Mathematical Problems. • Fact Retrieval.
• Logical Reasoning. • Yes/No Questions.
• Decision Making. Limited Context:
Detailed Explanations: • Brief Responses.
• Learning and Education. • Just-in-Time Information.
• Language Translation. Tasks Requiring High Efficiency:
Step-by-Step Instructions: • Real-time Applications.
• Technical Support. • Resource-Intensive Environments.
• Processes and Workflows. Repetitive or Predictable Responses:
Expanding Creativity: • Common Queries.
• Storytelling: • Standard Procedures.
• Brainstorming Ideas. Privacy and Security Sensitive Information:
Clarifying Ambiguities: • Sensitive Data Processing.
• Legal and Ethical Reasoning. • Anonymity and Confidentiality
• Philosophical Discussions.
lamp_review = """
Inferring Prompts Needed a nice lamp for my bedroom, and this one had \
additional storage and not too high of a price point. \
Got it fast. The string to our lamp broke during the \
transit and the company happily sent over a new one. \
Came within a few days as well. It was easy to put \
together. I had a missing part, so I contacted their \
support and they very quickly got me the missing piece! \
Lumina seems to me to be a great company that cares \
about their customers and products!!
"""
prompt = f"""
What is the sentiment of the following product review,
which is delimited with triple backticks?
Review: ```{prod_review}```
"""
response = get_completion(prompt)
print(response)
Thank You