3v324v23 commited on
Commit
a9ac143
·
1 Parent(s): 6e5fd41

Ultra-optimize AI and Scraper for niche items (needles, toothpicks, etc.) and product discovery logic

Browse files
server/src/controllers/search.controller.js CHANGED
@@ -83,10 +83,15 @@ async function handleSearch(req, res, next) {
83
  const checkSatisfaction = () => {
84
  if (!intent.isSpecific || !intent.specificItem) return true;
85
  const item = intent.specificItem.toLowerCase();
86
- return data.results.some(r =>
87
- r.name.toLowerCase().includes(item) ||
88
- (r.features && r.features.some(f => f.toLowerCase().includes(item)))
89
- );
 
 
 
 
 
90
  };
91
 
92
  const isDeeplySatisfied = checkSatisfaction();
 
83
  const checkSatisfaction = () => {
84
  if (!intent.isSpecific || !intent.specificItem) return true;
85
  const item = intent.specificItem.toLowerCase();
86
+ return data.results.some(r => {
87
+ const searchSpace = [
88
+ r.name,
89
+ ...(r.features || []),
90
+ r.rawCategory || '',
91
+ r.reviewSummary || ''
92
+ ].join(' ').toLowerCase();
93
+ return searchSpace.includes(item);
94
+ });
95
  };
96
 
97
  const isDeeplySatisfied = checkSatisfaction();
server/src/services/ai.service.js CHANGED
@@ -24,24 +24,28 @@ async function parseIntent(query, clarificationContext = null) {
24
  .replace(/\b(near me|around me|close to me|nearby|here)\b/gi, '')
25
  .trim();
26
 
27
- let systemPrompt = `You are a query intent parser for a location-based recommendation engine.
28
- Extract structured intent from user queries. Return ONLY valid JSON with these fields:
 
 
29
  {
30
- "reasoning": "string (Short one-sentence explanation of how you interpreted this query)",
31
- "isOutOfScope": "boolean (true if the query is NOT about finding a place, service, or specific product/brand. e.g., 'how to cook', 'who is the president', 'solve 2+2')",
32
- "scopeMessage": "string or null (If isOutOfScope is true, provide a polite response explaining that RedThread is a specialized discovery engine for locations, services, and local products.)",
33
- "needsClarification": "boolean (true ONLY if the query is a single ambiguous word like 'apple' or 'monster' and you have NO idea what it means. false if it's a multi-word category like 'test labs' or 'coffee shop')",
34
- "clarificationQuestion": "string or null (If needsClarification is true, provide a conversational follow-up question.)",
35
- "category": "string (The broad type of place, e.g., shop, restaurant, electronics_store, medical_lab, coworking_space)",
36
- "isSpecific": "boolean (true if the user is looking for a VERY specific brand, product, or niche item)",
37
- "specificItem": "string or null (The exact name of the brand/product/item requested)",
38
- "location": "string or null (Extract ONLY the major city name. Do NOT extract words like 'me', 'here', or 'nearby'. If no city is found, return null. DO NOT set needsClarification=true just because the location is missing.)",
39
- "neighborhood": "string or null (Extract specific area if mentioned)",
40
  "budget": { "min": number|null, "max": number|null, "currency": "string" },
41
- "features": ["array of desired features"],
42
  "occasion": "string or null",
43
  "sortBy": "string (rating|price|distance|relevance)"
44
- } - Note: If the query is multi-word (e.g. 'test labs'), match it to the closest category and set needsClarification: false. If it is completely unrelated to discovery, set isOutOfScope: true.`;
 
 
45
 
46
  if (clarificationContext) {
47
  systemPrompt += `\n\nCRITICAL CONTEXT: The user previously searched for "${clarificationContext.originalQuery}", and you asked them: "${clarificationContext.question}". The user answered: "${clarificationContext.answer}".
 
24
  .replace(/\b(near me|around me|close to me|nearby|here)\b/gi, '')
25
  .trim();
26
 
27
+ let systemPrompt = `You are a high-intelligence discovery engine. Your goal is to map ANY user query to a physical location, service, or product source.
28
+ Even for tiny items (e.g., "needle", "toothpick", "string"), your job is to find the most logical place where they are sold.
29
+
30
+ Return ONLY valid JSON:
31
  {
32
+ "reasoning": "string (Why you picked this category for this item)",
33
+ "isOutOfScope": "boolean (false for almost everything except pure knowledge queries like 'who is Newton' or 'math')",
34
+ "scopeMessage": "string or null",
35
+ "needsClarification": "boolean (ONLY true if the query is a single nonsense word. If it's a real word like 'string', pick a 'stationary' or 'hardware' category and proceed.)",
36
+ "clarificationQuestion": "string or null",
37
+ "category": "string (The BEST physical store type, e.g., stationary, pharmacy, hardware_store, department_store, grocery, electronics)",
38
+ "isSpecific": "boolean (Always true if the user mentions a specific product or tiny item beyond a general place name)",
39
+ "specificItem": "string or null (The exact item, e.g., 'needle')",
40
+ "location": "string or null (Extract city name. Use null if missing.)",
41
+ "neighborhood": "string or null",
42
  "budget": { "min": number|null, "max": number|null, "currency": "string" },
43
+ "features": ["array of likely features for this search"],
44
  "occasion": "string or null",
45
  "sortBy": "string (rating|price|distance|relevance)"
46
+ }
47
+
48
+ Thinking logic: If someone wants a "needle", they likely need a "stationary" or "tailoring_shop". If they want "monster", they want a "supermarket" or "convenience_store". Map the item to the STORES that likely carry it.`;
49
 
50
  if (clarificationContext) {
51
  systemPrompt += `\n\nCRITICAL CONTEXT: The user previously searched for "${clarificationContext.originalQuery}", and you asked them: "${clarificationContext.question}". The user answered: "${clarificationContext.answer}".