Prompt Engineering
Prompt Engineering
John F. Kennedy
JFK
Q: {question}
A: {correct_answer}
Although there are many tips and tricks for writing good
prompts, here I restrict the discussion to the ones that seem
the most fundamental (IMO) based on a handful of
references [1,3–5]. For a deeper dive, I recommend the
reader explore the sources cited here.
Without Trick
With Trick
Write me a birthday message for my dad no longer than 200 \
characters. This is a big birthday because he is turning 50. To celebrate, \
I booked us a boys' trip to Cancun. Be sure to include some cheeky humor,
he \
loves that.
Without Trick
Given the title of a Towards Data Science blog article, write a subtitle for
it.
With Trick
Given the title of a Towards Data Science blog article, write a subtitle for
it.
Title: A Practical Introduction to LLMs
Subtitle: 3 levels of using LLMs in practice
Without Trick
This trick was proposed by Wei et al. [7]. The basic idea is to
guide an LLM to think “step by step”. This helps break down
complex problems into manageable sub-problems, which
gives the LLM “time to think” [3,5]. Zhang et al. showed that
this could be as simple as including the text “Let’s think step
by step” in the prompt [8].
Without Trick
Write me a LinkedIn post based on the following Medium blog.
With Trick
Write me a LinkedIn post based on the step-by-step process and Medium blog \
given below.
Without Trick
Act as an NYC native and cabbie who knows everything about the city. \
Please make me a travel itinerary for a weekend in New York City based on \
your experience. Don't forget to include your charming NY accent in your \
response.
Without Trick
With Trick
Additionally, you can ask the LLM to refine not only its
responses but your prompts. This is a simple way to
automatically rewrite prompts so that they are easier for the
model to “understand”.
With Trick
Review your previous response, pinpoint areas for enhancement, and offer an \
improved version. Then explain your reasoning for how you improved the
response.
LangChain
Let’s see how to use LangChain for our automatic grader use
case. The example code is available on the GitHub Repo for
this article.
Imports
from sk import my_sk #importing secret key from another python file
**Q:** {question}
**A:** {correct_answer}
With our LLM and prompt, we can now define our chain.
# define chain
chain = LLMChain(llm=chat_model, prompt=prompt)
# define inputs
question = "Who was the 35th president of the United States of America?"
correct_answer = "John F. Kennedy"
student_answer = "FDR"
# run chain
chain.run({'question':question, 'correct_answer':correct_answer, \
'student_answer':student_answer})
While this chain can perform the grading task effectively, its
outputs may not be suitable for an automated process. For
instance, in the above code block, the LLM correctly said the
student’s answer of “FDR” was wrong, but it would be better
if the LLM gave us an output in a standard format that could
be used in downstream processing.
Output parser
# update chain
chain = LLMChain(
llm=chat_model,
prompt=prompt,
output_parser=GradeOutputParser()
)
Finally, we can run the chain for a whole list of student
answers and print the outputs.
# Output:
# John F. Kennedy - True
# JFK - True
# FDR - False
# John F. Kenedy - True
# John Kennedy - True
# Jack Kennedy - True
# Jacqueline Kennedy - False
# Robert F. Kenedy - False
Limitations
Prompt Engineering is more than asking ChatGPT for help
writing an email or learning about Quantum Computing. It is
a new programming paradigm that changes how
developers can build applications.
While this is a powerful innovation, it has its limitations. For
one, optimal prompting strategies are LLM-dependent. For
example, prompting GPT-3 to “think step-by-step” resulted in
significant performance gains on simple mathematical
reasoning tasks [8]. However, for the latest version of
ChatGPT, the same strategy doesn’t seem helpful (it already
thinks step-by-step).