This commit is contained in:
hailin 2025-05-10 03:11:22 +08:00
parent e942ad51e3
commit 8863e6bb47
1 changed files with 20 additions and 1 deletions

View File

@ -6,6 +6,7 @@ from llama_index.vector_stores.faiss import FaissVectorStore
from llama_index import VectorStoreIndex, ServiceContext, StorageContext from llama_index import VectorStoreIndex, ServiceContext, StorageContext
import os import os
import logging import logging
import chardet
router = APIRouter() router = APIRouter()
@ -16,6 +17,15 @@ logger = logging.getLogger(__name__)
class QueryRequest(BaseModel): class QueryRequest(BaseModel):
query: str query: str
# 自动检测文件编码并加载
def read_file_with_detected_encoding(file_path: str):
with open(file_path, "rb") as f:
raw_data = f.read()
result = chardet.detect(raw_data)
encoding = result['encoding'] # 获取检测到的编码
with open(file_path, "r", encoding=encoding) as f:
return f.read()
@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")):
try: try:
@ -33,7 +43,16 @@ def search_docs(request: QueryRequest, user_id: str = Query(..., description="
# 创建 StorageContext 并加载 Faiss 向量存储目录 # 创建 StorageContext 并加载 Faiss 向量存储目录
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)
# 自动读取文件内容并解码
vector_store_file = os.path.join(index_path, "vector_store.json")
if os.path.exists(vector_store_file):
file_content = read_file_with_detected_encoding(vector_store_file)
logger.info(f"Successfully read vector_store.json with detected encoding.")
else:
logger.error(f"vector_store.json not found at {vector_store_file}")
raise HTTPException(status_code=404, detail="vector_store.json not found")
# 加载 Faiss 向量存储 # 加载 Faiss 向量存储
faiss_store = FaissVectorStore.from_persist_path(storage_context) faiss_store = FaissVectorStore.from_persist_path(storage_context)
service_context = ServiceContext.from_defaults(embed_model=embedder, llm=None) service_context = ServiceContext.from_defaults(embed_model=embedder, llm=None)