Skip to content

Commit ee0bc31

Browse files
committed
Construct upload_file_map on runtime and avoid save as env var
1 parent 038bcd7 commit ee0bc31

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

src/api/main.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import Dict
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,28 @@ async def lifespan(app: fastapi.FastAPI):
7676

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

82103
except Exception as e:

src/api/routes.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,7 @@ 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+
files = request.app.state.upload_file_map
283276

284277
logger.info(f"File requested: {file_name}. Current file keys: {list(files.keys())}")
285278

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)