Welcome back! In the previous lesson, you learned how to use AI to generate new recipes from a list of ingredients. Now, let’s take the next step: extracting recipes from real-world web pages.
Many cooking websites have great recipes, but the information is often buried in messy HTML code. Our goal is to build a script that can take a raw HTML file, use AI to extract a clean recipe, and then store that recipe in our database. This process is a key part of making our AI Cooking Helper smarter and more useful.
By the end of this lesson, you’ll understand how to automate recipe extraction from HTML using prompt templates, LLM calls, and your existing database setup.
Before we dive in, let’s briefly remind ourselves how we previously generated recipes with AI.
In the last lesson, you learned how to:
- Use
prompt templatesto ask theAIfor a recipe based on a list of ingredients. - Send these prompts to the
AIand receive a structured recipe in response. - Parse the
AI’s response and use it in your application.
This time, instead of generating a recipe from scratch, we’ll use the AI to extract a recipe from a messy HTML page. The process is similar, but the input and prompts are a bit different.
Let’s look at the big picture before we break things down.
The script you’ll be working with is called extract_and_store_recipe.py. Its job is to:
- Read a raw
HTMLfile from your computer. - Use
AIto extract a clean recipe from thatHTML. - Parse the
AI’s response into structured data (name,ingredients,steps). - Store the recipe in your database using the ORM, making sure not to add duplicates.
Here’s a simple diagram of the flow:
This script brings together everything you’ve learned so far: prompt templates, LLM calls, and database operations.
The first step is to get the AI to read the HTML and return a clean recipe. We do this by sending it a carefully crafted prompt.
We use two prompt templates:
- A system prompt (
recipe_extract_system) that tells theAIit is an expert at structured information extraction. - A user prompt (
recipe_extract_user) that provides the rawHTMLcontent.
Here’s how the script loads and fills in these templates using our LLM manager:
generate_responseis our centralized function that loads the templates and replaces the{{html}}variable with the actual website content.- The
AIis instructed to return the data in a specific text-based format so we can easily parse it later.
System prompt:
User prompt:
Once the AI returns its response, we need to turn that text into structured data. The script uses a function called parse_recipe_string to break the text into parts.
The logic iterates through the lines of the response, looking for specific keywords to identify which section of the recipe it is currently reading.
- It uses the
prefixlogic to handle different ways theAImight number the steps (like1.or1)). - This results in a clean dictionary containing the
recipename, a list ofingredientstrings, and a list ofstepstrings.
After parsing, the final step is to save this data into our database. We use the Object-Relational Mapper (ORM) to handle the database tables and relationships.
To ensure the data is saved correctly, we use transaction.atomic(). This ensures that if something goes wrong (like a crash while saving ingredients), none of the partial data is saved, keeping our database clean.
Here is how the database logic works:
- Duplicate Prevention: We use
Recipe.objects.filter(name__iexact=name)to check if the recipe is already in our database before trying to save it. - Atomic Transactions: The
with transaction.atomic():block wraps all the saving operations. If any part fails, the whole block is rolled back. - Automatic Creation:
Ingredient.objects.get_or_create()is a powerful method that looks for an existing ingredient byname. If it finds one, it uses it; otherwise, it creates a new record automatically. - Normalization: We convert ingredient names to lowercase using
_normalizeso thatFlour,flour, andFLOURare all treated as the same ingredient.
In this lesson, you learned how to extract a recipe from a messy HTML page using AI and store it in your database. You saw how the script:
- Reads an
HTMLfile and sends it to theAIusing structuredprompt templates. - Uses
generate_responsefrom ourLLM utilityto manage the interaction. - Parses the
AI’s text response into a structured data format, cleaning up numbering andprefixes. - Safely saves the data using atomic transactions and
ORMmethods likeget_or_createto prevent duplicates and ensure data integrity.
Next, you’ll get hands-on practice running this script yourself. You’ll see how it handles real-world website content and learn how to verify that the recipes are correctly stored in your database!
