-
Notifications
You must be signed in to change notification settings - Fork 0
/
prints.py
212 lines (174 loc) · 6.42 KB
/
prints.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
import time
from datetime import datetime
# Print Assistants
def print_all_assistants(client):
assistants = client.beta.assistants.list()
for assistant in assistants.data:
human_time = datetime.fromtimestamp(assistant.created_at).strftime(
"%Y-%m-%d %H:%M:%S"
)
print(
f"ID: {assistant.id}\n",
f"Name: {assistant.name}\n",
f"Description: {assistant.description}\n",
f"Model: {assistant.model}\n",
# f"Instructions: {assistant.instructions}\n",
f"Tools: {assistant.tools}\n",
f"Created At: {human_time}\n",
)
def print_assistant(assistant):
human_time = datetime.fromtimestamp(assistant.created_at).strftime(
"%Y-%m-%d %H:%M:%S"
)
print(
f"ID: {assistant.id}, Name: {assistant.name}, Model: {assistant.model}\n",
# f"Description: {assistant.description}\n",
# f"Instructions: {assistant.instructions}\n",
f"Tools: {assistant.tools}\n",
f"Tool Resources: {assistant.tool_resources}\n",
f"Created At: {human_time}\n",
)
# Print Threads
def print_all_threads():
...
return None
def print_thread(thread):
human_time = datetime.fromtimestamp(thread.created_at).strftime("%Y-%m-%d %H:%M:%S")
print(
f"ID: {thread.id}\n",
f"Tool Resources: {thread.tool_resources}\n",
# f"Metadata: {thread.metadata}\n",
f"Created At: {human_time}\n",
)
# Print Messages
def print_all_messages(client, thread_id):
try:
messages = client.beta.threads.messages.list(thread_id=thread_id)
message_counter = 0
for message in messages.data:
print(
f"Message {message_counter} ---> Role: {message.role}, ID: {message.id}"
)
print(message.content[0].text.value, "\n")
message_counter += 1
except Exception:
print("No messages")
print(f"--- Total Messages: {message_counter} ---\n")
def print_last_message(client, thread_id):
messages = client.beta.threads.messages.list(thread_id=thread_id)
last_message = messages.data[-1]
print(last_message.content[0].text.value)
# Print Runs
def print_all_runs(client, thread_id):
runs = client.beta.threads.runs.list(thread_id=thread_id)
for run in runs.data:
print(
f"Run ID: {run.id}, Status: {run.status}, Assistant ID: {run.assistant_id}"
)
# Print Steps
def print_run_steps(client, thread_id, run_id):
run_steps = client.beta.threads.runs.steps.list(thread_id=thread_id, run_id=run_id)
for step in run_steps.data:
step_counter = 0
while True:
if step.completed_at:
print(f"Total Steps: {step_counter}")
print(
f"Step {step_counter} ---> ID: {step.id}, Type: {step.type}, Status: {step.status}"
)
step_counter += 1
break
else:
print(f"Step ---> ID: {step.id}, Status: {step.status}")
time.sleep(5)
# Print Response
def print_response(client, thread_id):
while True:
time.sleep(2)
messages = client.beta.threads.messages.list(thread_id=thread_id)
for message in messages.data:
try:
message.role == "assistant"
response = message.content[0].text.value
print(f"Assistant: {response}")
break
except Exception:
print("No response from the assistant.")
break
# Print Files
def list_files(client):
files_list = []
if not client.files.list().data:
print("No files found.")
return
files = client.files.list()
for counter, file in enumerate(files.data, start=1):
file_info = {
"id": file.id,
"filename": file.filename,
"purpose": file.purpose,
"bytes": file.bytes,
"status": file.status,
}
files_list.append(file_info)
print(
f"\nTotal Files: {len(files_list)}",
"-" * (100 - (len("Total Files: ") + len(str(len(files_list))))),
"\n"
+ f'{"No.":<5}{"Filename":<30}{"ID":<32}{"Size":<7}{"":<5}{"Purpose":<12}{"Status":<8}',
"\n" + "-" * 100,
)
for i, file_info in enumerate(files_list):
# Convert bytes to MB for proper display
size_mb = file_info["bytes"] / (1024 * 1024) # Convert bytes to MB
print(
f"{i + 1:<5}{file_info['filename']:<30}{file_info['id']:<32}"
+ f"{size_mb:<7.2f}{"MB":<5}{file_info['purpose']:<12}{file_info['status']:<8}"
)
print("")
return files_list
# Print User Files and Folders
def list_user_files(extension_list: list[str] = ['.pdf']) -> list[str]:
user_files_list = []
return user_files_list
def list_user_folders() -> list[str]:
user_folders_list = []
return user_folders_list
def print_files_and_folders(user_files_list: list[str], user_folders_list: list[str]) -> None:
...
pass
# Print Vector Stores
def list_vector_stores(client) -> list[str]:
vector_stores_list = []
vector_stores = client.beta.vector_stores.list()
for vector_store in vector_stores.data:
print(f"ID: {vector_store.id}")
vector_stores_list.append(vector_store.id)
return vector_stores_list
# Print Vector Store Files
def print_vs_n_files(client, vector_store):
print(f"\nID: {vector_store.id}, Name: {vector_store.name}, "
+ f"Usage Bytes: {vector_store.usage_bytes}, "
+ f"Status: {vector_store.status},\n"
+ f"File Counts: {vector_store.file_counts}\n"
+ f"Expires After: {vector_store.expires_after}, "
+ f"Expires At: {vector_store.expires_at}, "
+ f"Last Active: {vector_store.last_active_at}")
vs_files = client.beta.vector_stores.files.list(
vector_store_id=vector_store.id
)
print(f"Total Files: {len(vs_files.data)}")
for vs_file in vs_files.data:
print(
f"ID: {vs_file.id}, bytes: {vs_file.usage_bytes}, status: {vs_file.status}"
)
def list_vs_n_files(client, vector_stores=None) -> None:
# vs_n_files_list = []
if vector_stores is None:
vector_stores = client.beta.vector_stores.list()
for vector_store in vector_stores:
print_vs_n_files(client, vector_store)
return
else:
print_vs_n_files(client, vector_stores)
return None