Found by running typesafe-sdk 0.7.5 and @typesafe-ai/sdk 0.7.0 against a
local server and fixed:
- zstd (and gzip/deflate/br) request bodies are decoded; the SDKs compress
bodies of 1 KiB or more, which used to fail with a 500
- GET /v1/models also returns models: [{ name, description, release_date }]
- every systemone, batch and models response carries x-typesafe-request-id
- a base URL ending in /v1 (or /api/v1) resolves to the same handlers
- request errors carry a detail list; a non-JSON body is a 400, not a 500
- multipart (image asset) requests get a clear 415
Adds examples/official-sdk.py and .mjs, a /docs/typesafe-sdk page, a README
block and a COMPARISON.md section, limited to what was observed to pass.
99 lines
3.7 KiB
Python
99 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
"""openjev example: the official TypeSafe Python SDK, pointed at OpenJev.
|
|
|
|
Nothing here imports OpenJev code. It is the unmodified `typesafe-sdk` package
|
|
from PyPI with its base URL set to a local OpenJev server.
|
|
|
|
pip install typesafe-sdk
|
|
npm run dev # or: docker compose up
|
|
TYPESAFE_BASE_URL=http://localhost:8080 python examples/official-sdk.py
|
|
|
|
The SDK insists on an API key, so any placeholder value works against a
|
|
self-hosted server that does not issue keys.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
|
|
from typesafe_sdk import Choice, Noul, Score, TypeSafeBadRequestError, TypeSafeClient
|
|
|
|
BASE_URL = os.environ.get("TYPESAFE_BASE_URL", "http://localhost:8080")
|
|
API_KEY = os.environ.get("TYPESAFE_API_KEY", "not-needed-for-self-host")
|
|
TICKET = "I was charged twice for order A-104, please refund the duplicate."
|
|
|
|
|
|
def main() -> None:
|
|
with TypeSafeClient(base_url=BASE_URL, api_key=API_KEY) as client:
|
|
print(f"Server: {BASE_URL}")
|
|
|
|
choice = client.system_one(
|
|
state=TICKET,
|
|
questions={
|
|
"department": Choice(
|
|
instructions="Which team should handle this?",
|
|
criteria={
|
|
"billing": "Payments, refunds, invoices.",
|
|
"technical": "Bugs, crashes, outages.",
|
|
"sales": "Pricing, plans, upgrades.",
|
|
},
|
|
)
|
|
},
|
|
)
|
|
department = choice.choices["department"]
|
|
print(f"choice {department.choice} confidence={department.confidence} {department.probabilities}")
|
|
|
|
score = client.system_one(
|
|
state=TICKET,
|
|
questions={
|
|
"urgency": Score(
|
|
instructions="How urgent is this ticket?",
|
|
criteria=["low", "medium", "high", "critical"],
|
|
)
|
|
},
|
|
)
|
|
urgency = score.scores["urgency"]
|
|
print(f"score {urgency.score} {urgency.probabilities}")
|
|
|
|
noul = client.system_one(
|
|
state=TICKET,
|
|
questions={"is_refund": Noul(instructions="Is the customer asking for a refund?")},
|
|
)
|
|
print(f"noul {noul.nouls['is_refund'].noul}")
|
|
|
|
# Several typed questions over a structured state, answered in one request.
|
|
multi = client.system_one(
|
|
state={"ticket": TICKET, "order": {"id": "A-104", "amount": 49.0}},
|
|
questions={
|
|
"department": Choice(
|
|
instructions="Which team should handle this?",
|
|
criteria={"billing": None, "technical": None, "sales": None},
|
|
),
|
|
"urgency": Score(instructions="How urgent is this ticket?", criteria=["low", "medium", "high"]),
|
|
"is_refund": Noul(instructions="Is the customer asking for a refund?"),
|
|
},
|
|
)
|
|
print(
|
|
"multi "
|
|
f"department={multi.choices['department'].choice} "
|
|
f"urgency={multi.scores['urgency'].score} "
|
|
f"is_refund={multi.nouls['is_refund'].noul} "
|
|
f"model={multi.model} request_id={multi.request_id}"
|
|
)
|
|
|
|
models = client.models.list()
|
|
print("models " + ", ".join(model.name for model in models.models))
|
|
|
|
# A request the server rejects: a Choice needs at least two options.
|
|
try:
|
|
client.system_one(
|
|
state=TICKET,
|
|
questions={"department": Choice(instructions="Which team?", criteria={"billing": None})},
|
|
)
|
|
except TypeSafeBadRequestError as error:
|
|
print(f"invalid {error.status} {error.body['error']['message']} request_id={error.request_id}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|