in backend-apis/app/routers/p1_customer.py [0:0]
def get_reviews_summary(product_id: int) -> GetReviewsSummaryResponse:
"""
# Get reviews summary
## Path parameters
**product_id**: *string*
- Product id to get the reviews
## Response body [GetReviewsSummaryResponse]
**reviews_summary**: *string*
- Reviews summary for the product
## Raises
**HTTPException** - *400* - Cloud SQL Error
- Error connecting to Cloud SQL
**HTTPException** - *404* - Product not found
- Product was not found in the Cloud SQL database
**HTTPException** - 400 - Error getting reviews
- Firestore could not return the reviews
**HTTPException** - 400 - Error generating reviews summary
- PaLM could not generate the summary
"""
try:
product = utils_cloud_sql.get_product(product_id)
except Exception as e:
raise HTTPException(
status_code=400, detail="Cloud SQL error." + str(e)
) from e
if not product:
raise HTTPException(status_code=404, detail="Product not found.")
product_dict = utils_cloud_sql.convert_product_to_dict(product)
try:
reviews: list[GetReviewsResponse.ReviewDoc] = [
review_snapshot.to_dict()
for review_snapshot in db.collection("website_reviews")
.document(str(product_id))
.collection("reviews")
.get()
]
except GoogleAPICallError as e:
raise HTTPException(
status_code=400, detail="Error getting reviews " + str(e)
) from e
summary = "No reviews yet."
if reviews:
reviews_json = json.dumps(
{
"product": product_dict,
"reviews": reviews,
}
)
try:
summary = utils_gemini.generate_gemini_pro_text(
prompt=config["summary"]["prompt_reviews"].format(
reviews=reviews_json
),
max_output_tokens=1024,
temperature=0.2,
top_k=40,
top_p=0.8,
)
except GoogleAPICallError as e:
raise HTTPException(
status_code=400,
detail="Error generating reviews summary" + str(e),
) from e
return GetReviewsSummaryResponse(reviews_summary=summary)