Public API Link here
This API I created allows you to search for products in a catalog and received intelligent response and the retrieved items using natural language queries. It employs CoHERE, OpenAI's GPT-3, sentence-transformers and other libraries to provide intelligent responses to user queries. This Guide will show you step by step on how to run your own API on localhost, additionally, I hosted a public API here
- python 3.10
- Docker
- kubernetes (support for scale-up)
- Pytorch
- FastAPI
- Azure Cloud
- Sentence-Transformers
- GPT-3
- Cohere Embedding Models
- Faiss (for searching through vector database)
- sentence BERT Model
- NVIDIA-CUDA
Before running the API, please make sure you have the following prerequisites installed:
- Python 3.10
- Docker (optional, for containerization)
-
Clone this repository to the local machine:
git clone https://github.com/bmt621/Fashion-Product-Chat-Assistant.git
-
Navigate to the project directory:
cd Fashion-Product-Chat-Assistant -
Create a virtual environment (recommended):
python -m venv venv
-
Activate the virtual environment:
-
On Windows:
venv\Scripts\activate
-
On macOS and Linux:
source venv/bin/activate
-
-
Install the required Python packages:
pip install -r requirements.txt
-
Run the FastAPI application:
python api.py
- The API will be accessible at
http://localhost:8000on your local machine.
- The API will be accessible at
-
To check the status of the API, open your web browser and navigate to:
http://localhost:8000/ -
To search for products, you can use the
/search_productsendpoint. You can make a POST request with a JSON body containing your user query. For example:curl -X POST -H "Content-Type: application/json" -d '{"user_query": "show me shoes for women"}' http://localhost:8000/search_products
Replace
"show me shoes for women"with your desired user query. -
The API will respond with an intelligent product recommendation based on your query and also a chatbot response showing you some product
If you prefer to run the API within a Docker container, the docker container will allows you to easily deploy on any cloud servers of your choice, in this implementation, I deployed the docker on azure cloud. Follow these additional steps to build and run the docker.
-
Build the Docker image from the project directory:
docker build -t product-search-api . -
But if you don't want to wait for the docker container to finish building, you can pull from here 👇
docker pull orbitalsai/fashion-deployed
-
Run the Docker container:
docker run -p 8000:8000 product-search-api
The API will be accessible at http://localhost:8000 within the Docker container on your localhost machine.
This is the API Documentation.
The base URL for this API is http://localhost:8000 when running locally or you can access the public one directly http://20.1.254.170, I also added kubernetes for efficient scaling up.
-
Endpoint:
/ -
HTTP Method: GET
-
Description: Check the status of the API to ensure it's running successfully.
-
Request Example:
GET http://localhost:8000/
or
GET http://20.1.254.170/
-
Response Example:
{ "status": "success" }
-
Endpoint:
/search_products -
HTTP Method: POST
-
Description: Search for products based on a user's natural language query. The API provides intelligent responses with product recommendations.
-
Request Example:
POST http://localhost:8000/search_products
or
POST http://20.1.254.170/search_products
-
Request Body:
{ "user_query": "show me shoes for women" }
-
-
Response Example:
{ "response": "Here are some women's shoe options for you. Feel free to choose the one you like. If you still have trouble finding what you need, please search through the catalogs or leave us an email message.", "retrieved": [ { "id": 18, "category": "Footwear", "name": "Stylish Women's Shoes", "brand": "FashionCo", "color": "Black", "size": "7", "price": 49.99, "description": "Fashionable black women's shoes by FashionCo, available in size 7.", "image_url": "https://example.com/images/fashionco_womens_shoes_black.jpg", "shop_url": "https://example.com/shop/product/18" }, { "id": 22, "category": "Footwear", "name": "Casual Women's Sneakers", "brand": "SportyWear", "color": "White", "size": "6", "price": 39.99, "description": "Comfortable white women's sneakers by SportyWear, available in size 6.", "image_url": "https://example.com/images/sportywear_womens_sneakers_white.jpg", "shop_url": "https://example.com/shop/product/22" } ] }
I implemented route catching errors that will catch any route not available and will return appropriate response, for example
curl -X POST -H "Content-Type: application/json" -d '{"user_query": "show me shoes for women"}' http://localhost:8000/searchwill return this response:
{
"response": "POST Route not found", "retrieved",
"retrieved": null
}-
Ensure that the API is running locally on machine or on public by checking its status at
http://localhost:8000/orhttp://20.1.254.170 -
To search for products, make a POST request to
http://localhost:8000/search_products. Provide a JSON request body with the user's query. -
The API will respond with an intelligent product recommendation based on the user's query. The
responsefield contains the API's reply, and theretrievedfield contains a list of retrieved product recommendations. -
You can modify the user query in the request body to search for different products.
For interactive testing, you can access Swagger UI by navigating to:
http://localhost:8000/docs
or access the public endpoint here 👇
http://20.1.254.170/docs
Swagger provides a user-friendly interface for exploring and interacting with the API endpoints.
The API documentation I created outlines the functionality and usage of the Product Search API, enabling users to search for products and receive intelligent responses based on their queries.
In the realm of intelligent search engines, a common methodology involves harnessing the power of transformer-based large language models. These models enable us to decipher the contextual nuances within sentences. The transformation of each sentence results in a unique embedding vector. In the diagram provided, these embeddings are represented as points, with each point corresponding to a sentence. The proximity of points in this vector space signifies shared context or meaning.
My approach takes this a step further by supplying the model with a comprehensive repository of product information. Each piece of product data is transformed into an embedding, which is then linked to a unique identification number (ID). You can explore a sample of this format in the JSON file located here. When a user submits a query, the model translates the query into a vector and evaluates its similarity to each product's embedding vector. Products whose embeddings closely align with the query's embedding receive higher similarity scores.
To refine our search results and eliminate noise, I employ the cosine similarity metric, augmented with a user-defined threshold. Products surpassing this threshold are deemed pertinent. Once we've identified these relevant catalogs, I employ GPT-3 to craft context-aware responses, enhancing the overall user experience. below is a simple picture depicting how vector similarity works. 
