ParisNeo commited on
Commit
6163efc
·
unverified ·
1 Parent(s): 34159f3

Added docstrings to lightrag_server.py

Browse files
Files changed (1) hide show
  1. lightrag/api/lightrag_server.py +89 -2
lightrag/api/lightrag_server.py CHANGED
@@ -814,7 +814,23 @@ def create_app(args):
814
 
815
  @app.post("/documents/scan", dependencies=[Depends(optional_api_key)])
816
  async def scan_for_new_documents():
817
- """Manually trigger scanning for new documents"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
818
  try:
819
  new_files = doc_manager.scan_directory()
820
  indexed_count = 0
@@ -836,7 +852,27 @@ def create_app(args):
836
 
837
  @app.post("/documents/upload", dependencies=[Depends(optional_api_key)])
838
  async def upload_to_input_dir(file: UploadFile = File(...)):
839
- """Upload a file to the input directory"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
840
  try:
841
  if not doc_manager.is_supported_file(file.filename):
842
  raise HTTPException(
@@ -863,6 +899,25 @@ def create_app(args):
863
  "/query", response_model=QueryResponse, dependencies=[Depends(optional_api_key)]
864
  )
865
  async def query_text(request: QueryRequest):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
866
  try:
867
  response = await rag.aquery(
868
  request.query,
@@ -894,6 +949,16 @@ def create_app(args):
894
 
895
  @app.post("/query/stream", dependencies=[Depends(optional_api_key)])
896
  async def query_text_stream(request: QueryRequest):
 
 
 
 
 
 
 
 
 
 
897
  try:
898
  response = await rag.aquery( # Use aquery instead of query, and add await
899
  request.query,
@@ -943,6 +1008,17 @@ def create_app(args):
943
  dependencies=[Depends(optional_api_key)],
944
  )
945
  async def insert_text(request: InsertTextRequest):
 
 
 
 
 
 
 
 
 
 
 
946
  try:
947
  await rag.ainsert(request.text)
948
  return InsertResponse(
@@ -1176,6 +1252,15 @@ def create_app(args):
1176
  dependencies=[Depends(optional_api_key)],
1177
  )
1178
  async def clear_documents():
 
 
 
 
 
 
 
 
 
1179
  try:
1180
  rag.text_chunks = []
1181
  rag.entities_vdb = None
@@ -1188,7 +1273,9 @@ def create_app(args):
1188
  except Exception as e:
1189
  raise HTTPException(status_code=500, detail=str(e))
1190
 
 
1191
  # Ollama compatible API endpoints
 
1192
  @app.get("/api/version")
1193
  async def get_version():
1194
  """Get Ollama version information"""
 
814
 
815
  @app.post("/documents/scan", dependencies=[Depends(optional_api_key)])
816
  async def scan_for_new_documents():
817
+ """
818
+ Manually trigger scanning for new documents in the directory managed by `doc_manager`.
819
+
820
+ This endpoint facilitates manual initiation of a document scan to identify and index new files.
821
+ It processes all newly detected files, attempts indexing each file, logs any errors that occur,
822
+ and returns a summary of the operation.
823
+
824
+ Returns:
825
+ dict: A dictionary containing:
826
+ - "status" (str): Indicates success or failure of the scanning process.
827
+ - "indexed_count" (int): The number of successfully indexed documents.
828
+ - "total_documents" (int): Total number of documents that have been indexed so far.
829
+
830
+ Raises:
831
+ HTTPException: If an error occurs during the document scanning process, a 500 status
832
+ code is returned with details about the exception.
833
+ """
834
  try:
835
  new_files = doc_manager.scan_directory()
836
  indexed_count = 0
 
852
 
853
  @app.post("/documents/upload", dependencies=[Depends(optional_api_key)])
854
  async def upload_to_input_dir(file: UploadFile = File(...)):
855
+ """
856
+ Endpoint for uploading a file to the input directory and indexing it.
857
+
858
+ This API endpoint accepts a file through an HTTP POST request, checks if the
859
+ uploaded file is of a supported type, saves it in the specified input directory,
860
+ indexes it for retrieval, and returns a success status with relevant details.
861
+
862
+ Parameters:
863
+ file (UploadFile): The file to be uploaded. It must have an allowed extension as per
864
+ `doc_manager.supported_extensions`.
865
+
866
+ Returns:
867
+ dict: A dictionary containing the upload status ("success"),
868
+ a message detailing the operation result, and
869
+ the total number of indexed documents.
870
+
871
+ Raises:
872
+ HTTPException: If the file type is not supported, it raises a 400 Bad Request error.
873
+ If any other exception occurs during the file handling or indexing,
874
+ it raises a 500 Internal Server Error with details about the exception.
875
+ """
876
  try:
877
  if not doc_manager.is_supported_file(file.filename):
878
  raise HTTPException(
 
899
  "/query", response_model=QueryResponse, dependencies=[Depends(optional_api_key)]
900
  )
901
  async def query_text(request: QueryRequest):
902
+ """
903
+ Handle a POST request at the /query endpoint to process user queries using RAG capabilities.
904
+
905
+ Parameters:
906
+ request (QueryRequest): A Pydantic model containing the following fields:
907
+ - query (str): The text of the user's query.
908
+ - mode (ModeEnum): Optional. Specifies the mode of retrieval augmentation.
909
+ - stream (bool): Optional. Determines if the response should be streamed.
910
+ - only_need_context (bool): Optional. If true, returns only the context without further processing.
911
+
912
+ Returns:
913
+ QueryResponse: A Pydantic model containing the result of the query processing.
914
+ If a string is returned (e.g., cache hit), it's directly returned.
915
+ Otherwise, an async generator may be used to build the response.
916
+
917
+ Raises:
918
+ HTTPException: Raised when an error occurs during the request handling process,
919
+ with status code 500 and detail containing the exception message.
920
+ """
921
  try:
922
  response = await rag.aquery(
923
  request.query,
 
949
 
950
  @app.post("/query/stream", dependencies=[Depends(optional_api_key)])
951
  async def query_text_stream(request: QueryRequest):
952
+ """
953
+ This endpoint performs a retrieval-augmented generation (RAG) query and streams the response.
954
+
955
+ Args:
956
+ request (QueryRequest): The request object containing the query parameters.
957
+ optional_api_key (Optional[str], optional): An optional API key for authentication. Defaults to None.
958
+
959
+ Returns:
960
+ StreamingResponse: A streaming response containing the RAG query results.
961
+ """
962
  try:
963
  response = await rag.aquery( # Use aquery instead of query, and add await
964
  request.query,
 
1008
  dependencies=[Depends(optional_api_key)],
1009
  )
1010
  async def insert_text(request: InsertTextRequest):
1011
+ """
1012
+ Insert text into the Retrieval-Augmented Generation (RAG) system.
1013
+
1014
+ This endpoint allows you to insert text data into the RAG system for later retrieval and use in generating responses.
1015
+
1016
+ Args:
1017
+ request (InsertTextRequest): The request body containing the text to be inserted.
1018
+
1019
+ Returns:
1020
+ InsertResponse: A response object containing the status of the operation, a message, and the number of documents inserted.
1021
+ """
1022
  try:
1023
  await rag.ainsert(request.text)
1024
  return InsertResponse(
 
1252
  dependencies=[Depends(optional_api_key)],
1253
  )
1254
  async def clear_documents():
1255
+ """
1256
+ Clear all documents from the LightRAG system.
1257
+
1258
+ This endpoint deletes all text chunks, entities vector database, and relationships vector database,
1259
+ effectively clearing all documents from the LightRAG system.
1260
+
1261
+ Returns:
1262
+ InsertResponse: A response object containing the status, message, and the new document count (0 in this case).
1263
+ """
1264
  try:
1265
  rag.text_chunks = []
1266
  rag.entities_vdb = None
 
1273
  except Exception as e:
1274
  raise HTTPException(status_code=500, detail=str(e))
1275
 
1276
+ # -------------------------------------------------
1277
  # Ollama compatible API endpoints
1278
+ # -------------------------------------------------
1279
  @app.get("/api/version")
1280
  async def get_version():
1281
  """Get Ollama version information"""