Skip to content

14.12 Search & AI Integration Architecture ​

1. Plain English Overview ​

Search and AI architecture manages how users discover content (lexical text indexing vs semantic vector similarity) and how generative AI models are prompted, streamed, cached, and guarded to deliver accurate answers with minimal cost and latency.


2. Usage in Cooking Nexus ​

  • AI Streaming Gateway: Google's Gemini 3 Flash model via Lovable AI Gateway with Server-Sent Events ([chef-ai/index.ts](file:///Users/avishjain/avishjain9-repos/26-cooking-nexus/27-kitchen-companion-pro/supabase/functions/chef-ai/index.ts), [ai-stream.ts](file:///Users/avishjain/avishjain9-repos/26-cooking-nexus/27-kitchen-companion-pro/src/lib/ai-stream.ts)).
  • Prompt Personas: Conversational Chef (chat), Theatrical Comparative Gastronomy (ghostchef), Structured JSON Extraction (import).
  • Global Search: [src/pages/GlobalSearchPage.tsx](file:///Users/avishjain/avishjain9-repos/26-cooking-nexus/27-kitchen-companion-pro/src/pages/GlobalSearchPage.tsx) indexing dishes, chefs, tags, and preparation times.

3. Semantic Vector Search & AI Pipeline Architecture ​


4. Search Architecture Comparison ​

Search EngineMechanismQuery TypeLatencyInfrastructure OverheadFit for Cooking Nexus
MongoDB Text IndexInverted Index (BM25)Exact word matches, stemmingLow (Local DB index)Zero extra infrastructureIdeal for MVP recipe titles
Atlas Vector Search (Target)HNSW Vector EmbeddingsSemantic meaning & intentModerate (~25ms)Integrated in MongoDBOptimal for semantic recipe discovery
Elasticsearch / OpenSearchDistributed Lucene ClusterFull-text, fuzzy, aggregationsLow-ModerateHigh (Dedicated JVM cluster)Overkill for early stage
Algolia / MeilisearchHosted Instant Search APITypo-tolerant prefix searchUltra-Low (<10ms)Managed Cloud APIGreat, but high SaaS cost at scale
PineconeDedicated Vector SaaSHigh-dimensional k-NNLowManaged SaaS APIGood alternative to Atlas Vector

5. Structured Output & Hallucination Mitigation ​

  1. JSON Schema Injection: In import mode, the prompt enforces a rigid JSON schema specifying mandatory integer fields for prep_time and servings.
  2. Grounding via Retrieval-Augmented Generation (RAG): Rather than asking the LLM to invent an obscure Thai curry recipe from memory, the system retrieves verified culinary ingredient ratios from the database and injects them as grounding context.

6. What Breaks First at Scale? ​

  • At 10x: LLM cost spikes during viral user influxes if common queries ("substitute butter") are not cached.
  • At 100x: Vector embedding API rate limits when generating embeddings for thousands of newly uploaded creator recipes.
  • At 1,000x: Context window memory bloat if full recipe histories are fed into chat context without conversational summarization.

7. Senior Engineer Improvements ​

Implement a Semantic Redis Cache with Cosine Similarity: Before calling Gemini 3 Flash, compute the vector embedding of the user's question and query Redis with FT.SEARCH. If a prior query has a cosine similarity $> 0.96$, return the cached response immediately, slashing LLM API costs by up to 70%.


8. Spoken Interview Answer (30 Seconds) ​

"Our AI architecture combines Server-Sent Events streaming with prompt persona routing. To deliver sub-second culinary intelligence without hallucinations, we use Gemini 3 Flash for zero-shot recipe parsing and conversational substitutions, with strict system prompt guardrails. As our recipe catalogue scales, we plan to implement a RAG pipeline using Atlas Vector Search to retrieve verified ingredient ratios, ensuring our generative sous chef always grounds its answers in culinary facts."