Skip to content

Commit edb6e35

Browse files
author
Pierre
authored
Merge pull request #59 from WorkflowAI/pierre-use-reply-for-chatbot-examples
Use reply for chatbot examples
2 parents f4f8495 + e7a3afa commit edb6e35

File tree

2 files changed

+38
-101
lines changed

2 files changed

+38
-101
lines changed

examples/11_ecommerce_chatbot.py

Lines changed: 15 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,17 @@
22
This example demonstrates how to create an e-commerce chatbot that:
33
1. Understands customer queries about products
44
2. Provides helpful responses with product recommendations
5-
3. Maintains context through conversation
5+
3. Maintains context through conversation using .reply
66
4. Returns structured product recommendations
77
"""
88

99
import asyncio
10-
from enum import Enum
1110
from typing import Optional
1211

1312
from pydantic import BaseModel, Field
1413

1514
import 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

2618
class 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-
8363
class ChatbotOutput(BaseModel):
8464
"""Output model for the chatbot response."""
8565

@@ -89,12 +69,8 @@ class ChatbotOutput(BaseModel):
8969

9070

9171
class 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("\nExample 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("\nExample 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("\nExample 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("\nExample 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

examples/13_rag.py

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
This example demonstrates how to create a RAG-enabled chatbot that:
33
1. Uses a search tool to find relevant information from a knowledge base
44
2. Incorporates search results into its responses
5-
3. Maintains conversation context
5+
3. Maintains context through conversation using .reply
66
4. Provides well-structured, informative responses
77
88
Note: WorkflowAI does not manage the RAG implementation (yet). You need to provide your own
@@ -11,20 +11,11 @@
1111
"""
1212

1313
import asyncio
14-
from enum import Enum
15-
from typing import Optional
1614

1715
from pydantic import BaseModel, Field
1816

1917
import workflowai
20-
from workflowai import Model, Run
21-
22-
23-
class Role(str, Enum):
24-
"""Enum representing possible message roles."""
25-
26-
USER = "user"
27-
ASSISTANT = "assistant"
18+
from workflowai import Model
2819

2920

3021
class SearchResult(BaseModel):
@@ -79,38 +70,25 @@ async def search_faq(query: str) -> list[SearchResult]:
7970
]
8071

8172

82-
class Message(BaseModel):
83-
"""Model representing a chat message."""
73+
class AssistantMessage(BaseModel):
74+
"""Model representing a message from the assistant."""
8475

85-
role: Role
8676
content: str = Field(
8777
description="The content of the message",
8878
)
8979

9080

91-
class AssistantMessage(Message):
92-
"""Model representing a message from the assistant."""
93-
94-
role: Role = Role.ASSISTANT
95-
content: str = ""
96-
97-
9881
class ChatbotOutput(BaseModel):
9982
"""Output model for the chatbot response."""
10083

10184
assistant_message: AssistantMessage = Field(
102-
default_factory=AssistantMessage,
10385
description="The chatbot's response message",
10486
)
10587

10688

10789
class ChatInput(BaseModel):
108-
"""Input model containing the user's message and conversation history."""
90+
"""Input model containing the user's message."""
10991

110-
conversation_history: Optional[list[Message]] = Field(
111-
default=None,
112-
description="Previous messages in the conversation, if any",
113-
)
11492
user_message: str = Field(
11593
description="The current message from the user",
11694
)
@@ -124,7 +102,7 @@ class ChatInput(BaseModel):
124102
# The agent will automatically handle calling the tool and incorporating the results.
125103
tools=[search_faq],
126104
)
127-
async def chat_agent(chat_input: ChatInput) -> Run[ChatbotOutput]:
105+
async def chat_agent(chat_input: ChatInput) -> ChatbotOutput:
128106
"""
129107
Act as a knowledgeable assistant that uses search to find and incorporate relevant information.
130108
@@ -165,15 +143,27 @@ async def chat_agent(chat_input: ChatInput) -> Run[ChatbotOutput]:
165143

166144

167145
async def main():
168-
# Example: Question about return policy
169-
print("\nExample: Question about return policy")
146+
# Example 1: Initial question about return policy
147+
print("\nExample 1: Question about return policy")
170148
print("-" * 50)
171149

172-
chat_input = ChatInput(
173-
user_message="What is your return policy? Can I return items I bought online?",
150+
run = await chat_agent.run(
151+
ChatInput(user_message="What is your return policy? Can I return items I bought online?"),
174152
)
153+
print(run)
154+
155+
# Example 2: Follow-up question about shipping
156+
print("\nExample 2: Follow-up about shipping")
157+
print("-" * 50)
158+
159+
run = await run.reply(user_message="How long does shipping usually take?")
160+
print(run)
161+
162+
# Example 3: Specific question about refund processing
163+
print("\nExample 3: Question about refunds")
164+
print("-" * 50)
175165

176-
run = await chat_agent(chat_input)
166+
run = await run.reply(user_message="Once I return an item, how long until I get my refund?")
177167
print(run)
178168

179169

0 commit comments

Comments
 (0)