-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassistants.py
232 lines (192 loc) · 7.01 KB
/
assistants.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
from dataclasses import dataclass
from enum import Enum
from typing import List
import streamlit as st
import pandas as pd
from openai import OpenAI
import os
class Tool(Enum):
CODEINTERPRETER = "code_interpreter"
FILESEARCH = "file_search"
function = "function"
@dataclass
class ToolFiles:
id: str
filename: str
purpose: str
vstore_id: str = None
@dataclass
class ToolResources:
resources_type: Tool = None
vector_ids: List[str] = None
files: List[ToolFiles] = None
@dataclass
class Assistant:
id: str
name: str
description: str
instructions: str
tools: List[Tool]
tool_resources: ToolResources
def toastMessage(message):
if message.deleted:
st.toast(f"Delete {message.object} {message.id}")
else:
st.toast(f"Error: delete {message.object} {message.id} failed")
def listAssistants():
try:
response = st.session_state.client.beta.assistants.list()
return response
except Exception as e:
print(e)
return e
def deleteAssistant(assistant_id):
try:
response = st.session_state.client.beta.assistants.delete(assistant_id)
return response
except Exception as e:
return e
def deleteFile(file_id):
try:
response = st.session_state.client.files.delete(file_id)
return response
except Exception as e:
return e
def deleteVectorStore(vector_store_id):
try:
response = st.session_state.client.beta.vector_stores.delete(vector_store_id)
return response
except Exception as e:
return e
def updateIdUseCounts(id):
if id not in st.session_state.idUseCounts:
st.session_state.idUseCounts[id] = 1
else:
st.session_state.idUseCounts[id] += 1
def getFileDetails(file_id, vector_store_id=None):
file_details = st.session_state.client.files.retrieve(file_id)
file: ToolFiles = ToolFiles(
id=file_details.id,
filename=file_details.filename,
purpose=file_details.purpose,
vstore_id=vector_store_id,
)
return file
def getFileFromVectorStore(vector_store_ids):
files = []
for vstore in vector_store_ids:
vector_store_files = st.session_state.client.beta.vector_stores.files.list(
vector_store_id=vstore
)
files.extend([getFileDetails(f.id, vstore) for f in vector_store_files.data])
return files
def setData(assistants):
data: List[Assistant] = []
for assistant in assistants:
tools: List[Tool] = [Tool(t.type) for t in assistant.tools]
resources: List[ToolResources] = []
if assistant.tool_resources.code_interpreter:
resources_tmp: ToolResources = ToolResources(
vector_ids=[],
files=[
getFileDetails(f)
for f in assistant.tool_resources.code_interpreter.file_ids
],
resources_type=Tool.CODEINTERPRETER,
)
for f in resources_tmp.files:
updateIdUseCounts(f.id)
resources.append(resources_tmp)
if assistant.tool_resources.file_search:
resources_tmp: ToolResources = ToolResources(
vector_ids=assistant.tool_resources.file_search.vector_store_ids,
files=getFileFromVectorStore(
assistant.tool_resources.file_search.vector_store_ids
),
resources_type=Tool.FILESEARCH,
)
for f in resources_tmp.files:
updateIdUseCounts(f.id)
for v in resources_tmp.vector_ids:
updateIdUseCounts(v)
resources.append(resources_tmp)
data.append(
Assistant(
id=assistant.id,
name=assistant.name,
description=assistant.description,
instructions=assistant.instructions,
tools=tools,
tool_resources=resources,
)
)
return data
st.set_page_config(layout="wide")
st.title("Assistants Management")
if "openai_api_key" not in st.session_state:
st.session_state.openai_api_key = ""
if "client" not in st.session_state:
st.session_state.client = None
if "idUseCounts" not in st.session_state:
st.session_state.idUseCounts = {}
with st.sidebar:
if os.environ.get("OPENAI_API_KEY"):
print("Using OpenAI API key from environment variable")
st.session_state.openai_api_key = os.environ.get("OPENAI_API_KEY")
else:
print("Using OpenAI API key from user input")
st.session_state.openai_api_key = st.text_input(
"OpenAI API Key", value=st.session_state.openai_api_key , key="open_api_key", type="password",
)
if st.button("Reload"):
st.rerun()
if not st.session_state.openai_api_key:
st.warning("Please provide your OpenAI API key")
st.stop()
st.session_state.client = OpenAI(api_key=st.session_state.openai_api_key)
### Assistants ###
with st.spinner("Loading assistants..."):
my_assistants = listAssistants()
print("ici ")
print(my_assistants)
data = setData(my_assistants.data)
# with st.spinner("Loading files and vector stores..."):
# fileslist = listFiles()
# vector_stores = listVectorStores()
for assistant in data:
with st.container(border=True):
col1, col2 = st.columns(2)
with col1:
with st.container(border=True):
st.write("**Assistant Name**: ", assistant.name)
st.write(" - ID: ", assistant.id)
st.button(
"Delete",
on_click=deleteAssistant,
args=[assistant.id],
key=assistant.id,
)
with col2:
with st.container(border=True):
st.write("**Description**: ", assistant.description)
st.write("**Instruction**: ", assistant.instructions)
st.write("**Tools**:")
with st.container(border=True):
for t in assistant.tools:
st.write(" - Type: ", t.value)
st.write("**Tool Resources**:")
for resource in assistant.tool_resources:
with st.container(border=True):
if resource.resources_type == Tool.CODEINTERPRETER:
st.write("Code Interpreter:")
if resource.resources_type == Tool.FILESEARCH:
st.write("File Search:")
vstores = [v for v in resource.vector_ids]
df = pd.DataFrame(vstores, columns=["vector store id"])
st.dataframe(df, hide_index=True)
files = [f for f in resource.files]
df = pd.DataFrame(files, columns=["filename", "id"])
st.dataframe(df, hide_index=True)
# st.write("**useCounts:**", idUseCounts)
# st.write("**Files:**", listFiles())
# st.write("**Vector Stores:**", listVectorStores())