This commit is contained in:
parent
8863e6bb47
commit
3cd3bea1cb
|
|
@ -23,8 +23,12 @@ def read_file_with_detected_encoding(file_path: str):
|
||||||
raw_data = f.read()
|
raw_data = f.read()
|
||||||
result = chardet.detect(raw_data)
|
result = chardet.detect(raw_data)
|
||||||
encoding = result['encoding'] # 获取检测到的编码
|
encoding = result['encoding'] # 获取检测到的编码
|
||||||
with open(file_path, "r", encoding=encoding) as f:
|
try:
|
||||||
return f.read()
|
with open(file_path, "r", encoding=encoding, errors='ignore') as f:
|
||||||
|
return f.read()
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
logger.error(f"UnicodeDecodeError: Unable to decode the file using {encoding}.")
|
||||||
|
raise HTTPException(status_code=500, detail="Error reading the file due to encoding issues.")
|
||||||
|
|
||||||
@router.post("/search")
|
@router.post("/search")
|
||||||
def search_docs(request: QueryRequest, user_id: str = Query(..., description="用户ID")):
|
def search_docs(request: QueryRequest, user_id: str = Query(..., description="用户ID")):
|
||||||
|
|
@ -44,14 +48,14 @@ def search_docs(request: QueryRequest, user_id: str = Query(..., description="
|
||||||
logger.info(f"Loading Faiss vector store from path: {index_path}")
|
logger.info(f"Loading Faiss vector store from path: {index_path}")
|
||||||
storage_context = StorageContext.from_defaults(persist_dir=index_path)
|
storage_context = StorageContext.from_defaults(persist_dir=index_path)
|
||||||
|
|
||||||
# 自动读取文件内容并解码
|
# 自动读取 docstore.json 文件内容并解码
|
||||||
vector_store_file = os.path.join(index_path, "vector_store.json")
|
docstore_file = os.path.join(index_path, "docstore.json")
|
||||||
if os.path.exists(vector_store_file):
|
if os.path.exists(docstore_file):
|
||||||
file_content = read_file_with_detected_encoding(vector_store_file)
|
file_content = read_file_with_detected_encoding(docstore_file)
|
||||||
logger.info(f"Successfully read vector_store.json with detected encoding.")
|
logger.info(f"Successfully read docstore.json with detected encoding.")
|
||||||
else:
|
else:
|
||||||
logger.error(f"vector_store.json not found at {vector_store_file}")
|
logger.error(f"docstore.json not found at {docstore_file}")
|
||||||
raise HTTPException(status_code=404, detail="vector_store.json not found")
|
raise HTTPException(status_code=404, detail="docstore.json not found")
|
||||||
|
|
||||||
# 加载 Faiss 向量存储
|
# 加载 Faiss 向量存储
|
||||||
faiss_store = FaissVectorStore.from_persist_path(storage_context)
|
faiss_store = FaissVectorStore.from_persist_path(storage_context)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue