This project builds a recommendation system for products using LangChain, OpenAI, and Sentence Transformers. The system leverages product features, price, and image data to provide relevant recommendations to users.
- Setup and Installation
- Data Loading and Preprocessing
- Feature Extraction
- Recommendation Agents
- Feature-Based Agent
- Price-Based Agent
- Image-Retrieval-Generaion Agent
- Summary Agent
- Query Routing
- Text and Image Embeddings
- Product Recommendation using Sentence Transformers
- Gradio Interface
- Additional Testing
This section installs the necessary libraries using pip:
langchain,langchain_community,tiktoken,faiss-cpu,gradio,Pillow,transformersIt then imports the required libraries for data processing, machine learning, and LangChain functionality.
- Reads the Amazon cell phone dataset (
DB_Amazon_cellphone_features.csv). - Prepares the data for feature extraction and recommendation.
- Sets up OpenAI API key and models (GPT-4, GPT-3.5) HuggingFace SentenceTransformers, DALL-E, StableDiffusion.
- Defines a function
extract_input_featuresto extract key features from product descriptions using OpenAI's chat models. - Defines a function
recommend_similar_featuresto recommend products with similar features using TF-IDF vectorization and cosine similarity.
- Creates a
features_toolusing LangChain'sToolto recommend products based on features. - Initializes a
recommendation_agentusing LangChain'sinitialize_agentwith thefeatures_tool. - Uses GPT-4 as the language model.
- Defines a function
create_price_agentto create a price-based recommendation tool. - Creates a
price_toolusingcreate_price_agentand Amazon cell phone data. - Initializes a
price_agentusing LangChain'sinitialize_agentwith theprice_tool.
- Defines a function
route_query_to_agentto determine which agent should handle a user query. - Uses GPT-3.5 to route queries based on keywords related to price or features.
- Preprocesses image URLs and extracts image data.
- Generates image and text embeddings using Sentence Transformers.
- Defines functions
search_feature_textandsearch_feature_imageto search for similar products using text and image embeddings.
- Defines a function
recommend_product_featuresto recommend products based on feature similarity. - Creates a LangChain
chainusing GPT-3.5 and a custom prompt template.
- Defines a function
recommend_image_featuresto recommend products based on image similarity.
- Builds a Gradio interface to interact with the recommendation system.
- Allows users to enter queries and view product information, pros, cons, and images.
- Includes various test queries to showcase the capabilities of the recommendation system. User prompt: "Suggest a smartphone with 16GB RAM under $800." Expected Output: Relevant products extracted from features and reviews, also images. User prompt:: "Show me laptops with 512GB SSD." Expected Output: Laptops meeting criteria with features and ratings and generate missing Images.
This README provides a general overview of the code. For more detailed explanations, refer to the comments within the notebook.