Welcome to this lesson on generating new recipes with AI! So far, you have learned how to interact with large language models (LLMs), structure prompts, and manage LLM calls in your application. Now, you will see how these skills come together to create a feature that generates unique cooking recipes based on a list of ingredients provided by the user.
Imagine you have a few ingredients in your fridge and want to know what you can cook. With AI-powered recipe generation, your app can suggest creative, step-by-step recipes instantly. This not only makes your app more helpful but also demonstrates the power of combining AI with backend development.
In this lesson, you will learn how to connect your backend logic to the AI, send ingredients lists, and return structured recipes to your users. By the end, you will understand the full flow of generating a recipe with AI and preparing it for use in your application.
Before we dive in, let’s briefly recall two important concepts from previous lessons:
-
Prompt Templates: You learned how to use template files to create prompts for the AI. These templates include placeholders (like
{{ingredients}}) that are filled in with real values before being sent to the AI. -
LLM Manager: The LLM Manager (typically a function like
generate_response) is a helper that handles all communication with the language model. It loads the right prompts, fills in variables, sends the request, and returns the AI’s response.
These tools are the foundation for generating recipes with AI. In this lesson, you will see how they are used together in a real API view.
Let’s walk through how your app generates a recipe using AI, step by step.
Your app provides a specific function to handle the recipe generation request. Since this function receives data from the user, it is set up to handle POST requests.
- Decorators:
@require_http_methods(["POST"])ensures the endpoint only acceptsPOSTrequests, and@csrf_exemptallows the request to pass without a CSRF token (common for API endpoints). - Body Parsing: We use a helper function
_parse_json_bodyto manually extract the JSON data from therequest. - Validation: If the body is malformed or
ingredientsare missing, we return a structured error using_json_error.
Example request body:
The view uses two prompt templates: a system prompt and a user prompt. These are loaded and filled with the user’s ingredients using our LLM Manager.
generate_responseloads the template files and fills in the{{ingredients}}variable.- The system prompt defines the AI’s persona (a creative cooking assistant) and specifies the output format.
- The user prompt provides the actual list of
ingredients.
The AI returns a recipe as a formatted string. It follows the structure we requested in the system prompt:
After receiving the AI’s response, your app needs to extract the recipe name, ingredients, and steps so they can be returned as structured data (JSON).
The response is a single block of text. To process it, we split it into individual lines:
We iterate through the lines and check for keywords to determine which section of the recipe we are currently reading:
- Name: When a line starts with
name:, we grab the text following the colon. - Section Switching: When we see
ingredients:orsteps:, we update thesectionvariable so the code knows how to handle the following lines. - Steps logic: We look at the first two characters of a line, remove dots or parentheses, and check if what remains is a number. This identifies lines like
1. Step one.
Finally, the view returns the data using JsonResponse:
Example output:
Here is the complete implementation of the generate_recipe view. This function handles the request, interacts with the LLM, parses the response, and manages potential errors.
In this lesson, you learned how your app generates new recipes using AI. You saw how the app:
- Receives a list of
ingredientsthrough aPOSTrequest. - Uses
_parse_json_bodyto handle the incoming data. - Prepares and sends dynamic prompts to the AI using the
generate_responsehelper. - Parses a raw string
responseinto a structured format by iterating throughlinesand detecting sections. - Returns the final recipe as a structured
JsonResponseobject.
You are now ready to practice building and testing this feature yourself. In the upcoming exercises, you will implement this logic, experiment with different ingredients lists, and ensure the AI's output is correctly transformed for your users.
