Using Few-Shot Prompts with Langchain and OpenAI API in Real-World Applications

Using Few-Shot Prompts with Langchain and OpenAI API in Real-World Applications

Refer for a deeper understanding of prompts and Langchain

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.

Read the entire article here

Click here to view the entire notebook

Other AI topics

View my portfolio here

Book a consultation call with me

要查看或添加评论,请登录

Sushma Rao的更多文章

  • Building a Text-to-Speech(TTS) Application Using OpenAI and LangChain

    Building a Text-to-Speech(TTS) Application Using OpenAI and LangChain

    Introduction Text-to-speech (TTS) technology has significantly evolved. It allows machines to generate human-like…

  • Speech to text(STT) using whisper and langchain

    Speech to text(STT) using whisper and langchain

    Here you learn how to convert Speech to Text using Whisper an OpenAI speech transcribing model. Code link: https://lnkd.

  • Demonstrate the use of conditionals in LangGraph

    Demonstrate the use of conditionals in LangGraph

    Use case: Symptoms and diagnosis Medical chatbot Document retrieval is presented to the doctor for further analysis…

    2 条评论
  • A simple agent using LangGraph with RAG context and web search

    A simple agent using LangGraph with RAG context and web search

    Aim: To create a study assistant that can help in the preparation, notes, cheat sheets, guides, and much more. Along…

    1 条评论
  • LangGraph connected to a RAG

    LangGraph connected to a RAG

    Go through the introduction to LangGraph if you do not know what LangGraph is! The LangGraph loops multiple times to…

  • An introduction to LangGraph

    An introduction to LangGraph

    What is LangGraph? LangGraph is a Python-based framework that enables developers to create sophisticated, multi-step…

  • Langchain Tools and Agents use cases with examples

    Langchain Tools and Agents use cases with examples

    These 2 articles will give you some context What is LangChain? Vector Database & Langchain? What is LangChain? A…

    3 条评论
  • Vector Databases and LangChain

    Vector Databases and LangChain

    A vector database stores and queries high-dimensional vectors, representing data points in a mathematical space. Unlike…

    5 条评论

社区洞察