Prompting with External Data
Learn techniques to incorporate user-provided or dynamically retrieved data directly into your prompts.
Prompting with External Data is a free AI Prompt Engineering lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Prompt Engineering learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro: External Data
Welcome! In this lesson, we'll learn how to make our LLM prompts smarter by including external data. This means feeding information that isn't part of the LLM's training, but is relevant to your specific task.
Think of it as giving the LLM 'fresh eyes' or 'contextual clues' for a particular situation.
Why External Data Matters
Incorporating external data offers several key benefits:
- Accuracy: Provides up-to-date or specific facts an LLM might not know.
- Personalization: Tailors responses to individual users or situations.
- Relevance: Ensures the LLM focuses on the exact information you care about.
- Reduced Hallucinations: Grounds the LLM in real data, making it less likely to make things up.
Common Data Sources
External data can come from many places. Here are a few common examples:
- User Input: Text typed by a user, preferences, or profile details.
- Databases/APIs: Product catalogs, customer records, weather data, news feeds.
- Files: Documents, spreadsheets, logs, or other stored information.
- Sensors: Real-time data from devices (e.g., temperature, location).
Basic Data Injection
The simplest way to include external data is by directly inserting it into your prompt string. This is often done using string formatting or concatenation.
Always clearly label the data you're injecting to help the LLM understand its role.
Try running this example:
user_name = "Alice"
product_name = "Smartwatch"
prompt = f"User: {user_name}\n" \
f"Product: {product_name}\n" \
f"Please write a short, friendly product review for the {product_name}."
print(prompt)Structuring External Data
For more complex data, it's crucial to provide it in a structured format. This helps the LLM parse and understand the information more reliably.
Common ways to structure data include:
- Delimiters: Using special characters (e.g.,
---,###) to separate sections. - Key-Value Pairs: Explicitly naming data fields (e.g.,
"Name": "Alice"). - JSON/XML: Using standard data interchange formats for complex objects.
The key is consistency and clarity.
Personalizing with Data
Let's see how we can personalize a message using structured user data. We'll use a simple key-value format within the prompt.
Notice how the LLM is given explicit 'User Profile' data to work with.
Try running this example:
user_profile = {
"name": "Bob",
"membership_level": "Gold",
"last_purchase_date": "2023-10-26"
}
prompt = f"User Profile:\n" \
f"Name: {user_profile['name']}\n" \
f"Membership: {user_profile['membership_level']}\n" \
f"Last Purchase: {user_profile['last_purchase_date']}\n\n" \
f"Write a personalized welcome back message for Bob, " \
f"mentioning his Gold status and recent activity."
print(prompt)Dynamic Data & Context
External data can also provide dynamic, "live" context. Imagine fetching current stock prices or weather data and feeding it directly into your prompt.
This makes the LLM's responses much more relevant and timely.
Try running this example:
current_stock_price = "$150.25"
company_name = "TechCo"
market_status = "Up 1.5% today"
prompt = f"Current Market Data:\n" \
f"Company: {company_name}\n" \
f"Price: {current_stock_price}\n" \
f"Status: {market_status}\n\n" \
f"Summarize TechCo's current market performance for an investor."
print(prompt)Data Validation & Safety
When incorporating external data, especially from user input, it's crucial to consider data validation and safety.
- Sanitize Input: Remove or escape potentially harmful characters to prevent prompt injection attacks.
- Validate Data Types: Ensure numbers are numbers, dates are dates, etc.
- Limit Size: Prevent overly long inputs from consuming too many tokens or causing errors.
- Privacy: Be mindful of sensitive information and avoid sending it to the LLM if not necessary.
Quick Check: External Data
You're building a prompt to generate a product description. You have a dictionary with product details: {'name': 'Eco-Friendly Water Bottle', 'material': 'Recycled Plastic', 'capacity_ml': 750}.
Which approach best incorporates this data into a prompt for clear LLM understanding?
Recap & Next Steps
Great job! You've learned how to integrate external data into your prompts to enhance LLM responses.
- External data provides accuracy, personalization, and relevance.
- It can come from user input, databases, APIs, and more.
- Clear structuring (like key-value pairs or JSON) is vital for complex data.
- Always consider data validation and safety when using external sources.
Next, we'll dive into Retrieval Augmented Generation (RAG), which builds upon these concepts to fetch and use even more extensive external knowledge!
Frequently asked questions
Is the “Prompting with External Data” lesson free?
Yes — the full text of “Prompting with External Data” is free to read here on the web, and the AI Prompt Engineering course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Prompt Engineering course, upgrade to CoddyKit PRO.
What will I learn in “Prompting with External Data”?
Learn techniques to incorporate user-provided or dynamically retrieved data directly into your prompts. You practise AI Prompt Engineering with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Prompt Engineering?
No prior experience is required. AI Prompt Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Prompting with External Data” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Prompt Engineering lesson?
Yes. Every AI Prompt Engineering lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Prompting with External Data
- Retrieval Augmented Generation (RAG)
- Vector Databases for Prompting