Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -206,72 +206,55 @@ class AdvancedRAG:
|
|
| 206 |
self.assistant_id: Optional[str] = os.getenv("ASSISTANT_ID")
|
| 207 |
|
| 208 |
def create_thread(self) -> str:
|
| 209 |
-
thread = openai.beta.threads.create(
|
| 210 |
-
tool_resources={
|
| 211 |
-
"file_search": {
|
| 212 |
-
"vector_store_ids": [self.vector_store_id]
|
| 213 |
-
}
|
| 214 |
-
}
|
| 215 |
-
)
|
| 216 |
self.thread_id = thread.id
|
| 217 |
return self.thread_id
|
| 218 |
|
| 219 |
-
def
|
| 220 |
try:
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
openai.beta.vector_stores.delete(self.vector_store_id)
|
| 231 |
-
except Exception as e:
|
| 232 |
-
print(f"Could not delete previous vector store {self.vector_store_id}: {e}")
|
| 233 |
-
|
| 234 |
-
vector_store = openai.beta.vector_stores.create(name="knowledge_base")
|
| 235 |
-
self.vector_store_id = vector_store.id
|
| 236 |
-
self.file_ids = []
|
| 237 |
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
)
|
| 246 |
-
self.file_ids.append(file_obj.id)
|
| 247 |
-
openai.beta.vector_stores.files.create(
|
| 248 |
-
vector_store_id=self.vector_store_id,
|
| 249 |
-
file_id=file_obj.id
|
| 250 |
-
)
|
| 251 |
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
"vector_store_ids": [self.vector_store_id]
|
| 256 |
-
}
|
| 257 |
-
}
|
| 258 |
-
)
|
| 259 |
-
self.thread_id = thread.id
|
| 260 |
-
return file_obj.id
|
| 261 |
|
| 262 |
def ask_question(self, question: str) -> str:
|
| 263 |
try:
|
|
|
|
| 264 |
openai.beta.threads.messages.create(
|
| 265 |
thread_id=self.thread_id,
|
| 266 |
role="user",
|
| 267 |
content=question
|
| 268 |
)
|
| 269 |
|
|
|
|
| 270 |
run = openai.beta.threads.runs.create(
|
| 271 |
thread_id=self.thread_id,
|
| 272 |
assistant_id=self.assistant_id
|
| 273 |
)
|
| 274 |
|
|
|
|
| 275 |
waited = 0
|
| 276 |
while True:
|
| 277 |
run_status = openai.beta.threads.runs.retrieve(
|
|
@@ -287,6 +270,7 @@ class AdvancedRAG:
|
|
| 287 |
if waited > 60:
|
| 288 |
raise Exception("Run timed out after 60 seconds.")
|
| 289 |
|
|
|
|
| 290 |
messages = openai.beta.threads.messages.list(
|
| 291 |
thread_id=self.thread_id,
|
| 292 |
order='desc',
|
|
|
|
| 206 |
self.assistant_id: Optional[str] = os.getenv("ASSISTANT_ID")
|
| 207 |
|
| 208 |
def create_thread(self) -> str:
|
| 209 |
+
thread = openai.beta.threads.create()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
self.thread_id = thread.id
|
| 211 |
return self.thread_id
|
| 212 |
|
| 213 |
+
def upload_document(self, file) -> str:
|
| 214 |
try:
|
| 215 |
+
# Create a new file
|
| 216 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.name)[1]) as tmp:
|
| 217 |
+
tmp.write(file.read())
|
| 218 |
+
tmp.flush()
|
| 219 |
+
with open(tmp.name, "rb") as file_obj:
|
| 220 |
+
file_obj = openai.files.create(
|
| 221 |
+
file=file_obj,
|
| 222 |
+
purpose="assistants"
|
| 223 |
+
)
|
| 224 |
+
self.file_ids.append(file_obj.id)
|
| 225 |
|
| 226 |
+
# Create a new thread with the file
|
| 227 |
+
thread = openai.beta.threads.create()
|
| 228 |
+
self.thread_id = thread.id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
+
# Add the file to the thread
|
| 231 |
+
openai.beta.threads.messages.create(
|
| 232 |
+
thread_id=self.thread_id,
|
| 233 |
+
role="user",
|
| 234 |
+
content="I have uploaded a document. Please analyze it.",
|
| 235 |
+
file_ids=[file_obj.id]
|
| 236 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
+
return file_obj.id
|
| 239 |
+
except Exception as e:
|
| 240 |
+
raise Exception(f"Error uploading document: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
def ask_question(self, question: str) -> str:
|
| 243 |
try:
|
| 244 |
+
# Add the question to the thread
|
| 245 |
openai.beta.threads.messages.create(
|
| 246 |
thread_id=self.thread_id,
|
| 247 |
role="user",
|
| 248 |
content=question
|
| 249 |
)
|
| 250 |
|
| 251 |
+
# Create a run
|
| 252 |
run = openai.beta.threads.runs.create(
|
| 253 |
thread_id=self.thread_id,
|
| 254 |
assistant_id=self.assistant_id
|
| 255 |
)
|
| 256 |
|
| 257 |
+
# Wait for the run to complete
|
| 258 |
waited = 0
|
| 259 |
while True:
|
| 260 |
run_status = openai.beta.threads.runs.retrieve(
|
|
|
|
| 270 |
if waited > 60:
|
| 271 |
raise Exception("Run timed out after 60 seconds.")
|
| 272 |
|
| 273 |
+
# Get the latest message
|
| 274 |
messages = openai.beta.threads.messages.list(
|
| 275 |
thread_id=self.thread_id,
|
| 276 |
order='desc',
|