-
Notifications
You must be signed in to change notification settings - Fork 58
/
function_app.py
199 lines (172 loc) · 7.84 KB
/
function_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import logging
import json
# import asyncio
import os
import time
import datetime
from json import JSONEncoder
import jsonschema
import azure.functions as func
from chunking import DocumentChunker
from connectors import SharepointFilesIndexer, SharepointDeletedFilesPurger
from tools import BlobStorageClient
from utils.file_utils import get_filename
# -------------------------------
# Logging configuration
# -------------------------------
log_level = os.getenv('LOG_LEVEL', 'INFO').upper()
log_level = getattr(logging, log_level, logging.INFO)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
suppress_loggers = [
'azure',
'azure.core',
'azure.core.pipeline',
'azure.core.pipeline.policies.http_logging_policy',
'azsdk-python-search-documents',
'azsdk-python-identity',
'azure.ai.openai', # Assuming 'aoai' refers to Azure OpenAI
'azure.identity',
'azure.storage',
'azure.ai.*', # Wildcard-like suppression for any azure.ai sub-loggers
# Add any other specific loggers if necessary
]
for logger_name in suppress_loggers:
logger = logging.getLogger(logger_name)
logger.setLevel(logging.WARNING)
logger.propagate = False
# -------------------------------
# Azure Functions
# -------------------------------
app = func.FunctionApp()
# -------------------------------
# SharePoint Connector Functions (Timer Triggered)
# -------------------------------
@app.function_name(name="sharepoint_index_files")
@app.schedule(
schedule="0 */10 * * * *",
arg_name="timer",
run_on_startup=True
)
async def sharepoint_index_files(timer: func.TimerRequest) -> None:
logging.info("[sharepoint_index_files_function] Started sharepoint files indexing function.")
try:
indexer = SharepointFilesIndexer()
await indexer.run()
except Exception as e:
logging.error(f"[sharepoint_index_files_function] An unexpected error occurred: {e}", exc_info=True)
@app.function_name(name="sharepoint_purge_deleted_files")
@app.schedule(
schedule="0 */10 * * * *",
arg_name="timer",
run_on_startup=False
)
async def sharepoint_purge_deleted_files(timer: func.TimerRequest) -> None:
logging.info("[sharepoint_purge_deleted_files_function] Started sharepoint purge deleted files function.")
try:
purger = SharepointDeletedFilesPurger()
await purger.run()
except Exception as e:
logging.error(f"[sharepoint_purge_deleted_files_function] An unexpected error occurred: {e}", exc_info=True)
# -------------------------------
# Document Chunking Function (HTTP Triggered by AI Search)
# -------------------------------
# Document Chunking Function (HTTP Triggered by AI Search)
@app.route(route="document-chunking", auth_level=func.AuthLevel.FUNCTION)
def document_chunking(req: func.HttpRequest) -> func.HttpResponse:
try:
body = req.get_json()
jsonschema.validate(body, schema=get_request_schema())
if body:
# Log the incoming request
logging.info(f'[document_chunking_function] Invoked document_chunking skill. Number of items: {len(body["values"])}.')
input_data = {}
# Processing one item at a time to avoid exceeding the AI Search custom skill timeout (230 seconds)
# BatchSize should be set to 1 in the Skillset definition, if it is not set, will process just the last item
count_items = len(body["values"])
filename = ""
if count_items > 1:
logging.warning('BatchSize should be set to 1 in the Skillset definition. Processing only the last item.')
for i, item in enumerate(body["values"]):
input_data = item["data"]
filename = get_filename(input_data["documentUrl"])
logging.info(f'[document_chunking_function] Chunking document: File {filename}, Content Type {input_data["documentContentType"]}.')
start_time = time.time()
# Enrich the input data with the document bytes and file name
blob_client = BlobStorageClient(input_data["documentUrl"])
document_bytes = blob_client.download_blob()
input_data['documentBytes'] = document_bytes
input_data['fileName'] = filename
# Chunk the document
chunks, errors, warnings = DocumentChunker().chunk_documents(input_data)
# Enrich chunks with metadata to be indexed
for chunk in chunks: chunk["source"] = "blob"
# Debug logging
for idx, chunk in enumerate(chunks):
processed_chunk = chunk.copy()
processed_chunk.pop('contentVector', None)
if 'content' in processed_chunk and isinstance(processed_chunk['content'], str):
processed_chunk['content'] = processed_chunk['content'][:100]
logging.debug(f"[document_chunking][{filename}] Chunk {idx + 1}: {json.dumps(processed_chunk, indent=4)}")
# Format results
values = {
"recordId": item['recordId'],
"data": {"chunks": chunks},
"errors": errors,
"warnings": warnings
}
results = {"values": [values]}
result = json.dumps(results, ensure_ascii=False, cls=DateTimeEncoder)
end_time = time.time()
elapsed_time = end_time - start_time
logging.info(f'[document_chunking_function] Finished document_chunking skill in {elapsed_time:.2f} seconds.')
return func.HttpResponse(result, mimetype="application/json")
else:
error_message = "Invalid body."
logging.error(f"[document_chunking_function] {error_message}", exc_info=True)
return func.HttpResponse(error_message, status_code=400)
except ValueError as e:
error_message = f"Invalid body: {e}"
logging.error(f"[document_chunking_function] {error_message}", exc_info=True)
return func.HttpResponse(error_message, status_code=400)
except jsonschema.exceptions.ValidationError as e:
error_message = f"Invalid request: {e}"
logging.error(f"[document_chunking_function] {error_message}", exc_info=True)
return func.HttpResponse(error_message, status_code=400)
class DateTimeEncoder(JSONEncoder):
# Override the default method
def default(self, obj):
if isinstance(obj, (datetime.date, datetime.datetime)):
return obj.isoformat()
return super().default(obj)
def get_request_schema():
return {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"values": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"recordId": {"type": "string"},
"data": {
"type": "object",
"properties": {
"documentUrl": {"type": "string", "minLength": 1},
"documentSasToken": {"type": "string", "minLength": 0},
"documentContentType": {"type": "string", "minLength": 1}
},
"required": ["documentUrl", "documentContentType"],
},
},
"required": ["recordId", "data"],
},
}
},
"required": ["values"],
}