125 lines
3.9 KiB
QBasic
125 lines
3.9 KiB
QBasic
|
|
' Product Search with Autocomplete and AI Descriptions
|
||
|
|
' Provides fast product search with intelligent suggestions and enriched descriptions
|
||
|
|
|
||
|
|
PARAM query AS STRING DESCRIPTION "Search query (e.g., 'chai tea', 'chocolate', 'sauce')"
|
||
|
|
PARAM limit AS INTEGER OPTIONAL DESCRIPTION "Maximum results to return (default: 10)"
|
||
|
|
PARAM include_description AS BOOLEAN OPTIONAL DESCRIPTION "Generate AI-enhanced product descriptions"
|
||
|
|
|
||
|
|
DESCRIPTION "Search products with autocomplete and AI-generated descriptions"
|
||
|
|
|
||
|
|
' Set defaults
|
||
|
|
IF NOT limit THEN
|
||
|
|
limit = 10
|
||
|
|
END IF
|
||
|
|
|
||
|
|
' Perform fast search across products
|
||
|
|
results = SEARCH "Products.csv", query, limit
|
||
|
|
|
||
|
|
IF NOT results OR LEN(results) = 0 THEN
|
||
|
|
TALK "🔍 No products found matching '" + query + "'"
|
||
|
|
|
||
|
|
' Use AI to suggest alternatives
|
||
|
|
BEGIN SYSTEM PROMPT
|
||
|
|
You are a helpful store assistant. The user searched for a product that wasn't found.
|
||
|
|
Suggest what they might be looking for or alternative search terms.
|
||
|
|
END SYSTEM PROMPT
|
||
|
|
|
||
|
|
suggestion = ASK "The user searched for '" + query + "' but no products were found. Suggest alternatives or clarify what they might want."
|
||
|
|
TALK suggestion
|
||
|
|
|
||
|
|
RETURN { "results": [], "suggestions": suggestion }
|
||
|
|
END IF
|
||
|
|
|
||
|
|
' Load categories for enrichment
|
||
|
|
categories = FIND "Categories.csv"
|
||
|
|
|
||
|
|
' Build category lookup
|
||
|
|
category_map = {}
|
||
|
|
FOR EACH cat IN categories
|
||
|
|
category_map[cat.CategoryID] = cat.CategoryName
|
||
|
|
NEXT
|
||
|
|
|
||
|
|
' Enrich results with category names
|
||
|
|
enriched_results = []
|
||
|
|
FOR EACH product IN results
|
||
|
|
item = {}
|
||
|
|
item.id = product.ProductID
|
||
|
|
item.name = product.ProductName
|
||
|
|
item.price = product.UnitPrice
|
||
|
|
item.stock = product.UnitsInStock
|
||
|
|
item.category_id = product.CategoryID
|
||
|
|
item.category_name = category_map[product.CategoryID]
|
||
|
|
item.quantity_per_unit = product.QuantityPerUnit
|
||
|
|
item.discontinued = product.Discontinued
|
||
|
|
item.reorder_level = product.ReorderLevel
|
||
|
|
|
||
|
|
' Generate AI description if requested
|
||
|
|
IF include_description THEN
|
||
|
|
BEGIN SYSTEM PROMPT
|
||
|
|
You are a product copywriter. Write a brief, compelling product description (2-3 sentences).
|
||
|
|
Focus on benefits and key features. Be concise and engaging.
|
||
|
|
END SYSTEM PROMPT
|
||
|
|
|
||
|
|
desc_prompt = "Write a product description for: " + product.ProductName + " (Category: " + item.category_name + ", Quantity: " + product.QuantityPerUnit + ", Price: $" + product.UnitPrice + ")"
|
||
|
|
item.ai_description = ASK desc_prompt
|
||
|
|
END IF
|
||
|
|
|
||
|
|
PUSH enriched_results, item
|
||
|
|
NEXT
|
||
|
|
|
||
|
|
' Build autocomplete suggestions for future searches
|
||
|
|
related_terms = []
|
||
|
|
FOR EACH product IN results
|
||
|
|
words = SPLIT(product.ProductName, " ")
|
||
|
|
FOR EACH word IN words
|
||
|
|
IF LEN(word) > 3 AND NOT CONTAINS(related_terms, word) THEN
|
||
|
|
PUSH related_terms, word
|
||
|
|
END IF
|
||
|
|
NEXT
|
||
|
|
NEXT
|
||
|
|
|
||
|
|
' Display results
|
||
|
|
TALK "🔍 **Search Results for '" + query + "'** (" + LEN(enriched_results) + " found)"
|
||
|
|
TALK ""
|
||
|
|
|
||
|
|
FOR EACH item IN enriched_results
|
||
|
|
stock_status = "✅ In Stock"
|
||
|
|
IF item.stock = 0 THEN
|
||
|
|
stock_status = "❌ Out of Stock"
|
||
|
|
ELSE IF item.stock < item.reorder_level THEN
|
||
|
|
stock_status = "⚠️ Low Stock"
|
||
|
|
END IF
|
||
|
|
|
||
|
|
IF item.discontinued = 1 THEN
|
||
|
|
stock_status = "🚫 Discontinued"
|
||
|
|
END IF
|
||
|
|
|
||
|
|
TALK "**" + item.name + "** - $" + item.price
|
||
|
|
TALK " Category: " + item.category_name + " | " + item.quantity_per_unit
|
||
|
|
TALK " " + stock_status + " (" + item.stock + " units)"
|
||
|
|
|
||
|
|
IF include_description AND item.ai_description THEN
|
||
|
|
TALK " 📝 " + item.ai_description
|
||
|
|
END IF
|
||
|
|
TALK ""
|
||
|
|
NEXT
|
||
|
|
|
||
|
|
' Show related search terms
|
||
|
|
IF LEN(related_terms) > 0 THEN
|
||
|
|
TALK "**Related searches:** " + JOIN(related_terms, ", ")
|
||
|
|
END IF
|
||
|
|
|
||
|
|
' Log search for analytics
|
||
|
|
LOG "product_search", {
|
||
|
|
"query": query,
|
||
|
|
"results_count": LEN(enriched_results),
|
||
|
|
"timestamp": NOW()
|
||
|
|
}
|
||
|
|
|
||
|
|
RETURN {
|
||
|
|
"query": query,
|
||
|
|
"results": enriched_results,
|
||
|
|
"total": LEN(enriched_results),
|
||
|
|
"related_terms": related_terms
|
||
|
|
}
|