Files
gangyan/langchain-chat/server/chat/stop.py

26 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import time
import logging
from server.chat import utils
from fastapi.responses import JSONResponse
def stop(message_id: str):
try:
# start_time = time.time()
# tatus = None
# # 重试十秒
# while time.time() - start_time < 10:
status = utils.get_shared_variable(message_id)
# if status is not None:
# break
# logging.info(f"status未赋值, messageId: {message_id}")
# time.sleep(0.5) # 等待0.5秒后重试
if status is None:
return JSONResponse(content={"error": "未找到messegeId", "messageId": message_id}, status_code=500)
status["status"] = False
utils.set_shared_variable(message_id, status)
logging.info(f"停止请求成功messageId: {message_id}")
return JSONResponse(content={"event": "请求已成功停止", "messageId": message_id})
except Exception as e:
return JSONResponse(content={"error": str(e),"messageId": message_id}, status_code=500)