Two-Stage Fine-Tuning of Language Models for Hyperlocal Food Search
swiggy · E-commerce · 2024 · source Agentic: no · promotional bias: low · summary-only (no fetched source) · extracted with prompt 0.5.6 (schema v2)
Generated from extraction.json — do not edit by hand. Every claim cites verbatim source evidence; stated means the quote directly asserts the claim, implied means it supports it indirectly.
Problem
Domain: Hyperlocal food search relevance
Swiggy needed better semantic retrieval for complex Indian food-search queries that combine cuisine, diet, time, preparation style, and regional intent under strict real-time latency constraints.
Constraints:
- Food delivery is hyperlocal, with restaurants usually within about 5km.
- The catalog spans hundreds of thousands of restaurants and millions of food items.
- Real-time query embedding had a hard 100ms latency requirement.
- The model had to run within CPU-based search infrastructure.
- Training data used LLM augmentation but still required manual verification.
Architecture
Swiggy fine-tuned small embedding models in two stages. Stage 1 used TSDAE unsupervised domain adaptation on historical order-derived text. Stage 2 used supervised fine-tuning over manually curated and LLM-augmented query-item pairs, with Multiple Negatives training, to improve semantic food-search relevance while staying under the 100ms embedding latency target.
Diagram confidence: reconstructed (reconstructed from prose).
flowchart LR historical_order_corpus["Historical order corpus"] --> tsdae_domain_adaptation["TSDAE domain adaptation"] base_sentence_embedding_model["Base sentence embedding model"] --> tsdae_domain_adaptation tsdae_domain_adaptation --> supervised_retrieval_fine_tuning["Supervised retrieval fine-tuning"] llm_metadata_augmentation["LLM metadata augmentation"] --> curated_query_item_pairs["Curated query-item pairs"] curated_query_item_pairs --> supervised_retrieval_fine_tuning supervised_retrieval_fine_tuning --> food_search_ranking_service["Food search ranking service"]
Components
| Component | Role | Interacts with |
|---|---|---|
Historical order corpus |
Provides successful query-item examples for domain adaptation. | TSDAE domain adaptation |
TSDAE domain adaptation |
Unsupervised fine-tunes the sentence model on Swiggy food-search text. | Historical order corpus, Base sentence embedding model, Supervised retrieval fine-tuning |
Base sentence embedding model |
Provides the starting model for food search embeddings. | TSDAE domain adaptation, Supervised retrieval fine-tuning |
LLM metadata augmentation |
Generates additional cuisine, ingredient, preparation, and time-slot metadata. | Curated query-item pairs |
Curated query-item pairs |
Supervised training data verified by humans. | LLM metadata augmentation, Supervised retrieval fine-tuning |
Supervised retrieval fine-tuning |
Optimizes the embedding model for query-item relevance. | Base sentence embedding model, Curated query-item pairs, Food search ranking service |
Food search ranking service |
Uses the fine-tuned model under real-time latency constraints. | Supervised retrieval fine-tuning |
Data flow
Historical successful orders produce query-item-taxonomy text documents. TSDAE corrupts and reconstructs these texts to adapt the sentence model to food semantics. Then curated query-item pairs, augmented with cuisine/ingredient/preparation/time metadata, fine-tune the model for retrieval. At serving time, the model embeds user queries for real-time search.
3 supporting quotes
Why this architecture
Swiggy chose small fine-tuned embedding models because large LLMs could not meet real-time food-search latency and infrastructure constraints, while two-stage adaptation captured domain semantics from actual orders and curated examples.
Models used: all-mpnet-base-v2 · all-MiniLM-L6-v2 · TSDAE · LLMs for metadata augmentation
Scale
- Swiggy operates across hundreds of thousands of restaurants and millions of food items.“operating across hundreds of thousands of restaurants and serving millions of food items.”summary, stated
- Restaurant availability is roughly within 5km of the customer.“where only restaurants within approximately 5km can serve a customer”summary, stated
- Real-time embedding generation had a 100ms latency target.“Swiggy set a hard requirement of 100 milliseconds for real-time query embedding generation”summary, stated
- Stage 1 used approximately 996,000 historical text documents.“unsupervised fine-tuning on approximately 996,000 historical text documents.”summary, stated
- Stage 2 used over 300,000 curated query-item pairs.“over 300,000 manually curated query-item pairs.”summary, stated
Alternatives considered (roads not taken)
Use larger LLMs for retrieval embeddings
Rejected because: They could not meet Swiggy's real-time latency and CPU infrastructure constraints.
Use simple string matching
Rejected because: Complex queries require semantic understanding beyond dish-name matching.
Trust LLM-augmented training samples without review
Rejected because: All training samples were manually verified and edited.
Patterns
fine-tuning-adaptation (search embeddings)
Swiggy adapted a sentence embedding model with unsupervised domain adaptation and supervised fine-tuning.
synthetic-data-generation (training data augmentation)
LLMs generated additional metadata used to construct diverse query templates.
human-in-the-loop-gate (data quality)
Humans verified and edited training samples produced with LLM augmentation.
latency-optimization (real-time search)
Model choice was constrained by a 100ms query embedding target.
Tradeoffs
Use small language models for embeddings.
Chose all-mpnet-base-v2 / all-MiniLM-L6-v2 over Larger LLM embedding or reasoning models.
Rationale: Small models balanced semantic quality with 100ms production latency.
Separate domain adaptation from supervised retrieval training.
Chose TSDAE followed by curated query-item fine-tuning over One supervised training stage only.
Rationale: The first stage teaches food-domain relationships, while the second optimizes the search task.
Gotchas (what bit them)
Real-time search may require small models even when larger models are higher quality.
Root cause: Latency and CPU infrastructure constraints dominate production search.
Resolution: Fine-tune small embedding models for the domain.
LLM augmentation still needs manual quality gates.
Root cause: Generated metadata can introduce noisy or wrong training examples.
Resolution: Manually verify and edit all training samples.
Evaluation
Swiggy evaluated hyperlocal search using Mean Average Precision and Precision@5 across dense and sparse regions, plus qualitative examples of complex food queries.
Metrics: 100ms embedding latency constraint. · Approximately 996,000 historical documents. · Over 300,000 curated query-item pairs. · Mean Average Precision. · Precision@5.
Outcomes
- The fine-tuned model retrieved contextually relevant food items for complex queries.
- The system improved MAP and Precision@5 in dense and sparse regions.
- The feature was being tested with a small user cohort.
The feature targets a catalog across hundreds of thousands of restaurants and millions of items, with 100ms query embedding latency.