As e-commerce continues to evolve, understanding customer intent has become crucial for providing exceptional shopping experiences. In this article, I'll share how we built a sophisticated intent recognition system using Cohere's Command model and Gradio, creating an interactive demo that showcases the power of LLMs in e-commerce.
The Challenge
Traditional e-commerce search and navigation systems often struggle with understanding natural language queries like "I need a waterproof watch that won't break the bank" or "Can you help me find a gift for my tech-savvy dad?". These systems typically rely on keyword matching and predefined rules, missing the nuanced understanding that modern customers expect.
Solution: LLM-Powered Intent Recognition
We've built an interactive demo using Gradio that showcases how Large Language Models (LLMs) can revolutionize e-commerce interactions. Our system:
- Understands complex, natural language queries
- Extracts relevant entities and specifications
- Determines user intent with high accuracy
- Provides detailed analysis in real-time
Technical Implementation
1. Core Intent Recognition System
@dataclass
class IntentResponse:
intent: str
confidence: float
entities: Dict
suggested_action: str
explanation: str
class EcommerceLLMIntentRecognizer:
def __init__(self):
api_key = os.getenv('COHERE_API_KEY')
self.co = cohere.Client(api_key)
self.valid_intents = {
'product_search': 'SEARCH_CATALOG',
'price_inquiry': 'FETCH_PRICE',
'order_status': 'CHECK_ORDER_STATUS',
'return_request': 'INITIATE_RETURN',
'cart_management': 'MODIFY_CART',
'availability_check': 'CHECK_INVENTORY',
'checkout_help': 'ASSIST_CHECKOUT',
'shipping_info': 'PROVIDE_SHIPPING_INFO'
}
2. LLM Prompt Engineering
The key to accurate intent recognition lies in our carefully crafted prompt:
def _generate_prompt(self, query: str) -> str:
return f"""As an e-commerce AI assistant, analyze the following customer query and extract the shopping intent, relevant entities, and determine the appropriate action.
Valid intents are: {', '.join(self.valid_intents.keys())}
Customer Query: "{query}"
Provide your analysis in the following JSON format:
{{
"intent": "the_identified_intent",
"confidence": 0.XX,
"entities": {{
"product": "identified_product",
"category": "product_category",
"specifications": ["any", "relevant", "specs"],
"price_range": {{
"min": "if_mentioned",
"max": "if_mentioned"
}}
}},
"explanation": "Brief explanation of why this intent was chosen"
}}"""
3. Interactive Demo with Gradio
One of the most powerful aspects of our implementation is the interactive demo built with Gradio:
def process_query(user_query: str) -> str:
try:
recognizer = EcommerceLLMIntentRecognizer()
response = recognizer.recognize_intent(user_query)
return json.dumps({
'timestamp': datetime.now().isoformat(),
'query': user_query,
'intent': response.intent,
'confidence': response.confidence,
'entities': response.entities,
'suggested_action': response.suggested_action,
'explanation': response.explanation
}, indent=2)
except ValueError as e:
return json.dumps({
'error': str(e),
'hint': 'Please ensure COHERE_API_KEY is set in your .env file'
}, indent=2)
# Create Gradio interface
iface = gr.Interface(
fn=process_query,
inputs=gr.Textbox(label="Enter customer query"),
outputs=gr.JSON(label="Intent Analysis"),
title="E-commerce LLM Intent Recognition System",
description="Enter your query to see the detailed intent analysis.",
examples=[
["I'm looking for a waterproof smart watch under $300"],
["Can you compare the iPhone 13 and iPhone 14 Pro?"],
["Need to return my order #ABC123, it's the wrong size"],
["Do you have this dress in size medium and in red?"],
["What's your shipping time to California?"]
]
)
Real-World Examples
Let's look at some example queries and their analysis:
1. Product Search with Specifications
Query: "I need a waterproof smartwatch under $300"
Response:
{
"intent": "product_search",
"confidence": 0.95,
"entities": {
"product": "smartwatch",
"specifications": ["waterproof"],
"price_range": {
"max": 300
}
},
"explanation": "Query specifies a product type with specific features and price constraint"
}
2. Order Status Check
Query: "Where's my order #ABC123?"
Response:
{
"intent": "order_status",
"confidence": 0.98,
"entities": {
"order_id": "ABC123"
},
"explanation": "Direct question about order status with order number"
}
Benefits of Our Approach
1. Interactive Demo
- Instant feedback through Gradio interface
- Easy to test different queries
- Visual JSON output for clear understanding
2. Developer-Friendly
- Simple setup process
- Clear code structure
- Easy to extend and modify
3. Business Value
- Better customer understanding
- Improved search accuracy
- Automated query classification
Implementation Guide
Want to try it yourself? Here's how:
1. Install Requirements
pip install cohere gradio python-dotenv
2. Set Up Environment
Create a .env file:
COHERE_API_KEY=your_api_key_here
3. Run the Demo
python app.py
Future Enhancements
1. Enhanced Analysis
- Sentiment analysis
- Multiple intent detection
- Confidence scoring improvements
2. UI Improvements
- Visual intent mapping
- Real-time suggestions
- Mobile optimization
3. Integration Capabilities
- API endpoint creation
- Webhook support
- Database integration
Conclusion
By combining the power of Cohere's LLM with Gradio's user-friendly interface, we've created a powerful tool for understanding e-commerce customer intentions. This demo showcases how modern AI can transform the way we interact with e-commerce platforms.
The application is running as a Hugging Face Space over here. Feel free to reach out if you have questions or want to discuss implementing similar solutions for your e-commerce platform.
Author's Note: I'm passionate about building practical AI solutions that bridge the gap between advanced technology and real-world applications. Connect with me to discuss more about AI in e-commerce or to explore collaboration opportunities.
Top comments (0)