Loader pe stdlib (fara python-dotenv): mediul are prioritate, altfel .env de langa script. .env e in .gitignore, .env.example e sablonul versionat. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KHLUSsKP99G6ebv2fFUKQV
85 lines
3.3 KiB
Python
85 lines
3.3 KiB
Python
# Test TypeSafe System One - stdlib, fara dependinte.
|
|
# Rulare: pune cheia in .env apoi: python typesafe_test.py
|
|
import io, json, os, sys, urllib.request
|
|
|
|
|
|
def cheie():
|
|
"""Din mediu daca e setata, altfel din .env de langa script."""
|
|
k = os.environ.get("TYPESAFE_API_KEY")
|
|
if k:
|
|
return k
|
|
env = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env")
|
|
if os.path.exists(env):
|
|
for linie in io.open(env, encoding="utf-8"):
|
|
linie = linie.strip()
|
|
if linie.startswith("TYPESAFE_API_KEY="):
|
|
return linie.split("=", 1)[1].strip().strip("'\"")
|
|
return None
|
|
|
|
|
|
KEY = cheie()
|
|
if not KEY or KEY == "pune_cheia_aici":
|
|
sys.exit("Lipseste cheia: pune-o in .env (TYPESAFE_API_KEY=...) sau in mediu.")
|
|
|
|
# state = ce vede modelul. Aici: o linie de factura de furnizor + candidatii scosi "din Oracle".
|
|
payload = {
|
|
"model": "jev-latest",
|
|
"state": {
|
|
"linie_factura": "Consumabile birou: 5 x toner HP 26A, 3 x top hartie A4 80g",
|
|
"furnizor": "OFFICE DIRECT SRL",
|
|
"candidati_gestiune": ["Marfa", "Consumabile", "Obiecte de inventar", "Mijloace fixe"],
|
|
},
|
|
"questions": {
|
|
"gestiune": {
|
|
"type": "choice",
|
|
"instructions": "In ce gestiune intra bunurile de pe `linie_factura`?",
|
|
"criteria": {
|
|
"Marfa": "Bunuri cumparate pentru revanzare, nu pentru consum intern",
|
|
"Consumabile": "Bunuri consumate in activitatea curenta: hartie, toner, papetarie",
|
|
"Obiecte de inventar": "Bunuri de folosinta indelungata sub plafonul de mijloc fix",
|
|
"Mijloace fixe": "Bunuri amortizabile, peste plafonul legal",
|
|
"niciuna": "Linia nu descrie bunuri, sau nu se potriveste niciuna dintre gestiuni",
|
|
},
|
|
},
|
|
"e_serviciu": {
|
|
"type": "noul",
|
|
"instructions": "`linie_factura` descrie un serviciu, nu bunuri livrate?",
|
|
"criteria": {
|
|
"true": "Prestare de servicii: transport, abonament, mentenanta, consultanta",
|
|
"false": "Livrare de bunuri fizice",
|
|
},
|
|
},
|
|
"claritate": {
|
|
"type": "score",
|
|
"instructions": "Cat de clar descrie `linie_factura` ce s-a cumparat?",
|
|
"criteria": [
|
|
"Generica, nu se poate identifica produsul (ex. 'diverse', 'conform contract')",
|
|
"Categoria se intelege, produsul exact nu",
|
|
"Produs, cantitate si caracteristici identificabile",
|
|
],
|
|
},
|
|
},
|
|
}
|
|
|
|
req = urllib.request.Request(
|
|
"https://api.typesafe.ai/v1/systemone",
|
|
data=json.dumps(payload).encode("utf-8"),
|
|
headers={"Authorization": "Bearer " + KEY, "Content-Type": "application/json"},
|
|
)
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=60) as r:
|
|
out = json.load(r)
|
|
except urllib.error.HTTPError as e:
|
|
sys.exit("HTTP %s: %s" % (e.code, e.read().decode("utf-8", "replace")))
|
|
|
|
print(json.dumps(out, indent=2, ensure_ascii=False))
|
|
|
|
a = out["answers"]
|
|
print("\n--- ce ar face codul cu asta ---")
|
|
g = a["gestiune"]
|
|
print("gestiune: %s (conf %.2f) -> %s" % (
|
|
g["choice"], g["confidence"],
|
|
"aplica automat" if g["confidence"] >= 0.80 and g["choice"] != "niciuna" else "trimite la operator"))
|
|
print("serviciu: p=%.2f" % a["e_serviciu"]["noul"])
|
|
print("claritate: %.2f / 2" % a["claritate"]["score"])
|