jerome-white commited on
Commit
3f23604
1 Parent(s): bab7f2e

Take name into account when cleaning

Browse files
Files changed (2) hide show
  1. requirements.txt +1 -0
  2. tools/resource-cleanup.py +27 -17
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  gradio
2
  openai
3
  pandas
 
 
1
  gradio
2
  openai
3
  pandas
4
+ scipy
tools/resource-cleanup.py CHANGED
@@ -1,5 +1,7 @@
1
  import time
2
- from argparse imAAport ArgumentParser
 
 
3
 
4
  from scipy import constants
5
  from openai import OpenAI
@@ -26,28 +28,36 @@ class HourAgeCheck(AgeCheck):
26
  age = (other - self.now) * constants.hour
27
  return age < self.limit
28
 
 
 
 
 
 
 
 
 
 
 
29
  if __name__ == '__main__':
30
  arguments = ArgumentParser()
 
31
  arguments.add_argument('--max-age-hours', type=int)
32
  args = arguments.parse_args()
33
 
34
- client = OpenAI()
 
 
35
  if args.max_age_hours is None:
36
  acheck = NoAgeCheck()
37
  else:
38
  acheck = HourAgeCheck(args.max_age_hours)
39
-
40
- while True:
41
- assistants = client.beta.assistants.list()
42
- for a in assistants:
43
- if a.created_at in acheck:
44
- continue
45
- if a.tool_resources.file_search is not None:
46
- for i in a.tool_resources.file_search.vector_store_ids:
47
- Logger.warning(f'{a.id} {i}')
48
- vsm = VectorStoreManager(i)
49
- vsm.cleanup()
50
- client.beta.assistants.delete(a.id)
51
-
52
- if not assistants.has_more:
53
- break
 
1
  import time
2
+ import json
3
+ from pathlib import Path
4
+ from argparse import ArgumentParser
5
 
6
  from scipy import constants
7
  from openai import OpenAI
 
28
  age = (other - self.now) * constants.hour
29
  return age < self.limit
30
 
31
+ def assistants(client, age_limit, name):
32
+ while True:
33
+ page = client.beta.assistants.list()
34
+ for a in page:
35
+ if a.name == name and a.created_at not in age_limit:
36
+ yield a
37
+
38
+ if not page.has_more:
39
+ break
40
+
41
  if __name__ == '__main__':
42
  arguments = ArgumentParser()
43
+ arguments.add_argument('--config', type=Path)
44
  arguments.add_argument('--max-age-hours', type=int)
45
  args = arguments.parse_args()
46
 
47
+ config = (json
48
+ .loads(args.config.read_text())
49
+ .get('openai'))
50
  if args.max_age_hours is None:
51
  acheck = NoAgeCheck()
52
  else:
53
  acheck = HourAgeCheck(args.max_age_hours)
54
+ name = config['assistant_name']
55
+ client = OpenAI(api_key=config['api_key'])
56
+
57
+ for a in assistants(client, acheck, name):
58
+ if a.tool_resources.file_search is not None:
59
+ for i in a.tool_resources.file_search.vector_store_ids:
60
+ Logger.info(f'{a.id} {i}')
61
+ vsm = VectorStoreManager(i)
62
+ vsm.cleanup()
63
+ client.beta.assistants.delete(a.id)