fix: 移除一些库,添加错误判断

This commit is contained in:
sangge-redmi 2024-04-28 15:48:15 +08:00
parent be59c891e5
commit 4abd93f688

View File

@ -1,7 +1,5 @@
import json
import os
import re
import sys
from .utils import *
import openai
@ -15,22 +13,27 @@ def detect_gpt(filename: str):
{
"role": "system",
"content": "You are a Python code reviewer.Read the code below and identify any potential security vulnerabilities. Classify them by risk level (high, medium, low, none). Only report the line number and the risk level.\nYou should output the result as json format in one line. For example: "
"[{\"Line\": {the line number}, \"Risk\": \"{choose from (high,medium,low)}\",\"Reason\":\"{how it is vulnable}\"}] Each of these three field is required.\n"
"You are required to only output the json format. Do not output any other information.\n"
'[{"Line": {the line number}, "Risk": "{choose from (high,medium,low)}","Reason":"{how it is vulnable}"}] Each of these three field is required.\n'
"You are required to only output the json format. Do not output any other information.\n",
},
{
"role": "user",
"content": text,
}
},
],
model="gpt-3.5-turbo",
)
try:
res_json = json.loads(response.choices[0].message.content)
except:
print("Error: Could not parse the response. Please try again.")
sys.exit(1)
message_content = response.choices[0].message.content
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.")
classified_results = {"high": [], "medium": [], "low": [], "none": []}
for res in res_json:
classified_results[res["Risk"]].append((res["Line"], text.split("\n")[res["Line"] - 1].strip()))
return classified_results
classified_results[res["Risk"]].append(
(res["Line"], text.split("\n")[res["Line"] - 1].strip())
)
return classified_results