-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path11_ecommerce_chatbot.py
More file actions
153 lines (120 loc) · 4.45 KB
/
11_ecommerce_chatbot.py
File metadata and controls
153 lines (120 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
This example demonstrates how to create an e-commerce chatbot that:
1. Understands customer queries about products
2. Provides helpful responses with product recommendations
3. Maintains context through conversation using .reply
4. Returns structured product recommendations
"""
import asyncio
from typing import Optional
from pydantic import BaseModel, Field
import workflowai
from workflowai import Model
class Product(BaseModel):
"""Model representing a product recommendation."""
name: str = Field(
description="Name of the product",
examples=["Wireless Noise-Cancelling Headphones", "4K Smart TV"],
)
price: float = Field(
description="Price of the product in USD",
examples=[299.99, 799.99],
ge=0,
)
description: str = Field(
description="Brief description of the product",
examples=[
"Premium wireless headphones with active noise cancellation",
"65-inch 4K Smart TV with HDR support",
],
)
rating: Optional[float] = Field(
default=None,
description="Customer rating out of 5 stars",
examples=[4.5, 4.8],
ge=0,
le=5,
)
url: Optional[str] = Field(
default=None,
description="URL to view the product details",
examples=["https://example.com/products/wireless-headphones"],
)
class AssistantMessage(BaseModel):
"""Model representing a message from the assistant."""
content: str = Field(
description="The content of the message",
)
recommended_products: Optional[list[Product]] = Field(
default=None,
description="Product recommendations included with this message, if any",
)
class ChatbotOutput(BaseModel):
"""Output model for the chatbot response."""
assistant_message: AssistantMessage = Field(
description="The chatbot's response message",
)
class ChatInput(BaseModel):
"""Input model containing the user's message."""
user_message: str = Field(
description="The current message from the user",
)
@workflowai.agent(
id="ecommerce-chatbot",
model=Model.LLAMA_3_3_70B,
)
async def get_product_recommendations(chat_input: ChatInput) -> ChatbotOutput:
"""
Act as a knowledgeable e-commerce shopping assistant.
Guidelines:
1. Understand customer needs and preferences:
- Analyze the query for specific requirements (price range, features, etc.)
- Consider any context from conversation history
- Ask clarifying questions if needed
2. Provide helpful recommendations:
- Suggest 3-5 relevant products that match the criteria
- Include a mix of price points when appropriate
- Explain why each product is recommended
3. Maintain a friendly, professional tone:
- Be conversational but informative
- Highlight key features and benefits
- Acknowledge specific customer needs
4. Product information should be realistic:
- Use reasonable prices for the product category
- Include accurate descriptions and features
- Provide realistic ratings based on typical products
5. Format the response clearly:
- Start with a helpful message addressing the query
- Follow with relevant product recommendations
- Make it easy to understand the options
"""
...
async def main():
# Example 1: Initial query about headphones
print("\nExample 1: Looking for headphones")
print("-" * 50)
run = await get_product_recommendations.run(
ChatInput(user_message="I'm looking for noise-cancelling headphones for travel. My budget is around $300."),
)
print(run)
# Example 2: Follow-up question using reply
print("\nExample 2: Follow-up about battery life")
print("-" * 50)
run = await run.reply(user_message="Which one has the best battery life?")
print(run)
# Example 3: Specific question about a previously recommended product
print("\nExample 3: Question about a specific product")
print("-" * 50)
run = await run.reply(
user_message=(
"Tell me more about the noise cancellation features of the first headphone you recommended."
),
)
print(run)
# Example 4: Different product category
print("\nExample 4: Looking for a TV")
print("-" * 50)
run = await run.reply(user_message="I need a good TV for gaming. My budget is $1000.")
print(run)
if __name__ == "__main__":
asyncio.run(main())