Notion Recipe Generator
Create a dynamic recipe generator using Tune Studio and Notion.
This guide will show you how to create a CLI tool to generate recipes and save them in Notion. The user will enter a list of ingredients via the CLI, and the application will pass them to the LLM to generate a full recipe and save it in Notion with a title, cover image, description, list of ingredients, and method.
We will build the app using the following:
- Tune Studio with the Mixtral model
- Notion to save the recipes
- Serper image search API to generate cover images
- Fire to generate the CLI
You can find the complete application here.
Demonstration
Here is an example of a user asking for a recipe using ingredients they have available by running the following command:
The recipe will be created on Notion as follows:
Here is a video demonstration of the process:
https://github.com/AsavariD/cookbook/assets/69451908/366359fe-0a57-4041-a6a2-2d8311e08e4e
Getting started
You need to install some Python dependencies and manage them using a virtual environment.
In your project folder, run the following command to create a new virtual environment:
Activate the virtual environment with the following command:
In your project folder, create a requirements.txt
file and paste the following dependencies into it:
Install the dependencies by running the following command:
Adding environment variables
The application integrates with Tune Studio, Notion, and Serper API. We’ll use environment variables to manage the keys for these services.
In your project folder, create a new .env
file and add the following to it:
Find your Tune Studio API key by selecting View API keys from the profile dropdown at the top right of the Tune Studio dashboard or on your profile page.
If you don’t already have a Notion account, sign up here. You’ll need to create a Creator Profile and create a new internal integration. Find your Internal Integration Secret on the integration configuration page by clicking Configure integration settings on the integration creation success dialog. Create a new page in Notion and note down the page’s id. Connect the page to the integration you created by clicking on the three dot menu on the top-right. Click Connect to and search for your integration.
Sign up for a free Serper account here, and follow the instructions to complete your account registration. Click API key in the sidebar to find your API key.
Calling the LLM
We’ll start by writing a basic function to query the LLM and get a response from it. We can then pass this function to the various queries to generate all the parts of the recipe.
In your project folder, create a new main.py
file and add the following code to it:
Here we import the necessary modules and initialize the environment variables we configured earlier.
Let’s add some configurations for Notion and Tune Studio next:
This configuration code sets the headers for queries to the Notion API.
Now add the following function to the main.py
file:
This function receives a list of messages and queries the Tune Studio API for a response. We will use this function to generate the recipes by calling it with different prompts and supplying a model to use.
Defining helper functions
To generate the recipes, we need to query the model multiple times for different roles. We will write some helper functions to format the system and user messages for each query.
Add the following code to the main.py
file:
These are the functions we define here:
gen_recipe
, which receives a recipe title and ingredients, sets the system role to a recipe generator, queries the LLM to generate a recipe according to the provided ingredients, then returns the response from the LLM.gen_recipe_title
, which receives a list of ingredients, sets the system role to a recipe title generator, and queries the LLM for a simple recipe title incorporating those ingredients.gen_num_ingredients
, which receives a list of ingredients and queries the LLM to count the provided ingredients and return the count.gen_ingredient_list
, which receives the user input and queries the LLM to format the ingredients mentioned into a list.gen_description
, which receives the ingredients, the recipe, and the recipe title, sets the system role, and queries the model to generate a description for the provided recipe.get_cover_image
, which receives the recipe title, configures the Serper search headers, searches for images matching the title, and then returns the results.get_emoji
, which receives the recipe title, then provides the LLM with a list of emojis and tells it to pick an emoji according to the provided title.
All these functions use the call_llm
function we defined earlier to process the calls to the Tune Studio API. We can now use these helper functions to generate complete recipes in Notion.
Generating recipes in Notion
Now we’ll write the main function, which receives the user input, generates the recipe using the helper functions, and saves it to Notion.
Add the following code to the main.py
file:
Here’s what this function does:
- It receives the user’s query, a Notion page ID, and the model name.
- It calls the helper functions to count and list the ingredients, and generates a title, a recipe, and a description.
- It does some basic reformatting of the recipe to get rid of newlines or blank spaces in the LLM output.
- Next, it puts together a
data
dictionary containing the information needed to make a new page in the provided Notion page and usesrequests.post
to post this information to the Notion API. - Then it redefines the
data
dictionary to contain the information to be added to the recipe page, adds the recipe description and the headers for the ingredient list, loops through the ingredients list previously generated, and adds the ingredients as bullet points. - It loops through the
cleaned_steps
containing the cleaned recipe steps we defined earlier and appends the steps to thedata
dictionary. - Finally, it sends a
PATCH
request to the Notion API to update the recipe page with the collected information.
Running the application
We’ll use Fire to create a CLI for the application.
Add the following code to the main.py
file:
This code sets up a CLI we can use to interact with the script. You can test it out by running a command like the following:
You can now view the generated recipe in Notion.