|
6 | 6 | import os
|
7 | 7 | import sys
|
8 | 8 | import json
|
9 |
| -from typing import Dict |
| 9 | +from typing import Dict, Optional |
10 | 10 |
|
11 | 11 | from azure.ai.projects.aio import AIProjectClient
|
12 | 12 | from azure.ai.projects.models import FilePurpose, FileSearchTool, AsyncToolSet, FileSearchToolResource
|
@@ -81,21 +81,27 @@ async def lifespan(app: fastapi.FastAPI):
|
81 | 81 | file_map: Dict[str, Dict[str, str]] = {}
|
82 | 82 |
|
83 | 83 | logger.info(f"Creating UPLOADED_FILE_MAP")
|
| 84 | + folder_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'files')) |
84 | 85 | if agent.tool_resources and agent.tool_resources.file_search:
|
85 | 86 | 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 |
| 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 |
96 | 102 |
|
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} |
| 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} |
99 | 105 | app.state.upload_file_map = file_map
|
100 | 106 | logger.info(f"Set UPLOADED_FILE_MAP {file_map}")
|
101 | 107 | yield
|
|
0 commit comments