umpirix/examples/triage-tickets.py
caelum0x 60c6d3ebba fix: keyless triage example, lite README import name, JSON-RPC parse error on /v1/mcp
- examples/triage-tickets.py no longer passes a placeholder key or says the SDK requires one
- sdk/js-lite README imported from @caelum/typesafe, which does not resolve; use @openjev/lite
- POST /v1/mcp answers a non-JSON body with a JSON-RPC -32700 error instead of the REST envelope
2026-11-11 09:33:19 +03:00

105 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""openjev example: triage support tickets with typed decisions.
Batches a Choice (department) + Score (urgency) + Noul (refund flag) per
ticket in ONE request each, then applies a routing policy in code over the
returned probabilities -- the model only judges, code decides what to do.
pip install ./sdk/python # or: uv add ./sdk/python
npm run dev # or: docker compose up
OPENJEV_BASE_URL=http://localhost:8080 python examples/triage-tickets.py
A self-hosted server needs no key; set OPENJEV_API_KEY if yours requires one.
"""
from __future__ import annotations
import os
from dataclasses import dataclass
from openjev import Choice, Noul, Score, TypeSafeClient
# --- Candidate tickets (the "state" each question set is judged against) ---
TICKETS = [
{"id": "T-1", "text": "I was charged twice for order A-104, please refund the duplicate."},
{"id": "T-2", "text": "The app crashes every time I open the camera screen on Android."},
{"id": "T-3", "text": "Do you offer an annual plan instead of monthly billing?"},
{"id": "T-4", "text": "This is the third time support has ignored my emails. Unacceptable."},
]
def questions_for() -> dict:
return {
"department": Choice(
instructions="Which team should handle this?",
criteria={
"billing": "Payments, refunds, invoices.",
"technical": "Bugs, crashes, outages.",
"sales": "Pricing, plans, upgrades.",
},
),
"urgency": Score(
instructions="How urgent is this ticket?",
criteria=["low", "medium", "high", "critical"],
),
"is_refund": Noul(instructions="Is the customer asking for a refund?"),
}
@dataclass(frozen=True)
class Routed:
ticket_id: str
department: str
confidence: float
urgency: float
is_refund: float
priority: float
def route(ticket_id: str, result) -> Routed:
dept = result.choices["department"]
urgency = result.scores["urgency"]
refund = result.nouls["is_refund"]
# Policy lives in CODE, not in the model: escalate refunds, weight by urgency.
priority = urgency.score + (1.5 if refund.noul > 0.5 else 0.0)
return Routed(
ticket_id=ticket_id,
department=dept.choice,
confidence=round(dept.confidence, 2),
urgency=round(urgency.score, 2),
is_refund=round(refund.noul, 2),
priority=round(priority, 2),
)
def main() -> None:
base_url = os.environ.get("OPENJEV_BASE_URL", "http://localhost:8080")
# No api_key argument: the SDK reads OPENJEV_API_KEY when it is set and
# sends no Authorization header when it is not (openjev-client 0.7.2+),
# which is what a self-hosted server without keys expects.
questions = questions_for()
rows: list[Routed] = []
with TypeSafeClient(base_url=base_url) as client:
for ticket in TICKETS:
result = client.system_one(ticket["text"], questions, model="system-one")
rows.append(route(ticket["id"], result))
rows.sort(key=lambda r: r.priority, reverse=True)
print(f"Triaged {len(rows)} tickets against {base_url}\n")
print("rank ticket dept conf urgency refund priority")
print("---- ------ ---------- ---- ------- ------ --------")
for i, r in enumerate(rows, start=1):
print(
f"{i:<4} {r.ticket_id:<6} {r.department:<10} {r.confidence:<4} "
f"{r.urgency:<7} {r.is_refund:<6} {r.priority}"
)
top = rows[0]
print(f"\nHandle first: {top.ticket_id} -> {top.department} (priority {top.priority}).")
if __name__ == "__main__":
main()