Skip to content

Commit

Permalink
fixed file uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
ParisNeo committed Dec 26, 2024
1 parent f2b52a2 commit 5fcfb05
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
8 changes: 4 additions & 4 deletions lightrag/api/azure_openai_lightrag_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,11 @@ async def stream_generator():
@app.post("/documents/text", response_model=InsertResponse)
async def insert_text(request: InsertTextRequest):
try:
rag.insert(request.text)
await rag.ainsert(request.text)
return InsertResponse(
status="success",
message="Text successfully inserted",
document_count=len(rag),
document_count=1,
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
Expand All @@ -365,7 +365,7 @@ async def insert_file(file: UploadFile = File(...), description: str = Form(None
return InsertResponse(
status="success",
message=f"File '{file.filename}' successfully inserted",
document_count=len(rag),
document_count=1,
)
except UnicodeDecodeError:
raise HTTPException(status_code=400, detail="File encoding not supported")
Expand Down Expand Up @@ -397,7 +397,7 @@ async def insert_batch(files: List[UploadFile] = File(...)):
return InsertResponse(
status="success" if inserted_count > 0 else "partial_success",
message=status_message,
document_count=len(rag),
document_count=len(files),
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
Expand Down
12 changes: 6 additions & 6 deletions lightrag/api/lollms_lightrag_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ async def scan_for_new_documents():
try:
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
rag.insert(content)
await rag.ainsert(content)
doc_manager.mark_as_indexed(file_path)
indexed_count += 1
except Exception as e:
Expand Down Expand Up @@ -250,7 +250,7 @@ async def upload_to_input_dir(file: UploadFile = File(...)):
# Immediately index the uploaded file
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
rag.insert(content)
await rag.ainsert(content)
doc_manager.mark_as_indexed(file_path)

return {
Expand Down Expand Up @@ -313,7 +313,7 @@ async def insert_file(file: UploadFile = File(...), description: str = Form(None

if file.filename.endswith((".txt", ".md")):
text = content.decode("utf-8")
rag.insert(text)
await rag.ainsert(text)
else:
raise HTTPException(
status_code=400,
Expand All @@ -323,7 +323,7 @@ async def insert_file(file: UploadFile = File(...), description: str = Form(None
return InsertResponse(
status="success",
message=f"File '{file.filename}' successfully inserted",
document_count=len(rag),
document_count=1,
)
except UnicodeDecodeError:
raise HTTPException(status_code=400, detail="File encoding not supported")
Expand All @@ -341,7 +341,7 @@ async def insert_batch(files: List[UploadFile] = File(...)):
content = await file.read()
if file.filename.endswith((".txt", ".md")):
text = content.decode("utf-8")
rag.insert(text)
await rag.ainsert(text)
inserted_count += 1
else:
failed_files.append(f"{file.filename} (unsupported type)")
Expand All @@ -355,7 +355,7 @@ async def insert_batch(files: List[UploadFile] = File(...)):
return InsertResponse(
status="success" if inserted_count > 0 else "partial_success",
message=status_message,
document_count=len(rag),
document_count=len(files),
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
Expand Down
14 changes: 7 additions & 7 deletions lightrag/api/ollama_lightrag_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ async def scan_for_new_documents():
try:
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
rag.insert(content)
await rag.ainsert(content)
doc_manager.mark_as_indexed(file_path)
indexed_count += 1
except Exception as e:
Expand Down Expand Up @@ -250,7 +250,7 @@ async def upload_to_input_dir(file: UploadFile = File(...)):
# Immediately index the uploaded file
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
rag.insert(content)
await rag.ainsert(content)
doc_manager.mark_as_indexed(file_path)

return {
Expand Down Expand Up @@ -297,7 +297,7 @@ async def stream_generator():
@app.post("/documents/text", response_model=InsertResponse)
async def insert_text(request: InsertTextRequest):
try:
rag.insert(request.text)
await rag.ainsert(request.text)
return InsertResponse(
status="success",
message="Text successfully inserted",
Expand All @@ -313,7 +313,7 @@ async def insert_file(file: UploadFile = File(...), description: str = Form(None

if file.filename.endswith((".txt", ".md")):
text = content.decode("utf-8")
rag.insert(text)
await rag.ainsert(text)
else:
raise HTTPException(
status_code=400,
Expand All @@ -323,7 +323,7 @@ async def insert_file(file: UploadFile = File(...), description: str = Form(None
return InsertResponse(
status="success",
message=f"File '{file.filename}' successfully inserted",
document_count=len(rag),
document_count=1,
)
except UnicodeDecodeError:
raise HTTPException(status_code=400, detail="File encoding not supported")
Expand All @@ -341,7 +341,7 @@ async def insert_batch(files: List[UploadFile] = File(...)):
content = await file.read()
if file.filename.endswith((".txt", ".md")):
text = content.decode("utf-8")
rag.insert(text)
await rag.ainsert(text)
inserted_count += 1
else:
failed_files.append(f"{file.filename} (unsupported type)")
Expand All @@ -355,7 +355,7 @@ async def insert_batch(files: List[UploadFile] = File(...)):
return InsertResponse(
status="success" if inserted_count > 0 else "partial_success",
message=status_message,
document_count=len(rag),
document_count=len(files),
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
Expand Down
4 changes: 2 additions & 2 deletions lightrag/api/openai_lightrag_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ async def insert_file(file: UploadFile = File(...), description: str = Form(None
return InsertResponse(
status="success",
message=f"File '{file.filename}' successfully inserted",
document_count=len(rag),
document_count=1,
)
except UnicodeDecodeError:
raise HTTPException(status_code=400, detail="File encoding not supported")
Expand Down Expand Up @@ -359,7 +359,7 @@ async def insert_batch(files: List[UploadFile] = File(...)):
return InsertResponse(
status="success" if inserted_count > 0 else "partial_success",
message=status_message,
document_count=len(rag),
document_count=len(files),
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
Expand Down

0 comments on commit 5fcfb05

Please sign in to comment.