forked from danasaur/nlp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgpt.py
67 lines (48 loc) · 1.73 KB
/
chatgpt.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
import json
import os
from pathlib import Path
import openai
import requests
openai.organization = os.getenv("OPENAI_ORGANIZATION")
openai.api_key = os.getenv("OPENAI_API_KEY")
# openai.Model.list()
URL = "https://api.openai.com/v1/chat/completions"
def ask_gpt(prompt, model, n_calls, save_path_base_filename):
"""make a request to the openai api with a prompt for a given model
Args:
prompt (str): prompt to pass to chatgpt
model (str): "gpt-3.5-turbo" or "gpt-4"
ref for latest options: https://platform.openai.com/docs/models/gpt-4
n_calls (int): number of times to call the openai api
save_path_base_filename (str): where to save the output
"""
payload = {
"model": model,
"messages": [{"role": "user", "content": prompt}],
"temperature" : 1.0,
"top_p":1.0,
"n" : 1,
"stream": False,
"presence_penalty":0,
"frequency_penalty":0,
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {openai.api_key}"
}
for n in range(n_calls):
save_path_filename = f"{save_path_base_filename}_{n}.json"
response = requests.post(URL, headers=headers, json=payload, stream=False)
print(response.content)
with open(save_path_filename, "w+") as f:
json.dump(response.json(), f)
if __name__ == "__main__":
model = "gpt-3.5-turbo"
prompt = "Write 10 haikus about cats"
n_calls = 10
base_filename = "cat_haikus_10"
save_path = "./data/gpt_generated/cat_haikus"
Path(save_path).mkdir(parents=True, exist_ok=True)
save_path_base_filename = f"{save_path}/{base_filename}"
print(save_path_base_filename)
ask_gpt(prompt, n_calls, save_path_base_filename)