PandaRec
PandaRec is a recipe-based recommendation system for the Pandas library, built as my bachelor thesis at Heidelberg University. Pandas is a powerful data manipulation library for Python, but its size and technical documentation make it hard to discover the right function for a task - especially for researchers without a programming background.
The system lets users describe what they want to do in plain English inside a Jupyter Notebook. It then recommends recipes - named, documented code snippets - that the user can copy directly into their notebook cell.
Approach
The core idea is a modular recommendation engine backed by a curated recipe dataset. Recipes are simple data structures: a name, a description, a code snippet, and optional keywords. Three recipe datasets were evaluated: one generated from Pandas docstrings, one from the official cookbook, and one from VSCode snippets.
The GUI is implemented as a Jupyter widget using ipydatagrid for the DataFrame
view and ipywidgets for the search box and results. Results show a name, score,
code snippet with a copy button, and a description. The widget debounces queries to avoid
unnecessary API calls.
Multiple ranking strategies were implemented and compared:
- Name Search - literal substring match on recipe names (proof of concept)
- Fuzzy Search - Levenshtein-distance matching on names and descriptions via
rapidfuzz - Index Search - inverted index over lemmatized descriptions (NLTK), built once and cached to disk
- Semantic Search - sentence embeddings via Sentence-Transformers
(
all-mpnet-base-v2), compared against the query embedding using cosine similarity - OpenAI Embeddings - same approach using
text-embedding-ada-002 - OpenAI Chat Completion - free-form recipe generation from GPT, not bound to the predefined recipe set
- WebSocket Strategy - offloads ranking to a server, enabling usage data collection for future fine-tuning
The strategy pattern makes algorithms interchangeable. Each implements a search() method and an optional feedback() method for collecting user signals.
Results
Algorithms were evaluated on a custom NDCG@5 benchmark derived from 30 common Pandas tasks.
Semantic search with all-mpnet-base-v2 performed best among offline algorithms,
scoring 0.62 on the docstring dataset and up to 0.80 on the VSCode snippet dataset. OpenAI
Embeddings on the snippet dataset reached 0.88. Lexical approaches (fuzzy, index) scored
significantly lower, likely due to the technical brevity of recipe descriptions.
| Algorithm | NDCG@5 (docstring) | NDCG@5 (snippets) | Search delay |
|---|---|---|---|
| Fuzzy Description Search | 0.26 | 0.48 | ~10 ms |
| Index Search | 0.21 | 0.29 | ~1.7 ms |
| Semantic Search (mpnet) | 0.62 | 0.80 | ~38 ms |
| OpenAI Embeddings | 0.54 | 0.88 | ~670 ms |
All local algorithms except OpenAI Embeddings fell under the 50 ms real-time threshold. Setup time drops dramatically when using pre-built indices or cached embeddings (from ~85 s to ~680 ms for semantic search).
The VSCode snippet dataset, despite having fewer recipes than the docstring-generated set, outperformed it on every algorithm - its descriptions are cleaner and better match how users phrase queries. The snippets dataset did cover fewer queries overall; combining datasets is a natural next step.
Compared to ChatGPT, semantic search is faster and fully offline, but ChatGPT handles edge cases and context better. The main advantage of the recipe approach is determinism, speed, and the ability to fine-tune on domain-specific data.
Stack
- Python, Jupyter Notebooks
- Sentence-Transformers (SBERT), OpenAI API
- NLTK (lemmatization, stop words)
- ipydatagrid, ipywidgets
- rapidfuzz, websockets
Here is an extract showing the semantic search strategy:
# pandaRec/strategies.py
class SemanticSearch(RankingStrategy):
def __init__(self, recipes, path="",
model="sentence-transformers/all-mpnet-base-v2"):
self.model = SentenceTransformer(model)
descriptions = [r.description for r in recipes]
self.embeddings = self.model.encode(descriptions) if not path \
else load_embeddings(path)
def search(self, context, recipes, num_results=10):
query_embedding = self.model.encode(context.query, convert_to_tensor=True)
cos_scores = [
util.cos_sim(query_embedding, emb).item()
for emb in self.embeddings
]
result = [RecipeResult(score, recipe)
for recipe, score in zip(recipes, cos_scores)]
result.sort(key=lambda r: r.score, reverse=True)
return result[:num_results]