Logo
All posts

How Swiggy uses SLMs - AI Engineering Case Study #2

In this blog we will see how Swiggy fine-tuned a small language model to find the right dish.

Gaurav Sen/10 min read

In 2024, Swiggy delivered food from hundreds of thousands of restaurants across India, with millions of dishes on its app.[1] Swiggy called understanding each customer's search query critical to its business.[1]

India made that hard. Dish names change from region to region, and international dishes are cooked to the Indian palate. One query often mixes a dish, a diet and the time of day, and only the restaurants within about 5 kilometres can serve it.[1]

When search misses, the customer can't find the dish they want to order. Swiggy's goal was to make dishes easier to find, and to improve customer trust and satisfaction.[5]

Swiggy's engineers faced two problems:

  1. A text search matches words, and a query like "Karnataka famous sweets" shares no word with the right dish.
  2. Each query had to be understood within 100 milliseconds, on Swiggy's CPU servers.

In this blog we will see how Swiggy fine-tuned a small language model to find the right dish.

Swiggy's search model, built step by step: a pre-trained model is fine-tuned in Stage 1 on 996,000 past orders, then in Stage 2 on 300,000 curated pairs of a query and a dish. The fine-tuned model reads a search query and returns the closest dishes within 5 km.
Swiggy's search model, built step by step: a pre-trained model is fine-tuned in Stage 1 on 996,000 past orders, then in Stage 2 on 300,000 curated pairs of a query and a dish. The fine-tuned model reads a search query and returns the closest dishes within 5 km.

1. What a text search can't find

What does a customer type into a food app? Usually a dish.

Typical queries search for a specific food item, which can be served by a simple text search, but some search queries require contextual awareness like regional preferences, calorie count, and healthy options.

Swiggy's search bar with three queries in turn: Biryani, a dish name a text search finds; I want to have a local delicacy, a query with no dish in it; and Zero calorie ice cream, a query about calories.
Swiggy's search bar with three queries in turn: Biryani, a dish name a text search finds; I want to have a local delicacy, a query with no dish in it; and Zero calorie ice cream, a query about calories.

Swiggy saw such queries mix a dish, a diet and the customer's mood.[1]

One such query is "Karnataka famous sweets". The right dishes are Mysore Pak and Chandrahara, and neither name has a word of the query in it. The search has to match what the query means.

Swiggy kept string matching for plain dish names. The work below is for every other query.

2. Why a small model

How does a search match meaning? A language model turns a text into an embedding, a list of numbers. Two texts that mean similar things get numbers close to each other.

So the model turns every dish name into an embedding once. When a query comes in, it turns the query into one too, and the search returns the dishes closest to it.

An LLM can be used here to answer the queries, but fine tuning them is expensive and the responses are slow.

Swiggy gave this step 100 milliseconds for each query, on its existing CPU servers. So the engineers started from the smallest open models that already did well at semantic search: all-mpnet-base-v2 and all-MiniLM-L6-v2.[1]

Instead, the engineers at Swiggy chose a Small Language Model, which is fine tuned with historical data and manually curated responses.

3. Stage 1: learn from past orders

What does a public model know about Indian food? Very little. It hasn't seen which dishes Swiggy's customers order for the same kind of search.

So the first stage reads Swiggy's own history: searches that ended in an order. Each becomes a short document.[1]

Swiggy's search model with Stage 1 marked: the pre-trained model, then 996,000 past orders going into Stage 1.
Swiggy's search model with Stage 1 marked: the pre-trained model, then 996,000 past orders going into Stage 1.

> Customer searching for 'punjabi curries' bought the item 'Paneer lababdar'. This item belongs to 'curry' category, 'Punjabi' preparation style.

There were 996,000 such documents. Nobody had to label them.

The model was trained to repair them. A few words of each document are deleted or swapped, and the model has to rebuild the original sentence from its embedding alone. This method is called TSDAE, a denoising auto-encoder.

After this stage, dishes ordered for the same kind of search moved closer together. A dosa ended up nearer an idli than a burger. The difference in similarity between those two pairs grew from 0.11 to 0.30.

4. Stage 2: curated pairs of a query and a dish

Which queries does Stage 1 still miss? Past searches don't hold many hard ones: dish descriptions, diets and regional styles that customers rarely typed.

So the team wrote those queries themselves. For every food category they listed the queries a customer might type, and the dishes that answer each one. Large language models filled in each dish's regional cuisine, main ingredient and preparation style, and people checked every pair.

Swiggy's search model with Stage 2 marked: 300,000 curated pairs going into Stage 2, the fine-tuned model out of it, and a search query through it to the closest dishes within 5 km.
Swiggy's search model with Stage 2 marked: 300,000 curated pairs going into Stage 2, the fine-tuned model out of it, and a search query through it to the closest dishes within 5 km.

That gave more than 300,000 pairs of a query and a dish. The model was trained to pull each query close to its dish, and away from the other dishes in the same batch.[1]

Take the query "I just finished gym, show me some good options". Here are the top three dishes before and after.

ModelTop three dishes
Pre-trainedGrilled Protein Meal; Beans Carrot Poriyal; Homestyle Dal and Rotis
After both stagesWeight Loss smoothie; Fig shake; Ultimate Whey Protein

The team kept this stage running. Queries the search serves badly are collected, their right dishes curated, and fed back into Stage 2.

5. What Swiggy measured

How much better did search get? Swiggy tested on more than 5,000 queries. They covered 20 cuisines and more than 10 dietary preferences.[1]

For each test query, every dish returned was marked relevant or not. P@1 asks if the first dish is relevant.

P@5 asks how many of the top five are. MAP scores the whole ranked list.

Swiggy's results on 3.7 lakh serviceable items. After both stages all-mpnet-base-v2 scores 0.95 MAP, 0.93 P@1 and 0.89 P@5, against 0.82, 0.78 and 0.75 pre-trained, and 0.61, 0.39 and 0.50 for BM25, a text search.
Swiggy's results on 3.7 lakh serviceable items. After both stages all-mpnet-base-v2 scores 0.95 MAP, 0.93 P@1 and 0.89 P@5, against 0.82, 0.78 and 0.75 pre-trained, and 0.61, 0.39 and 0.50 for BM25, a text search.

The model trained in both stages scored best on MAP and P@5. A classic text search, BM25, put a relevant dish first only 39% of the time.

Stage 2 alone scored a higher P@1, 0.98, but a lower MAP. Without Stage 1, dishes with different names but the same use never moved close. The rest of the list was worse for it.

The model held up in a sparse area too, with about one sixth of the dishes. It scored 0.95 MAP again, where the pre-trained model's scores weren't consistent.

Now customers can ask more generic queries, which can't be answered by a simple text search.

I explained the two-stage model in a one-minute reel.

Summary

  1. A text search finds dish names. It can't find "Karnataka famous sweets", whose right answers share no word with it.
  2. An embedding search matches meaning: the query and every dish become lists of numbers, and the closest dishes come back.
  3. Swiggy gave a query 100 milliseconds on CPUs. It started from small open models such as all-mpnet-base-v2.
  4. Stage 1 trained the model on 996,000 unlabelled documents of past searches and orders.
  5. Stage 2 trained it on more than 300,000 query and dish pairs that people checked.
  6. After both stages the model scored 0.95 MAP and 0.89 P@5, against 0.61 and 0.50 for a text search.

Sources

  1. Improving search relevance in hyperlocal food delivery using (small) language models, Aditya Kiran and others, Swiggy Bytes, 13 March 2024.
  2. The same work as a paper, CODS-COMAD 2024, Bangalore, January 2024.
  3. Improving search relevance in hyperlocal food delivery using (small) language models, talk by Aditya Kiran at The Fifth Elephant 2024.
  4. Q-commerce apps are integrating AI to search, Gaurav Sen, YouTube, 19 February 2025.
  5. Reflecting on a year of generative AI at Swiggy, Jose Mathew, Swiggy Bytes, 22 February 2024.
WhatsApp