-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhello_claude.py
More file actions
63 lines (53 loc) · 1.85 KB
/
Copy pathhello_claude.py
File metadata and controls
63 lines (53 loc) · 1.85 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
61
62
63
import requests
MODEL = "anthropic/claude-haiku-4.5"
# Keep the LaunchCode-provided course key in a local file instead of pasting it into code.
try:
with open("claude_cred.txt", "r", encoding="utf-8") as f:
api_key = f.read().strip()
except FileNotFoundError:
raise SystemExit(
"Could not find claude_cred.txt. Create that file in this folder and paste in your LaunchCode-provided course key as the only content."
)
if not api_key:
raise SystemExit(
"claude_cred.txt is empty. Paste your LaunchCode-provided course key into that file as the only content."
)
payload = {
# This course key can access only the approved model listed here.
"model": MODEL,
"messages": [
{
"role": "user",
"content": "Hello, Claude. Please reply with a short greeting.",
}
],
}
try:
response = requests.post(
"https://openrouter.ai/api/v1/chat/completions",
json=payload,
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
},
timeout=60,
)
response.raise_for_status()
body = response.json()
except requests.exceptions.HTTPError as e:
status_code = e.response.status_code
error_body = e.response.text
if status_code == 401:
raise SystemExit(
"401 Unauthorized: check that claude_cred.txt contains only your LaunchCode-provided course key on a single line."
)
if status_code == 404:
raise SystemExit(
"404 from OpenRouter: your course key is expected to work only with anthropic/claude-haiku-4.5. See example-open-router-responses/example_404_guardrail_error.json."
)
raise SystemExit(f"{status_code} error from OpenRouter:\n{error_body}")
except requests.exceptions.RequestException as e:
raise SystemExit(
f"Could not reach OpenRouter: {e}. Check your internet connection and try again."
)
print(body["choices"][0]["message"]["content"])