-
Notifications
You must be signed in to change notification settings - Fork 0
/
ask-gpt.py
70 lines (46 loc) · 1.62 KB
/
ask-gpt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# coding: utf-8
# In[6]:
import subprocess
import openai
# Read API key from file
with open('../../api_key.txt', 'r', encoding='utf-8') as file:
api_key = file.read().strip()
# Set the OpenAI API key
openai.api_key = api_key
# In[2]:
def ask_to_gpt(question, content):
# Make a question using the API
question = question + content
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-16k",
messages=[
{"role": "user", "content": question}
],
)
# generated answer
answer = response['choices'][0]['message']['content'].strip()
# Record the answer
try:
with open("response.txt", 'a', encoding='utf-8') as f:
f.write(answer)
f.write("\n====================\n")
except:
print(f"Answer Write 오류")
def read_files(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
return content
# In[3]:
# 현재 디렉토리에서 자바 파일 읽기
file_path = "73_after_BCECPrivateKey.java"
content = read_files(file_path)
question = "Can you check the following code and if there is any CWE or CVE related vulnerability, can you point it out the number of CWE or CVE and describe it?\n"
ask_to_gpt(question, content)
# In[4]:
question = "Doesn't the following code have potential CWE vulnerability, which isCWE-497: Exposed Public Static Mutable Field?"
ask_to_gpt(question, content)
content = content + read_files("response.txt")
question = "Can you pointed out the line where those vulnerability occurs?\n"
ask_to_gpt(question, content)
# In[ ]: