Inspiration
There are many AI-powered chatbots for generating recipes, but I haven't been able to find one that gives nutrient data for the ingredients from the USDA nutrient database https://fdc.nal.usda.gov. I have 2 young kids and when my wife and I get new recipes, we'd like to know the nutritional value from a reliable source like the USDA. So, for this Ingenium STEM weekend hack I decided to build a chatbot my family can use that integrates OpenAI with the USDA Food Central API to better understand how healthy are new recipes for our family.
What it does
This is an AI-powered chatbot chef that takes ingredients you give it for a meal you'd like to make, and whips up a recipe for you, with nutrient values of the final ingredients from the USDA Food Central API.
How I built it
I use OpenAI GPT 4o as the large language model to generate recipes from ingredients provided by the user in the Gradio app. I also use OpenAI to extract the ingredients from the generated recipes and pass those ingredients to the USDA Food Central API to retrieve their nutrient values. Finally I use OpenAI again to format the nutrient data results.
Challenges I ran into
The USDA Food Central API is not well-documented, so I had to experiment to figure out which data types to specify to get ingredients for some foods. For example, if I specify data type "Foundation" for the food "tofu", the API returns no matches, but if I specify data type "SR Legacy", then it finds "tofu" and returns the nutrient values.
Also, I found that many different recipes shared common ingredients, so calling the API again and again for the same ingredient is wasting bandwidth and increasing the chance of exceeding the API's rate limit. So, I added caching of the nutrient results for each food to avoid hitting the API unnecessarily. I then noticed the cache could use a lot of memory due to recipes having so many ingredients and the API returning so many nutrients for each ingredient. To minimize the memory usage, I implemented compression of the nutrient data in the cache.
The API returns many nutrients for a given food, so to avoid overwhelming the user with so much data, I sort the nutrients returned by weight (having to convert from different units like grams and milligrams), and take the top 10 by weight to give to the user. For the food matches as well, the API can return multiple matches so I picked the best match based on the score provided by the API for each match.
Accomplishments that I'm proud of
I'm proud I was able to figure out the USDA Food Central API in such a short time and how to pull a manageable size of data out of it because the nutrient data is very detailed and granular with no built-in filtering. I didn't expect a food nutrient API would be so challenging to use.
What I learned
I learned how to design a wireframe using Balsamiq and implement the design in Gradio using Gradio Blocks and to keep session state in Gradio. I also learned how to engineer prompts for OpenAI. My chatbot uses 3 prompts: (1) generate a recipe from ingredients, (2) extract final ingredients from the generated recipe, (3) reformat a Python data structure of USDA nutrient data to a user-friendly output.
What's next for Ingenium Nutrium
I'd like to dive into the nutrient data returned by the USDA Food Central API more to better understand how to derive more useful results for the user like my wife and me in preparing meals for our family. I'd also like to experiment with other large language models to compare usage costs.
Built With
- gradio
- langchain
- openai
- python
- usda-foodcentral-api

Log in or sign up for Devpost to join the conversation.