Skip to content

Commit

Permalink
The link of upload files is broken in the chat view pingcap#466
Browse files Browse the repository at this point in the history
  • Loading branch information
MingWei Liu committed Dec 23, 2024
1 parent b6b3439 commit 60ea247
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
28 changes: 28 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
ENVIRONMENT=production

# You can generate a new secret key by running the following command
# $ python3 -c "import secrets; print(secrets.token_urlsafe(32))"
# SECRET_KEY is very important, please do not share it with others,
# SECRET_KEY must greater or equal to 32 characters.
SECRET_KEY=

# Replace with your own sentry dsn, leave it commented if you don't want to use sentry
# SENTRY_DSN=https://[email protected]/xxxxxx

# Replace with your own TiDB cluster connection information,
# TiDB Serverless is recommended. You can quickly create one from https://tidbcloud.com/
TIDB_HOST=xxxxx.prod.aws.tidbcloud.com
TIDB_USER=
TIDB_PASSWORD=
TIDB_DATABASE=
TIDB_SSL=true

# EMBEDDING_MAX_TOKENS indicates the max size of document chunks.
#
# EMBEDDING_MAX_TOKENS should be smaller than the embedding model's max tokens due
# to the tokenizer difference. (see: https://github.com/pingcap/autoflow/issues/397)
#
# Go to https://tidb.ai/docs/embedding-model to check the max tokens of the embedding model.
#
# Notice: this variable will be deprecated in the future.
EMBEDDING_MAX_TOKENS=2048
2 changes: 1 addition & 1 deletion backend/app/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
api_router.include_router(feedback.router, tags=["chat"])
api_router.include_router(user.router, tags=["user"])
api_router.include_router(api_key.router, tags=["auth"])
api_router.include_router(download.router)
api_router.include_router(download.router, tags=["download_file"])
api_router.include_router(admin_chat_engine.router, tags=["admin/chat_engine"])
api_router.include_router(admin_document_router, tags=["admin/documents"])
api_router.include_router(admin_feedback.router, tags=["admin/feedback"])
Expand Down
36 changes: 20 additions & 16 deletions backend/app/api/routes/download.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
from fastapi import FastAPI, HTTPException
from fastapi import FastAPI, HTTPException, APIRouter
from fastapi.responses import FileResponse

from app.models import Upload
from app.rag.datasource import FileDataSource
from app.file_storage import default_file_storage
from sqlmodel import select, Session, col
from app.api.deps import SessionDep
from app.models import Document
from app.repositories import document_repo
import os

router = APIRouter()

@router.get("/documents/{document_id}/download")
def download_file(document_id: int):
isfound = False
for f_config in FileDataSource.config:
if f_config["file_id"] == document_id:
isfound = True
# 找到了就返回文件
if isfound == True:
upload = FileDataSource.session.get(Upload, document_id)
return FileResponse(path=upload.path, filename=upload.name, media_type='application/octet-stream')
# 没找到应该 302 到对应的 url,但先404
@router.get("/documents/{doc_id}/download")
def download_file(
doc_id: int,
session: SessionDep
):
doc = session.get(Document, doc_id)
if not doc:
raise HTTPException(status_code = 404, detail = "File not found")

DATA_PATH = "../data"
source_uri = os.path.join(DATA_PATH, doc.source_uri)
if os.path.exists(source_uri):
return FileResponse(path = source_uri, filename = doc.name, media_type = doc.mime_type)
else:
raise HTTPException(status_code=404, detail="lmw : File not found")
raise HTTPException(status_code = 404, detail = "File not found")


0 comments on commit 60ea247

Please sign in to comment.