Skip to content

Commit 212e46c

Browse files
committed
refactor: improve logging and documentation across various modules
1 parent 58de48e commit 212e46c

30 files changed

Lines changed: 135 additions & 92 deletions

File tree

libs/admin-api-lib/src/admin_api_lib/api_endpoints/file_uploader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99

1010
class FileUploader(UploaderBase):
11+
"""File uploader implementation."""
1112

1213
@abstractmethod
1314
async def upload_file(
@@ -16,7 +17,7 @@ async def upload_file(
1617
file: UploadFile,
1718
) -> None:
1819
"""
19-
Uploads a source file for content extraction.
20+
Upload a source file for content extraction.
2021
2122
Parameters
2223
----------

libs/admin-api-lib/src/admin_api_lib/api_endpoints/source_uploader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async def upload_source(
2121
timeout: Optional[float],
2222
) -> None:
2323
"""
24-
Uploads the parameters for source content extraction.
24+
Upload the parameters for source content extraction.
2525
2626
Parameters
2727
----------

libs/admin-api-lib/src/admin_api_lib/api_endpoints/uploader_base.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ class UploaderBase:
77
"""Base class for uploader API endpoints."""
88

99
def __init__(self):
10-
"""
11-
Initialize the UploaderBase.
12-
"""
10+
"""Initialize the UploaderBase."""
1311
self._background_threads = []
1412

1513
def _prune_background_threads(self) -> list[Thread]:

libs/admin-api-lib/src/admin_api_lib/apis/admin_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ async def upload_file(
149149
request: Request,
150150
) -> None:
151151
"""
152-
Uploads user selected sources.
152+
Upload user selected sources.
153153
154154
Parameters
155155
----------
@@ -181,7 +181,7 @@ async def upload_source(
181181
key_value_pair: List[KeyValuePair] = Body(None, description="The key-value pairs for the source"),
182182
) -> None:
183183
"""
184-
Uploads user selected sources.
184+
Upload user selected sources.
185185
186186
Parameters
187187
----------

libs/admin-api-lib/src/admin_api_lib/impl/api_endpoints/default_document_reference_retriever.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import io
44
import logging
5-
import traceback
65

76
from fastapi import HTTPException, Response, status
87

@@ -54,10 +53,8 @@ async def adocument_reference_id_get(self, identification: str) -> Response:
5453
self._file_service.download_file(identification, document_buffer)
5554
logger.debug("DONE retrieving document with id: %s", identification)
5655
document_data = document_buffer.getvalue()
57-
except Exception as e:
58-
logger.error(
59-
"Error retrieving document with id: %s. Error: %s %s", identification, e, traceback.format_exc()
60-
)
56+
except Exception:
57+
logger.exception("Error retrieving document with id: %s", identification)
6158
raise ValueError(f"Document with id '{identification}' not found.")
6259
finally:
6360
document_buffer.close()

libs/admin-api-lib/src/admin_api_lib/impl/api_endpoints/default_file_uploader.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
"""Module for the default file uploader implementation."""
2+
13
import logging
24
from pathlib import Path
3-
import traceback
45
import urllib
56
import tempfile
67
import asyncio
@@ -78,7 +79,7 @@ async def upload_file(
7879
file: UploadFile,
7980
) -> None:
8081
"""
81-
Uploads a source file for content extraction.
82+
Upload a source file for content extraction.
8283
8384
Parameters
8485
----------
@@ -109,7 +110,7 @@ async def upload_file(
109110
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
110111
except Exception as e:
111112
self._key_value_store.upsert(source_name, Status.ERROR)
112-
logger.error("Error while uploading %s = %s", source_name, str(e))
113+
logger.exception("Error while uploading %s", source_name)
113114
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))
114115

115116
def _log_task_exception(self, task: asyncio.Task) -> None:
@@ -124,19 +125,16 @@ def _log_task_exception(self, task: asyncio.Task) -> None:
124125
if task.done() and not task.cancelled():
125126
try:
126127
task.result() # This will raise the exception if one occurred
127-
except Exception as e:
128-
logger.error("Background task failed with exception: %s", str(e))
129-
logger.debug("Background task exception traceback: %s", traceback.format_exc())
128+
except Exception:
129+
logger.exception("Background task failed with exception")
130130

131131
def _prune_background_tasks(self) -> None:
132-
"""
133-
Remove completed background tasks from the list.
134-
"""
132+
"""Remove completed background tasks from the list."""
135133
self._background_tasks = [task for task in self._background_tasks if not task.done()]
136134

137135
def _check_if_already_in_processing(self, source_name: str) -> None:
138136
"""
139-
Checks if the source is already in processing state.
137+
Check if the source is already in processing state.
140138
141139
Parameters
142140
----------
@@ -196,9 +194,9 @@ async def _handle_source_upload(
196194
await asyncio.to_thread(self._rag_api.upload_information_piece, rag_information_pieces)
197195
self._key_value_store.upsert(source_name, Status.READY)
198196
logger.info("Source uploaded successfully: %s", source_name)
199-
except Exception as e:
197+
except Exception:
200198
self._key_value_store.upsert(source_name, Status.ERROR)
201-
logger.error("Error while uploading %s = %s", source_name, str(e))
199+
logger.exception("Error while uploading %s", source_name)
202200

203201
def _add_file_url(self, file_name: str, base_url: str, chunked_documents: list[Document]):
204202
document_url = f"{base_url.rstrip('/')}/document_reference/{urllib.parse.quote_plus(file_name)}"
@@ -229,6 +227,6 @@ async def _asave_new_document(
229227

230228
self._file_service.upload_file(Path(temp_file_path), filename)
231229
return filename
232-
except Exception as e:
233-
logger.error("Error during document saving: %s %s", e, traceback.format_exc())
230+
except Exception:
231+
logger.exception("Error during document saving")
234232
self._key_value_store.upsert(source_name, Status.ERROR)

libs/admin-api-lib/src/admin_api_lib/impl/api_endpoints/default_source_uploader.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Module for the default source uploader implementation."""
2+
13
import logging
24
import asyncio
35
from threading import Thread
@@ -28,6 +30,7 @@
2830

2931

3032
class DefaultSourceUploader(SourceUploader):
33+
"""Default implementation of the SourceUploader."""
3134

3235
def __init__(
3336
self,
@@ -78,7 +81,7 @@ async def upload_source(
7881
kwargs: list[KeyValuePair],
7982
) -> None:
8083
"""
81-
Uploads the parameters for source content extraction.
84+
Upload the parameters for source content extraction.
8285
8386
Parameters
8487
----------
@@ -95,7 +98,6 @@ async def upload_source(
9598
-------
9699
None
97100
"""
98-
99101
self._prune_background_threads()
100102

101103
source_name = f"{source_type}:{sanitize_document_name(name)}"
@@ -111,12 +113,12 @@ async def upload_source(
111113
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
112114
except Exception as e:
113115
self._key_value_store.upsert(source_name, Status.ERROR)
114-
logger.error("Error while uploading %s = %s", source_name, str(e))
116+
logger.exception("Error while uploading %s", source_name)
115117
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))
116118

117119
def _check_if_already_in_processing(self, source_name: str) -> None:
118120
"""
119-
Checks if the source is already in processing state.
121+
Check if the source is already in processing state.
120122
121123
Parameters
122124
----------
@@ -197,6 +199,6 @@ async def _handle_source_upload(
197199
await asyncio.to_thread(self._rag_api.upload_information_piece, rag_information_pieces)
198200
self._key_value_store.upsert(source_name, Status.READY)
199201
logger.info("Source uploaded successfully: %s", source_name)
200-
except Exception as e:
202+
except Exception:
201203
self._key_value_store.upsert(source_name, Status.ERROR)
202-
logger.error("Error while uploading %s = %s", source_name, str(e))
204+
logger.exception("Error while uploading %s", source_name)

libs/admin-api-lib/src/admin_api_lib/impl/file_services/s3_service.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Class to handle I/O with S3 storage."""
22

33
import logging
4-
import traceback
54
from pathlib import Path
65
from typing import BinaryIO
76

@@ -125,7 +124,7 @@ def delete_file(self, file_name: str) -> None:
125124
try:
126125
file_name = f"/{file_name}" if not file_name.startswith("/") else file_name
127126
self._s3_client.delete_object(Bucket=self._s3_settings.bucket, Key=file_name)
128-
logger.info(f"File {file_name} successfully deleted.")
129-
except Exception as e:
130-
logger.error("Error deleting file %s: %s %s" % (file_name, e, traceback.format_exc()))
127+
logger.info("File %s successfully deleted.", file_name)
128+
except Exception:
129+
logger.exception("Error deleting file %s", file_name)
131130
raise

libs/admin-api-lib/src/admin_api_lib/impl/summarizer/langchain_summarizer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Module for the LangchainSummarizer class."""
22

33
import logging
4-
import traceback
54
from typing import Optional
65

76
from langchain.text_splitter import RecursiveCharacterTextSplitter
@@ -66,7 +65,7 @@ async def ainvoke(self, query: SummarizerInput, config: Optional[RunnableConfig]
6665
assert query, "Query is empty: %s" % query # noqa S101
6766
config = ensure_config(config)
6867
tries_remaining = config.get("configurable", {}).get("tries_remaining", 3)
69-
logger.debug("Tries remaining %d" % tries_remaining)
68+
logger.debug("Tries remaining %d", tries_remaining)
7069

7170
if tries_remaining < 0:
7271
raise Exception("Summary creation failed.")
@@ -81,8 +80,8 @@ async def ainvoke(self, query: SummarizerInput, config: Optional[RunnableConfig]
8180
# Extract content from AIMessage if it's not already a string
8281
content = result.content if hasattr(result, "content") else str(result)
8382
outputs.append(content)
84-
except Exception as e:
85-
logger.error("Error in summarizing langchain doc: %s %s", e, traceback.format_exc())
83+
except Exception:
84+
logger.exception("Error in summarizing langchain doc")
8685
config["tries_remaining"] = tries_remaining - 1
8786
result = await self._create_chain().ainvoke({"text": langchain_document.page_content}, config)
8887
# Extract content from AIMessage if it's not already a string
@@ -93,8 +92,9 @@ async def ainvoke(self, query: SummarizerInput, config: Optional[RunnableConfig]
9392
return outputs[0]
9493
summary = " ".join(outputs)
9594
logger.debug(
96-
"Reduced number of chars from %d to %d"
97-
% (len("".join([x.page_content for x in langchain_documents])), len(summary))
95+
"Reduced number of chars from %d to %d",
96+
len("".join([x.page_content for x in langchain_documents])),
97+
len(summary),
9898
)
9999
return await self.ainvoke(summary, config)
100100

libs/extractor-api-lib/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ per-file-ignores = """
3232
./src/extractor_api_lib/apis/extractor_api.py: B008,WOT001,
3333
./src/extractor_api_lib/impl/extractor_api_impl.py: B008,
3434
./src/extractor_api_lib/container.py: CCE002,CCE001,
35-
./src/extractor_api_lib/apis/extractor_api_base.py: WOT001,
36-
./tests/*: S101,E501,
35+
./src/extractor_api_lib/apis/extractor_api_base.py: WOT001,D105,
36+
./tests/*: S101,E501,D105,D100,D102,
3737
"""
3838

3939
[tool.black]

0 commit comments

Comments
 (0)