-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrouting.py
More file actions
141 lines (102 loc) · 4.42 KB
/
routing.py
File metadata and controls
141 lines (102 loc) · 4.42 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
import asyncio
from enum import Enum
from typing import TypedDict
from pydantic import BaseModel, Field # pyright: ignore [reportUnknownVariableType]
import workflowai
from workflowai import Model
class QueryType(str, Enum):
GENERAL = "general"
REFUND = "refund"
TECHNICAL = "technical"
class ComplexityLevel(str, Enum):
SIMPLE = "simple"
COMPLEX = "complex"
class ClassificationInput(BaseModel):
"""Input for query classification."""
query: str = Field(description="The customer query to classify.")
class ClassificationOutput(BaseModel):
"""Output containing query classification details."""
reasoning: str = Field(description="Brief reasoning for the classification.")
type: QueryType = Field(description="Type of the query (general, refund, or technical).")
complexity: ComplexityLevel = Field(description="Complexity level of the query.")
# Uses GPT-4O for its strong analytical and classification capabilities
@workflowai.agent(id="query-classifier", model=Model.GPT_4O_LATEST)
async def classify_query_agent(
_: ClassificationInput,
) -> ClassificationOutput:
"""
Classify the customer query by:
1. Query type (general, refund, or technical)
2. Complexity (simple or complex)
3. Provide brief reasoning for classification
"""
...
class ResponseInput(BaseModel):
"""Input for generating customer response."""
query: str = Field(description="The customer query to respond to.")
query_type: QueryType = Field(description="Type of the query for specialized handling.")
class ResponseOutput(BaseModel):
"""Output containing the response to the customer."""
response: str = Field(description="The generated response to the customer query.")
# Uses Claude 3.5 Sonnet for its strong conversational abilities and empathy
@workflowai.agent(model=Model.CLAUDE_3_5_SONNET_LATEST)
async def handle_general_query(_: ResponseInput) -> ResponseOutput:
"""Expert customer service agent handling general inquiries."""
...
# Uses GPT-4O Mini for efficient policy-based responses
@workflowai.agent(model=Model.GPT_4O_MINI_LATEST)
async def handle_refund_query(_: ResponseInput) -> ResponseOutput:
"""Customer service agent specializing in refund requests."""
...
# Uses O1 Mini for its technical expertise and problem-solving capabilities
@workflowai.agent(model=Model.O1_MINI_LATEST)
async def handle_technical_query(_: ResponseInput) -> ResponseOutput:
"""Technical support specialist providing troubleshooting assistance."""
...
class QueryResult(TypedDict):
response: str
classification: ClassificationOutput
async def handle_customer_query(query: str) -> QueryResult:
"""
Handle a customer query through a workflow:
1. Classify the query type and complexity
2. Route to appropriate specialized agent
3. Return response and classification details
"""
# Step 1: Classify the query
classification = await classify_query_agent(ClassificationInput(query=query))
# Step 2: Route to appropriate handler based on type and complexity
handlers = {
(QueryType.GENERAL, ComplexityLevel.SIMPLE): handle_general_query,
(QueryType.GENERAL, ComplexityLevel.COMPLEX): handle_general_query,
(QueryType.REFUND, ComplexityLevel.SIMPLE): handle_refund_query,
(QueryType.REFUND, ComplexityLevel.COMPLEX): handle_refund_query,
(QueryType.TECHNICAL, ComplexityLevel.SIMPLE): handle_technical_query,
(QueryType.TECHNICAL, ComplexityLevel.COMPLEX): handle_technical_query,
}
# Get appropriate handler
handler = handlers[(classification.type, classification.complexity)]
# Generate response
result = await handler(
ResponseInput(
query=query,
query_type=classification.type,
),
)
return {
"response": result.response,
"classification": classification,
}
if __name__ == "__main__":
query = "I'm having trouble logging into my account."
" It keeps saying invalid password even though I'm sure it's correct."
result = asyncio.run(handle_customer_query(query))
print("\n=== Customer Query ===")
print(query)
print("\n=== Classification ===")
print(f"Type: {result['classification'].type}")
print(f"Complexity: {result['classification'].complexity}")
print(f"Reasoning: {result['classification'].reasoning}")
print("\n=== Response ===")
print(result["response"])
print()