Skip to content

Commit 791f3a0

Browse files
authored
Merge pull request #52 from Azure-Samples/howie/upload-file-map
Construct upload_file_map on runtime and avoid save as env var
2 parents 038bcd7 + 4124358 commit 791f3a0

File tree

3 files changed

+10
-28
lines changed

3 files changed

+10
-28
lines changed

src/api/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import os
77
import sys
88
import json
9-
from typing import Dict
9+
from typing import Dict, Optional
1010

1111
from azure.ai.projects.aio import AIProjectClient
12-
from azure.ai.projects.models import FilePurpose, FileSearchTool, AsyncToolSet
12+
from azure.ai.projects.models import FilePurpose, FileSearchTool, AsyncToolSet, FileSearchToolResource
1313
from azure.identity import DefaultAzureCredential
1414

1515
import fastapi
@@ -76,7 +76,7 @@ async def lifespan(app: fastapi.FastAPI):
7676

7777
app.state.ai_client = ai_client
7878
app.state.agent = agent
79-
79+
8080
yield
8181

8282
except Exception as e:

src/api/routes.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -272,22 +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-
# Reconstruct the file dictionary from the env variable:
276-
files_env = os.environ['UPLOADED_FILE_MAP']
277-
try:
278-
files = json.loads(files_env)
279-
logger.info("Successfully parsed UPLOADED_FILE_MAP from environment variable.")
280-
except json.JSONDecodeError:
281-
files = {}
282-
logger.warning("Failed to parse UPLOADED_FILE_MAP from environment variable.", exc_info=True)
275+
folder_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'files'))
283276

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

286-
if file_name not in files:
279+
if file_name not in os.listdir(folder_path):
287280
raise HTTPException(status_code=404, detail="File not found")
288281

289282
try:
290-
file_path = files[file_name]["path"]
291283
data = await asyncio.to_thread(read_file, file_path)
292284
return PlainTextResponse(data)
293285
except Exception as e:

src/gunicorn.conf.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) Microsoft. All rights reserved.
22
# Licensed under the MIT license.
33
# See LICENSE file in the project root for full license information.
4-
from typing import Dict
4+
from typing import Dict, List
55

66
import asyncio
77
import csv
@@ -110,7 +110,7 @@ async def get_available_toolset(
110110
:return: The tool set, available based on the environment.
111111
"""
112112
# File name -> {"id": file_id, "path": file_path}
113-
files: Dict[str, Dict[str, str]] = {}
113+
file_ids: List[str] = []
114114
# First try to get an index search.
115115
conn_id = ""
116116
if os.environ.get('AZURE_AI_SEARCH_INDEX_NAME'):
@@ -129,11 +129,6 @@ async def get_available_toolset(
129129
index_name=os.environ.get('AZURE_AI_SEARCH_INDEX_NAME'))
130130

131131
toolset.add(ai_search)
132-
# Register the files
133-
for file_name in FILES_NAMES:
134-
file_path = _get_file_path(file_name)
135-
files[file_name] = {"id": file_name, "path": file_path}
136-
logger.info("agent: initialized index")
137132
else:
138133
logger.info(
139134
"agent: index was not initialized, falling back to file search.")
@@ -144,22 +139,17 @@ async def get_available_toolset(
144139
file = await ai_client.agents.upload_file_and_poll(
145140
file_path=file_path, purpose=FilePurpose.AGENTS)
146141
# Store both file id and the file path using the file name as key.
147-
files[file_name] = {"id": file.id, "path": file_path}
142+
file_ids.append(file.id)
148143

149144
# Create the vector store using the file IDs.
150145
vector_store = await ai_client.agents.create_vector_store_and_poll(
151-
file_ids=[info["id"] for info in files.values()],
146+
file_ids=file_ids,
152147
name="sample_store"
153148
)
154149
logger.info("agent: file store and vector store success")
155150

156151
file_search_tool = FileSearchTool(vector_store_ids=[vector_store.id])
157152
toolset.add(file_search_tool)
158-
# Serialize and store files information in the environment variable (so
159-
# workers see it)
160-
os.environ["UPLOADED_FILE_MAP"] = json.dumps(files)
161-
logger.info(
162-
f"Set env UPLOADED_FILE_MAP = {os.environ['UPLOADED_FILE_MAP']}")
163153

164154
return toolset
165155

0 commit comments

Comments
 (0)