fix: 添加超时报错,修复一个错误的测试
Some checks failed
Python application test / build (pull_request) Failing after 26s
Some checks failed
Python application test / build (pull_request) Failing after 26s
This commit is contained in:
@@ -2,12 +2,29 @@ import json
|
||||
import os
|
||||
from .utils import *
|
||||
import openai
|
||||
import signal
|
||||
|
||||
|
||||
class TimeoutException(Exception):
|
||||
"""Custom exception to handle timeouts."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
def timeout_handler(signum, frame):
|
||||
"""Handle the SIGALRM signal by raising a TimeoutException."""
|
||||
raise TimeoutException
|
||||
|
||||
|
||||
def detectGPT(content: str):
|
||||
api_key = os.getenv("OPENAI_API_KEY")
|
||||
if api_key is None:
|
||||
raise ValueError("env OPENAI_API_KEY no set")
|
||||
|
||||
# Set alarm timer
|
||||
signal.signal(signal.SIGTERM, timeout_handler)
|
||||
signal.alarm(10)
|
||||
|
||||
client = openai.OpenAI(api_key=api_key)
|
||||
text = content
|
||||
# client = openai.OpenAI(api_key="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") #测试用key
|
||||
@@ -31,9 +48,16 @@ def detectGPT(content: str):
|
||||
if message_content is None:
|
||||
raise ValueError("API response content is None")
|
||||
res_json = json.loads(message_content)
|
||||
|
||||
except json.JSONDecodeError:
|
||||
raise ValueError("Error: Could not parse the response. Please try again.")
|
||||
|
||||
except TimeoutException:
|
||||
raise TimeoutException("The api call timed out")
|
||||
|
||||
finally:
|
||||
signal.alarm(0)
|
||||
|
||||
classified_results = {"high": [], "medium": [], "low": [], "none": []}
|
||||
for res in res_json:
|
||||
classified_results[res["Risk"]].append(
|
||||
|
||||
Reference in New Issue
Block a user