E-Commerce AI Agent in Python - Full Tutorial

NeuralNineAbout 6 min readMay 29, 2025Watch original
THE SUMMARYAI-generated

Key Concepts

  • Real-time E-commerce AI Agent: An AI agent that uses current, up-to-date information from the web to find products based on user-specified criteria.
  • LangChain: A framework for developing applications powered by language models.
  • LangGraph: A library for building robust and stateful multi-actor applications with LLMs.
  • Bright Data: A platform providing proxies and web scraping tools.
  • Model Context Protocol (MCP): A standardized protocol for integrating functionality into AI agents.
  • React Agent (Reasoning and Acting): An agent architecture that synergizes reasoning and acting in language models.
  • Pydantic: A Python library for data validation and settings management using type annotations.
  • Flask: A micro web framework for Python.

E-commerce AI Agent Overview

The video demonstrates building a real-time e-commerce AI agent using Python, LangChain, LangGraph, Bright Data's web scraping tools, and GPT-4o. The agent takes a user's product search query and a list of e-commerce platforms, then scrapes those platforms in real-time to find products matching the criteria. The results are presented in a structured format, including URLs, titles, and ratings.

Architecture

The architecture involves an AI agent powered by GPT-4o, equipped with tools for web searching and scraping. The agent receives a request, calls the necessary tools (provided by Bright Data), exchanges information with the language model, and outputs a structured JSON object. This JSON is then rendered into a web page displaying the search results.

Implementation Steps

  1. Setting up the Environment:

    • Create an .env file to store API keys and credentials.
    • Install required Python packages using pip or uv: python-dotenv, flask, mcp, langchain, langchain-mcp-adapters, langgraph, langchain-openai, and pydantic.
    • Obtain an OpenAI API key from the OpenAI platform.
    • Sign up for Bright Data, create instances of the Web Unlocker API and Scraping Browser API, and obtain the API key, Web Unlocker zone identifier (mcp_unlocker), and Scraping Browser URL.
    • Populate the .env file with the OpenAI API key, Bright Data API token, Web Unlocker zone, and Scraping Browser URL.
  2. Coding the Flask Application (app.py):

    • Import necessary Python modules and packages: os, json, typing, pydantic, flask, mcp, langchain, and langgraph.
    • Load environment variables using load_dotenv().
    • Initialize the GPT-4o model using ChatOpenAI(model_name="gpt-4o").
    • Define the SDIO server parameters for connecting to the Bright Data MCP server using stdio_server_parameters.
    • Authenticate with Bright Data by providing the API token, Web Unlocker zone, and Scraping Browser URL in the environment dictionary.
    • Define a system prompt to guide the agent's behavior, specifying when to use the search engine and web data tools.
    • Define a list of e-commerce platforms to offer to the user (e.g., Amazon, eBay, Walmart).
    • Define Pydantic data models for the structured output:
      • Hit: Represents a single product hit with URL, title, and rating (with descriptions for each field).
      • PlatformBlock: Represents a block of hits for a specific platform, including the platform name and a list of Hit objects.
      • ProductSearchResponse: Represents the overall response, containing a list of PlatformBlock objects.
    • Create an asynchronous function run_agent(query, platforms) to:
      • Connect to the MCP server using stdio_client and client_session.
      • Load the tools from the MCP server using load_mcp_tools().
      • Create a React agent using create_react_agent(), passing the model, tools, and the ProductSearchResponse data model as the response format.
      • Construct a prompt combining the user's query and the selected platforms.
      • Invoke the agent using agent.invoke() with the prompt.
      • Extract the structured response from the result using result["structured_response"].
      • Dump the Pydantic model to JSON using structured_response.model_dump_json() and return it.
    • Define a Flask route / that handles both GET and POST requests:
      • On POST, extract the query and selected platforms from the form data.
      • Call the run_agent() function to get the search results.
      • Render the index.html template, passing the query, selected platforms, available platforms, and the JSON response.
      • On GET, render the index.html template with empty values for the query, selected platforms, and response.
    • Run the Flask application using app.run(host="0.0.0.0", port=8000, debug=True).
  3. Creating HTML Templates (templates/):

    • Create a base.html file with the basic HTML structure, including meta tags, styling, navigation, and a placeholder for flash messages.
    • Create an index.html file that extends base.html and contains:
      • A form with a text input for the query and checkboxes for selecting platforms.
      • A submit button.
      • Code to iterate over the platforms list and generate checkboxes.
      • Code to iterate over the response_json and display the search results, grouped by platform.
  4. Adding Styling (static/style.css):

    • Create a style.css file with basic CSS styling for the application.

Key Arguments and Perspectives

  • The video emphasizes the importance of using real-time data for e-commerce applications, as opposed to relying on outdated datasets.
  • It highlights the benefits of using AI agents to automate the process of searching and comparing products across multiple platforms.
  • The use of Bright Data's web scraping tools simplifies the process of extracting data from websites, avoiding the complexities of manual scraping.
  • The structured output format (using Pydantic) makes it easier to process and display the search results in a user-friendly way.

Notable Quotes

  • "This essentially means an AI agent that uses real time information from the web not some past data not some Kaggle data set but actual information that is currently available online and up to date to find certain products that you specify in a prompt."
  • "What we're building today is we have our own AI agent with a backbone of intelligence which is GPT 40 in our case and a bunch of tools that this agent has access to."
  • "The cool thing is in today's video we don't even have to interact with their product at all. We don't really have to do anything. We don't have to use some SDK. We don't need to manually do anything. All we have to do is we have to connect their MCP server."

Technical Terms and Concepts

  • API Key: A code used to authenticate and authorize access to an API.
  • Web Scraping: The process of extracting data from websites.
  • Proxies: Intermediary servers that forward requests between clients and servers, often used to mask IP addresses and bypass restrictions.
  • JSON (JavaScript Object Notation): A lightweight data-interchange format.
  • Asynchronous Function: A function that can execute independently of the main program flow, allowing for non-blocking operations.
  • Ginga Templating Engine: A template engine used by Flask to dynamically generate HTML pages.

Logical Connections

The video follows a logical progression, starting with an overview of the project, then detailing the setup process, the code implementation, and finally, a demonstration of the working application. Each section builds upon the previous one, providing a clear and comprehensive guide to building the e-commerce AI agent.

Data, Research Findings, or Statistics

The video doesn't present specific research findings or statistics, but it implicitly relies on the capabilities of GPT-4o and Bright Data's web scraping tools to accurately extract and process information from the web.

Synthesis/Conclusion

The video provides a practical guide to building a real-time e-commerce AI agent using Python, LangChain, LangGraph, Bright Data, and GPT-4o. By leveraging these technologies, developers can create intelligent applications that automate product searches, compare prices, and provide users with up-to-date information from multiple e-commerce platforms. The key takeaways are the importance of real-time data, the power of AI agents, and the ease of integrating web scraping tools into AI-powered applications.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.