Skip to content

Commit dc0ba24

Browse files
committed
reduce upload_file_map completely
1 parent cb7c50e commit dc0ba24

File tree

2 files changed

+3
-31
lines changed

2 files changed

+3
-31
lines changed

src/api/main.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -77,33 +77,6 @@ async def lifespan(app: fastapi.FastAPI):
7777
app.state.ai_client = ai_client
7878
app.state.agent = agent
7979

80-
81-
file_map: Dict[str, Dict[str, str]] = {}
82-
83-
logger.info(f"Creating UPLOADED_FILE_MAP")
84-
folder_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'files'))
85-
if agent.tool_resources and agent.tool_resources.file_search:
86-
for vector_store_id in agent.tool_resources.file_search.vector_store_ids:
87-
after: Optional[str] = None
88-
has_more = True
89-
while has_more:
90-
files = await ai_client.agents.list_vector_store_files(vector_store_id, after=after)
91-
has_more = files.has_more
92-
after = files.last_id
93-
for file in files.data:
94-
openAI_file = await ai_client.agents.get_file(file.id)
95-
logger.info(f"Retrieved file, file ID: {openAI_file.filename}")
96-
file_name = openAI_file.filename
97-
file_path = os.path.join(folder_path, file_name)
98-
99-
if not os.path.exists(file_path):
100-
logger.warning(f"File path does not exist: {file_path}")
101-
continue
102-
103-
# Store both file id and the file path using the file name as key.
104-
file_map[file_name] = {"id": file.id, "path": file_path}
105-
app.state.upload_file_map = file_map
106-
logger.info(f"Set UPLOADED_FILE_MAP {file_map}")
10780
yield
10881

10982
except Exception as e:

src/api/routes.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,14 @@ async def fetch_document(request: Request):
272272
if not file_name:
273273
raise HTTPException(status_code=400, detail="file_name is required")
274274

275-
files = request.app.state.upload_file_map
275+
folder_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'files'))
276276

277-
logger.info(f"File requested: {file_name}. Current file keys: {list(files.keys())}")
277+
file_path = os.path.join(folder_path, file_name)
278278

279-
if file_name not in files:
279+
if not os.path.exists(file_path):
280280
raise HTTPException(status_code=404, detail="File not found")
281281

282282
try:
283-
file_path = files[file_name]["path"]
284283
data = await asyncio.to_thread(read_file, file_path)
285284
return PlainTextResponse(data)
286285
except Exception as e:

0 commit comments

Comments
 (0)