Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to support upload local files #67

Merged
merged 5 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ curl -X 'POST' http://127.0.0.1:8000/service/batch_evaluate/response
}
```

3. 上传

支持通过API的方式上传本地文件,并支持指定不同的faiss_path,每次发送API请求会返回一个task_id,之后可以通过task_id来查看文件上传状态(processing、completed、failed)。

- **(1)上传(upload_local_data)**

```bash
curl -X 'POST' http://127.0.0.1:8000/service/upload_local_data -H 'Content-Type: multipart/form-data' -F 'file=@local_path/PAI.txt' -F 'faiss_path=localdata/storage'

# Return: {"task_id": "2c1e557733764fdb9fefa063538914da"}
```

- **(2)查看上传状态(upload_local_data)**

```bash
curl http://127.0.0.1:8077/service/get_upload_state\?task_id\=2c1e557733764fdb9fefa063538914da

# Return: {"task_id":"2c1e557733764fdb9fefa063538914da","status":"completed"}
```

### 独立脚本文件:不依赖于整体服务的启动,可独立运行

1. 向当前索引存储中插入新文件
Expand Down
39 changes: 38 additions & 1 deletion src/pai_rag/app/api/query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any
from fastapi import APIRouter, Body, BackgroundTasks
from fastapi import APIRouter, Body, BackgroundTasks, File, UploadFile, Form
import uuid
import os
from pai_rag.core.rag_service import rag_service
from pai_rag.app.api.models import (
RagQuery,
Expand All @@ -10,6 +11,7 @@
LlmResponse,
DataInput,
)
from pai_rag.app.web.view_model import _transform_to_dict

router = APIRouter()

Expand Down Expand Up @@ -86,3 +88,38 @@ async def batch_evaluate():
type="all"
)
return {"status": 200, "result": eval_results}


@router.post("/upload_local_data")
async def upload_local_data(
file: UploadFile = File(),
faiss_path: str = Form(),
background_tasks: BackgroundTasks = BackgroundTasks(),
):
task_id = uuid.uuid4().hex
if not file:
return {"message": "No upload file sent"}
else:
fn = file.filename
save_path = f"./localdata/upload_files/{task_id}/"
moria97 marked this conversation as resolved.
Show resolved Hide resolved
if not os.path.exists(save_path):
os.makedirs(save_path, exist_ok=True)

save_file = os.path.join(save_path, fn)

f = open(save_file, "wb")
data = await file.read()
f.write(data)
f.close()
sessioned_config = rag_service.rag_configuration.get_value()
moria97 marked this conversation as resolved.
Show resolved Hide resolved
if faiss_path:
sessioned_config = rag_service.rag_configuration.get_value().copy()
sessioned_config.index.update({"persist_path": faiss_path})
rag_service.reload(_transform_to_dict(sessioned_config))
background_tasks.add_task(
rag_service.add_knowledge_async,
task_id=task_id,
file_dir=save_path,
enable_qa_extraction=False,
)
return {"task_id": task_id}
Loading