Table of Contents
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.
|
- BAML
- Python 3.12
- FastAPI
- OpenAI
- Pydantic
- Pandas
- Numpy
- Swagger
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
Please follow these steps for manual setup:
-
Download this GitHub repository.
-
Create a virtual environment.
python3 -m venv <myenvname>
-
Activate virtual environment.
cd venv Scripts\Activate.ps1
Or different Activate script, if you are not working from Visual Code.
-
Install packages from requirements.txt
pip install -r /path/to/requirements.txt
-
Download VS Code extension for BAML
-
Go to any .baml file and click Save (CTRL + S). This will generate python code from .BAML.
-
If the baml_client got generated in the root directory, move it inside app/backend.
-
Inside baml_src > clients.baml, replace your api_key for GPT4oMini model. Also do this for the .env file's
OPENAI_API_KEY
key. -
Run the app:
python main.py
You can find System Design notes in the Product Search.pdf file.
in the .env file, replace these environment variables with your own values.
Name | Default value | Description |
---|---|---|
OPENAI_API_KEY | OpenAI API KEY |
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.
This project is licensed under the MIT license. Feel free to edit and distribute this template as you like.
See LICENSE for more information.