#!/usr/bin/env python3 # SPDX-License-Identifier: Apache-2.0 """Minimal client of the laya server (standard library only). python3 client.py "Hi, we were billed twice for March. Please refund the duplicate." python3 client.py --url http://127.0.0.1:20000 --questions questions.json "some ticket text" """ from __future__ import annotations import argparse import json import sys import urllib.error import urllib.request EXAMPLE_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?"}, } def post(url: str, payload: dict, timeout: float = 120.0, headers=None): h = {"Content-Type": "application/json", **(headers or {})} req = urllib.request.Request(url, data=json.dumps(payload).encode(), headers=h) try: with urllib.request.urlopen(req, timeout=timeout) as r: return r.status, json.loads(r.read() or b"{}") except urllib.error.HTTPError as e: try: return e.code, json.loads(e.read() or b"{}") except Exception: # noqa: BLE001 return e.code, {} def get_json(url: str, timeout: float = 10.0): with urllib.request.urlopen(url, timeout=timeout) as r: return json.loads(r.read()) def systemone(base: str, state, questions, **kw): code, body = post(base.rstrip("/") + "/v1/systemone", {"state": state, "questions": questions, **kw}) if code != 200: raise RuntimeError(f"HTTP {code}: {body}") return body def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("state") ap.add_argument("--url", default="http://127.0.0.1:20000") ap.add_argument("--questions", default=None, help="JSON file of questions (default: a support-triage example)") ap.add_argument("--min-confidence", type=float, default=None) a = ap.parse_args() qs = json.load(open(a.questions)) if a.questions else EXAMPLE_QUESTIONS kw = {"min_confidence": a.min_confidence} if a.min_confidence is not None else {} print(json.dumps(systemone(a.url, a.state, qs, **kw), indent=1)) return 0 if __name__ == "__main__": sys.exit(main())