feat: USDA FoodData Central per-ingredient nutrition lookup (v0.70.0)
estimateNutrition() (lib/ai/features/estimate-nutrition.ts) is now a
hybrid: for each ingredient, estimateGrams() (lib/ingredient-grams.ts)
converts its quantity+unit to grams where possible (weight units
exactly; volume units -- cup/tbsp/tsp/ml/l/etc -- via a 1ml~=1g water-
density approximation, documented as a real simplification but far
better than skipping them). Convertible ingredients get looked up in
USDA FoodData Central (lib/usda.ts, SR Legacy + Survey (FNDDS)
datasets -- the two suited to generic/raw ingredients, not Branded
packaged products or narrower Foundation) and summed. Whatever's left
(count-based units like "2 cloves", no USDA match, or USDA_API_KEY
unset entirely) goes through one AI call asking for just that
subset's total contribution, which sums directly with the USDA
totals -- no whole-recipe AI estimate to reconcile against.
Same exported signature and return shape as before
(NutritionEstimate / {perServing: {...}}), so every existing caller
(the nutrition route, meal-plan generation) gets more accurate
results automatically once USDA_API_KEY is set in Admin -> Settings,
with zero behavior change when it isn't.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -23,6 +23,14 @@ const SETTING_GROUPS = [
|
||||
"never touches code or repo settings, just creates issues.",
|
||||
keys: ["GITEA_URL", "GITEA_TOKEN", "GITEA_REPO"] as const,
|
||||
},
|
||||
{
|
||||
title: "USDA FoodData Central",
|
||||
description:
|
||||
"Improves recipe nutrition estimates with real per-ingredient data instead of AI-only guesses, " +
|
||||
"for ingredients whose quantity/unit can be converted to grams. Free — get a key at " +
|
||||
"fdc.nal.usda.gov/api-key-signup. Unset falls back to the previous AI-only estimation for every ingredient.",
|
||||
keys: ["USDA_API_KEY"] as const,
|
||||
},
|
||||
];
|
||||
|
||||
export default async function AdminSettingsPage() {
|
||||
|
||||
@@ -26,6 +26,7 @@ const ALLOWED_KEYS: SiteSettingKey[] = [
|
||||
"GITEA_TOKEN",
|
||||
"GITEA_REPO",
|
||||
"GITEA_WEBHOOK_SECRET",
|
||||
"USDA_API_KEY",
|
||||
];
|
||||
|
||||
export async function PUT(req: NextRequest) {
|
||||
|
||||
Reference in New Issue
Block a user