Skip to main content

Command Palette

Search for a command to run...

Prompting in OpenAI SDK

Published
โ€ข7 min read

Prompting means giving instructions or inputs to the AI to get a desired output. Think of it like asking a question or giving a task to the AI.

1. Zero-shot Prompting

๐Ÿ”น Definition:

Zero-shot Prompting: The model is given a direct question or task without prior examples.

๐Ÿ”น Detailed Explanation:

In zero-shot prompting, you donโ€™t show the model how to solve the problem โ€” you just tell it what you want. The model relies on its pre-trained knowledge to understand your intent and generate a response.

โœ… Best for:

  • Simple questions

  • Language translation

  • Summarization

  • Quick data extraction


๐Ÿ”น Real-Time Example:

Task: Summarize the paragraph in one sentence.

Prompt:

"Summarize this: OpenAI has developed advanced AI models that can understand and generate human-like language, helping developers create smarter applications."

๐Ÿ”น Ready-to-Run Code:

import openai
openai.api_key = "your-api-key"

def zero_shot_prompt():
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "user", "content": "Summarize this: OpenAI has developed advanced AI models that can understand and generate human-like language, helping developers create smarter applications."}
        ]
    )
    print("Zero-shot Response:\n", response['choices'][0]['message']['content'])

zero_shot_prompt()

2. Few-shot Prompting

๐Ÿ”น Definition:

Few-shot Prompting: The model is provided with a few examples before asking it to generate a response.

๐Ÿ”น Detailed Explanation:

Here, you give 2โ€“5 examples in the prompt. This helps the model understand the format, style, or logic of what you expect. Itโ€™s like teaching by example.

โœ… Best for:

  • Custom formatting

  • Conversions (currency, date formats)

  • Custom Q&A or grammar correction


๐Ÿ”น Real-Time Example:

Task: Convert temperatures from Celsius to Fahrenheit.

Prompt:

yamlCopyEditConvert Celsius to Fahrenheit:
C: 0 โ†’ F: 32
C: 10 โ†’ F: 50
C: 25 โ†’ F:

๐Ÿ”น Code:

def few_shot_prompt():
    prompt = """Convert Celsius to Fahrenheit:
C: 0 โ†’ F: 32
C: 10 โ†’ F: 50
C: 25 โ†’ F:"""

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt}]
    )
    print("Few-shot Response:\n", response['choices'][0]['message']['content'])

few_shot_prompt()

3. Chain-of-Thought (CoT) Prompting

๐Ÿ”น Definition:

Chain-of-Thought Prompting: The model is encouraged to break down reasoning step by step before arriving at an answer.

๐Ÿ”น Detailed Explanation:

Instead of just giving an answer, the model explains its thinking process step by step. This helps in logical or mathematical problems and improves accuracy.

โœ… Best for:

  • Math problems

  • Logical reasoning

  • Code analysis


๐Ÿ”น Real-Time Example:

Task: Solve a math word problem step-by-step.

Prompt:

"If a train travels 60 km in 1.5 hours, what is its speed? Think step by step."

๐Ÿ”น Code:

def cot_prompt():
    prompt = "If a train travels 60 km in 1.5 hours, what is its speed? Think step by step."

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt}]
    )
    print("Chain-of-Thought Response:\n", response['choices'][0]['message']['content'])

cot_prompt()

4. Self-Consistency Prompting

๐Ÿ”น Definition:

Self-Consistency: The model generates multiple answers and selects the most consistent one (majority vote).

๐Ÿ”น Detailed Explanation:

Instead of taking the first answer, you sample multiple outputs (e.g., 5), and then choose the most common or logical one. This reduces randomness and improves reasoning reliability.

โœ… Best for:

  • Critical tasks

  • Logic-heavy answers

  • Higher confidence output


๐Ÿ”น Real-Time Example:

Same as CoT, but get 5 outputs and choose the best.

๐Ÿ”น Code:

def self_consistency_prompt():
    prompt = "If a train travels 60 km in 1.5 hours, what is its speed? Think step by step."
    results = []

    for _ in range(5):
        res = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            temperature=1.0,
            messages=[{"role": "user", "content": prompt}]
        )
        results.append(res['choices'][0]['message']['content'])

    print("Self-Consistency Responses:")
    for i, ans in enumerate(results, 1):
        print(f"\nAttempt {i}:\n{ans.strip()}")

self_consistency_prompt()

5. Instruction Prompting

๐Ÿ”น Definition:

Instruction Prompting: The model is explicitly instructed to follow a particular format or guideline.

๐Ÿ”น Detailed Explanation:

You give clear commands like โ€œWrite a summary in bullet pointsโ€ or โ€œReply only in JSONโ€. The model will follow those instructions closely.

โœ… Best for:

  • Structured outputs

  • APIs or automation

  • Custom formats


๐Ÿ”น Real-Time Example:

Task: Describe Apple Inc. in bullet points.

Prompt:

"List key facts about Apple Inc. in bullet points."

๐Ÿ”น Code:

def instruction_prompt():
    prompt = "List key facts about Apple Inc. in bullet points."

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt}]
    )
    print("Instruction Response:\n", response['choices'][0]['message']['content'])

instruction_prompt()

6. Direct Answer Prompting

๐Ÿ”น Definition:

Direct Answer Prompting: The model is asked to give a concise and direct response without explanation.

๐Ÿ”น Detailed Explanation:

Great for chatbots, APIs, or command-line tools where you just want the answer โ€” no fluff.

โœ… Best for:

  • Yes/No answers

  • API returns

  • Quick data


๐Ÿ”น Real-Time Example:

Prompt:

"What is the capital of Germany? Answer only."

๐Ÿ”น Code:

def direct_answer_prompt():
    prompt = "What is the capital of Germany? Answer only."

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt}]
    )
    print("Direct Answer:\n", response['choices'][0]['message']['content'])

direct_answer_prompt()

7. Persona-based Prompting

๐Ÿ”น Definition:

Persona-based Prompting: The model is instructed to respond as if it were a particular character or professional.

๐Ÿ”น Detailed Explanation:

You can make the model act like a doctor, teacher, poet, or even a movie character. It adjusts tone, vocabulary, and attitude accordingly.

โœ… Best for:

  • Character simulations

  • Creative writing

  • Expert-style responses


๐Ÿ”น Real-Time Example:

Prompt:

"You are a nutritionist. Suggest a healthy breakfast for weight loss."

๐Ÿ”น Code:

def persona_prompt():
    messages = [
        {"role": "system", "content": "You are a certified nutritionist."},
        {"role": "user", "content": "Suggest a healthy breakfast for weight loss."}
    ]

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages
    )
    print("Persona-based Response:\n", response['choices'][0]['message']['content'])

persona_prompt()

8. Role-Playing Prompting

๐Ÿ”น Definition:

Role-Playing Prompting: The model assumes a specific role and interacts accordingly.

๐Ÿ”น Detailed Explanation:

Itโ€™s similar to persona-based but more interactive. You can create simulations like โ€œYou are a job interviewerโ€ and conduct a mock interview.

โœ… Best for:

  • Training bots

  • Scenario simulation

  • Education & entertainment


๐Ÿ”น Real-Time Example:

Prompt:

"You are an interviewer. Ask me 3 questions for a software engineer position."

๐Ÿ”น Code:

def role_playing_prompt():
    messages = [
        {"role": "system", "content": "You are a job interviewer."},
        {"role": "user", "content": "Ask me 3 questions for a software engineer position."}
    ]

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages
    )
    print("Role-Playing Response:\n", response['choices'][0]['message']['content'])

role_playing_prompt()

9. Contextual Prompting

๐Ÿ”น Definition:

Contextual Prompting: The prompt includes background information to improve response quality.

๐Ÿ”น Detailed Explanation:

You provide extra context (past conversation, company details, etc.) so the model gives relevant, coherent answers.

โœ… Best for:

  • Long conversations

  • Business apps

  • Chatbots


๐Ÿ”น Real-Time Example:

Prompt:

"Company: EcoTech. Product: Solar Chargers. Task: Write a one-line pitch for investors."

๐Ÿ”น Code:

def contextual_prompt():
    prompt = "Company: EcoTech. Product: Solar Chargers. Task: Write a one-line pitch for investors."

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt}]
    )
    print("Contextual Response:\n", response['choices'][0]['message']['content'])

contextual_prompt()

10. Multimodal Prompting

๐Ÿ”น Definition:

Multimodal Prompting: The model is given a combination of text, images, or other modalities to generate a response.

๐Ÿ”น Detailed Explanation:

Works with GPT-4-Vision. You can upload an image + add a prompt like โ€œDescribe this pictureโ€ or โ€œWhatโ€™s in this chart?โ€

โœ… Best for:

  • Image captions

  • Visual understanding

  • Diagrams, screenshots


๐Ÿ”น Real-Time Example (text + image description):

โš ๏ธ This requires an image and GPT-4 Vision model.

import base64

def multimodal_prompt():
    # Load image and encode it to base64
    with open("solar_panel.jpg", "rb") as image_file:
        base64_image = base64.b64encode(image_file.read()).decode("utf-8")

    response = openai.ChatCompletion.create(
        model="gpt-4-vision-preview",
        messages=[
            {
                "role": "user",
                "content": [
                    {"type": "text", "text": "Describe the image in detail."},
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": f"data:image/jpeg;base64,{base64_image}"
                        }
                    }
                ]
            }
        ]
    )

    print("Multimodal Response:\n", response['choices'][0]['message']['content'])

# multimodal_prompt()  # Uncomment after setting up GPT-4-Vision access and image

โœ… Summary Table

TypeUse CaseStrength
Zero-shotDirect questionFast & simple
Few-shotFormat-specific answersLearn by examples
CoTReasoning/mathsStep-by-step thinking
Self-ConsistencyComplex logicMore accurate
InstructionFormat controlPrecise output
Direct AnswerShort repliesChatbots, APIs
Persona-basedExpert adviceCustom tone
Role-PlayingSimulationsFun & realistic
ContextualBusiness/chatbotsDeep understanding
MultimodalImages + textVisual tasks