22This example demonstrates how to create an e-commerce chatbot that:
331. Understands customer queries about products
442. Provides helpful responses with product recommendations
5- 3. Maintains context through conversation
5+ 3. Maintains context through conversation using .reply
664. Returns structured product recommendations
77"""
88
99import asyncio
10- from enum import Enum
1110from typing import Optional
1211
1312from pydantic import BaseModel , Field
1413
1514import workflowai
16- from workflowai import Model , Run
17-
18-
19- class Role (str , Enum ):
20- """Enum representing possible message roles."""
21-
22- USER = "user"
23- ASSISTANT = "assistant"
15+ from workflowai import Model
2416
2517
2618class Product (BaseModel ):
@@ -56,30 +48,18 @@ class Product(BaseModel):
5648 )
5749
5850
59- class Message (BaseModel ):
60- """Model representing a chat message."""
51+ class AssistantMessage (BaseModel ):
52+ """Model representing a message from the assistant ."""
6153
62- role : Role = Field ()
6354 content : str = Field (
6455 description = "The content of the message" ,
65- examples = [
66- "I'm looking for noise-cancelling headphones for travel" ,
67- "Based on your requirements, here are some great headphone options..." ,
68- ],
6956 )
7057 recommended_products : Optional [list [Product ]] = Field (
7158 default = None ,
7259 description = "Product recommendations included with this message, if any" ,
7360 )
7461
7562
76- class AssistantMessage (Message ):
77- """Model representing a message from the assistant."""
78-
79- role : Role = Role .ASSISTANT
80- content : str = ""
81-
82-
8363class ChatbotOutput (BaseModel ):
8464 """Output model for the chatbot response."""
8565
@@ -89,12 +69,8 @@ class ChatbotOutput(BaseModel):
8969
9070
9171class ChatInput (BaseModel ):
92- """Input model containing the user's message and conversation history ."""
72+ """Input model containing the user's message."""
9373
94- conversation_history : Optional [list [Message ]] = Field (
95- default = None ,
96- description = "Previous messages in the conversation, if any" ,
97- )
9874 user_message : str = Field (
9975 description = "The current message from the user" ,
10076 )
@@ -104,7 +80,7 @@ class ChatInput(BaseModel):
10480 id = "ecommerce-chatbot" ,
10581 model = Model .LLAMA_3_3_70B ,
10682)
107- async def get_product_recommendations (chat_input : ChatInput ) -> Run [ ChatbotOutput ] :
83+ async def get_product_recommendations (chat_input : ChatInput ) -> ChatbotOutput :
10884 """
10985 Act as a knowledgeable e-commerce shopping assistant.
11086
@@ -142,63 +118,34 @@ async def main():
142118 print ("\n Example 1: Looking for headphones" )
143119 print ("-" * 50 )
144120
145- chat_input = ChatInput (
146- user_message = "I'm looking for noise-cancelling headphones for travel. My budget is around $300." ,
121+ run = await get_product_recommendations . run (
122+ ChatInput ( user_message = "I'm looking for noise-cancelling headphones for travel. My budget is around $300." ) ,
147123 )
148-
149- run = await get_product_recommendations (chat_input )
150124 print (run )
151125
152- # Example 2: Follow-up question with conversation history
126+ # Example 2: Follow-up question using reply
153127 print ("\n Example 2: Follow-up about battery life" )
154128 print ("-" * 50 )
155129
156- chat_input = ChatInput (
157- user_message = "Which one has the best battery life?" ,
158- conversation_history = [
159- Message (
160- role = Role .USER ,
161- content = "I'm looking for noise-cancelling headphones for travel. My budget is around $300." ,
162- ),
163- run .output .assistant_message ,
164- ],
165- )
166-
167- run = await get_product_recommendations (chat_input )
130+ run = await run .reply (user_message = "Which one has the best battery life?" )
168131 print (run )
169132
170133 # Example 3: Specific question about a previously recommended product
171134 print ("\n Example 3: Question about a specific product" )
172135 print ("-" * 50 )
173136
174- chat_input = ChatInput (
175- user_message = "Tell me more about the noise cancellation features of the first headphone you recommended." ,
176- conversation_history = [
177- Message (
178- role = Role .USER ,
179- content = "I'm looking for noise-cancelling headphones for travel. My budget is around $300." ,
180- ),
181- run .output .assistant_message ,
182- Message (
183- role = Role .USER ,
184- content = "Which one has the best battery life?" ,
185- ),
186- run .output .assistant_message ,
187- ],
137+ run = await run .reply (
138+ user_message = (
139+ "Tell me more about the noise cancellation features of the first headphone you recommended."
140+ ),
188141 )
189-
190- run = await get_product_recommendations (chat_input )
191142 print (run )
192143
193144 # Example 4: Different product category
194145 print ("\n Example 4: Looking for a TV" )
195146 print ("-" * 50 )
196147
197- chat_input = ChatInput (
198- user_message = "I need a good TV for gaming. My budget is $1000." ,
199- )
200-
201- run = await get_product_recommendations (chat_input )
148+ run = await run .reply (user_message = "I need a good TV for gaming. My budget is $1000." )
202149 print (run )
203150
204151
0 commit comments