-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
406 lines (332 loc) · 16.7 KB
/
server.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import json
import backend_import_cfg
backend_import_cfg.import_expensive_modules = True
import tc_peek_backend as backend
from flask import request, Flask, render_template
sess = backend.Session()
sess.name = 'Untitled'
# create a default feature list
default_feature_list_idx = sess.create_feature_list(name="Default feature list", description=None)
# create a default prompt
first_prompt_idx = sess.create_new_prompt(name="My first prompt", description=None)
sess.change_cur_feature_list_for_prompt(first_prompt_idx, default_feature_list_idx)
app = Flask(__name__, template_folder='.')
@app.get("/style.css")
def stylefile():
with open("./style.css", "r") as fp:
return fp.read()
@app.get('/')
def index():
return render_template('./index.html')
@app.put('/session/render')
def render():
try:
outpath = request.json['path']
except KeyError:
outpath = 'output.html'
try:
sess.render(outpath=outpath)
except Exception as e:
return {"error": str(e)}, 500
return {}, 204
# set up routes for all the Session methods
@app.post("/model")
def load_model():
try:
name = request.json['name']
except KeyError:
name = None
dtype_str = request.json.get('dtype_str', None)
try:
sess.load_model_from_pretrained(name, dtype_str=dtype_str)
except ValueError:
# TODO: use standardized error codes instead of strings
return {'error': 'Invalid model name'}, 400
return {}, 204
@app.get('/model')
def get_model_info():
return sess.get_model_info()
@app.get('/prompts')
def list_info_for_all_prompts():
return sess.list_info_for_all_prompts()
@app.get('/prompts/<int:idx>')
def list_info_for_prompt(idx):
return sess.list_info_for_prompt_by_id(idx)
@app.delete('/prompts/<int:idx>')
def delete_prompt(idx):
sess.delete_prompt_by_id(idx)
return {}, 204
@app.post('/prompts')
def create_new_prompt():
name = request.json.get('name', None)
description = request.json.get('description', None)
new_id = sess.create_new_prompt(name=name, description=description)
return {'prompt_id': new_id}
@app.get('/saes')
def list_info_for_all_saes():
return sess.list_info_for_all_saes()
@app.delete('/saes/<int:idx>')
def delete_sae_by_id(idx):
sess.delete_sae_by_id(idx)
return {}, 204
@app.post('/saes')
def load_sae_from_path():
try:
if 'hf_repo' in request.json:
hf_repo = request.json['hf_repo']
new_id = sess.load_saes_from_hf_repo(hf_repo)
elif 'path' in request.json:
path = request.json['path']
new_id = sess.load_sae_from_path(path)
except Exception as e:
print(traceback.format_exc())
return {'error': str(e)}, 500
return {'sae_id': new_id}
@app.put('/saes/<int:idx>')
def rename_sae(idx):
name = request.json['name']
sess.rename_sae(idx, name)
return {}, 204
@app.get('/feature_lists')
def list_info_for_all_feature_lists():
return sess.list_info_for_all_feature_lists()
@app.delete('/feature_lists/<int:idx>')
def delete_feature_list_by_id(idx):
sess.delete_feature_list_by_id(idx)
return {}, 204
@app.post('/feature_lists')
def create_feature_list():
name = request.json.get('name', None)
description = request.json.get('description', None)
copy_from_id = request.json.get('copy_from_id', None)
if copy_from_id is not None: copy_from_id = int(copy_from_id)
new_id = sess.create_feature_list(name=name, description=description, copy_from_id=copy_from_id)
return {'feature_list_id': new_id}
@app.put('/feature_lists/<int:idx>')
def rename_feature_list(idx):
name = request.json.get('name', None)
description = request.json.get('description', None)
sess.rename_feature_list(idx, name=name, description=description)
return {}, 204
@app.put('/prompts/<int:idx>')
def rename_prompt(idx):
name = request.json.get('name', None)
description = request.json.get('description', None)
sess.rename_prompt(idx, name=name, description=description)
return {}, 204
@app.put('/prompts/<int:idx>/run')
def run_model_on_prompt_text(idx):
text = request.json.get('text')
tokens = sess.run_model_on_prompt_text(idx, text)
return {'tokens': tokens}
@app.get('/prompts/<int:idx>/feature_list')
def get_cur_feature_list_idx_for_prompt(idx):
idx = sess.get_cur_feature_list_idx_for_prompt(idx)
return {'feature_list_id': idx}
@app.put('/prompts/<int:idx>/feature_list')
def change_cur_feature_list_for_prompt(idx):
feature_list_idx = request.json['feature_list_idx']
sess.change_cur_feature_list_for_prompt(idx, feature_list_idx)
return {}, 204
@app.get('/feature_lists/<int:feature_list_idx>/features/<int:feature_idx>')
def get_feature_info_dict_by_idx(feature_list_idx, feature_idx):
return get_feature_info_dict_by_idx(self, feature_list_idx, feature_idx)
@app.get('/feature_lists/<int:feature_list_idx>')
def get_feature_list_info(feature_list_idx):
return json.dumps(sess.get_feature_list_info(feature_list_idx))
@app.post('/feature_lists/<int:feature_list_idx>/features')
def add_feature(feature_list_idx):
feature_type = request.json['feature_type']
name = request.json.get('name', None)
description = request.json.get('description', None)
if feature_type == 'observable':
observable_tokens = request.json['observable_tokens']
observable_weights = request.json['observable_weights']
do_unembed_pullback = request.json.get('do_unembed_pullback', False)
try:
new_id = sess.add_feature_from_observable( feature_list_idx, observable_tokens, observable_weights, name=name, description=description, do_unembed_pullback=do_unembed_pullback)
except Exception as e:
return {'error': str(e)}, 400
elif feature_type == 'sae':
sae_idx = request.json['sae_idx']
sae_feature_idx = request.json['sae_feature_idx']
new_id = sess.add_feature_from_sae(feature_list_idx, sae_idx, sae_feature_idx, name=name, description=description)
else:
return {'error': f'Invalid feature type: {feature_type}'}, 400
return {'feature_id': new_id}
@app.delete('/feature_lists/<int:feature_list_idx>/features/<int:feature_idx>')
def remove_feature_from_list(feature_list_idx, feature_idx):
sess.remove_feature_from_list(feature_list_idx, feature_idx)
return {}, 204
@app.put('/feature_lists/<int:feature_list_idx>/features/<int:feature_idx>')
def rename_feature(feature_list_idx, feature_idx):
name = request.json.get('name', None)
description = request.json.get('description', None)
sess.rename_feature(feature_list_idx, feature_idx, name=name, description=description)
return {}, 204
@app.get('/prompts/<int:prompt_idx>/feature_activs/feature_lists/<int:feature_list_idx>/features/<int:feature_idx>')
def get_feature_activs_on_prompt(prompt_idx, feature_list_idx, feature_idx):
feature_activs = sess.get_feature_activs_on_prompt(prompt_idx, feature_list_idx, feature_idx)
return feature_activs
@app.get('/prompts/<int:prompt_idx>/tokens/<int:token_pos>/feature_lists/<int:feature_list_idx>')
def get_feature_list_activs_on_token(prompt_idx, token_pos, feature_list_idx):
return sess.get_feature_list_activs_on_token(prompt_idx, token_pos, feature_list_idx)
@app.get('/prompts/<int:prompt_idx>/tokens/<int:token_pos>/saes/<int:sae_idx>')
def get_sae_activs_on_token(prompt_idx, token_pos, sae_idx):
k = request.args.get('k', None)
if k is not None: k = int(k)
return sess.get_sae_activs_on_token(prompt_idx, token_pos, sae_idx, k=k)
@app.put('/prompts/<int:prompt_idx>/tokens/<int:token_pos>/features/<int:feature_idx>/set_current_path')
def set_cur_comp_path_to_feature(prompt_idx, token_pos, feature_idx):
sess.set_cur_comp_path_to_feature(prompt_idx, token_pos, feature_idx)
return {}, 204
@app.get('/prompts/<int:prompt_idx>/comp_paths')
def list_comp_paths_for_prompt(prompt_idx):
return sess.list_comp_paths_for_prompt(prompt_idx)
@app.delete('/prompts/<int:prompt_idx>/comp_paths/<path_idx>')
def delete_comp_path_by_id(prompt_idx, path_idx):
sess.delete_comp_path_by_id(prompt_idx, path_idx)
return {}, 204
@app.put('/prompts/<int:prompt_idx>/comp_paths/<path_idx>/nodes/<int:feature_pos>')
@app.put('/prompts/<int:prompt_idx>/comp_paths/<path_idx>', defaults={'feature_pos': -1})
def select_and_view_comp_path(prompt_idx, path_idx, feature_pos):
top_k_children = request.args.get('k', 7)
top_mlp_k = request.args.get('top_mlp_k', 5) #TODO: set to None by default; allow this to be changed client-side
if path_idx == 'default': path_idx = None
else: path_idx = int(path_idx)
return sess.select_and_view_comp_path(prompt_idx, path_idx=path_idx, feature_pos=feature_pos, top_k_children=top_k_children, top_mlp_k=top_mlp_k)
@app.post('/prompts/<int:prompt_idx>/comp_paths/<path_idx>/nodes/<int:node_idx>/extend')
@app.post('/prompts/<int:prompt_idx>/comp_paths/<path_idx>/extend', defaults={'node_idx': -1})
def get_child_attrib_for_comp_path(prompt_idx, path_idx, node_idx):
top_k_children = request.args.get('k', 7)
if path_idx == 'default': path_idx = None
else: path_idx = int(path_idx)
return sess.get_child_attrib_for_comp_path(prompt_idx, request.json, path_idx=path_idx, top_k_children=top_k_children, extend=True, cur_node_idx=node_idx)
@app.post('/prompts/<int:prompt_idx>/comp_paths/<path_idx>/replace')
def replace_comp_path_with_component(prompt_idx, path_idx):
top_k_children = request.args.get('k', 7)
if path_idx == 'default': path_idx = None
else: path_idx = int(path_idx)
return sess.get_child_attrib_for_comp_path(prompt_idx, request.json, path_idx=path_idx, top_k_children=top_k_children, extend=False)
@app.post('/prompts/<int:prompt_idx>/comp_paths')
def save_cur_comp_path(prompt_idx):
name = request.json.get('name', None)
description = request.json.get('description', None)
new_id = sess.save_cur_comp_path(prompt_idx, name=name, description=description)
return {'comp_path_id': new_id}
@app.put('/prompts/<int:prompt_idx>/comp_paths/<int:path_idx>/rename')
def update_comp_path(prompt_idx, path_idx):
name = request.json.get('name', None)
description = request.json.get('description', None)
sess.update_comp_path(prompt_idx, path_idx, name=name, description=description)
return {}, 204
@app.delete('/prompts/<int:prompt_idx>/comp_paths/<int:path_idx>')
def remove_comp_path(prompt_idx, path_idx):
sess.remove_comp_path(prompt_idx, path_idx)
return {}, 204
@app.get('/prompts/<int:prompt_idx>/comp_paths/<path_idx>/nodes/<int:feature_pos>')
@app.get('/prompts/<int:prompt_idx>/comp_paths/<path_idx>', defaults={'feature_pos': -1})
def get_feature_info_from_comp_path(prompt_idx, path_idx, feature_pos):
top_k_children = request.args.get('k', None)
if path_idx == 'default': path_idx = None
else: path_idx = int(path_idx)
return sess.get_feature_info_from_comp_path(prompt_idx, path_idx=path_idx, feature_pos=feature_pos)
"""@app.post('/prompts/<int:prompt_idx>/comp_paths/<path_idx>/nodes/<int:feature_pos>/add_to_feature_list/<int:feature_list_idx>')
@app.post('/prompts/<int:prompt_idx>/comp_paths/<path_idx>/add_to_feature_list/<int:feature_list_idx>', defaults={'feature_pos': -1})
def add_feature_from_comp_path_to_feature_list(prompt_idx, path_idx, feature_list_idx, feature_pos):"""
@app.post('/prompts/<int:prompt_idx>/comp_paths/default/nodes/<int:feature_pos>/add_to_feature_list')
@app.post('/prompts/<int:prompt_idx>/add_feature_from_cur_comp_path', defaults={'feature_pos': -1})
def add_feature_from_comp_path_to_feature_list(prompt_idx, feature_pos):
name = request.json.get('name', None)
description = request.json.get('description', None)
new_id = sess.add_feature_from_comp_path_to_feature_list(prompt_idx, None, None, feature_pos, name=name, description=description)
return {'feature_id': new_id}
@app.put('/prompts/<int:prompt_idx>/comp_paths/default/nodes/<int:node_idx>/rename')
def update_comp_path_node(prompt_idx, node_idx):
name = request.json.get('name', None)
description = request.json.get('description', None)
sess.update_comp_path_node(prompt_idx, node_idx, name, description)
return {}, 204
"""@app.get('/prompts/<int:prompt_idx>/comp_paths/<path_idx>/nodes/<int:feature_pos>/input_invar_features')
@app.get('/prompts/<int:prompt_idx>/comp_paths/<path_idx>/input_invar_features', defaults={'feature_pos': -1})
def top_input_invar_features_from_comp_path(prompt_idx, path_idx, feature_pos):
sae_idx = int(request.args['sae_idx'])
k = int(request.args['k'])
return sess.top_input_invar_features_from_comp_path(prompt_idx, path_idx, sae_idx, feature_pos=feature_pos, k=k)"""
@app.get('/feature_lists/<int:feature_list_idx>/features/<int:feature_idx>/input_invar_features')
def top_input_invar_features_from_feature_list(feature_list_idx, feature_idx):
sae_idx = int(request.args['sae_idx'])
k = int(request.args['k'])
return sess.top_input_invar_features_from_feature_list(feature_list_idx, feature_idx, sae_idx, feature_pos=feature_pos, k=k)
@app.get('/prompts/<int:prompt_idx>/comp_paths/<path_idx>/nodes/<int:feature_pos>/deembeddings')
@app.get('/prompts/<int:prompt_idx>/comp_paths/<path_idx>/deembeddings', defaults={'feature_pos': -1})
def top_deembeddings_from_comp_path(prompt_idx, path_idx, feature_pos):
k = int(request.args.get('k', 7))
if path_idx == 'default': path_idx = None
else: path_idx = int(path_idx)
return sess.top_deembeddings_from_comp_path(prompt_idx, path_idx, feature_pos=feature_pos, k=k)
@app.get('/prompts/<int:prompt_idx>/comp_paths/<path_idx>/nodes/<int:feature_pos>/input_invar')
@app.get('/prompts/<int:prompt_idx>/comp_paths/<path_idx>/input_invar', defaults={'feature_pos': -1})
def top_input_invar_features_from_comp_path(prompt_idx, path_idx, feature_pos):
k = int(request.args.get('k', 7))
sae_idx = int(request.args.get('sae_idx', 0))
if path_idx == 'default': path_idx = None
else: path_idx = int(path_idx)
return sess.top_input_invar_features_from_comp_path(prompt_idx, path_idx, sae_idx, feature_pos=feature_pos, k=k)
@app.get('/feature_lists/<int:feature_list_idx>/features/<int:feature_idx>/deembeddings')
def top_deembeddings_from_feature_list(feature_list_idx, feature_idx):
sae_idx = int(request.args['sae_idx'])
k = int(request.args['k'])
return sess.top_deembeddings_from_feature_list(feature_list_idx, feature_idx, sae_idx, feature_pos=feature_pos, k=k)
# steering vector stuff now
@app.get("/prompts/<int:prompt_idx>/steering_vectors")
def list_steering_vectors_for_prompt(prompt_idx):
return sess.list_steering_vectors_for_prompt(prompt_idx)
@app.post('/prompts/<int:prompt_idx>/comp_paths/default/nodes/<int:feature_pos>/add_steering_vector')
@app.post('/prompts/<int:prompt_idx>/add_steering_vector_from_cur_comp_path', defaults={'feature_pos': -1})
def add_steering_vector(prompt_idx, feature_pos):
name = request.json.get('name', None)
description = request.json.get('description', None)
steering_coefficient = float(request.json.get('steering_coefficient', 1.0))
do_clamp = bool(request.json.get('do_clamp', True))
use_encoder = bool(request.json.get('use_encoder', False))
all_tokens = bool(request.json.get('all_tokens', False))
new_id = sess.add_steering_vector_from_comp_path_to_prompt(prompt_idx, comp_path_idx=None, feature_pos=feature_pos, do_clamp=do_clamp, steering_coefficient=steering_coefficient, name=name, description=description, all_tokens=all_tokens, use_encoder=use_encoder)
return {'steering_vector_id': new_id}
@app.delete('/prompts/<int:prompt_idx>/steering_vectors/<int:steering_vector_idx>')
def remove_steering_vector(prompt_idx, steering_vector_idx):
sess.remove_steering_vector(prompt_idx, steering_vector_idx)
return {}, 204
@app.get('/prompts/<int:prompt_idx>/generate')
def generate_tokens(prompt_idx):
use_steering_vectors = not 'no_steering' in request.args
return {'new_tokens': sess.prompt_list.dict[prompt_idx].generate_tokens(sess.model, use_steering_vectors=use_steering_vectors, temperature=float(request.args.get('temperature', 0.8)))}
import traceback
@app.post('/session/load')
def load_session():
path = request.json['path']
try:
sess.load_wrapper(path)
except Exception as e:
return {"error": traceback.format_exc()}, 500
return {}, 204
@app.post('/session/save')
def save_session():
path = request.json['path']
sess.save_wrapper(path, save_feature_tensors=request.json.get('save_feature_tensors', True))
return {}, 204
@app.put('/session/rename')
def rename_session():
name = request.json.get('name', None)
description = request.json.get('description', None)
if name is not None: sess.name = name
if description is not None: sess.description = description
return {}, 204
@app.get("/session")
def get_session_details():
return {'name': sess.name, 'description': sess.description}
if __name__ == '__main__':
# the port number corresponds to the word "intrp" when input on a cell phone keypad
app.jinja_env.auto_reload = True
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.run(host="0.0.0.0", port=46877)