Skip to content

Commit 88c3805

Browse files
author
Pierre
authored
Merge pull request #44 from WorkflowAI/pierre-example-chatbot-ecom
Create 11_ecommerce_chatbot.py
2 parents 6650d48 + 17a0ca3 commit 88c3805

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed

examples/11_ecommerce_chatbot.py

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
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 AssistantMessage(Message):
74+
"""Model representing a message from the assistant."""
75+
role: Role = Role.ASSISTANT
76+
content: str = ""
77+
78+
79+
class ChatbotOutput(BaseModel):
80+
"""Output model for the chatbot response."""
81+
assistant_message: AssistantMessage = Field(
82+
description="The chatbot's response message",
83+
)
84+
85+
86+
class ChatInput(BaseModel):
87+
"""Input model containing the user's message and conversation history."""
88+
conversation_history: Optional[List[Message]] = Field(
89+
default=None,
90+
description="Previous messages in the conversation, if any",
91+
)
92+
user_message: str = Field(
93+
description="The current message from the user",
94+
)
95+
96+
97+
@workflowai.agent(
98+
id="ecommerce-chatbot",
99+
model=Model.LLAMA_3_3_70B,
100+
)
101+
async def get_product_recommendations(chat_input: ChatInput) -> Run[ChatbotOutput]:
102+
"""
103+
Act as a knowledgeable e-commerce shopping assistant.
104+
105+
Guidelines:
106+
1. Understand customer needs and preferences:
107+
- Analyze the query for specific requirements (price range, features, etc.)
108+
- Consider any context from conversation history
109+
- Ask clarifying questions if needed
110+
111+
2. Provide helpful recommendations:
112+
- Suggest 3-5 relevant products that match the criteria
113+
- Include a mix of price points when appropriate
114+
- Explain why each product is recommended
115+
116+
3. Maintain a friendly, professional tone:
117+
- Be conversational but informative
118+
- Highlight key features and benefits
119+
- Acknowledge specific customer needs
120+
121+
4. Product information should be realistic:
122+
- Use reasonable prices for the product category
123+
- Include accurate descriptions and features
124+
- Provide realistic ratings based on typical products
125+
126+
5. Format the response clearly:
127+
- Start with a helpful message addressing the query
128+
- Follow with relevant product recommendations
129+
- Make it easy to understand the options
130+
"""
131+
...
132+
133+
134+
async def main():
135+
# Example 1: Initial query about headphones
136+
print("\nExample 1: Looking for headphones")
137+
print("-" * 50)
138+
139+
chat_input = ChatInput(
140+
user_message="I'm looking for noise-cancelling headphones for travel. My budget is around $300.",
141+
)
142+
143+
run = await get_product_recommendations(chat_input)
144+
print(run)
145+
146+
# Example 2: Follow-up question with conversation history
147+
print("\nExample 2: Follow-up about battery life")
148+
print("-" * 50)
149+
150+
chat_input = ChatInput(
151+
user_message="Which one has the best battery life?",
152+
conversation_history=[
153+
Message(
154+
role=Role.USER,
155+
content="I'm looking for noise-cancelling headphones for travel. My budget is around $300.",
156+
),
157+
run.output.assistant_message,
158+
],
159+
)
160+
161+
run = await get_product_recommendations(chat_input)
162+
print(run)
163+
164+
# Example 3: Specific question about a previously recommended product
165+
print("\nExample 3: Question about a specific product")
166+
print("-" * 50)
167+
168+
chat_input = ChatInput(
169+
user_message="Tell me more about the noise cancellation features of the first headphone you recommended.",
170+
conversation_history=[
171+
Message(
172+
role=Role.USER,
173+
content="I'm looking for noise-cancelling headphones for travel. My budget is around $300.",
174+
),
175+
run.output.assistant_message,
176+
Message(
177+
role=Role.USER,
178+
content="Which one has the best battery life?",
179+
),
180+
run.output.assistant_message,
181+
],
182+
)
183+
184+
run = await get_product_recommendations(chat_input)
185+
print(run)
186+
187+
# Example 4: Different product category
188+
print("\nExample 4: Looking for a TV")
189+
print("-" * 50)
190+
191+
chat_input = ChatInput(
192+
user_message="I need a good TV for gaming. My budget is $1000.",
193+
)
194+
195+
run = await get_product_recommendations(chat_input)
196+
print(run)
197+
198+
199+
if __name__ == "__main__":
200+
asyncio.run(main())

0 commit comments

Comments
 (0)