118 lines
4 KiB
QBasic
118 lines
4 KiB
QBasic
|
|
' Product Classification Tool
|
||
|
|
' Classifies products into categories using AI and store taxonomy
|
||
|
|
|
||
|
|
PARAM product_name AS STRING DESCRIPTION "Product name to classify (e.g., 'nike air max shoes')"
|
||
|
|
PARAM include_suggestions AS BOOLEAN OPTIONAL DESCRIPTION "Include alternative category suggestions"
|
||
|
|
|
||
|
|
DESCRIPTION "Classify a product into the store's category taxonomy using AI"
|
||
|
|
|
||
|
|
' Load store categories for context
|
||
|
|
categories = FIND "Categories.csv"
|
||
|
|
|
||
|
|
' Build taxonomy tree for AI context
|
||
|
|
taxonomy = ""
|
||
|
|
FOR EACH cat IN categories
|
||
|
|
taxonomy = taxonomy + cat.CategoryID + ": " + cat.CategoryName + " - " + cat.Description + "\n"
|
||
|
|
NEXT
|
||
|
|
|
||
|
|
' Search existing products for similar items
|
||
|
|
similar_products = SEARCH "Products.csv", product_name, 5
|
||
|
|
|
||
|
|
similar_context = ""
|
||
|
|
IF similar_products THEN
|
||
|
|
FOR EACH p IN similar_products
|
||
|
|
similar_context = similar_context + "- " + p.ProductName + " (Category: " + p.CategoryID + ")\n"
|
||
|
|
NEXT
|
||
|
|
END IF
|
||
|
|
|
||
|
|
' Use AI to classify the product
|
||
|
|
BEGIN SYSTEM PROMPT
|
||
|
|
You are a product classification expert. Your task is to classify products into the correct category based on the store's taxonomy.
|
||
|
|
|
||
|
|
Available Categories:
|
||
|
|
${taxonomy}
|
||
|
|
|
||
|
|
Similar existing products in catalog:
|
||
|
|
${similar_context}
|
||
|
|
|
||
|
|
Rules:
|
||
|
|
1. Return the most specific category that fits
|
||
|
|
2. Consider product attributes like brand, type, material
|
||
|
|
3. If uncertain, suggest the most likely category with confidence level
|
||
|
|
4. Provide reasoning for the classification
|
||
|
|
END SYSTEM PROMPT
|
||
|
|
|
||
|
|
classification_prompt = "Classify this product: '" + product_name + "'\n\nReturn JSON format: {\"category_id\": number, \"category_name\": string, \"confidence\": number (0-100), \"reasoning\": string, \"attributes\": {\"brand\": string, \"type\": string, \"material\": string}}"
|
||
|
|
|
||
|
|
result = ASK classification_prompt
|
||
|
|
|
||
|
|
' Parse AI response
|
||
|
|
classification = PARSE JSON result
|
||
|
|
|
||
|
|
' Validate category exists
|
||
|
|
valid_category = FIND "Categories.csv", "CategoryID = " + classification.category_id
|
||
|
|
|
||
|
|
IF NOT valid_category THEN
|
||
|
|
' Fallback to closest match
|
||
|
|
TALK "⚠️ AI suggested invalid category, finding closest match..."
|
||
|
|
valid_category = FIND "Categories.csv", "CategoryName LIKE '%" + classification.category_name + "%'"
|
||
|
|
IF valid_category THEN
|
||
|
|
classification.category_id = valid_category.CategoryID
|
||
|
|
END IF
|
||
|
|
END IF
|
||
|
|
|
||
|
|
' Build response
|
||
|
|
response = {}
|
||
|
|
response.product_name = product_name
|
||
|
|
response.category_id = classification.category_id
|
||
|
|
response.category_name = classification.category_name
|
||
|
|
response.confidence = classification.confidence
|
||
|
|
response.reasoning = classification.reasoning
|
||
|
|
response.attributes = classification.attributes
|
||
|
|
|
||
|
|
' Add suggestions if requested
|
||
|
|
IF include_suggestions THEN
|
||
|
|
suggestions_prompt = "Suggest 2 alternative categories for '" + product_name + "' from: " + taxonomy
|
||
|
|
alt_result = ASK suggestions_prompt
|
||
|
|
response.alternative_categories = PARSE JSON alt_result
|
||
|
|
END IF
|
||
|
|
|
||
|
|
' Log classification for analytics
|
||
|
|
LOG "product_classification", {
|
||
|
|
"product": product_name,
|
||
|
|
"category": classification.category_id,
|
||
|
|
"confidence": classification.confidence,
|
||
|
|
"timestamp": NOW()
|
||
|
|
}
|
||
|
|
|
||
|
|
TALK "📦 **Product Classification Result**"
|
||
|
|
TALK ""
|
||
|
|
TALK "**Product:** " + product_name
|
||
|
|
TALK "**Category:** " + classification.category_name + " (ID: " + classification.category_id + ")"
|
||
|
|
TALK "**Confidence:** " + classification.confidence + "%"
|
||
|
|
TALK "**Reasoning:** " + classification.reasoning
|
||
|
|
TALK ""
|
||
|
|
|
||
|
|
IF classification.attributes THEN
|
||
|
|
TALK "**Detected Attributes:**"
|
||
|
|
IF classification.attributes.brand THEN
|
||
|
|
TALK " • Brand: " + classification.attributes.brand
|
||
|
|
END IF
|
||
|
|
IF classification.attributes.type THEN
|
||
|
|
TALK " • Type: " + classification.attributes.type
|
||
|
|
END IF
|
||
|
|
IF classification.attributes.material THEN
|
||
|
|
TALK " • Material: " + classification.attributes.material
|
||
|
|
END IF
|
||
|
|
END IF
|
||
|
|
|
||
|
|
IF include_suggestions AND response.alternative_categories THEN
|
||
|
|
TALK ""
|
||
|
|
TALK "**Alternative Categories:**"
|
||
|
|
FOR EACH alt IN response.alternative_categories
|
||
|
|
TALK " • " + alt.category_name + " (" + alt.confidence + "% confidence)"
|
||
|
|
NEXT
|
||
|
|
END IF
|
||
|
|
|
||
|
|
RETURN response
|