Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 15 additions & 68 deletions examples/11_ecommerce_chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@
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
3. Maintains context through conversation using .reply
4. Returns structured product recommendations
"""

import asyncio
from enum import Enum
from typing import Optional

from pydantic import BaseModel, Field

import workflowai
from workflowai import Model, Run


class Role(str, Enum):
"""Enum representing possible message roles."""

USER = "user"
ASSISTANT = "assistant"
from workflowai import Model


class Product(BaseModel):
Expand Down Expand Up @@ -56,30 +48,18 @@ class Product(BaseModel):
)


class Message(BaseModel):
"""Model representing a chat message."""
class AssistantMessage(BaseModel):
"""Model representing a message from the assistant."""

role: Role = Field()
content: str = Field(
description="The content of the message",
examples=[
"I'm looking for noise-cancelling headphones for travel",
"Based on your requirements, here are some great headphone options...",
],
)
recommended_products: Optional[list[Product]] = Field(
default=None,
description="Product recommendations included with this message, if any",
)


class AssistantMessage(Message):
"""Model representing a message from the assistant."""

role: Role = Role.ASSISTANT
content: str = ""


class ChatbotOutput(BaseModel):
"""Output model for the chatbot response."""

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


class ChatInput(BaseModel):
"""Input model containing the user's message and conversation history."""
"""Input model containing the user's message."""

conversation_history: Optional[list[Message]] = Field(
default=None,
description="Previous messages in the conversation, if any",
)
user_message: str = Field(
description="The current message from the user",
)
Expand All @@ -104,7 +80,7 @@ class ChatInput(BaseModel):
id="ecommerce-chatbot",
model=Model.LLAMA_3_3_70B,
)
async def get_product_recommendations(chat_input: ChatInput) -> Run[ChatbotOutput]:
async def get_product_recommendations(chat_input: ChatInput) -> ChatbotOutput:
"""
Act as a knowledgeable e-commerce shopping assistant.

Expand Down Expand Up @@ -142,63 +118,34 @@ async def main():
print("\nExample 1: Looking for headphones")
print("-" * 50)

chat_input = ChatInput(
user_message="I'm looking for noise-cancelling headphones for travel. My budget is around $300.",
run = await get_product_recommendations.run(
ChatInput(user_message="I'm looking for noise-cancelling headphones for travel. My budget is around $300."),
)

run = await get_product_recommendations(chat_input)
print(run)

# Example 2: Follow-up question with conversation history
# Example 2: Follow-up question using reply
print("\nExample 2: Follow-up about battery life")
print("-" * 50)

chat_input = ChatInput(
user_message="Which one has the best battery life?",
conversation_history=[
Message(
role=Role.USER,
content="I'm looking for noise-cancelling headphones for travel. My budget is around $300.",
),
run.output.assistant_message,
],
)

run = await get_product_recommendations(chat_input)
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)

chat_input = ChatInput(
user_message="Tell me more about the noise cancellation features of the first headphone you recommended.",
conversation_history=[
Message(
role=Role.USER,
content="I'm looking for noise-cancelling headphones for travel. My budget is around $300.",
),
run.output.assistant_message,
Message(
role=Role.USER,
content="Which one has the best battery life?",
),
run.output.assistant_message,
],
run = await run.reply(
user_message=(
"Tell me more about the noise cancellation features of the first headphone you recommended."
),
)

run = await get_product_recommendations(chat_input)
print(run)

# Example 4: Different product category
print("\nExample 4: Looking for a TV")
print("-" * 50)

chat_input = ChatInput(
user_message="I need a good TV for gaming. My budget is $1000.",
)

run = await get_product_recommendations(chat_input)
run = await run.reply(user_message="I need a good TV for gaming. My budget is $1000.")
print(run)


Expand Down
56 changes: 23 additions & 33 deletions examples/13_rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
This example demonstrates how to create a RAG-enabled chatbot that:
1. Uses a search tool to find relevant information from a knowledge base
2. Incorporates search results into its responses
3. Maintains conversation context
3. Maintains context through conversation using .reply
4. Provides well-structured, informative responses

Note: WorkflowAI does not manage the RAG implementation (yet). You need to provide your own
Expand All @@ -11,20 +11,11 @@
"""

import asyncio
from enum import Enum
from typing import Optional

from pydantic import BaseModel, Field

import workflowai
from workflowai import Model, Run


class Role(str, Enum):
"""Enum representing possible message roles."""

USER = "user"
ASSISTANT = "assistant"
from workflowai import Model


class SearchResult(BaseModel):
Expand Down Expand Up @@ -79,38 +70,25 @@ async def search_faq(query: str) -> list[SearchResult]:
]


class Message(BaseModel):
"""Model representing a chat message."""
class AssistantMessage(BaseModel):
"""Model representing a message from the assistant."""

role: Role
content: str = Field(
description="The content of the message",
)


class AssistantMessage(Message):
"""Model representing a message from the assistant."""

role: Role = Role.ASSISTANT
content: str = ""


class ChatbotOutput(BaseModel):
"""Output model for the chatbot response."""

assistant_message: AssistantMessage = Field(
default_factory=AssistantMessage,
description="The chatbot's response message",
)


class ChatInput(BaseModel):
"""Input model containing the user's message and conversation history."""
"""Input model containing the user's message."""

conversation_history: Optional[list[Message]] = Field(
default=None,
description="Previous messages in the conversation, if any",
)
user_message: str = Field(
description="The current message from the user",
)
Expand All @@ -124,7 +102,7 @@ class ChatInput(BaseModel):
# The agent will automatically handle calling the tool and incorporating the results.
tools=[search_faq],
)
async def chat_agent(chat_input: ChatInput) -> Run[ChatbotOutput]:
async def chat_agent(chat_input: ChatInput) -> ChatbotOutput:
"""
Act as a knowledgeable assistant that uses search to find and incorporate relevant information.

Expand Down Expand Up @@ -165,15 +143,27 @@ async def chat_agent(chat_input: ChatInput) -> Run[ChatbotOutput]:


async def main():
# Example: Question about return policy
print("\nExample: Question about return policy")
# Example 1: Initial question about return policy
print("\nExample 1: Question about return policy")
print("-" * 50)

chat_input = ChatInput(
user_message="What is your return policy? Can I return items I bought online?",
run = await chat_agent.run(
ChatInput(user_message="What is your return policy? Can I return items I bought online?"),
)
print(run)

# Example 2: Follow-up question about shipping
print("\nExample 2: Follow-up about shipping")
print("-" * 50)

run = await run.reply(user_message="How long does shipping usually take?")
print(run)

# Example 3: Specific question about refund processing
print("\nExample 3: Question about refunds")
print("-" * 50)

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


Expand Down