-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathagent.py
More file actions
155 lines (127 loc) · 4.63 KB
/
Copy pathagent.py
File metadata and controls
155 lines (127 loc) · 4.63 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Tencent is pleased to support the open source community by making tRPC-Agent-Python available.
#
# Copyright (C) 2026 Tencent. All rights reserved.
#
# tRPC-Agent-Python is licensed under Apache-2.0.
"""Deterministic fake agent for the eval-optimize-loop example.
The example's default mode must run without an API key, so this module exposes
an async ``call_agent`` that derives behavior from the current prompt text on
disk. The pipeline still uses AgentEvaluator for scoring; this module only
stands in for the model.
"""
from __future__ import annotations
import json
import re
from pathlib import Path
PROMPT_DIR = Path(__file__).parent / "prompts"
ROUTER_PROMPT_PATH = PROMPT_DIR / "router.md"
SYSTEM_PROMPT_PATH = PROMPT_DIR / "system.md"
SKILL_PROMPT_PATH = PROMPT_DIR / "skill.md"
_SPACE_RE = re.compile(r"\s+")
def _compact_json(payload: dict[str, str]) -> str:
return json.dumps(payload, sort_keys=True, separators=(",", ":"))
def normalize_json_text(raw: str) -> str:
"""Normalize JSON-like model output for stable exact matching."""
try:
parsed = json.loads(raw)
except json.JSONDecodeError:
return _SPACE_RE.sub(" ", raw.strip())
return _compact_json(parsed)
def _read_prompt_text() -> str:
return "\n\n".join(
path.read_text(encoding="utf-8")
for path in (ROUTER_PROMPT_PATH, SYSTEM_PROMPT_PATH, SKILL_PROMPT_PATH)
).lower()
def _prompt_flags() -> dict[str, bool]:
prompt = _read_prompt_text()
return {
"refund_rule": "treat double charge" in prompt or "vip refund requests" in prompt,
"keep_outage_p1": "p1 for urgent production outages" in prompt,
"keep_plan_p3": "p3 for low-risk informational requests" in prompt,
"overfit_payment_outage": "overfit_payment_outage" in prompt,
}
def answer_for_query(query: str) -> str:
"""Return the fake model answer for one support ticket."""
text = query.lower()
flags = _prompt_flags()
if "production checkout outage" in text:
if flags["overfit_payment_outage"]:
return _compact_json({
"category": "billing",
"priority": "p2",
"action": "refund_review",
})
priority = "p1" if flags["keep_outage_p1"] else "p2"
return _compact_json({
"category": "technical",
"priority": priority,
"action": "escalate",
})
if "vip customer" in text and "double charged" in text:
if flags["refund_rule"]:
return _compact_json({
"category": "billing",
"priority": "p1",
"action": "refund_review",
})
return _compact_json({
"category": "account",
"priority": "p2",
"action": "answer",
})
if "double charged" in text or "refund" in text:
if flags["refund_rule"]:
return _compact_json({
"category": "billing",
"priority": "p2",
"action": "refund_review",
})
return _compact_json({
"category": "account",
"priority": "p2",
"action": "answer",
})
if "password reset" in text or "email address" in text:
return _compact_json({
"category": "account",
"priority": "p3",
"action": "answer",
})
if "plan comparison" in text or "pricing tiers" in text:
priority = "p3" if flags["keep_plan_p3"] else "p2"
return _compact_json({
"category": "billing",
"priority": priority,
"action": "answer",
})
if "policy citation" in text or "pol-77" in text:
return _compact_json({
"category": "billing",
"action": "answer",
})
if "guaranteed instant fix" in text or "repeated invoice confusion" in text:
return _compact_json({
"category": "billing",
"priority": "p3",
"action": "answer",
})
if "mobile app crashes" in text:
return _compact_json({
"category": "technical",
"priority": "p2",
"action": "troubleshooting",
})
if "legacy desktop sync" in text:
return _compact_json({
"category": "technical",
"priority": "p2",
"action": "troubleshooting",
})
return _compact_json({
"category": "technical",
"priority": "p2",
"action": "troubleshooting",
})
async def call_agent(query: str) -> str:
"""AgentEvaluator / AgentOptimizer compatible black-box entry point."""
return answer_for_query(query)