-
Notifications
You must be signed in to change notification settings - Fork 1
/
GPT4.py
35 lines (26 loc) · 945 Bytes
/
GPT4.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
import requests
import json
API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
API_ENDPOINT = "https://api.openai.com/v1/chat/completions"
def generate_chat_completion(messages, model="gpt-4", temperature=1, max_tokens=None):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}",
}
data = {
"model": model,
"messages": messages,
"temperature": temperature,
}
if max_tokens is not None:
data["max_tokens"] = max_tokens
response = requests.post(API_ENDPOINT, headers=headers, data=json.dumps(data))
if response.status_code == 200:
return response.json()["choices"][0]["message"]["content"]
else:
raise Exception(f"Error {response.status_code}: {response.text}")
messages = [
{"role": "user", "content": "what openai model am I talking to?'"}
]
response_text = generate_chat_completion(messages)
print(response_text)