AI Recipe Generation

Introduction: AI-Powered Recipe Generation

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 TypeScript application using Express. 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 modern web development in TypeScript.

In this lesson, you will learn how to connect your Express backend to the AI, send ingredient 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.

Quick Recall: Prompts and LLM Manager

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 can include placeholders (like {{ingredients}}) that are filled in with real values before being sent to the AI.

  • LLM Manager:
    The LLM Manager 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 Express route.

How Recipe Generation Works in Express

Let’s walk through how your Express app generates a recipe using AI, step by step.

1. The API Route

Your app provides a special route for generating recipes:

import { Router } from "express";
import { z } from "zod";
import { generateResponse } from "../services/llm";
import { parseAiRecipe } from "../utils/parser";

export const recipes = Router();

recipes.post("/generate", async (req, res) => {
  const body = z.object({ ingredients: z.array(z.string()).min(1) }).safeParse(req.body);
  if (!body.success) return res.status(400).json({ error: "No ingredients provided" });
  const ingredientStr = body.data.ingredients.join(", ");

  // ... (rest of the code)
});
  • This route listens for POST requests at /generate.
  • It expects a JSON body with an ingredients array.
  • If no ingredients are provided, it returns an error.

Example request:

{
  "ingredients": ["chicken", "rice", "broccoli"]
}
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal