SyedHutter commited on
Commit
8e7aaf2
·
verified ·
1 Parent(s): 6dc2e31

app.py Beta 2 (Push 7)

Browse files
Files changed (1) hide show
  1. app.py +12 -5
app.py CHANGED
@@ -76,22 +76,29 @@ def search_products_by_keywords(keywords: List[str]) -> List[Dict[str, Any]]:
76
  logger.info("No keywords provided, returning empty product list.")
77
  return []
78
  query = {"$or": [{"name": {"$regex": keyword, "$options": "i"}} for keyword in keywords]}
79
- matched_products = [dict(p, id=str(p["_id"])) for p in products_collection.find(query)]
 
 
 
 
 
 
 
 
 
80
  return matched_products
81
 
82
  def get_product_context(products: List[Dict]) -> str:
83
  if not products:
84
  return ""
85
  product_str = "Here are some products: "
86
- # Use .get() to handle missing 'skuNumber' with a fallback
87
- product_str += ", ".join([f"'{p['name']}' (SKU: {p.get('skuNumber', 'N/A')}) - {p['description']}" for p in products[:2]])
88
  return product_str
89
 
90
  def format_response(response: str, products: List[Dict], intent: str) -> str:
91
  if intent in ["recommend_shirt", "recommend_shorts"] and products:
92
  product = products[0]
93
- sku = product.get('skuNumber', 'N/A') # Handle missing skuNumber
94
- return f"{response} For example, check out our '{product['name']}' (SKU: {sku})—it’s {product['description'].lower()}!"
95
  elif intent == "company_info":
96
  return f"{response} At Hutter Products GmbH, we specialize in sustainable product design and production!"
97
  return response
 
76
  logger.info("No keywords provided, returning empty product list.")
77
  return []
78
  query = {"$or": [{"name": {"$regex": keyword, "$options": "i"}} for keyword in keywords]}
79
+ # Convert ObjectId to string and sanitize document
80
+ matched_products = [
81
+ {
82
+ "id": str(p["_id"]),
83
+ "name": p.get("name", "Unknown"),
84
+ "skuNumber": p.get("skuNumber", "N/A"),
85
+ "description": p.get("description", "No description available")
86
+ }
87
+ for p in products_collection.find(query)
88
+ ]
89
  return matched_products
90
 
91
  def get_product_context(products: List[Dict]) -> str:
92
  if not products:
93
  return ""
94
  product_str = "Here are some products: "
95
+ product_str += ", ".join([f"'{p['name']}' (SKU: {p['skuNumber']}) - {p['description']}" for p in products[:2]])
 
96
  return product_str
97
 
98
  def format_response(response: str, products: List[Dict], intent: str) -> str:
99
  if intent in ["recommend_shirt", "recommend_shorts"] and products:
100
  product = products[0]
101
+ return f"{response} For example, check out our '{product['name']}' (SKU: {product['skuNumber']})—it’s {product['description'].lower()}!"
 
102
  elif intent == "company_info":
103
  return f"{response} At Hutter Products GmbH, we specialize in sustainable product design and production!"
104
  return response