Back to blog
    Fine-Tune a Product Recommendation Model for E-Commerce: Full Walkthrough
    ecommercefine-tuningrecommendationsproductwalkthroughsegment:agency

    Fine-Tune a Product Recommendation Model for E-Commerce: Full Walkthrough

    Generic recommendation engines miss semantic product relationships. Here's how to fine-tune a model on your catalog and purchase history to build recommendations that increase average order value.

    EErtas Team·

    Collaborative filtering recommends "other customers also bought X." It works for popular products but fails on the long tail — and it has zero semantic understanding. A customer buying a camping stove should probably see fire starters and waterproof matches. Collaborative filtering only knows this if enough other customers bought exactly those items together.

    A fine-tuned language model understands product relationships semantically. You can train it on your catalog, your purchase history, and your customer interactions to build a recommendation engine that knows what your products are actually for.

    What You Are Building

    A model that takes a product (or a cart) as input and returns ranked recommendations. Example:

    Input:

    Customer is viewing: Jetboil Flash Camping Stove
    Cart contains: Sleeping bag, tent stakes
    Recommend 5 complementary products from our catalog.
    

    Output:

    1. MSR Fuel Canister 110g — Required fuel for Jetboil Flash operation
    2. UCO Stormproof Matches — Backup ignition, waterproof
    3. Sea to Summit Alpha Light Spork — Lightweight utensil for camp cooking
    4. Nalgene Wide Mouth Bottle 32oz — Water container for cooking
    5. Bear Canister — Required food storage for backcountry camping
    

    This is not collaborative filtering. The model knows a camping stove needs fuel, that waterproof matches are backup ignition, and that cooking utensils are contextually relevant. It produces this from semantic understanding of your catalog.

    Data Requirements

    Minimum viable dataset: 500 (product/cart, recommendations) pairs

    Recommended dataset size: 2,000-5,000 examples

    Data you need:

    1. Product catalog with descriptions (SKU, name, description, category, attributes)
    2. Purchase history (order ID, product IDs per order)
    3. Existing recommendations that converted (if you have recommendation data)

    How to construct training examples from purchase history:

    • Take orders with 3+ items
    • Use item(s) purchased as the "cart" input
    • Use the remaining item(s) as the "correct recommendation" output
    • Filter for orders where the recommended item was not an obvious category match (removes noise)

    Step 1: Prepare Your Dataset

    Export your product catalog as JSON:

    {
      "sku": "JB-FLASH-BLK",
      "name": "Jetboil Flash Camping Stove",
      "description": "Fast, lightweight backpacking stove. Boils 1L water in 100 seconds. Requires Jetboil fuel canister.",
      "category": "Camping Stoves",
      "attributes": {
        "weight": "13.1 oz",
        "compatible_fuel": "Jetboil fuel canisters",
        "use_case": "backpacking, ultralight camping"
      }
    }
    

    Export purchase history and construct recommendation pairs as JSONL:

    {"messages": [{"role": "user", "content": "Customer viewing: Jetboil Flash Camping Stove. Recommend 3 complementary products."}, {"role": "assistant", "content": "1. Jetboil JetPower Fuel 100g — Required fuel for Jetboil Flash\n2. UCO Stormproof Match Kit — Backup ignition\n3. Sea to Summit Alpha Light Spork Set — Lightweight eating utensil for camp cooking"}]}
    {"messages": [{"role": "user", "content": "Cart: 2-person backpacking tent, sleeping bag. Recommend 3 complementary products."}, {"role": "assistant", "content": "1. Sleeping bag liner — Adds warmth and extends temperature range\n2. Tent footprint — Protects tent floor from abrasion\n3. Headlamp — Essential for camp use at night"}]}
    

    The system message should describe the model's role and your catalog scope:

    {"messages": [{"role": "system", "content": "You are a product recommendation assistant for [Brand], an outdoor gear retailer. Recommend products from our catalog that are genuinely complementary — based on functional need, common use together, or upgrade path. Be specific about why each product complements the item(s) in the cart."}, ...]}
    

    Step 2: Train With Ertas

    1. Upload your JSONL dataset in Ertas
    2. Validate the dataset (Ertas will flag format errors)
    3. Select a base model (Mistral 7B or Llama 3 8B work well for recommendation tasks)
    4. Start the training job

    With a 2,000-example dataset, training typically takes 30-60 minutes. Run with default LoRA settings unless you have a specific reason to adjust.

    Step 3: Evaluate

    Your evaluation set should be held out before training — use 10-15% of your dataset for evaluation.

    Key metrics:

    • Does the model recommend real products from your catalog (hallucination rate)?
    • Are recommendations semantically relevant?
    • Are recommendations specific enough to be useful?

    Evaluation prompt: Run 50-100 products from your evaluation set through the model. Manually score each recommendation set: 3 = all relevant, 2 = mostly relevant, 1 = mostly irrelevant. Target average score: ≥2.5.

    Common issues:

    • Model recommends products not in your catalog → Add more examples with specific SKU references
    • Recommendations too generic → Add more specific examples with functional explanation
    • Model repeats same products → Add examples with more catalog diversity

    Step 4: Integrate Into Your Shopify / E-Commerce Platform

    Export the trained model as GGUF from Ertas, deploy with Ollama.

    Shopify integration (via custom app or Shopify Functions):

    // In your product page component
    async function getRecommendations(product, cart) {
      const response = await fetch('http://your-ollama-server:11434/api/chat', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          model: 'your-recommendation-model',
          messages: [
            {
              role: 'user',
              content: `Customer viewing: ${product.title}. Cart: ${cart.map(i => i.title).join(', ')}. Recommend 4 complementary products.`
            }
          ],
          stream: false
        })
      });
    
      const data = await response.json();
      return parseRecommendations(data.message.content);
    }
    

    Parse the model's output to extract product names, then match them to your catalog via Shopify Product API.

    Measuring Improvement

    Track before and after:

    • Recommendation click-through rate (CTR on the recommendations widget)
    • Add-to-cart from recommendations (secondary add rate)
    • Average order value (the ultimate metric)

    Typical improvements with a well-trained recommendation model vs generic collaborative filtering:

    • CTR: +15-30%
    • Secondary add rate: +20-40%
    • AOV: +3-8%

    For a store with $50,000 monthly revenue, a 5% AOV improvement is $2,500/month — $30,000/year. The model pays back its training cost in weeks.


    Ship AI that runs on your users' devices.

    Ertas early bird pricing starts at $14.50/mo — locked in for life. Plans for builders and agencies.

    Further Reading

    Ship AI that runs on your users' devices.

    Early bird pricing starts at $14.50/mo — locked in for life. Plans for builders and agencies.

    Keep reading