Using Openai To Search For Recipes
I recently got access to OpenAI’s beta, and I’ve been playing around with it. It’s not perfect, and at times I wondered if I was just reinventing the wheel with the stuff I was building. But it is really cool. I do think it’ll transform our jobs quite soon.
(Code available here)
As motivation, and to keep scope limited, I wanted to build a small search engine for recipes with natural language queries. I had a few thoughts going into this:
- Results on Google are terrible because recipes are notoriously SEO’d
- I didn’t want the AI to be generating its own recipe ideas. I wanted real, human recipes
- Navigating a specific recipe site is not always great either (see below)
Two of the top four results for “quick hearty meat” are vegetarian dishes
A lot of the AI examples we see recently are generative and “creative”. Like I said, I wanted to get real recommendations from the AI. I figured I had two options:
- I could finetune the model with example prompts like “I want to make a creamy tomato pasta” and give it specific examples
- I could use embeddings to basically build a document search index on top of the model
I chose 2 because building the embeddings was relatively straightforward. I figured I’d just limit my scope to seriouseats.com. I just scraped all the recipes on seriouseats.com, extracted the text, and embedded that.
It cost me like $15 to embed about 2,000 recipes.
After that, I was able to enter queries like “quick hearty meat” and get back results.
$ Enter a query: hearty meat dish quick https://www.seriouseats.com/how-to-make-vegetarian-tamale-pie https://www.seriouseats.com/flank-steak-with-bitter-greens-and-peaches-is-a-one-pan-wonder https://www.seriouseats.com/hearty-winter-vegetable-soup-vegan-recipe
Okay that’s not great either unfortunately.
But if I mention meat more then it starts to get better:
$ Enter a query: hearty meat meat meat https://www.seriouseats.com/grilling-planked-meatloaf https://www.seriouseats.com/hoisin-glazed-cocktail-meatballs https://www.seriouseats.com/quick-easy-ground-beef-recipe-ideas-tacos-tamale-pie-meatloaf
If I do the same on seriouseats.com, the results are still lackluster:
Other cool things I was able to use OpenAI for with this:
I was able to label every URL on seriouseats.com as a recipe or something else. If you search on seriouseats.com, you don’t just get recipes, but stuff like knife guides. I was able to finetune the model with several examples and it got very good at filtering out non-recipe links.
I was able to use OpenAI to parse ingredients from the HTML with a small prompt. While not particularly hard to do with normal scraping techniques, it’s stil annoying to extract, format, and segment the text nicely while dealing with nested divs and spans. It took much less time and effort to just prompt the AI. I was then able to send the ingredients over to a calorie counter app.
I was able to use the AI to recommend ingredient substitutions.
With this, you could imagine a fuzzy recipe finder/builder based on the ingredients you have at home.