From efdb454c7d7b4dab1e10eeb65fa9e58c4bc54b03 Mon Sep 17 00:00:00 2001 From: fanpt <320622572@qq.com> Date: Wed, 12 Jun 2024 14:52:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6=E7=BC=96?= =?UTF-8?q?=E7=A0=81=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fast_api.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/fast_api.py b/fast_api.py index 10003fc..5507dc3 100644 --- a/fast_api.py +++ b/fast_api.py @@ -1,4 +1,5 @@ -# coding=gbk +# -*- coding: utf-8 -*- + from fastapi import FastAPI, HTTPException, BackgroundTasks from qa_Ask import QAService, match_query, store_data from pydantic import BaseModel @@ -18,13 +19,13 @@ from faiss_kb_service import EmbeddingsFunAdapter app = FastAPI() -# 配置日志记录到文件和终端 +# 閰嶇疆鏃ュ織璁板綍鍒版枃浠跺拰缁堢 logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[ logging.FileHandler('log/app.log'), - logging.StreamHandler(sys.stdout) # 这里添加控制台处理程序 + logging.StreamHandler(sys.stdout) # 杩欓噷娣诲姞鎺у埗鍙板鐞嗙▼搴 ] ) logger = logging.getLogger(__name__) @@ -60,7 +61,7 @@ max_knowledge_bases = config_data['max_knowledge_bases'] def load_knowledge_bases(): - """加载知识库名称列表""" + """鍔犺浇鐭ヨ瘑搴撳悕绉板垪琛""" if os.path.exists(knowledge_base_file): with open(knowledge_base_file, "r") as file: return file.read().splitlines() @@ -69,13 +70,13 @@ def load_knowledge_bases(): def save_knowledge_bases(names): - """保存知识库名称列表到文件""" + """淇濆瓨鐭ヨ瘑搴撳悕绉板垪琛ㄥ埌鏂囦欢""" with open(knowledge_base_file, "w") as file: file.write("\n".join(names)) def update_kb(kb_name, qa_service, path, max_knowledge_bases): - """更新知识库""" + """鏇存柊鐭ヨ瘑搴""" store_data(qa_service, path) if len(recent_knowledge_bases) == max_knowledge_bases: @@ -87,11 +88,11 @@ def update_kb(kb_name, qa_service, path, max_knowledge_bases): os.remove(path) logger.info(f"Knowledge base updated: {kb_name}\n" - f"Please wait while the database is being updated···") + f"Please wait while the database is being updated路路路") def fetch_and_write_data(api_url, path): - """从API获取数据并写入文件""" + """浠嶢PI鑾峰彇鏁版嵁骞跺啓鍏ユ枃浠""" try: response = requests.get(api_url) response_data = response.json() @@ -117,7 +118,7 @@ faiss_service = FaissKBService(kb_name= recent_knowledge_bases[-1], embed_model_ @app.post("/embeddings/", response_model=EmbeddingResponse) async def get_embeddings(request: EmbeddingRequest): - """使用FaissKBService实例来获取嵌入向量""" + """浣跨敤FaissKBService瀹炰緥鏉ヨ幏鍙栧祵鍏ュ悜閲""" embed_func = EmbeddingsFunAdapter(faiss_service.embed_model_path, faiss_service.device) try: embeddings = embed_func.embed_query(request.text) @@ -128,7 +129,7 @@ async def get_embeddings(request: EmbeddingRequest): @app.post("/updateDatabase") async def save_to_json(question_items: list[QuestionItem], background_tasks: BackgroundTasks): - """接收问题数据并异步保存为JSON文件,触发后台更新任务""" + """鎺ユ敹闂鏁版嵁骞跺紓姝ヤ繚瀛樹负JSON鏂囦欢锛岃Е鍙戝悗鍙版洿鏂颁换鍔""" try: json_data = json.dumps([item.dict() for item in question_items], ensure_ascii=False, indent=2) path = "output.json" @@ -145,17 +146,17 @@ async def save_to_json(question_items: list[QuestionItem], background_tasks: Bac update_kb, kb_name, qa_service, path, max_knowledge_bases ) - return {"status": "success", "message": "Please wait while the database is being updated···"} + return {"status": "success", "message": "Please wait while the database is being updated路路路"} except Exception as e: logger.error(f"Error saving data to file or scheduling knowledge base update task: {e}") # raise HTTPException(status_code=500, detail=f"Internal Server Error: {str(e)}") - return {"status": "error", "message": "update task error···"} + return {"status": "error", "message": "update task error路路路"} @app.post("/matchQuestion") def match_question(request: QuestionRequest): - """匹配问题的端点""" + """鍖归厤闂鐨勭鐐""" try: logger.info(f"match_question:Request: {request}") start_time = time.time()