Using Few-Shot Prompts with Langchain and OpenAI API in Real-World Applications
Sushma Rao
Expert Vetted freelancer on Upwork(Top 1%) | Backend & GenAI | Langchain Langgraph LLM| AI ML development/Automation | Algorithms expert| Cloud development I help clients get more business through software development
A python code sample to create Multiple choice questions for kids based on the level of difficulty set by the user via prompt template examples.
Setup the openAI API key
Look into the tutorials here for more details.
import os
import openai
api_key = os.getenv("OPENAI_API_KEY")
What is a few-shot prompt?
Few-shot prompting is a technique where you give the model contextual information about the requested tasks. In this technique, you provide examples of both the task and the output you want. Providing this context, or a?few shots, in the prompt conditions the model is to follow the task guidance closely.
Langchain and open AI integration using a few-shot prompt template
from langchain import PromptTemplate, FewShotPromptTemplate
# First, create the list of few shot examples.
examples = [
{"multipleChoiceQuestionContext": "Generate a Math question for a 7 year old", "level":"easy","aianswer": "What is 2+3? A.5 B.6 C.8 D.100 answer: 5"},
{"multipleChoiceQuestionContext": "Generate a Math question for a 7 year old", "level":"medium","aianswer":"What is 25+88? A.113 B.6 C.8 D.100 answer: 113"},
{"multipleChoiceQuestionContext": "Generate a Math question for a 8 year old", "level":"easy","aianswer":"What is 26+883? A.5 B.6 C.909 D.100 answer: 909"},
{"multipleChoiceQuestion": "Generate a Math question for a 8 year old", "level":"hard","aianswer":"What is 1014+883? A.1014 B.884 C.1896 D.1897 answer: 1897"}
]
# Next, specify the template to format the examples we have provided.
example_formatter_template = """MultipleChoiceQuestionContext: {multipleChoiceQuestionContext}
level: {level}
AIAnswer: {aianswer}
"""
example_mcq_prompt = PromptTemplate(
input_variables=["multipleChoiceQuestionContext","level"],
template=example_formatter_template,
)
A few shot prompt template looks like this:
few_shot_mcq_prompt = FewShotPromptTemplate(
# These are the examples we want to insert into the prompt.
examples=examples,
# This is how we want to format the examples when we insert them into the prompt.
example_prompt=example_mcq_prompt,
# The prefix is some text that goes before the examples in the prompt.
# Usually, this consists of intructions.
prefix="Give the MCQ with options and answer of every input based on their level\n",
# The suffix is some text that goes after the examples in the prompt.
# Usually, this is where the user input will go
suffix="MultipleChoiceQuestion: {input} Level: {level} \Answer: ",
# The input variables are the variables that the overall prompt expects.
input_variables=["input","level"],
# The example_separator is the string we will use to join the prefix, examples, and suffix together with.
example_separator="\n",
)
Print out how the input is sent to the model.
print(few_shot_mcq_prompt.format(input="Generate a Math question for a 7 year old",level="medium"))
Output:
The output shows how the prompt is formatted as per the user input.
Give the MCQ with options and answer of every input based on their level
MultipleChoiceQuestionContext: Generate a Math question for a 7 year old
level: easy
AIAnswer: What is 2+3? A.5 B.6 C.8 D.100 answer: 5
MultipleChoiceQuestionContext: Generate a Math question for a 7 year old
level: medium
AIAnswer: What is 25+88? A.113 B.6 C.8 D.100 answer: 113
MultipleChoiceQuestionContext: Generate a Math question for a 8 year old
level: easy
AIAnswer: What is 26+883? A.5 B.6 C.909 D.100 answer: 909
MultipleChoiceQuestionContext: Generate a Math question for a 8 year old
level: hard
AIAnswer: What is 1014+883? A.1014 B.884 C.1896 D.1897 answer: 1897
MultipleChoiceQuestionContext: Generate a Math question for a 7 year old Level: medium \AIAnswer:
Now, I call the llm with the few_shot_mcq_prompt.
llm(few_shot_mcq_prompt.format(input="Generate a Math question for a 7 year old",level="medium"))
Output:
'Here is the MCQ with options and answer:\n\n**Level: medium**\nWhat is 43+17?\nA.50\nB.60\nC.70\nD.80\n\n**Answer:** B.60'
Using these separators one can split the string and store them in a CSV file in a required format. Looping this llm query inside a while or a for loop to run it a certain number of times will give you a list of questions as you need them.
I thank Krish Naik for his YouTube video on prompting with Langchain.