Commit 3b6c3e15 authored by BRellu's avatar BRellu

adding the product name param for product reviews

parent 81afdc4b
......@@ -9,6 +9,7 @@ class ProductReview:
# id: Optional[ObjectId] = None
product_id: str = ""
review: Optional[str] = Field(default=None, max_length=5000, description="Review text")
product_Name: Optional[str] = None
rating: float = 0.0
# features: List[Dict[str, FeatureSentiment]] = field(default_factory=list)
features: List[Dict[str, Dict[str, Any]]] = field(default_factory=list)
......@@ -21,17 +21,17 @@ password = urllib.parse.quote_plus(os.getenv("MONGODB_PASSWORD")) # escape spec
cluster = os.getenv("MONGODB_CLUSTER")
appname = os.getenv("MONGODB_APPNAME")
# def get_db_connection(collection_name: str): #LOCAL DB
# client = MongoClient("mongodb://localhost:27017/") # Update with your MongoDB connection
# db = client["productRater"] # Change to your database name
# return db[collection_name] # db["productFeatureRater"] # Change to your collection name
def get_db_connection(collection_name: str): #LOCAL DB
client = MongoClient("mongodb://localhost:27017/") # Update with your MongoDB connection
db = client["productRater"] # Change to your database name
return db[collection_name] # db["productFeatureRater"] # Change to your collection name
def get_db_connection(collection_name: str): # get_db_cloud_connection
mongo_uri = f"mongodb+srv://{username}:{password}@{cluster}/?retryWrites=true&w=majority&appName={appname}"
client = MongoClient(mongo_uri)
db = client["productRater"] # Change to your database name
return db[collection_name]
# def get_db_connection(collection_name: str): # get_db_cloud_connection
# mongo_uri = f"mongodb+srv://{username}:{password}@{cluster}/?retryWrites=true&w=majority&appName={appname}"
# client = MongoClient(mongo_uri)
# db = client["productRater"] # Change to your database name
# return db[collection_name]
def insert_product(product_id): # Use this method to insert if null
......@@ -109,7 +109,7 @@ def add_review(input_data, reviews_by_feature):
# adding New Reviews to productReview Collection
def add_review_features(input_data, reviews_by_feature):
def add_review_features(input_data, reviews_by_feature, productName):
collection = get_db_connection("productReviews")
# Create a dictionary to store features with their corresponding sentiments
......@@ -136,6 +136,7 @@ def add_review_features(input_data, reviews_by_feature):
# Create the ProductReview object
new_review = ProductReview(
product_id=input_data.product_id,
product_Name =productName,
review=input_data.new_review,
rating=input_data.new_rating,
features=features_list
......@@ -149,7 +150,7 @@ def add_review_features(input_data, reviews_by_feature):
# ✅ Add review to vector DB
try:
review_adder = ReviewAdder()
vector_id = review_adder.add_review_vector(input_data.product_id, input_data.new_review)
vector_id = review_adder.add_review_vector(productName, input_data.product_id, input_data.new_review)
print(f"Review also added to Vector DB with ID: {vector_id} ")
except Exception as e:
print(f"Failed to add review to vector DB: {e}")
......@@ -158,10 +159,10 @@ def add_review_features(input_data, reviews_by_feature):
return new_review.__dict__
def product_feature(product_id):
def product_details(product_id):
collection = get_db_connection("productFeatures")
product = collection.find_one({"product_id": product_id}, {"_id": 0, "feature": 1})
product = collection.find_one({"product_id": product_id}, {"_id": 0, "feature": 1, "name":1})
if product and "feature" in product:
return product["feature"]
return {"features":product["feature"], "name":product["name"]}
return []
......@@ -7,7 +7,7 @@ from src.reviewsense_ecom.model.Product import Product
from src.reviewsense_ecom.model.ReviewResponseModel import ReviewResponseModel
from src.reviewsense_ecom.model.product_review_input import ProductReviewInput
from src.reviewsense_ecom.mongo.mongo_db_config import get_product_by_id, update_product, add_review_features, \
insert_product, product_feature
insert_product, product_details
from src.reviewsense_ecom.service.FeatureExtractor import FeatureExtractor
from src.reviewsense_ecom.service.FeatureUpdater import FeatureUpdater
from src.reviewsense_ecom.service.ReviewService import ReviewService
......@@ -89,14 +89,14 @@ async def fetch_review_by_feature(product_id: str, features: str): # Required
async def update_product_data(input_data, product_data):
# ✅ Second Flow: Update Feature Ratings in Another Collection FROM LLM
features = product_feature(input_data.product_id)
productDetails = product_details(input_data.product_id)
reviews_by_feature = feature_extractor.extract_feature_reviews(
input_data.new_review, features)
input_data.new_review, productDetails["features"])
ratings = review_service.fetch_feature_ratings(reviews_by_feature)
logger.info(f"Generated ratings: {ratings}")
# ✅ First Flow: Add Review to a Separate Collection TO REVIEWS
new_review_data = add_review_features(input_data, reviews_by_feature)
new_review_data = add_review_features(input_data, reviews_by_feature, productDetails["name"])
if new_review_data:
logger.info(f"Successfully added review data: {new_review_data}")
else:
......
......@@ -9,6 +9,6 @@ class ReviewAdder:
def __init__(self):
self.vector_store = get_vector_store()
def add_review_vector(self, product_id: str, review: str) -> str:
review_document = Document(page_content=review, metadata={"title": product_id})
def add_review_vector(self, product_name: str, product_id: str, review: str) -> str:
review_document = Document(page_content=review, metadata={"title": product_name, "product_id": product_id})
return self.vector_store.add_documents([review_document])[0]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment