-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieve_raw_data.py
85 lines (77 loc) · 2.6 KB
/
retrieve_raw_data.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import sqlite3
import openai
from time import sleep
# The API Key is expected to exist in a file,
# called 'config.py' (in the same directory as this script),
# and to contain a variable named api_key
# with the value of your API Key.
from config import api_key
def init_raw_data_table(cur):
# Create a table in the SQL Database to store
# raw_responses from the ChatGPT.
return cur.execute('''
CREATE TABLE IF NOT EXISTS Responses (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
response TEXT
)
''')
def request_number():
# Make a request to ChatGPT throught it's API
# asking for a number to get back it's response.
openai.api_key = api_key
api_response = openai.ChatCompletion.create(
model = 'gpt-3.5-turbo',
messages = [
{ 'role' : 'system', 'content' : 'number' },
{ 'role' : 'user', 'content' : 'give a number' },
]
)
return api_response.to_dict_recursive()['choices'][0]['message']['content']
def retrieve_data_point(cur):
# A single data point gets collected and stored in the DB.
raw_response = request_number()
print(raw_response)
cur.execute('INSERT INTO Responses (response) VALUES ( ? )', ( raw_response, ))
return raw_response
def input_getter():
while True:
try:
n = input("How many data points to collect? ")
except KeyboardInterrupt:
print('\nUser Interrupt.')
exit()
try:
n = int(n)
except:
print('Unexpected input. Try again.')
continue
if n < 0:
print('..invalid input. Give a non-negatige number.')
continue
else:
break
return n
def retrieve_raw_data():
# Ask user for the size of the sample
n = input_getter()
# Init Connection to SQLite DB
conn = sqlite3.connect('./data/rawdb.sqlite')
cur = conn.cursor()
# Init Table with Raw Data (if does not exists)
init_raw_data_table(cur)
# Collect n data points
for i in range(n):
try:
retrieve_data_point(cur)
except KeyboardInterrupt:
print('\nUser Interrupt.')
exit()
except:
# When a request fails for any reason
# the process hangs for a minute
# Mainly this is to conform with the ChatGPT's API
# that accepts either 20 or 30 request per minute
sleep(60)
conn.commit()
# Close Connection to SQLite3
conn.close()