This project is a FastAPI-based service designed to assist users in finding and learning about products. It integrates a LangChain knowledge base with a Milvus vector store and OpenAI GPT model to deliver personalized product recommendations and summaries.
- CSV-Based Knowledge Base: Dynamically loads product data from CSV files in the specified directory.
- Vector Store Integration: Uses Milvus for efficient storage and retrieval of vectorized product data.
- Embedding Models: Utilizes OpenAI embeddings for generating vector representations of product information.
- Agent Functionality: A custom agent, "Product Recommender," interacts with users, retrieves relevant products, and summarizes their descriptions.
- API Endpoint: Accepts user queries via a
/queryendpoint and provides detailed responses in JSON format. - Personalized Recommendations: Offers tailored product suggestions based on user queries and retrieved knowledge.
- FastAPI: Framework for building the REST API.
- Milvus: Vector database for similarity searches.
- Phi Framework: Used for creating the custom agent and integrating knowledge bases.
- LangChain: Facilitates document loading and vectorized queries.
- OpenAI GPT: Leverages the
gpt-4o-minimodel for natural language processing. - Python Dotenv: Manages environment variables.
.
├── app.py # Main API and agent logic
├── your_directory_path/ # Directory containing CSV files with product data
├── requirements.txt # Python dependencies
├── .env # Environment variables (e.g., OpenAI API key)
-
Clone the repository:
git clone https://github.com/your-repo/product-recommender-api.git cd product-recommender-api -
Set up a Python virtual environment:
python3 -m venv venv source venv/bin/activate # For Linux/macOS venv\Scripts\activate # For Windows
-
Install dependencies:
pip install -r requirements.txt
-
Configure Environment Variables: Create a
.envfile in the project root with your OpenAI API key:OPENAI_API_KEY=your_openai_api_key
-
Prepare CSV Data:
- Add product data as CSV files to the
your_directory_path/directory. - Ensure each file follows a consistent format, with headers like
Index,Height,Weight, etc. Update the field names in the code to match your data structure.
- Add product data as CSV files to the
-
Run the FastAPI server:
uvicorn app:app --host 0.0.0.0 --port 8000
-
Query the Agent: Use the
/queryendpoint to interact with the product recommender:- Request Format:
{ "query": "Recommend a lightweight laptop for office use." } - Response Format:
{ "response": "Here are some recommended lightweight laptops for office use: ..." }
- Request Format:
-
Customize Data:
- Modify the
your_directory_pathdirectory and update the CSV loading logic in the code to fit your specific dataset. - Adjust the knowledge base configurations or embedding model as needed.
- Modify the
- Knowledge Base Path: Update the
directory_pathvariable to point to your dataset. - Milvus Vector Store: Modify the
URIfor the database connection as required. - CSV File Format: Ensure the field names in the
csv_argsmatch your dataset schema. - Model and Embeddings: Replace
OpenAIChatandOpenAIEmbeddingswith alternative models or embeddings if needed.
faissfastapiphilangchain_communitylangchain_openailangchain_milvusuvicornpython-dotenv
Install all dependencies with:
pip install -r requirements.txt- Ensure that the Milvus vector store is properly set up and accessible.
- Customize the agent's instructions for specific interaction styles or use cases.
- Extend the system to support additional product categories or data sources.