umairahmad89 commited on
Commit
ebe102a
1 Parent(s): d0c74c0

Add list files function

Browse files
Files changed (1) hide show
  1. assistant.py +17 -7
assistant.py CHANGED
@@ -5,6 +5,8 @@ from openai import OpenAI
5
  from assistant_file_handler import FileHandler
6
  from openai.types.beta.thread import Thread
7
  from openai.types.beta.threads.message import Message
 
 
8
 
9
  import structlog
10
  from openai.pagination import SyncCursorPage
@@ -87,13 +89,12 @@ class OAIAssistant:
87
  return new_message
88
  except Exception as e:
89
  self.log.error("OAIAssistant: Error generating response", error=str(e))
90
- import traceback
91
-
92
- self.log.error(traceback.print_exc())
93
  return "OAIAssistant: An error occurred while generating the response."
94
 
95
  def create_thread(self) -> Thread:
96
- thread: Thread = self.client.beta.threads.create(tool_resources={"file_search": {"vector_store_ids": [self.vectorstore_id]}})
 
 
97
  return thread
98
 
99
  def delete_thread(self, thread_id: str):
@@ -139,10 +140,11 @@ class OAIAssistant:
139
  )
140
  if run.status == "failed":
141
  self.log.error(
142
- "OAIAssistant: Assistant run failed",
143
  run_id=run.id,
144
  )
145
- self.log.error(run)
 
146
  return "OAIAssistant: Error in generating response", []
147
 
148
  messages: SyncCursorPage[Message] = self.client.beta.threads.messages.list(
@@ -171,4 +173,12 @@ class OAIAssistant:
171
  return new_message, file_ids
172
  except Exception as e:
173
  self.log.error("OAIAssistant: Error extracting messages", error=str(e))
174
- raise
 
 
 
 
 
 
 
 
 
5
  from assistant_file_handler import FileHandler
6
  from openai.types.beta.thread import Thread
7
  from openai.types.beta.threads.message import Message
8
+ from openai.types.beta.vector_stores.vector_store_file import VectorStoreFile
9
+
10
 
11
  import structlog
12
  from openai.pagination import SyncCursorPage
 
89
  return new_message
90
  except Exception as e:
91
  self.log.error("OAIAssistant: Error generating response", error=str(e))
 
 
 
92
  return "OAIAssistant: An error occurred while generating the response."
93
 
94
  def create_thread(self) -> Thread:
95
+ thread: Thread = self.client.beta.threads.create(
96
+ tool_resources={"file_search": {"vector_store_ids": [self.vectorstore_id]}}
97
+ )
98
  return thread
99
 
100
  def delete_thread(self, thread_id: str):
 
140
  )
141
  if run.status == "failed":
142
  self.log.error(
143
+ "OAIAssistant: Assistant run failed",
144
  run_id=run.id,
145
  )
146
+ self.log.info(run)
147
+
148
  return "OAIAssistant: Error in generating response", []
149
 
150
  messages: SyncCursorPage[Message] = self.client.beta.threads.messages.list(
 
173
  return new_message, file_ids
174
  except Exception as e:
175
  self.log.error("OAIAssistant: Error extracting messages", error=str(e))
176
+ raise
177
+
178
+ def get_files_list(self):
179
+ files: SyncCursorPage[VectorStoreFile] = (
180
+ self.client.beta.vector_stores.files.list(
181
+ vector_store_id=self.vectorstore_id
182
+ )
183
+ )
184
+ return [file.id for file in files]