Tutorial

How to Build an AI Shopping Assistant with LangChain and Amazon Data

Create an AI-powered shopping assistant using LangChain and the Scavio Amazon API. Answer natural language product queries with real-time Amazon search results.

An AI shopping assistant accepts natural language queries like "find me wireless headphones under $100 with good reviews" and returns ranked product recommendations backed by live Amazon data. This type of assistant combines an LLM for intent parsing and recommendation generation with a real-time product search API for fresh inventory and pricing. This tutorial builds such an assistant using LangChain, ScavioSearch configured for Amazon, and a simple conversational loop.

Prerequisites

  • Python 3.10 or higher
  • pip install langchain langchain-scavio langchain-openai
  • A Scavio API key
  • An OpenAI API key

Walkthrough

Step 1: Configure the Amazon search tool

Instantiate ScavioSearch with the amazon platform. This gives the LangChain agent access to live Amazon product search.

Python
from langchain_scavio import ScavioSearch

amazon_tool = ScavioSearch(
    api_key="your_scavio_api_key",
    platform="amazon",
    marketplace="US",
    max_results=10
)

Step 2: Build the system prompt

Define a system prompt that instructs the LLM to act as a shopping assistant and format recommendations clearly.

Python
SYSTEM_PROMPT = (
    "You are a helpful shopping assistant. When the user asks for product recommendations, "
    "use the search tool to find current Amazon listings. Always mention price, rating, and "
    "a brief reason for each recommendation. Keep responses concise."
)

Step 3: Create the agent

Wire the Amazon tool into a LangChain agent with the shopping assistant system prompt.

Python
from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate

llm = ChatOpenAI(model="gpt-4o", temperature=0)
prompt = ChatPromptTemplate.from_messages([
    ("system", SYSTEM_PROMPT),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])
agent = create_tool_calling_agent(llm, [amazon_tool], prompt)
executor = AgentExecutor(agent=agent, tools=[amazon_tool])

Step 4: Run a shopping query

Invoke the assistant with a natural language shopping request and print the response.

Python
response = executor.invoke({
    "input": "Find me the best wireless headphones under $150 with noise cancellation"
})
print(response["output"])

Python Example

Python
import os
from langchain_scavio import ScavioSearch
from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate

os.environ["OPENAI_API_KEY"] = "your_openai_key"

tool = ScavioSearch(api_key=os.environ["SCAVIO_API_KEY"], platform="amazon", marketplace="US")
llm = ChatOpenAI(model="gpt-4o", temperature=0)
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful shopping assistant. Use the search tool to find products."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])
agent = create_tool_calling_agent(llm, [tool], prompt)
executor = AgentExecutor(agent=agent, tools=[tool], verbose=True)

if __name__ == "__main__":
    result = executor.invoke({"input": "Best noise-canceling headphones under $150"})
    print(result["output"])

JavaScript Example

JavaScript
const { ChatOpenAI } = require("@langchain/openai");
const { DynamicTool } = require("@langchain/core/tools");
const { AgentExecutor, createToolCallingAgent } = require("langchain/agents");
const { ChatPromptTemplate } = require("@langchain/core/prompts");

const API_KEY = process.env.SCAVIO_API_KEY;

const amazonTool = new DynamicTool({
  name: "amazon_search",
  description: "Search Amazon for products. Input is a product search query.",
  func: async (query) => {
    const res = await fetch("https://api.scavio.dev/api/v1/search", {
      method: "POST",
      headers: { "x-api-key": API_KEY, "Content-Type": "application/json" },
      body: JSON.stringify({ platform: "amazon", query, marketplace: "US" })
    });
    const data = await res.json();
    return JSON.stringify((data.products || []).slice(0, 5));
  }
});

async function main() {
  const llm = new ChatOpenAI({ model: "gpt-4o" });
  const prompt = ChatPromptTemplate.fromMessages([
    ["system", "You are a helpful shopping assistant."],
    ["human", "{input}"],
    ["placeholder", "{agent_scratchpad}"]
  ]);
  const agent = await createToolCallingAgent({ llm, tools: [amazonTool], prompt });
  const executor = new AgentExecutor({ agent, tools: [amazonTool] });
  const result = await executor.invoke({ input: "Best wireless headphones under $150" });
  console.log(result.output);
}
main().catch(console.error);

Expected Output

JSON
Based on current Amazon listings, here are the top noise-canceling headphones under $150:

1. Anker Soundcore Q45 — $59.99 (4.4 stars, 28,000+ reviews)
   Great ANC for the price, up to 50 hours battery life.

2. Sony WH-CH720N — $149.00 (4.5 stars, 15,000+ reviews)
   Lightweight with Sony's proprietary ANC, folds flat.

Related Tutorials

Frequently Asked Questions

Most developers complete this tutorial in 15 to 30 minutes. You will need a Scavio API key (free tier works) and a working Python or JavaScript environment.

Python 3.10 or higher. pip install langchain langchain-scavio langchain-openai. A Scavio API key. An OpenAI API key. A Scavio API key gives you 500 free credits per month.

Yes. The free tier includes 500 credits per month, which is more than enough to complete this tutorial and prototype a working solution.

Scavio has a native LangChain package (langchain-scavio), an MCP server, and a plain REST API that works with any HTTP client. This tutorial uses LangChain, but you can adapt to your framework of choice.

Start Building

Create an AI-powered shopping assistant using LangChain and the Scavio Amazon API. Answer natural language product queries with real-time Amazon search results.