|
@@ -29,15 +29,8 @@ app.add_middleware(
|
|
|
# max_age=1000
|
|
|
)
|
|
|
|
|
|
-# 获取当前脚本所在的绝对路径
|
|
|
-current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
-
|
|
|
-# 构建默认路径
|
|
|
-DEFAULT_DB_PATH = os.path.join(current_dir, "knowledge_db")
|
|
|
-DEFAULT_PERSIST_PATH = os.path.join(current_dir, "vector_db", "chroma")
|
|
|
-
|
|
|
-# 初始化 QA 链接
|
|
|
-chat_qa_chain = Chat_QA_chain_self(file_path=DEFAULT_DB_PATH, persist_path=DEFAULT_PERSIST_PATH, embedding='m3e')
|
|
|
+# 初始化你的Chat_QA_chain_self实例
|
|
|
+chat_qa_chain = Chat_QA_chain_self(file_path="./knowledge_db", persist_path="./vector_db/chroma")
|
|
|
|
|
|
@app.post('/clear/history')
|
|
|
async def clear_history():
|
|
@@ -46,9 +39,10 @@ async def clear_history():
|
|
|
res = success({"message": "Chat history has been cleared."}, "success")
|
|
|
return JSONResponse(res)
|
|
|
except Exception as e:
|
|
|
- res = failed({}, {"error": f"{e}"})
|
|
|
+ res = success({"response": "请尝试换个方式提问。"}, "success")
|
|
|
return JSONResponse(res)
|
|
|
|
|
|
+
|
|
|
@app.post('/view/history')
|
|
|
async def view_history():
|
|
|
try:
|
|
@@ -63,9 +57,26 @@ async def view_history():
|
|
|
res = success({"history": formatted_history}, "success")
|
|
|
return JSONResponse(res)
|
|
|
except Exception as e:
|
|
|
- res = failed({}, {"error": f"{e}"})
|
|
|
+ res = success({"response": "请尝试换个方式提问。"}, "success")
|
|
|
return JSONResponse(res)
|
|
|
|
|
|
+
|
|
|
+# @app.post('/ask')
|
|
|
+# async def ask(request: Request):
|
|
|
+# try:
|
|
|
+# data = await request.json()
|
|
|
+# question = data.get('question')
|
|
|
+# chat_history = data.get('chat_history', [])
|
|
|
+# response = chat_qa_chain.build_chain(question,chat_history)
|
|
|
+# # response = chain.invoke({"question": question, "chat_history": chat_history})
|
|
|
+# # 添加新的聊天记录到历史记录中
|
|
|
+# chat_qa_chain.add_to_chat_history(question, response)
|
|
|
+# res = success({"response": response}, "success")
|
|
|
+# return JSONResponse(res)
|
|
|
+# except Exception as e:
|
|
|
+# res = failed({}, {"error": f"{e}"})
|
|
|
+# return JSONResponse(res)
|
|
|
+
|
|
|
@app.post('/ask')
|
|
|
async def ask(request: Request):
|
|
|
try:
|
|
@@ -83,10 +94,9 @@ async def ask(request: Request):
|
|
|
res = success({"response": response}, "success")
|
|
|
return JSONResponse(res)
|
|
|
except Exception as e:
|
|
|
- res = failed({}, {"error": f"{e}"})
|
|
|
+ res = success({"response": "请尝试换个方式提问。"}, "success")
|
|
|
return JSONResponse(res)
|
|
|
|
|
|
-# 纯rag问答
|
|
|
@app.post('/ask/rag')
|
|
|
async def ask_rag(request: Request):
|
|
|
try:
|
|
@@ -100,9 +110,10 @@ async def ask_rag(request: Request):
|
|
|
res = success({"response": response}, "success")
|
|
|
return JSONResponse(res)
|
|
|
except Exception as e:
|
|
|
- res = failed({}, {"error": f"{e}"})
|
|
|
+ res = success({"response": "请尝试换个方式提问。"}, "success")
|
|
|
return JSONResponse(res)
|
|
|
|
|
|
+
|
|
|
# build_text_chain
|
|
|
@app.post('/ask/unstructure/rag')
|
|
|
async def ask_rag(request: Request):
|
|
@@ -117,9 +128,10 @@ async def ask_rag(request: Request):
|
|
|
res = success({"response": response}, "success")
|
|
|
return JSONResponse(res)
|
|
|
except Exception as e:
|
|
|
- res = failed({}, {"error": f"{e}"})
|
|
|
+ res = success({"response": "请尝试换个方式提问。"}, "success")
|
|
|
return JSONResponse(res)
|
|
|
|
|
|
+
|
|
|
@app.post('/file/list')
|
|
|
async def file_list():
|
|
|
try:
|
|
@@ -135,10 +147,11 @@ async def file_list():
|
|
|
return JSONResponse(res)
|
|
|
|
|
|
except Exception as e:
|
|
|
- res = failed({}, {"error": f"{e}"})
|
|
|
+ res = success({"response": "请尝试换个方式提问。"}, "success")
|
|
|
return JSONResponse(res)
|
|
|
|
|
|
|
|
|
+
|
|
|
@app.post("/file/upload")
|
|
|
async def upload_file(file: UploadFile = File(...)):
|
|
|
try:
|
|
@@ -155,14 +168,17 @@ async def upload_file(file: UploadFile = File(...)):
|
|
|
data = []
|
|
|
loader = TextLoader(f'{UPLOAD_FOLDER}/{file.filename}', encoding='utf8')
|
|
|
data.extend(loader.load())
|
|
|
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=150)
|
|
|
+ text_splitter = RecursiveCharacterTextSplitter(
|
|
|
+ chunk_size=500, chunk_overlap=150)
|
|
|
split_docs = text_splitter.split_documents(data)
|
|
|
- vector_db = get_vectordb(file_path=DEFAULT_DB_PATH, persist_path=DEFAULT_PERSIST_PATH,embedding="m3e")
|
|
|
+ vector_db = get_vectordb(file_path="./knowledge_db", persist_path="./vector_db/chroma",embedding="m3e")
|
|
|
vector_db.add_documents(split_docs)
|
|
|
|
|
|
return JSONResponse(success({"response": "File uploaded successfully"}, "success"))
|
|
|
except Exception as e:
|
|
|
- return JSONResponse(failed({}, {"error": str(e)}))
|
|
|
+ res = success({"response": "请尝试换个方式提问。"}, "success")
|
|
|
+ return JSONResponse(res)
|
|
|
+
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
import uvicorn
|