Spaces:
Runtime error
Runtime error
Create Customer_Support/handlers.py
Browse files- Customer_Support/handlers.py +105 -0
Customer_Support/handlers.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Handler functions for the customer support workflow nodes.
|
| 3 |
+
"""
|
| 4 |
+
from .models import State
|
| 5 |
+
from config.app_config import setup_api
|
| 6 |
+
|
| 7 |
+
# Initialize the model
|
| 8 |
+
model = setup_api()
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def categorize(state: State) -> State:
|
| 12 |
+
"""Categorize the customer query into Technical, Billing, or General."""
|
| 13 |
+
prompt = (
|
| 14 |
+
"Categorize the following customer query into one of these categories: "
|
| 15 |
+
"Technical, Billing, or General.\n"
|
| 16 |
+
f"Query: {state['query']}"
|
| 17 |
+
)
|
| 18 |
+
response = model.generate_content(prompt)
|
| 19 |
+
category = response.text.strip()
|
| 20 |
+
|
| 21 |
+
return {
|
| 22 |
+
**state,
|
| 23 |
+
"category": category
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def analyze_sentiment(state: State) -> State:
|
| 28 |
+
"""Analyze the sentiment of the customer query as Positive, Neutral, or Negative."""
|
| 29 |
+
prompt = (
|
| 30 |
+
"Analyze the sentiment of the following customer query. "
|
| 31 |
+
"Respond with either 'Positive', 'Neutral', or 'Negative'.\n"
|
| 32 |
+
f"Query: {state['query']}"
|
| 33 |
+
)
|
| 34 |
+
response = model.generate_content(prompt)
|
| 35 |
+
sentiment = response.text.strip()
|
| 36 |
+
|
| 37 |
+
return {
|
| 38 |
+
**state,
|
| 39 |
+
"sentiment": sentiment
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def handle_technical(state: State) -> State:
|
| 44 |
+
"""Provide a technical support response to the query."""
|
| 45 |
+
prompt = (
|
| 46 |
+
"Provide a technical support response to the following query: "
|
| 47 |
+
f"{state['query']}"
|
| 48 |
+
)
|
| 49 |
+
response = model.generate_content(prompt)
|
| 50 |
+
technical_response = response.text.strip()
|
| 51 |
+
|
| 52 |
+
return {
|
| 53 |
+
**state,
|
| 54 |
+
"response": technical_response
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def handle_billing(state: State) -> State:
|
| 59 |
+
"""Provide a billing support response to the query."""
|
| 60 |
+
prompt = (
|
| 61 |
+
"Provide a billing support response to the following query: "
|
| 62 |
+
f"{state['query']}"
|
| 63 |
+
)
|
| 64 |
+
response = model.generate_content(prompt)
|
| 65 |
+
billing_response = response.text.strip()
|
| 66 |
+
|
| 67 |
+
return {
|
| 68 |
+
**state,
|
| 69 |
+
"response": billing_response
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def handle_general(state: State) -> State:
|
| 74 |
+
"""Provide a general support response to the query."""
|
| 75 |
+
prompt = (
|
| 76 |
+
"Provide a general support response to the following query: "
|
| 77 |
+
f"{state['query']}"
|
| 78 |
+
)
|
| 79 |
+
response = model.generate_content(prompt)
|
| 80 |
+
general_response = response.text.strip()
|
| 81 |
+
|
| 82 |
+
return {
|
| 83 |
+
**state,
|
| 84 |
+
"response": general_response
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def escalate(state: State) -> State:
|
| 89 |
+
"""Escalate the query to a human agent due to negative sentiment."""
|
| 90 |
+
return {
|
| 91 |
+
**state,
|
| 92 |
+
"response": "This query has been escalated to a human agent due to its negative sentiment."
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def route_query(state: State) -> str:
|
| 97 |
+
"""Route the query based on its sentiment and category."""
|
| 98 |
+
if state["sentiment"] == "Negative":
|
| 99 |
+
return "escalate"
|
| 100 |
+
elif state["category"] == "Technical":
|
| 101 |
+
return "handle_technical"
|
| 102 |
+
elif state["category"] == "Billing":
|
| 103 |
+
return "handle_billing"
|
| 104 |
+
else:
|
| 105 |
+
return "handle_general"
|