From 1776c1afcd18601718d4b5531d648ef036c4ed14 Mon Sep 17 00:00:00 2001 From: Saifeddine ALOUI Date: Fri, 24 Jan 2025 21:01:34 +0100 Subject: [PATCH 01/16] fixed linting --- lightrag/api/lightrag_server.py | 57 +++++++-------- lightrag/api/static/index.html | 122 ++++++++++++++++---------------- 2 files changed, 90 insertions(+), 89 deletions(-) diff --git a/lightrag/api/lightrag_server.py b/lightrag/api/lightrag_server.py index e5f68d72..e505c1c5 100644 --- a/lightrag/api/lightrag_server.py +++ b/lightrag/api/lightrag_server.py @@ -468,13 +468,12 @@ def parse_args() -> argparse.Namespace: help="Path to SSL private key file (required if --ssl is enabled)", ) parser.add_argument( - '--auto-scan-at-startup', - action='store_true', + "--auto-scan-at-startup", + action="store_true", default=False, - help='Enable automatic scanning when the program starts' + help="Enable automatic scanning when the program starts", ) - args = parser.parse_args() return args @@ -912,7 +911,7 @@ def create_app(args): """Lifespan context manager for startup and shutdown events""" # Startup logic # Now only if this option is active, we can scan. This is better for big databases where there are hundreds of - # files. Makes the startup faster + # files. Makes the startup faster if args.auto_scan_at_startup: ASCIIColors.info("Auto scan is active, rescanning the input directory.") try: @@ -923,8 +922,10 @@ def create_app(args): except Exception as e: trace_exception(e) logging.error(f"Error indexing file {file_path}: {str(e)}") - - logging.info(f"Indexed {len(new_files)} documents from {args.input_dir}") + + logging.info( + f"Indexed {len(new_files)} documents from {args.input_dir}" + ) except Exception as e: logging.error(f"Error during startup indexing: {str(e)}") @@ -932,17 +933,17 @@ def create_app(args): async def scan_for_new_documents(): """ Manually trigger scanning for new documents in the directory managed by `doc_manager`. - + This endpoint facilitates manual initiation of a document scan to identify and index new files. It processes all newly detected files, attempts indexing each file, logs any errors that occur, and returns a summary of the operation. - + Returns: dict: A dictionary containing: - "status" (str): Indicates success or failure of the scanning process. - "indexed_count" (int): The number of successfully indexed documents. - "total_documents" (int): Total number of documents that have been indexed so far. - + Raises: HTTPException: If an error occurs during the document scanning process, a 500 status code is returned with details about the exception. @@ -970,25 +971,25 @@ def create_app(args): async def upload_to_input_dir(file: UploadFile = File(...)): """ Endpoint for uploading a file to the input directory and indexing it. - - This API endpoint accepts a file through an HTTP POST request, checks if the + + This API endpoint accepts a file through an HTTP POST request, checks if the uploaded file is of a supported type, saves it in the specified input directory, indexes it for retrieval, and returns a success status with relevant details. - + Parameters: file (UploadFile): The file to be uploaded. It must have an allowed extension as per `doc_manager.supported_extensions`. - + Returns: - dict: A dictionary containing the upload status ("success"), - a message detailing the operation result, and + dict: A dictionary containing the upload status ("success"), + a message detailing the operation result, and the total number of indexed documents. - + Raises: HTTPException: If the file type is not supported, it raises a 400 Bad Request error. If any other exception occurs during the file handling or indexing, it raises a 500 Internal Server Error with details about the exception. - """ + """ try: if not doc_manager.is_supported_file(file.filename): raise HTTPException( @@ -1017,23 +1018,23 @@ def create_app(args): async def query_text(request: QueryRequest): """ Handle a POST request at the /query endpoint to process user queries using RAG capabilities. - + Parameters: request (QueryRequest): A Pydantic model containing the following fields: - query (str): The text of the user's query. - mode (ModeEnum): Optional. Specifies the mode of retrieval augmentation. - stream (bool): Optional. Determines if the response should be streamed. - only_need_context (bool): Optional. If true, returns only the context without further processing. - + Returns: - QueryResponse: A Pydantic model containing the result of the query processing. + QueryResponse: A Pydantic model containing the result of the query processing. If a string is returned (e.g., cache hit), it's directly returned. Otherwise, an async generator may be used to build the response. - + Raises: HTTPException: Raised when an error occurs during the request handling process, with status code 500 and detail containing the exception message. - """ + """ try: response = await rag.aquery( request.query, @@ -1074,7 +1075,7 @@ def create_app(args): Returns: StreamingResponse: A streaming response containing the RAG query results. - """ + """ try: response = await rag.aquery( # Use aquery instead of query, and add await request.query, @@ -1134,7 +1135,7 @@ def create_app(args): Returns: InsertResponse: A response object containing the status of the operation, a message, and the number of documents inserted. - """ + """ try: await rag.ainsert(request.text) return InsertResponse( @@ -1772,7 +1773,7 @@ def create_app(args): "max_tokens": args.max_tokens, }, } - + # Serve the static files static_dir = Path(__file__).parent / "static" static_dir.mkdir(exist_ok=True) @@ -1780,13 +1781,13 @@ def create_app(args): return app - + def main(): args = parse_args() import uvicorn app = create_app(args) - display_splash_screen(args) + display_splash_screen(args) uvicorn_config = { "app": app, "host": args.host, diff --git a/lightrag/api/static/index.html b/lightrag/api/static/index.html index 66dc37bb..6d75e47b 100644 --- a/lightrag/api/static/index.html +++ b/lightrag/api/static/index.html @@ -11,7 +11,7 @@ - +