-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatgpt-training-df-gen.qmd
182 lines (139 loc) · 3.53 KB
/
chatgpt-training-df-gen.qmd
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
---
title: "chatgpt training"
date: today
author: Marian Klose
execute:
echo: true
message: true
warning: true
format:
html:
toc: true
code-fold: show
code-tools: true
number-sections: true
embed-resources: true
---
# Preamble
## Library
```{python}
# load packages
import openai
import requests
import time
import json
import random
import re
import pandas as pd
from bs4 import BeautifulSoup
from pprint import pprint
# import own custom functions
from functions import *
```
## PAT / API Tokens
```{python}
# define pats and api tokens safely via gitignored file
from my_secrets import gh_api_token
from my_secrets import ai_api_token
```
## Global Options and Parameters
```{python}
# define if we want to actually create discussions or just test
create_dis_bool = False
# Define repository and category IDs
repository_id = 'R_kgDOKYhvWw'
category_id = 'DIC_kwDOKYhvW84CZobi'
# add headers to requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.3'
}
# define time until the request times out
timeout = 60
# define max number of retries when scraping
retries = 3
# define sleep time
sleep_time = 0.5
# Hardcoded dictionary for email classification categories
category_dict = {
"Announcements": "DIC_kwDOKYhvW84CZvWP",
"Open Positions": "DIC_kwDOKYhvW84CZvWQ",
"Technicalities": "DIC_kwDOKYhvW84CZvWX",
"Others": "DIC_kwDOKYhvW84CZvWY"
}
# define categories for eamil classification
categories = list(category_dict.keys())
# define chatgpt model
model = "gpt-3.5-turbo"
# model = "gpt-4"
# sample vector of message IDs
n_msg = 50
recent_id = 8691
my_array = list(range(1, recent_id))
msg_ids = random.sample(my_array, n_msg)
```
# Scraping
## Fetching List of Messages
```{python}
#| results: asis
# Convert numerics to strings and paste 0 in front
msg_ids = [str(num).zfill(5) for num in msg_ids]
# show
msg_ids
# Initialize an empty dictionary to hold the msg information
msg = {}
# Loop through each msg id, extract the details and append
for id in msg_ids:
# Fetch the details
single_msg = fetch_details(
msg_number = id,
headers = headers,
timeout = timeout,
retries = retries
)
# sleep for n seconds
time.sleep(sleep_time)
# Append to msg
msg[id] = single_msg
# Extract threads
thread_dict = extract_threads(msg)
# Fetch any missing messages in the threads
fetch_missing_messages(
thread_dict = thread_dict,
msg = msg,
headers = headers,
timeout = timeout,
retries = retries,
first_only = True
)
# print the result
pprint(thread_dict, indent=4)
```
## Create Training Dataset
```{python}
# init data list
data_list = []
# Loop through each element in the dictionary
for thread_id, thread_info in thread_dict.items():
# define initial message id
inital_msg_id = thread_info['ids'][0]
# retrieve initial thread starting message
cur_msg = msg[inital_msg_id]
# store data from message
data = {
'id': cur_msg['id'],
'date': cur_msg['date'],
'subject': cur_msg['subject'],
'message': cur_msg['message']
}
# append data to list
data_list.append(data)
# Convert the list of dictionaries into a pandas DataFrame
df = pd.DataFrame(data_list)
# add empty column for labels and category
df['category'] = ''
df['labels'] = ''
# Save the DataFrame to a CSV file
df.to_csv('train_df_raw.csv', index=False)
# print the result
pprint(df, indent=4)
```