Repository navigation
Expand file tree
/
Copy path01_test_call.py
More file actions
60 lines (47 loc) · 1.52 KB
/
Copy path01_test_call.py
File metadata and controls
60 lines (47 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Raw demo of a Decisions API call: a plain requests.post, no jev.py import."""
import json
import os
import requests
from dotenv import load_dotenv
load_dotenv()
URL = "https://openrouter.ai/api/alpha/decisions"
message = "Hi, I was charged twice for my order last week. Can I get a refund on those?"
question = {
"is_refund": {
"type": "noul",
"instructions": "Is the customer asking for a refund?",
"criteria": {
"true": "They want money back.",
"false": "They want something else.",
},
}
}
def main() -> None:
response = requests.post(
URL,
headers={"Authorization": f"Bearer {os.environ['OPENROUTER_API_KEY']}"},
json={
# Same model configuration source as jev.py: the MODEL entry in .env
"model": os.environ["MODEL"],
"state": message,
"questions": question,
},
timeout=60,
)
# Raise a clear HTTPError on 4xx/5xx instead of failing later on json parsing.
response.raise_for_status()
data = response.json()
print(json.dumps(data, indent=2))
try:
ans = data["answers"]["is_refund"]["noul"]
except KeyError:
raise RuntimeError(
f"Unexpected API response, no answers['is_refund']['noul']: {data}"
) from None
print(f"\n Chance this is a refund request: {ans:.0%}")
if ans > 0.5:
print("-> Sending to the refund team ")
else:
print("-> Normal support queue ")
if __name__ == "__main__":
main()