Skip to content

Commit a6edb30

Browse files
authored
Support gpt4o (#233)
* fix: ๐Ÿ› fix OPENAI key setting issue and update readme * feat: ๐ŸŽธ add visual parsing for GPT4o * feat: ๐ŸŽธ update default API model
1 parent bb768c1 commit a6edb30

File tree

6 files changed

+82
-243
lines changed

6 files changed

+82
-243
lines changed

โ€Žpentestgpt/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ def main():
2222
parser.add_argument(
2323
"--reasoning_model",
2424
type=str,
25-
default="gpt-4-turbo",
25+
default="gpt-4-o",
2626
help="reasoning models are responsible for higher-level cognitive tasks, choose 'gpt-4' or 'gpt-4-turbo'",
2727
)
2828
# 2. Parsing Model
2929
parser.add_argument(
3030
"--parsing_model",
3131
type=str,
32-
default="gpt-4-turbo",
32+
default="gpt-4-o",
3333
help="parsing models deal with the structural and grammatical aspects of language, choose 'gpt-4-turbo' or 'gpt-3.5-turbo-16k'",
3434
)
3535

โ€Žpentestgpt/utils/APIs/chatgpt_api.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77

88
import loguru
99
import openai
10-
import tiktoken
1110
from langfuse.model import InitialGeneration, Usage
1211
from openai import OpenAI
13-
from tenacity import *
1412

1513
from pentestgpt.utils.llm_api import LLMAPI
1614

@@ -61,12 +59,12 @@ def __init__(self, config_class, use_langfuse_logging=False):
6159
from langfuse import Langfuse
6260

6361
self.langfuse = Langfuse()
64-
62+
6563
self.model = config_class.model
6664
self.log_dir = config_class.log_dir
6765
self.history_length = 5 # maintain 5 messages in the history. (5 chat memory)
6866
self.conversation_dict: Dict[str, Conversation] = {}
69-
self.error_waiting_time = 3 # wait for 3 seconds
67+
self.error_wait_time = config_class.error_wait_time
7068

7169
logger.add(sink=os.path.join(self.log_dir, "chatgpt.log"), level="WARNING")
7270

@@ -77,7 +75,7 @@ def _chat_completion(
7775
# use model if provided, otherwise use self.model; if self.model is None, use gpt-4-1106-preview
7876
if model is None:
7977
if self.model is None:
80-
model = "gpt-4-1106-preview"
78+
model = "gpt-4o-2024-05-13"
8179
else:
8280
model = self.model
8381
try:
@@ -102,7 +100,7 @@ def _chat_completion(
102100
except openai._exceptions.RateLimitError as e: # give one more try
103101
logger.warning("Rate limit reached. Waiting for 5 seconds")
104102
logger.error("Rate Limit Error: ", e)
105-
time.sleep(5)
103+
time.sleep(self.error_wait_time)
106104
response = openai.ChatCompletion.create(
107105
model=model,
108106
messages=history,
@@ -129,7 +127,7 @@ def _chat_completion(
129127
if isinstance(response, tuple):
130128
logger.warning("Response is not valid. Waiting for 5 seconds")
131129
try:
132-
time.sleep(5)
130+
time.sleep(self.error_wait_time)
133131
response = openai.ChatCompletion.create(
134132
model=model,
135133
messages=history,
@@ -165,12 +163,19 @@ def _chat_completion(
165163

166164

167165
if __name__ == "__main__":
168-
from module_import import GPT4ConfigClass
166+
from module_import import GPT4O
169167

170-
config_class = GPT4ConfigClass()
171-
config_class.log_dir = "logs"
172-
chatgpt = ChatGPTAPI(config_class, use_langfuse_logging=True)
168+
local_config_class = GPT4O()
169+
local_config_class.log_dir = "logs"
170+
chatgpt = ChatGPTAPI(local_config_class, use_langfuse_logging=True)
173171
# test is below
172+
# 0. A single test initialized with image.
173+
result, conversation_id = chatgpt.send_new_message(
174+
"What's in the image?",
175+
image_url="https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
176+
)
177+
print("Answer 1")
178+
print(result)
174179
# 1. create a new conversation
175180
result, conversation_id = chatgpt.send_new_message(
176181
"""You're an excellent cybersecurity penetration tester assistant.
@@ -203,3 +208,12 @@ def _chat_completion(
203208
)
204209
print("Answer 2")
205210
print(result)
211+
212+
# 3. send a image related conversation
213+
result = chatgpt.send_message(
214+
"What's in the image?",
215+
conversation_id,
216+
image_url="https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
217+
)
218+
print("Answer 3")
219+
print(result)

โ€Žpentestgpt/utils/APIs/chatgpt_vision_api.py

Lines changed: 0 additions & 207 deletions
This file was deleted.

โ€Žpentestgpt/utils/APIs/module_import.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class GPT4ConfigClass:
6464
)
6565
error_wait_time: float = 20
6666
is_debugging: bool = False
67+
log_dir: str = None
6768

6869

6970
@dataclasses.dataclass
@@ -78,6 +79,7 @@ class GPT35Turbo16kConfigClass:
7879
)
7980
error_wait_time: float = 20
8081
is_debugging: bool = False
82+
log_dir: str = None
8183

8284

8385
@dataclasses.dataclass
@@ -90,8 +92,9 @@ class GPT4Turbo:
9092
print(
9193
"Your OPENAI_API_KEY is not set. Please set it in the environment variable."
9294
)
93-
error_wait_time: float = 20
95+
error_wait_time: float = 10
9496
is_debugging: bool = False
97+
log_dir: str = None
9598

9699

97100
@dataclasses.dataclass
@@ -104,8 +107,9 @@ class GPT4O:
104107
print(
105108
"Your OPENAI_API_KEY is not set. Please set it in the environment variable."
106109
)
107-
error_wait_time: float = 20
110+
error_wait_time: float = 10
108111
is_debugging: bool = False
112+
log_dir: str = None
109113

110114

111115
@dataclasses.dataclass
@@ -130,6 +134,7 @@ class AzureGPT35ConfigClass:
130134
)
131135
error_wait_time: float = 20
132136
is_debugging: bool = False
137+
log_dir: str = None
133138

134139

135140
@dataclasses.dataclass
@@ -145,6 +150,7 @@ class Gemini10ConfigClass: # New dataclass for Gemini 1.0
145150
)
146151
error_wait_time: float = 20
147152
is_debugging: bool = False
153+
log_dir: str = None
148154

149155

150156
@dataclasses.dataclass
@@ -160,6 +166,7 @@ class Gemini15ConfigClass: # New dataclass for Gemini 1.5
160166
)
161167
error_wait_time: float = 20
162168
is_debugging: bool = False
169+
log_dir: str = None
163170

164171

165172
def dynamic_import(module_name, log_dir, use_langfuse_logging=False) -> object:

0 commit comments

Comments
ย (0)