-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
69 lines (62 loc) · 2.28 KB
/
api.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
63
64
65
66
67
68
69
import requests
from typing import List, Optional, Dict, Any
class Message:
def __init__(self, role: str, content: str, name: Optional[str] = None):
self.role = role
self.content = content
self.name = name
def to_dict(self):
if self.name is not None:
return {
"role": self.role,
"name": self.name,
"content": self.content,
}
else:
return {
"role": self.role,
"content": self.content,
}
class ChatCompletion:
def __init__(self, data: Dict[str, Any]):
self.id = data['id']
self.object = data['object']
self.created = data['created']
self.model = data['model']
self.usage = data['usage']
self.choices = data['choices']
def chat_completion(api_key: str, model: str, messages: List[Message], temperature: Optional[float] = None, n: Optional[int] = None) -> ChatCompletion:
url = "https://api.openai.com/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}",
}
data = {
"model": model,
"messages": [message.to_dict() if isinstance(message, Message) else message for message in messages],
# "temperature": temperature,
# "n": n,
}
# print(data)
response = requests.post(url, json=data, headers=headers)
if response.status_code != 200:
raise Exception(f"Failed to complete chat: {response.status_code} {response.reason}")
response_data = response.json()
return ChatCompletion(response_data)
class OpenAIChatModel:
def __init__(self, api_key: str, model: str = "gpt-3.5-turbo"):
self.api_key = api_key
self.model = model
def chat_complete(self, prompt_messages: List[Message], stop: List[str] = []) -> Message:
response = chat_completion(self.api_key, self.model, prompt_messages)
return Message(**response.choices[0]['message'])
if __name__ == "__main__":
proxy = "http://127.0.0.1:9999"
import os
os.environ["http_proxy"] = proxy
os.environ["https_proxy"] = proxy
api = OpenAIChatModel("sk-xxxxxxxxx")
msg = api.chat_complete([
Message("user", "hello!"),
])
print(msg.__dict__)