Spaces:
Sleeping
Sleeping
Add rule-based fallback agent for evaluation without API
Browse files- inference.py +62 -0
inference.py
CHANGED
|
@@ -80,7 +80,69 @@ def parse_response(text: str) -> dict:
|
|
| 80 |
raise
|
| 81 |
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
def get_model_action(client: OpenAI, obs, history: List[str]) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
user_prompt = json.dumps({
|
| 85 |
"ticket_id": obs.ticket_id,
|
| 86 |
"ticket_text": obs.ticket_text,
|
|
|
|
| 80 |
raise
|
| 81 |
|
| 82 |
|
| 83 |
+
CATEGORY_KEYWORDS = {
|
| 84 |
+
"billing": ["charge", "invoice", "payment", "bill", "refund", "subscription", "price", "cost", "fee", "money"],
|
| 85 |
+
"technical": ["error", "bug", "crash", "not working", "broken", "issue", "problem", "fail", "500", "api"],
|
| 86 |
+
"account": ["login", "password", "account", "access", "sign in", "email", "username", "cancel"],
|
| 87 |
+
"refund": ["refund", "return", "money back", "reimburse", "cancel order"],
|
| 88 |
+
"general": ["hours", "contact", "phone", "help", "question", "info", "support"],
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
def rule_based_action(obs) -> dict:
|
| 92 |
+
"""Simple deterministic fallback agent — no API needed."""
|
| 93 |
+
text = obs.ticket_text.lower()
|
| 94 |
+
# Classify by keywords
|
| 95 |
+
if not obs.current_category:
|
| 96 |
+
best_cat = "general"
|
| 97 |
+
best_score = 0
|
| 98 |
+
for cat, keywords in CATEGORY_KEYWORDS.items():
|
| 99 |
+
score = sum(1 for kw in keywords if kw in text)
|
| 100 |
+
if score > best_score:
|
| 101 |
+
best_score = score
|
| 102 |
+
best_cat = cat
|
| 103 |
+
return {"action_type": "classify", "category": best_cat}
|
| 104 |
+
# After classification — choose action based on category
|
| 105 |
+
cat = obs.current_category
|
| 106 |
+
if cat == "technical":
|
| 107 |
+
return {"action_type": "escalate", "reason": "Technical issue requires engineering team"}
|
| 108 |
+
elif cat == "general":
|
| 109 |
+
return {"action_type": "close", "reason": "General inquiry resolved"}
|
| 110 |
+
else:
|
| 111 |
+
return {
|
| 112 |
+
"action_type": "reply",
|
| 113 |
+
"reply_text": f"Thank you for contacting us about your {cat} issue. We are looking into it and will resolve it shortly."
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
|
| 117 |
def get_model_action(client: OpenAI, obs, history: List[str]) -> dict:
|
| 118 |
+
"""Try LLM first, fall back to rule-based if API unavailable."""
|
| 119 |
+
if not API_KEY:
|
| 120 |
+
return rule_based_action(obs)
|
| 121 |
+
user_prompt = json.dumps({
|
| 122 |
+
"ticket_id": obs.ticket_id,
|
| 123 |
+
"ticket_text": obs.ticket_text,
|
| 124 |
+
"task_id": obs.task_id,
|
| 125 |
+
"current_category": obs.current_category,
|
| 126 |
+
"step_count": obs.step_count,
|
| 127 |
+
"feedback": obs.feedback,
|
| 128 |
+
})
|
| 129 |
+
messages = [
|
| 130 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 131 |
+
{"role": "user", "content": user_prompt},
|
| 132 |
+
]
|
| 133 |
+
try:
|
| 134 |
+
completion = client.chat.completions.create(
|
| 135 |
+
model=MODEL_NAME,
|
| 136 |
+
messages=messages,
|
| 137 |
+
temperature=0.0,
|
| 138 |
+
max_tokens=256,
|
| 139 |
+
stream=False,
|
| 140 |
+
)
|
| 141 |
+
text = (completion.choices[0].message.content or "").strip()
|
| 142 |
+
return parse_response(text)
|
| 143 |
+
except Exception as exc:
|
| 144 |
+
print(f"[DEBUG] Model request failed, using fallback: {exc}", flush=True)
|
| 145 |
+
return rule_based_action(obs)
|
| 146 |
user_prompt = json.dumps({
|
| 147 |
"ticket_id": obs.ticket_id,
|
| 148 |
"ticket_text": obs.ticket_text,
|