forked from pingcap/autoflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The link of upload files is broken in the chat view pingcap#466
- Loading branch information
MingWei Liu
committed
Dec 23, 2024
1 parent
b6b3439
commit 60ea247
Showing
3 changed files
with
49 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
|