Skip to content

GintasS/FastAPI-product-search-using-ChatGPT-and-vector-embeddings

Repository files navigation

FastAPI product search using ChatGPT-4o mini and vector embeddings



Table of Contents

About

This project is a backend system built with FastAPI, designed to enable product search using natural language queries. It offers endpoints for searching products by description and price. The solution integrates the ChatGPT-4.0 mini model for prompt engineering tasks, including categorizing user inputs, extracting product descriptions, and retrieving price details. Additionally, it utilizes the text-embedding-ada-002 model to generate semantic embeddings for more accurate and efficient product matching.

The searchable products are in a product_embeddings.csv file with pre-computed embeddings. These are stored in a pandas dataframe, in-memory.
The embedding for each product includes:

  • product title
  • product description
  • product vendor
  • product type
  • product tags

Built With

  • BAML
  • Python 3.12
  • FastAPI
  • OpenAI
  • Pydantic
  • Pandas
  • Numpy
  • Swagger

API Endpoints

These endpoints allow users to retrieve product recommendations based on descriptions or filter products by price range:

POST /v1/chat/message/

  • Description: Retrieve a product recommendation from a description and/or filter products by a price range.
  • Request Model: CreateChatMessageRequest
  • Response Model: CreateChatMessageResponse

POST /v1/chat/product/description/

  • Description: Recommend other products by a product description.
  • Request Model: CreateChatMessageRequest
  • Response Model: CreateChatMessageResponse

POST /v1/chat/product/price/

  • Description: Filter products by a price range.
  • Request Model: GetProductsInPriceRangeRequest
  • Response Model: CreateChatMessageResponse

Usage

Manual setup

Please follow these steps for manual setup:

  1. Download this GitHub repository.

  2. Create a virtual environment.

    python3 -m venv <myenvname>
    
  3. Activate virtual environment.

    cd venv
    Scripts\Activate.ps1
    

    Or different Activate script, if you are not working from Visual Code.

  4. Install packages from requirements.txt

    pip install -r /path/to/requirements.txt
    
  5. Download VS Code extension for BAML

  6. Go to any .baml file and click Save (CTRL + S). This will generate python code from .BAML.

  7. If the baml_client got generated in the root directory, move it inside app/backend.

  8. Inside baml_src > clients.baml, replace your api_key for GPT4oMini model. Also do this for the .env file's OPENAI_API_KEY key.

  9. Run the app:

    python main.py
    

System Design

You can find System Design notes in the Product Search.pdf file.

Environment variables

in the .env file, replace these environment variables with your own values.

Name Default value Description
OPENAI_API_KEY OpenAI API KEY

Screenshots


The main Swagger view.


The main endpoint for recommending products and filtering price.


The response from the main endpoint that both recommends products and can filter by price.


The second endpoint for recommending products by product description only.


The third endpoint for filtering products by the price only.


The response from the third endpoint that filters products by the price only.


The example of how a prompt to ChatGPT can look like.

Video demo

License

This project is licensed under the MIT license. Feel free to edit and distribute this template as you like.

See LICENSE for more information.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages