-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclaude.py
62 lines (50 loc) · 1.84 KB
/
claude.py
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
import os
import time
from anthropic import Anthropic
class Claude:
def __init__(self, model_name="claude-3-5-sonnet-20241022", configs=None) -> None:
'''
model_name: api version of a claude model (https://docs.anthropic.com/en/docs/about-claude/models)
'''
self.model_name = model_name
self.client = Anthropic(
base_url=os.environ.get("BASE_URL"),
api_key=os.environ.get("API_KEY")
)
self.config = {
'temperature': 1,
'top_p': 1,
'max_tokens': 1024
}
if configs is not None:
self.config = configs
def fit_message(self, msg, system_prompt=None):
if system_prompt is not None:
conversation = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": msg}
]
else:
conversation = [
{"role": "user", "content": msg}
]
return conversation
def query(self, msg, system_prompt=None, **kwargs):
start = time.time()
while True:
try:
raw_response = self.client.messages.create(
model=self.model_name,
messages=self.fit_message(msg, system_prompt),
**kwargs)
self.raw_response = raw_response
return raw_response.content[0].text
except Exception as e:
print(e)
current = time.time()
if current - start > 60: return None
time.sleep(10)
def __call__(self, msg, system_prompt=None):
return self.query(msg, system_prompt, **self.config)
# chatbot = Claude()
# print(chatbot("Hello, Claude"))