-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
72 lines (64 loc) · 1.8 KB
/
api.py
File metadata and controls
72 lines (64 loc) · 1.8 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
64
65
66
67
68
69
70
71
72
import anthropic
import dotenv
import os
from typing import TypedDict, Literal
import hashlib
dotenv.load_dotenv()
MODEL = "claude-opus-4-1-20250805"
API_KEY = os.getenv("CLAUDE_API_KEY")
CACHE_FOLDER = "cache"
client = anthropic.Anthropic(
api_key=API_KEY,
)
Message = TypedDict(
"Message",
{
"role": Literal["user", "assistant"],
"content": str,
},
)
def hash_messages(messages: list[Message], system_prompt: str) -> str:
hasher = hashlib.sha256()
hasher.update(system_prompt.encode("utf-8"))
for m in messages:
hasher.update(m["role"].encode("utf-8"))
hasher.update(m["content"].encode("utf-8"))
return hasher.hexdigest()
def sample(
messages: list[Message], system_prompt: str = "", max_tokens: int = 5000
) -> str:
os.makedirs(CACHE_FOLDER, exist_ok=True)
hash_value = hash_messages(messages, system_prompt)
cache_path = os.path.join(CACHE_FOLDER, f"{hash_value}.txt")
if os.path.exists(cache_path):
with open(cache_path, "r") as f:
print(f"Using cached response for {hash_value}")
return f.read()
api_messages = []
for m in messages:
api_messages.append(
{
"role": m["role"],
"content": [
{
"type": "text",
"text": m["content"],
}
],
}
)
response = client.messages.create(
model=MODEL,
max_tokens=max_tokens,
temperature=1,
system=system_prompt,
messages=api_messages,
)
try:
result = response.content[-1].text
with open(cache_path, "w") as f:
f.write(result)
return result
except Exception as e:
print(response)
raise e