·AI & ChatGPT

Module 4 Lesson 3: Conditional Prompts

Implementing logic and branching in your prompts. Teach the AI to handle different scenarios automatically.

Conditional Prompts

A conditional prompt tells the AI to take different actions based on the input it receives. This allows you to create "Self-Operating" instructions that handle multiple scenarios.

1. The IF/THEN Structure

Even though it's a language model, you can use programming logic.

Example:

*"Analyze this customer support ticket.

  • IF the sentiment is angry, generate a refund template.
  • IF the sentiment is a question about a feature, point them to the documentation.
  • IF the sentiment is happy, ask for a review."*

2. Using "Switch" Patterns

For more than two options, use a structured list of triggers.

graph TD
    Input[User Message] --> Check{What is the Topic?}
    Check -->|Coding| ResultA[Write Python Code]
    Check -->|Business| ResultB[Write a Memo]
    Check -->|Other| ResultC[Ask for Clarification]

3. Negative Constraints (The "Else")

It is often more important to tell the AI what to do when it doesn't find what it's looking for.

Example Prompt:

"Extract the phone number from this text. If no phone number is present, simply output 'NONE'. Do not say 'I couldn't find a phone number'."

4. Complexity and Rulesets

You can build a whole "Logic Board" for the AI to follow.

"Rule 1: If the input is in Spanish, reply in Spanish. Rule 2: If the input contains a date, add it to a 'Calendar' section. Rule 3: Otherwise, ignore Rule 2."


Hands-on: The Logic Trigger

Create a prompt that processes a "Grocery List."

  • If the item is a fruit, categorize it under 'Produce'.
  • If the item is milk or cheese, categorize it under 'Dairy'.
  • If it's anything else, categorize it under 'General'.

Try it with a list like: "Apples, Milk, Bread, Cheese, Bananas, Toothpaste."

Key Takeaways

  • Logic makes your prompts "Smart."
  • Use If/Then specifically for different sentiments or data types.
  • Always provide an Empty Case (what to do if nothing is found).

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn