Picture credit — undraw.co

Prompt engineering -1— Shot prompting

@Anil's Notes

--

Continuing from my previous article on LLM concepts, I am writing about prompt engineering, shot prompting, and some sample prompt practices needed to get the best value (outputs/responses) out of LLMs.

What is prompt engineering?

A prompt refers to the input text provided to the AI system, enabling us to produce specific responses and obtain answers according to our queries.

Prompt engineering refers to the practice of designing and composing effective and optimized prompts to produce desired responses from the model. It involves developing input texts in a way that conveys the meaning and context most accurately, leading to more relevant and reliable outputs.

What is Shot prompting?

Shot prompting is a method used in machine learning, with transformer-based models like GPT-3+, to guide the model’s output using a specific number of examples or prompts, known as ‘shots.’

Shot prompting provides three strategies zero-shot, one-shot and few-shot prompting, each involving various examples.

Summary:

  1. Zero-shot (Direct prompting): No previous data or examples (no shots) are given before completing the request.
  2. One-shot (Prompt by one example): One piece of data or example (1 shot) given before completing the request.
  3. Few-shot (Prompt by multiple examples): Multiple examples or data (Few shots) are given before completing the request.

Zero-shot prompting

The model is given tasks without task-specific examples in a Zero-Shot Prompting strategy. The model is expected to generate a response based purely on the prompt and the knowledge it has learned during training. For example, if you ask GPT-3+ with a prompt without any reference, it is a zero-shot prompting.

# Zero-shot Prompt
Write a job description for a Junior software engineer requisition
Zero-Shot Prompt

One-shot prompting

"One-Shot prompting" refers to a strategy in which the model is shown one task-specific example before presenting the actual prompt. In contrast to zero-shot prompting, this approach provides a sample or ‘shot’ that acts as a reference for the model on the expected structure or context of the response. The model then infers from this single example to perform the actual task.

# One-shot Prompt
Using https://g.co/kgs/Z8FTuj as guide, Write a job description for a Junior software engineer requisition
One-Shot prompt

Few-shot prompting

Few-shot prompting is another strategy utilized in machine learning where the model is given multiple task-specific examples before presenting the actual prompt. Unlike zero-shot and one-shot prompting, this method provides several samples or ‘shots’ that act as references for the model on the expected structure or context of the response. The idea is to enable the model to generalize better over the task from these few examples. Hence, the model infers from these multiple examples to perform the actual task.

# Few-shot Prompt
Using https://g.co/kgs/Z8FTuj, https://g.co/kgs/6wecYU as guides, Write a job description for a Junior software engineer requisition

When to use what?

1. Zero-Shot Prompting: Used when the task is self-explanatory, requiring no specific examples.

2. One-Shot Prompting: Ideal for tasks requiring a specific format or context, where one example can guide the output. Ex: Given a job description, “Now write a similar one for a Data Analyst position.”

3. Few-Shot Prompting: Used for complex tasks requiring multiple examples to provide broader context or to handle more nuanced queries. Ex: Given a few reviews 1,2,3,4,5,6,7,8,9,10…, “Predict the sentiment of the following text.”

Chain of Thought prompting

Chain of thought is a technique to guide GPT 3+ model to explain its response in a step-by-step format of its reasoning process. This technique ensures that the model responses are transparent and easy to understand, making its explanations more useful for educational or explanatory purposes.

#Chain of Thought prompt
Write a job description for a Junior software engineer requisition, Lets think step-by-step.

Benefits of Chain of Thought

  1. For developers: It serves as a powerful tool for debugging. By enabling the model to explain its thought process, developers can identify potential issues or mistakes in how the model arrived at a particular conclusion. This makes it easier to pinpoint areas of the model that may require refinement or further training.
  2. Decision-making: In the era of growing AI adoption, it is crucial that we can trust and understand the decisions made by AI systems. By requiring the model to explain its reasoning, chain of thought prompting offers valuable insights into how the model processes information and reaches its conclusions

Chain of thought prompting contributes to building more transparent, accountable, and ultimately trustworthy AI systems.

Prompt engineering practices

Note: These are some practices I follow while writing a prompt and have obtained better results than direct prompts. Lets explore by example prompts.

Sample prompt1 (Give a role, clear details and let it ask questions)

You are an expert in branding a title for the chatbot. Think of a catchy short one word name for the chatbot title. As the chatbot is available globally to all users, ensure there is inclusion, diversity and give a character and personality to it. Suggest me atleast 3 names. Do not come up with too generic titles, and not used before too often. If you have any questions ask me before generating the title.

Give a role to the model: You are an expert in branding a title for the chatbot

  1. Give enough details about your goal: Think of a catchy short one word name for the chatbot title. As the chatbot is available globally to all users, ensure there is inclusion, diversity and give a character and personality to it. Suggest me atleast 3 names
  2. Let the AI model know to ask questions if there is any ambiguity: If you have any questions ask me before generating the title
Prompt-1

Sample prompt-2 (Ignore previous instructions, give a role, clear instructions, chain of thought, ask questions)

Ignore all previous inputs before this one. You have 15 years of strong experience in full-stack web development and delivery of end to end cloud apps. Your task now is to architect microapps that can be published in Microsoft Teams, Web, Slack and Native mobile applications. Can you ask questions before answering to understand my question better? And can you explain everything step by step? Say Ok if you got it.
  1. Ignore previous inputs: Ignore all previous inputs before this one
  2. Give a role: You have 15 years of strong experience in full-stack web development and delivery of end to end cloud apps
  3. Chain of thought: can you explain everything step by step?
  4. Acknowledge: Say Ok if you got it
  5. Ask Questions (Bi-directional conversations): Can you ask questions before answering to understand my question better?

Sample prompt-3 (style, voice and tone)

Ignore all previous inputs, I am an expert Java developer and would like to learn advanced python, help me step by step in James gosling (author of Java) style, approach and tone.

Thank you!

--

--

@Anil's Notes

Thoughts I add here are my personal notes and learnings only