DEV Community

Ashutosh Sarangi
Ashutosh Sarangi

Posted on

Prompt Engineering, Techniques

🔧 Prompting Techniques with Templates

1. Zero-shot Prompting

Use when: The task is simple and doesn’t need examples.

🧪 Template:

Summarize the following email in one sentence: 
[Insert email text here]
Enter fullscreen mode Exit fullscreen mode

🧠 Tip: Works best with clear, unambiguous instructions.


2. Few-shot Prompting

Use when: You want to guide the model with examples.

🧪 Template:

Convert the following sentences into passive voice:

Example 1:
Input: The cat chased the mouse.
Output: The mouse was chased by the cat.

Example 2:
Input: She writes a letter.
Output: A letter is written by her.

Now convert:
Input: They built a house.
Output:
Enter fullscreen mode Exit fullscreen mode

🧠 Tip: Keep examples close in structure to your actual input.


3. Chain-of-Thought (CoT) Prompting

Use when: The task involves reasoning or multiple steps.

🧪 Template:

Question: A bookstore sells books for $12 each. If you buy 3 books and pay with a $50 bill, how much change do you get? 
Think step by step.
Enter fullscreen mode Exit fullscreen mode

🧠 Tip: Add “Let’s think step by step” to encourage reasoning.


4. ReAct Prompting (Reasoning + Acting)

Use when: You want the model to reason and then take an action (e.g., search, call a tool).

🧪 Template (for agent-based systems):

Question: What is the capital of the country where the Eiffel Tower is located?

Thought: The Eiffel Tower is in Paris, which is in France. So the capital of France is Paris.

Action: Return "Paris"
Enter fullscreen mode Exit fullscreen mode

🧠 Tip: Combine with LangGraph or LangChain agents for tool use.


5. Role Prompting

Use when: You want the model to adopt a specific persona or tone.

🧪 Template:

You are a senior backend engineer mentoring a junior developer. Explain the concept of RESTful APIs in simple terms with examples.
Enter fullscreen mode Exit fullscreen mode

🧠 Tip: Great for tone control, teaching, or simulations.


6. Self-Consistency Prompting

Use when: You want more reliable answers from multiple generations.

🧪 Template:

Question: If a car travels 60 km in 1.5 hours, what is its average speed? Think step by step.
Enter fullscreen mode Exit fullscreen mode

🧠 Tip: Run multiple generations and pick the most consistent answer.


7. Instruction + Format-Constrained Prompting

Use when: You need structured output (e.g., JSON, YAML, Markdown).

🧪 Template:

Extract the following email into structured JSON with keys: sender, subject, summary.

Email:
---
From: John Doe <john@example.com>
Subject: Meeting Reminder
Body: Just a reminder that we have a meeting tomorrow at 10 AM.
---

Output:
{
  "sender": "John Doe",
  "subject": "Meeting Reminder",
  "summary": "Reminder about a meeting scheduled for tomorrow at 10 AM."
}
Enter fullscreen mode Exit fullscreen mode

🧠 Tip: Use this when integrating with APIs or downstream systems.


🧠 How to Avoid Hallucinations (with Prompting Tips)

Strategy Prompting Tip
✅ Be explicit “Only use facts from the following context:”
✅ Use RAG “Based on the retrieved documents, answer the question.”
✅ Ask for sources “Cite your sources for each fact.”
✅ Add verification “Now review the above and point out any factual errors.”
✅ Lower temperature Use temperature=0.2 for factual tasks
✅ Avoid overloading Keep prompts focused and relevant

Top comments (0)