Skip to content

Commit 375ac5d

Browse files
author
Pierre
committed
Create 11_ecommerce_chatbot.py
1 parent ced63fe commit 375ac5d

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed

examples/11_ecommerce_chatbot.py

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
"""
2+
This example demonstrates how to create an e-commerce chatbot that:
3+
1. Understands customer queries about products
4+
2. Provides helpful responses with product recommendations
5+
3. Maintains context through conversation
6+
4. Returns structured product recommendations
7+
"""
8+
9+
import asyncio
10+
from enum import Enum
11+
from typing import List, Optional
12+
13+
from pydantic import BaseModel, Field
14+
15+
import workflowai
16+
from workflowai import Model, Run
17+
18+
19+
class Role(str, Enum):
20+
"""Enum representing possible message roles."""
21+
USER = "user"
22+
ASSISTANT = "assistant"
23+
24+
25+
class Product(BaseModel):
26+
"""Model representing a product recommendation."""
27+
name: str = Field(
28+
description="Name of the product",
29+
examples=["Wireless Noise-Cancelling Headphones", "4K Smart TV"],
30+
)
31+
price: float = Field(
32+
description="Price of the product in USD",
33+
examples=[299.99, 799.99],
34+
ge=0,
35+
)
36+
description: str = Field(
37+
description="Brief description of the product",
38+
examples=[
39+
"Premium wireless headphones with active noise cancellation",
40+
"65-inch 4K Smart TV with HDR support",
41+
],
42+
)
43+
rating: Optional[float] = Field(
44+
default=None,
45+
description="Customer rating out of 5 stars",
46+
examples=[4.5, 4.8],
47+
ge=0,
48+
le=5,
49+
)
50+
url: Optional[str] = Field(
51+
default=None,
52+
description="URL to view the product details",
53+
examples=["https://example.com/products/wireless-headphones"],
54+
)
55+
56+
57+
class Message(BaseModel):
58+
"""Model representing a chat message."""
59+
role: Role = Field()
60+
content: str = Field(
61+
description="The content of the message",
62+
examples=[
63+
"I'm looking for noise-cancelling headphones for travel",
64+
"Based on your requirements, here are some great headphone options...",
65+
],
66+
)
67+
recommended_products: Optional[List[Product]] = Field(
68+
default=None,
69+
description="Product recommendations included with this message, if any",
70+
)
71+
72+
73+
class ChatbotOutput(BaseModel):
74+
"""Output model for the chatbot response."""
75+
assistant_message: Message = Field(
76+
description="The chatbot's response message",
77+
)
78+
79+
80+
class ChatInput(BaseModel):
81+
"""Input model containing the user's message and conversation history."""
82+
conversation_history: Optional[List[Message]] = Field(
83+
default=None,
84+
description="Previous messages in the conversation, if any",
85+
)
86+
user_message: str = Field(
87+
description="The current message from the user",
88+
examples=[
89+
"I'm looking for noise-cancelling headphones for travel",
90+
"What's the best TV under $1000?",
91+
],
92+
)
93+
94+
95+
@workflowai.agent(
96+
id="ecommerce-chatbot",
97+
model=Model.LLAMA_3_3_70B,
98+
)
99+
async def get_product_recommendations(chat_input: ChatInput) -> Run[ChatbotOutput]:
100+
"""
101+
Act as a knowledgeable e-commerce shopping assistant.
102+
103+
Guidelines:
104+
1. Understand customer needs and preferences:
105+
- Analyze the query for specific requirements (price range, features, etc.)
106+
- Consider any context from conversation history
107+
- Ask clarifying questions if needed
108+
109+
2. Provide helpful recommendations:
110+
- Suggest 3-5 relevant products that match the criteria
111+
- Include a mix of price points when appropriate
112+
- Explain why each product is recommended
113+
114+
3. Maintain a friendly, professional tone:
115+
- Be conversational but informative
116+
- Highlight key features and benefits
117+
- Acknowledge specific customer needs
118+
119+
4. Product information should be realistic:
120+
- Use reasonable prices for the product category
121+
- Include accurate descriptions and features
122+
- Provide realistic ratings based on typical products
123+
124+
5. Format the response clearly:
125+
- Start with a helpful message addressing the query
126+
- Follow with relevant product recommendations
127+
- Make it easy to understand the options
128+
"""
129+
...
130+
131+
132+
async def main():
133+
# Example 1: Initial query about headphones
134+
print("\nExample 1: Looking for headphones")
135+
print("-" * 50)
136+
137+
chat_input = ChatInput(
138+
user_message="I'm looking for noise-cancelling headphones for travel. My budget is around $300.",
139+
)
140+
141+
run = await get_product_recommendations(chat_input)
142+
print(run)
143+
144+
# Example 2: Follow-up question with conversation history
145+
print("\nExample 2: Follow-up about battery life")
146+
print("-" * 50)
147+
148+
chat_input = ChatInput(
149+
user_message="Which one has the best battery life?",
150+
conversation_history=[
151+
Message(
152+
role=Role.USER,
153+
content="I'm looking for noise-cancelling headphones for travel. My budget is around $300.",
154+
),
155+
run.output.assistant_message,
156+
],
157+
)
158+
159+
run = await get_product_recommendations(chat_input)
160+
print(run)
161+
162+
# Example 3: Specific question about a previously recommended product
163+
print("\nExample 3: Question about a specific product")
164+
print("-" * 50)
165+
166+
chat_input = ChatInput(
167+
user_message="Tell me more about the noise cancellation features of the first headphone you recommended.",
168+
conversation_history=[
169+
Message(
170+
role=Role.USER,
171+
content="I'm looking for noise-cancelling headphones for travel. My budget is around $300.",
172+
),
173+
run.output.assistant_message,
174+
Message(
175+
role=Role.USER,
176+
content="Which one has the best battery life?",
177+
),
178+
run.output.assistant_message,
179+
],
180+
)
181+
182+
run = await get_product_recommendations(chat_input)
183+
print(run)
184+
185+
# Example 4: Different product category
186+
print("\nExample 4: Looking for a TV")
187+
print("-" * 50)
188+
189+
chat_input = ChatInput(
190+
user_message="I need a good TV for gaming. My budget is $1000.",
191+
)
192+
193+
run = await get_product_recommendations(chat_input)
194+
print(run)
195+
196+
197+
if __name__ == "__main__":
198+
asyncio.run(main())

0 commit comments

Comments
 (0)