|
235 | 235 | "429": { "$ref": "#/components/responses/TooManyRequests" } |
236 | 236 | } |
237 | 237 | } |
| 238 | + }, |
| 239 | + "/v2/ai-natural-language-search": { |
| 240 | + "post": { |
| 241 | + "summary": "Premium AI-Powered Natural Language Search", |
| 242 | + "description": "Parses complex meal descriptions using Cloudflare Workers AI (Meta Llama 2 7B) to produce structured food items, quantity estimates, and confidence scores. **Requires a Pro-tier API key.** The endpoint caches responses and returns USDA search suggestions alongside the AI interpretation.", |
| 243 | + "operationId": "ai_natural_language_search", |
| 244 | + "security": [{ "ApiKeyAuth": [] }], |
| 245 | + "tags": ["Food", "Premium"], |
| 246 | + "requestBody": { |
| 247 | + "required": true, |
| 248 | + "content": { |
| 249 | + "application/json": { |
| 250 | + "schema": { |
| 251 | + "type": "object", |
| 252 | + "required": ["text"], |
| 253 | + "properties": { |
| 254 | + "text": { |
| 255 | + "type": "string", |
| 256 | + "description": "Natural language meal description (500 characters max).", |
| 257 | + "example": "A grilled chicken breast with 2 cups of steamed broccoli" |
| 258 | + }, |
| 259 | + "maxResults": { |
| 260 | + "type": "integer", |
| 261 | + "minimum": 1, |
| 262 | + "maximum": 25, |
| 263 | + "default": 5, |
| 264 | + "description": "Maximum USDA search results to return per parsed food item." |
| 265 | + }, |
| 266 | + "confidence": { |
| 267 | + "type": "number", |
| 268 | + "minimum": 0, |
| 269 | + "maximum": 1, |
| 270 | + "default": 0.8, |
| 271 | + "description": "Minimum string-similarity confidence required for USDA results." |
| 272 | + }, |
| 273 | + "filterForSuggestions": { |
| 274 | + "type": "boolean", |
| 275 | + "default": false, |
| 276 | + "description": "When true, results are filtered to only suggestions above the confidence threshold." |
| 277 | + } |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + }, |
| 283 | + "responses": { |
| 284 | + "200": { |
| 285 | + "description": "Successful AI parsing and USDA lookup.", |
| 286 | + "headers": { |
| 287 | + "X-Cache-Status": { |
| 288 | + "schema": { "type": "string" }, |
| 289 | + "description": "Cache status for the AI response (MISS, HIT, or STALE)." |
| 290 | + }, |
| 291 | + "X-RateLimit-Limit": { |
| 292 | + "schema": { "type": "integer" }, |
| 293 | + "description": "Maximum requests allowed in the current window." |
| 294 | + }, |
| 295 | + "X-RateLimit-Remaining": { |
| 296 | + "schema": { "type": "integer" }, |
| 297 | + "description": "Remaining requests in the current window." |
| 298 | + }, |
| 299 | + "X-RateLimit-Reset": { |
| 300 | + "schema": { "type": "integer" }, |
| 301 | + "description": "Seconds until the current window resets." |
| 302 | + } |
| 303 | + }, |
| 304 | + "content": { |
| 305 | + "application/json": { |
| 306 | + "schema": { |
| 307 | + "$ref": "#/components/schemas/AiNaturalLanguageSearchResponse" |
| 308 | + } |
| 309 | + } |
| 310 | + } |
| 311 | + }, |
| 312 | + "400": { "$ref": "#/components/responses/BadRequest" }, |
| 313 | + "401": { "$ref": "#/components/responses/Unauthorized" }, |
| 314 | + "403": { |
| 315 | + "description": "Forbidden – API key tier is not authorized for premium AI endpoints.", |
| 316 | + "content": { |
| 317 | + "application/json": { |
| 318 | + "schema": { |
| 319 | + "$ref": "#/components/schemas/ErrorResponse" |
| 320 | + } |
| 321 | + } |
| 322 | + } |
| 323 | + }, |
| 324 | + "429": { "$ref": "#/components/responses/TooManyRequests" } |
| 325 | + } |
| 326 | + } |
238 | 327 | } |
239 | 328 | }, |
240 | 329 | "components": { |
|
406 | 495 | } |
407 | 496 | } |
408 | 497 | }, |
| 498 | + "ParsedAiFoodItem": { |
| 499 | + "type": "object", |
| 500 | + "properties": { |
| 501 | + "quantity": { "type": "number" }, |
| 502 | + "unit": { "type": "string", "nullable": true }, |
| 503 | + "foodName": { "type": "string" }, |
| 504 | + "quantityInGrams": { "type": "number", "nullable": true }, |
| 505 | + "modifiers": { |
| 506 | + "type": "array", |
| 507 | + "items": { "type": "string" }, |
| 508 | + "description": "Preparation or context modifiers detected by Workers AI." |
| 509 | + }, |
| 510 | + "category": { "type": "string", "nullable": true }, |
| 511 | + "combinedWith": { "type": "string", "nullable": true } |
| 512 | + } |
| 513 | + }, |
| 514 | + "AiNaturalLanguageSearchResponse": { |
| 515 | + "type": "object", |
| 516 | + "properties": { |
| 517 | + "success": { "type": "boolean" }, |
| 518 | + "data": { |
| 519 | + "type": "object", |
| 520 | + "properties": { |
| 521 | + "query": { "type": "string" }, |
| 522 | + "searchResults": { |
| 523 | + "type": "array", |
| 524 | + "description": "Filtered USDA search results enriched with confidence scores.", |
| 525 | + "items": { |
| 526 | + "type": "object", |
| 527 | + "additionalProperties": true |
| 528 | + } |
| 529 | + }, |
| 530 | + "totalResults": { "type": "integer" }, |
| 531 | + "foodNameConfidence": { |
| 532 | + "type": "number", |
| 533 | + "description": "Average confidence score across parsed items." |
| 534 | + }, |
| 535 | + "parsedItems": { |
| 536 | + "type": "array", |
| 537 | + "items": { "$ref": "#/components/schemas/ParsedAiFoodItem" } |
| 538 | + } |
| 539 | + } |
| 540 | + }, |
| 541 | + "meta": { |
| 542 | + "type": "object", |
| 543 | + "properties": { |
| 544 | + "requestId": { "type": "string" }, |
| 545 | + "cacheStatus": { "type": "string" }, |
| 546 | + "model": { "type": "string", "example": "@cf/meta/llama-2-7b-chat-int8" } |
| 547 | + } |
| 548 | + } |
| 549 | + } |
| 550 | + }, |
409 | 551 | "ParsedFoodItem": { |
410 | 552 | "type": "object", |
411 | 553 | "properties": { |
|
0 commit comments