-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
235 lines (178 loc) · 6.96 KB
/
utils.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import numpy as np
import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
import json
import openai
import random
import os
import copy
import collections
import scipy.stats as stats
import netgraph
import powerlaw as pwl
import seaborn as sns
import replicate
import anthropic
with open('params.json') as f:
params = json.load(f)
openai.api_key = params['OPENAI_API_KEY']
openai.organization = params['OPENAI_ORG']
MEDIUM_SIZE = 24
SMALL_SIZE = 0.85 * MEDIUM_SIZE
BIGGER_SIZE = 1.5 * MEDIUM_SIZE
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
claude_client = anthropic.Anthropic(api_key=params['ANTHROPIC_API_KEY'])
replicate_client = replicate.Client(api_token=params['REPLICATE_API_KEY'])
openai_client = openai.Client(api_key=params['OPENAI_API_KEY'])
def get_response(prompt, model, temperature=0.9, system_prompt="You are mimicking a real-life person who wants to make friends."):
if model.startswith('gpt'):
result = openai_client.chat.completions.create(
model=model,
temperature=temperature,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt},
])
return result.choices[0].message.content
elif model.startswith('claude'):
global claude_client
result = claude_client.messages.create(
model = model,
temperature = temperature,
system = system_prompt,
max_tokens = 1000,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt
}
]
}
])
return result.content[0].text
else:
global replicate_client
replicate_input = {
'prompt' : prompt,
'temperature' : temperature,
}
result = replicate_client.run(model, replicate_input)
return ''.join(result)
def summarize_reasons(filename, model, outfile, title, n_samples=20, n_categories=5, n_resamples=5, degrees=False, categories=None):
random.seed(1)
np.random.seed(1)
suffix = os.path.splitext(filename)[0]
with open(filename) as f:
lines = f.read().splitlines()
data = []
for line in lines:
data.append(json.loads(line))
reason_list = collections.defaultdict(list)
all_reasons = []
for d in data:
for result in d["reasons"]:
if result and 'reason' in result.keys():
reason_list[d['temperature']].append(result['reason'])
all_reasons.append(result['reason'])
if categories is None:
categorization_prompt = f"""
# Task
You are given a list of reasons and your task to find {n_categories} categories that best describe the reasons.
# Input
The input is a list of reasons. The list is given below after chevrons:
<REASONS>
{json.dumps(random.sample(all_reasons, len(reason_list) * n_samples))}
</REASONS>
# Output
The output should be given in JSON format with the following structure:
[
{{
"category" : category,
"description" : short description of the category
}}, ...
]
# Notes
* The names of the categories must be descriptive and mutually exclusive.
```json
"""
for _ in range(10):
try:
ans = get_response(categorization_prompt, temperature=0, system_prompt="You are a helpful assistant", model=model)
categories = json.loads(ans.split('```')[0])
print(categories)
break
except Exception as e:
print(e)
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
category_list = [c['category'] for c in categories]
records = []
for i, (k, v) in enumerate(reason_list.items()):
print('Temperature', k)
if len(v) <= n_samples:
n_resamples = 1
for r in range(n_resamples):
prompt = f"""
# Task
You are given a list of reasons and your task is to classify them into categories.
# Input
The input is a list of reasons. The list is given below after chevrons:
<REASONS>
{json.dumps(random.sample(v, n_samples), indent=4)}
</REASONS>
## Categories
The names of the categories are given below after chevrons:
<CATEGORIES>
{json.dumps(categories, indent=4)}
</CATEGORIES>
Each reason must be assigned to exactly one of the categories.
# Output
The output should be given as a list of JSON objects with the following structure:
[
{{
"reason" : reason,
"category" : category name
}}, ...
]
```json
"""
for _ in range(10):
try:
ans = get_response(prompt, temperature=0, system_prompt="You are a helpful assistant", model=model)
try:
result = json.loads(ans.split('```')[0])
except:
result = json.loads(ans.split('```json')[1].split('```')[0])
assert(isinstance(result, list))
reason_types = collections.defaultdict(float)
total = 0
for reason in result:
if reason['category'] in category_list:
reason_types[reason['category']] += 1
total += 1
break
except Exception as e:
print(e)
for key, val in reason_types.items():
records.append({
'Temperature' : k,
'Category' : key,
'Frequency' : val,
'Resample' : r
})
df = pd.DataFrame.from_records(records)
fig.suptitle(title, fontsize=MEDIUM_SIZE)
sns.barplot(data=df, x='Category', y='Frequency', hue='Temperature', ax=ax, palette='Set2')
plt.legend(fontsize=0.75*SMALL_SIZE, title='Temperature')
plt.xticks(rotation=0, fontsize=SMALL_SIZE)
fig.tight_layout()
fig.savefig(outfile, dpi=300, bbox_inches='tight')