laya / examples /quickstart.py
changh95's picture
tt-model push laya (container)
f882f26 verified
Raw History Blame Contribute Delete
1.81 kB
# SPDX-License-Identifier: Apache-2.0
"""Quickstart: ask a laya server (tt serve changh95/laya) typed questions about a support email.
python3 examples/quickstart.py [--url http://localhost:20000]
"""
import argparse
import json
import urllib.request
ap = argparse.ArgumentParser()
ap.add_argument("--url", default="http://localhost:20000")
a = ap.parse_args()
body = {
"state": {"from": "user@example.com", "subject": "Duplicate charge on invoice #4411",
"body": "Hi, we were billed twice for March. Please refund the duplicate today or we will cancel our plan."},
"questions": {
"department": {"type": "choice", "instructions": "Which department should handle this request?",
"criteria": {"billing": "invoices, payments, refunds", "technical": "bugs, outages, system errors",
"sales": "pricing, new contracts", "other": "everything else"}},
"urgency": {"type": "score", "instructions": "How urgent is this request?",
"criteria": ["not urgent", "soon", "critical deadline or blocking issue"]},
"churn_risk": {"type": "noul", "instructions": "Does the user threaten to cancel or leave?"},
"refund_requested": {"type": "noul", "instructions": "Does the user explicitly request a refund?"},
},
}
req = urllib.request.Request(a.url.rstrip("/") + "/v1/systemone", data=json.dumps(body).encode(),
headers={"Content-Type": "application/json"})
with urllib.request.urlopen(req, timeout=60) as r:
out = json.loads(r.read())
for qid, ans in out["answers"].items():
value = ans.get("choice", ans.get("score", ans.get("noul")))
print(f"{qid:18s} {value!s:10s} answer_confidence={ans['answer_confidence']}")
print("usage:", out["usage"])