Search Alchemy
Building Production Search Pipelines with Fireworks AI
Powered by
Try Example Queries: Select a category and specificity level to auto-load an example
Category
Query Specificity
import bm25s
import pandas as pd
# Step 1: Create BM25 index (one-time setup)
df = pd.read_parquet("data/amazon_products.parquet")
corpus = df["FullText"].tolist()
corpus_tokens = bm25s.tokenize(corpus, stopwords="en")
retriever = bm25s.BM25()
retriever.index(corpus_tokens)
retriever.save("data/bm25_index")
# Step 2: Load index and search
bm25_index = bm25s.BM25.load("data/bm25_index", load_corpus=False)
query_tokens = bm25s.tokenize(query, stopwords="en")
results, scores = bm25_index.retrieve(query_tokens, k=5)
# Extract top results
top_products = [df.iloc[idx] for idx in results[0]]
from openai import OpenAI
import faiss
import numpy as np
client = OpenAI(
base_url="https://api.fireworks.ai/inference/v1"
)
# Generate embeddings
response = client.embeddings.create(
model="accounts/fireworks/models/qwen3-embedding-8b",
input=[query] + documents
)
# Extract embeddings
query_emb = np.array(response.data[0].embedding)
doc_embs = np.array([d.embedding for d in response.data[1:]])
# FAISS search
index = faiss.IndexFlatIP(doc_embs.shape[1])
index.add(doc_embs)
scores, indices = index.search(query_emb.reshape(1, -1), k=5)
# Query expansion with LLM
response = client.chat.completions.create(
model="accounts/fireworks/models/llama-v3p1-8b-instruct",
messages=[{
"role": "user",
"content": f"Extract 2-3 key search concepts from: {query}"
}]
)
expanded_query = response.choices[0].message.content
# Search with expanded query
response = client.embeddings.create(
model="accounts/fireworks/models/qwen3-embedding-8b",
input=[expanded_query] + documents
)
# Continue with embedding search...
# First get top 20 candidates from Stage 3
top_20_results = get_stage_3_results(query, k=20)
# Rerank with Fireworks reranker
rerank_response = client.post(
"https://api.fireworks.ai/inference/v1/rerank",
json={
"model": "fireworks/qwen3-reranker-8b",
"query": query,
"documents": [r["text"] for r in top_20_results],
"top_n": 5
}
)
# Get final ranked results
final_results = [
top_20_results[r["index"]]
for r in rerank_response.json()["results"]
]
Explore the dataset used for this search demo
Dataset Category Distribution
| Category | Count | Percentage |
|---|---|---|
| Toys & Games | 650 | 26.2% |
| Home & Kitchen | 632 | 25.4% |
| Clothing, Shoes & Jewelry | 547 | 22.0% |
| Sports & Outdoors | 461 | 18.6% |
| Baby Products | 194 | 7.8% |
| Total | 2,484 | 100.0% |
Sample Products from Dataset
| Product Name | Main Category | Secondary Category | Description |
|---|---|---|---|
| SignMission Big Cookies 72" Banner Concession Stand Food Truck Single Sided, Size: 24" X 72" | Home & Kitchen | Home Décor | Proudly Made In The USA – Buy with confidence from an American owned and operate... |
| PAW Patrol Boys' Toddler Group Short Sleeve T-Shirt, Navy Heather, 3T | Clothing, Shoes & Jewelry | Novelty & More | Great Condition. |
| Bachmann Trains E-Z TRACK REVERSING 18" RADIUS CURVED (4/card) - NICKEL SILVER Rail With Grey Roadbed - HO Scale | Toys & Games | Hobbies | Nickel Silver Rail with Gray Roadbed. |
| Fun World Black Skull Lord Ninja Costume | Clothing, Shoes & Jewelry | Costumes & Accessories | Great Condition. |
| The Complete Common Core: State Standards Kit, Grade 5 | Toys & Games | Learning & Education | Pre-printed, easily organized system that provides an entire list of Common Core... |