← All projects

AI / ML

Amazon Product RAG Chatbot

A ChatGPT-style interface for asking plain-English questions about ~55K Amazon products. Questions become SQL, the results come back as data, and an LLM turns them into answers that come from the data, not the model’s memory.

Amazon logo Not affiliated with Amazon
Type
Team of 3
Category
AI / ML
Stack
Python · Flask · Next.js · SQLite · OpenAI API

Structure

How the parts fit together
Architecture diagram: Next.js chat UI and Flask API (question ⇄ answer); Flask API to generate_sql (question); generate_sql to SQLite (run_sql); SQLite to generate_answer (rows); generate_answer to Flask API (answer text)

A question becomes one read-only SQL query, the rows come back from SQLite, and a second model call writes the answer. The UI turns product mentions into Amazon links and images.

SheetStructure
ProjectAmazon Product RAG Chatbot
Parts5

One question, step by step

Follow a single question through the chatbot, from the moment it is typed to the answer on screen. It also shows what happens when something goes wrong along the way.

Step 1 of 6

1 · The question

It starts the way any chat does: someone types a question in plain English into the chat box.

The chat page sends that question to a small server that runs the chatbot. The server is called the backend.

Illustrative example Made-up products and numbers, to show the flow. Not a captured run.

2 · The question becomes a database query

The data lives in a database of about 55,000 Amazon products. A database is read with a small language called SQL, so the question has to be translated first.

An AI model (GPT-4.1) does the translating. It is given the list of columns it may use and the product categories that exist, and it writes one read-only query.

SQL: a short instruction that asks a database for specific information.

Illustrative example Made-up products and numbers, to show the flow. Not a captured run.

The query the model wrote

SELECT asin, title, image, buy_box_current, reviews_rating, reviews_count
FROM keepaproducts
WHERE category = 'Pet Supplies'
  AND buy_box_current < 25
  AND reviews_count >= 100
ORDER BY reviews_rating DESC, reviews_count DESC
LIMIT 25;

3 · The safety checks

Before anything runs, the query must pass two checks. The first rejects words that could change data, such as insert, update, delete, drop, alter and create. The second does a dry run with EXPLAIN, which checks the query makes sense without running it.

If a check fails, the error message is added to the instructions and the model tries again. It gets up to 5 tries in total. If every try fails, the chatbot replies with a friendly message instead of an answer.

Model writes a query Check 1 blocked words Check 2 dry run (EXPLAIN) Run it all clear pass pass fail fail Add the error to the instructions and ask the model to try again try again (up to 5) still failing after 5 tries Friendly fallback message "I'm unable to answer this question..."

4 · Run the query

Now the query is run on the database. It returns rows, like lines in a spreadsheet, and each row is one product.

The backend keeps those rows to use in the next step. The size of the result is also sent back to the chat page as the row count.

Illustrative example Made-up products and numbers, to show the flow. Not a captured run.

Rows returned: 25

asintitlebuy_box_currentreviews_ratingreviews_count
B0EXAMPLE01Whiskerwell Cozy Cat Cave18.994.92310
B0EXAMPLE02Pawsome Chew Ring12.494.91875
B0EXAMPLE03Tailwind Slow Feeder Bowl9.994.83402

A few of the rows, shown as a sample.

5 · The rows become an answer

The rows are not shown to the visitor as they are. They go to the AI model a second time, together with the original question.

This time the model writes a short answer in plain English. Whenever it names a product, it tags that product with its ASIN (Amazon's product ID) and its image addresses.

Model: an AI program that reads and writes text.

What the model hands back

I found 25 well-reviewed pet supplies under $25. Here are the top three.

**Whiskerwell Cozy Cat Cave** [metadata: {"asin": "B0EXAMPLE01", "images": ["https://example.com/img/B0EXAMPLE01.jpg"]}]
Rated 4.9 stars from 2,310 reviews, at $18.99.

**Pawsome Chew Ring** [metadata: {"asin": "B0EXAMPLE02", "images": ["https://example.com/img/B0EXAMPLE02.jpg"]}]
Also rated 4.9 stars, from 1,875 reviews, at $12.49.

**Tailwind Slow Feeder Bowl** [metadata: {"asin": "B0EXAMPLE03", "images": ["https://example.com/img/B0EXAMPLE03.jpg"]}]
Rated 4.8 stars from 3,402 reviews, at $9.99.

The ** marks bold text. The metadata tags are hidden from the visitor in the next step.

6 · What you see

The chat page reads the tags and turns each tagged product name into a link to its Amazon page. It also shows the first product image.

The visitor only sees a clean answer, with clickable products.

Illustrative example Made-up products and numbers, to show the flow. Not a captured run.

I found 25 well-reviewed pet supplies under $25. Here are the top three.

Whiskerwell Cozy Cat Cave Rated 4.9 stars from 2,310 reviews, at $18.99.
Pawsome Chew Ring Also rated 4.9 stars, from 1,875 reviews, at $12.49.
Tailwind Slow Feeder Bowl Rated 4.8 stars from 3,402 reviews, at $9.99.

The problem

Amazon product data is huge and numeric: prices, ratings, rankings, categories. Keyword search can’t answer questions like “which budget headphones have the best rating?”, which need filtering, sorting and aggregating across the whole catalog. This project lets anyone ask those questions in plain English and get an answer backed by real data.

Approach

  • Users ask a question in a ChatGPT-style chat interface built with Next.js
  • A Flask API passes the question to OpenAI’s models, which translate it into a SQL query
  • The query runs against a SQLite database of Keepa data covering ~55K Amazon products
  • The returned rows go back to the LLM as context, so the final answer is grounded in the data (retrieval-augmented generation) rather than the model’s memory
  • Frontend and backend are separate apps: the Next.js client talks to the Flask API over HTTP, and each has its own setup and docs

Highlights

  • Text-to-SQL RAG pipeline over ~55K products
  • Product data from Keepa, stored in SQLite
  • Answers come from query results rather than model memory
  • Co-built with a team of 3 across a Flask backend and Next.js frontend

Ask my site

Ask about Amazon Product RAG Chatbot.

Answers come only from the content on this site. If it isn't here, it'll say so and point you to my email.

Answers are generated from this site's content, so double-check anything important.

Psst, this page has a cheat code.