Skip to content

Commit 45fa1d8

Browse files
committed
♻️
1 parent 6269be2 commit 45fa1d8

File tree

3 files changed

+66
-105
lines changed

3 files changed

+66
-105
lines changed

src/main/java/com/github/plexpt/chatgpt/ChatGTP.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,9 @@ public static void main(String[] args) {
5050

5151
String configString = FileUtil.readUtf8String(new File("config.json"));
5252

53-
Map<String, String> params = JSON.parseObject(configString,
54-
new TypeReference<Map<String, String>>() {
55-
});
53+
Config config = JSON.parseObject(configString, Config.class);
5654

57-
Chatbot chatbot = new Chatbot(params, null);
55+
Chatbot chatbot = new Chatbot(config, null);
5856
String prompt;
5957
while (true) {
6058
prompt = getInput("\nYou:\n");
@@ -79,10 +77,7 @@ public static void main(String[] args) {
7977
chatbot.rollbackConversation();
8078
System.out.println("Chat session rolled back.");
8179
continue;
82-
} else if (prompt.equals("!config")) {
83-
System.out.println(JSON.toJSONString(chatbot.getConfig()));
84-
continue;
85-
} else if (prompt.equals("!exit")) {
80+
} else if (prompt.equals("!exit")) {
8681
break;
8782
}
8883
}

src/main/java/com/github/plexpt/chatgpt/Chatbot.java

Lines changed: 60 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Map;
1010
import java.util.UUID;
1111

12+
import cn.hutool.core.util.StrUtil;
1213
import cn.hutool.http.HttpResponse;
1314
import cn.hutool.http.HttpUtil;
1415
import lombok.Data;
@@ -17,10 +18,12 @@
1718

1819
@Data
1920
public class Chatbot {
20-
private String cfClearance;
21-
private Map<String, String> config = new HashMap<>();
22-
private String conversationId;
21+
private String conversationId = null;
22+
23+
private String sessionToken;
2324

25+
private String authorization = "";
26+
private String cfClearance;
2427
private String userAgent;
2528
private String parentId;
2629
private Map<String, String> headers;
@@ -29,11 +32,17 @@ public class Chatbot {
2932
private String parentIdPrev;
3033

3134

32-
public Chatbot(Map<String, String> config, String conversationId) {
33-
this.config = config;
35+
public Chatbot(Config config) {
36+
this(config, null);
37+
}
38+
39+
public Chatbot(Config config, String conversationId) {
40+
this.sessionToken = config.getSession_token();
41+
this.cfClearance = config.getCfClearance();
42+
this.userAgent = config.getUserAgent();
3443
this.conversationId = conversationId;
3544
this.parentId = UUID.randomUUID().toString();
36-
if (config.containsKey("session_token")) {
45+
if (StrUtil.isNotEmpty(sessionToken)) {
3746
refreshSession();
3847
}
3948
}
@@ -48,7 +57,7 @@ public Chatbot(Map<String, String> config, String conversationId) {
4857
public Chatbot(String sessionToken, String cfClearance, String userAgent) {
4958
this.userAgent = userAgent;
5059
this.cfClearance = cfClearance;
51-
config.put("session_token", sessionToken);
60+
this.sessionToken = sessionToken;
5261
this.parentId = UUID.randomUUID().toString();
5362
refreshSession();
5463
}
@@ -62,15 +71,14 @@ public void resetChat() {
6271

6372
// Refreshes the headers -- Internal use only
6473
public void refreshHeaders() {
65-
if (!config.containsKey("Authorization")) {
66-
config.put("Authorization", "");
67-
} else if (config.get("Authorization") == null) {
68-
config.put("Authorization", "");
74+
75+
if (StrUtil.isEmpty(authorization)) {
76+
authorization = "";
6977
}
7078
this.headers = new HashMap<String, String>() {{
7179
put("Host", "chat.openai.com");
7280
put("Accept", "text/event-stream");
73-
put("Authorization", "Bearer " + config.get("Authorization"));
81+
put("Authorization", "Bearer " + authorization);
7482
put("Content-Type", "application/json");
7583
put("User-Agent", userAgent);
7684
put("X-Openai-Assistant-App-Id", "");
@@ -142,7 +150,7 @@ public Map<String, Object> getChatText(Map<String, Object> data) {
142150
session.setHeaders(this.headers);
143151

144152
// Set multiple cookies
145-
session.getCookies().put("__Secure-next-auth.session-token", config.get("session_token"));
153+
session.getCookies().put("__Secure-next-auth.session-token", sessionToken);
146154
session.getCookies().put("__Secure-next-auth.callback-url", "https://chat.openai.com/");
147155
session.getCookies().put("cf_clearance", cfClearance);
148156

@@ -197,12 +205,12 @@ public Map<String, Object> getChatText(Map<String, Object> data) {
197205
}
198206

199207
private void setupProxy(Session session) {
200-
if (config.get("proxy") != null && !config.get("proxy").equals("")) {
201-
Map<String, String> proxies = new HashMap<>();
202-
proxies.put("http", config.get("proxy"));
203-
proxies.put("https", config.get("proxy"));
204-
session.setProxies(proxies);
205-
}
208+
// if (config.get("proxy") != null && !config.get("proxy").equals("")) {
209+
// Map<String, String> proxies = new HashMap<>();
210+
// proxies.put("http", config.get("proxy"));
211+
// proxies.put("https", config.get("proxy"));
212+
// session.setProxies(proxies);
213+
// }
206214
}
207215

208216
public Map<String, Object> getChatResponse(String prompt, String output) {
@@ -240,95 +248,50 @@ public Map<String, Object> getChatResponse(String prompt) {
240248

241249
@SneakyThrows
242250
public void refreshSession() {
243-
if (!config.containsKey("session_token")) {
244-
throw new RuntimeException("No tokens provided");
245-
} else {
246-
String sessionToken = config.get("session_token");
247-
if (sessionToken == null || sessionToken.equals("")) {
248-
throw new RuntimeException("No tokens provided");
249-
}
250-
Session session = new Session();
251251

252-
// Set proxies
253-
setupProxy(session);
252+
if (sessionToken == null || sessionToken.equals("")) {
253+
throw new RuntimeException("No tokens provided");
254+
}
255+
Session session = new Session();
254256

255-
// Set cookies
256-
session.getCookies().put("__Secure-next-auth.session-token", config.get(
257-
"session_token"));
258-
session.getCookies().put("cf_clearance", cfClearance);
257+
// Set proxies
258+
setupProxy(session);
259259

260-
String urlSession = "https://chat.openai.com/api/auth/session";
261-
HttpResponse response = session.get2(urlSession,
262-
Collections.singletonMap("User-Agent", userAgent));
260+
// Set cookies
261+
session.getCookies().put("__Secure-next-auth.session-token", sessionToken);
262+
session.getCookies().put("cf_clearance", cfClearance);
263263

264-
try {
265-
String name = "__Secure-next-auth.session-token";
266-
String cookieValue = response.getCookieValue(name);
267-
config.put("session_token", cookieValue);
264+
String urlSession = "https://chat.openai.com/api/auth/session";
265+
HttpResponse response = session.get2(urlSession,
266+
Collections.singletonMap("User-Agent", userAgent));
267+
if (response.getStatus() != 200) {
268+
System.out.println("err code: " + response.getStatus());
269+
System.out.println("cf_clearance: " + cfClearance);
270+
System.out.println("token: " + sessionToken);
271+
System.out.println("userAgent: " + userAgent);
268272

269-
String body = response.body();
270-
System.out.println("session_token: " + cookieValue);
271-
JSONObject responseObject = JSON.parseObject(body);
273+
return;
274+
}
272275

273-
String accessToken = responseObject.getString("accessToken");
274-
System.out.println("accessToken: " + accessToken);
276+
try {
277+
String name = "__Secure-next-auth.session-token";
278+
String cookieValue = response.getCookieValue(name);
279+
sessionToken = cookieValue;
275280

276-
config.put("Authorization", accessToken);
281+
String body = response.body();
282+
System.out.println("session_token: " + cookieValue);
283+
JSONObject responseObject = JSON.parseObject(body);
277284

278-
this.refreshHeaders();
279-
} catch (Exception e) {
280-
System.out.println("Error refreshing session");
281-
throw new Exception("Error refreshing session", e);
282-
}
283-
}
284-
}
285+
String accessToken = responseObject.getString("accessToken");
286+
System.out.println("accessToken: " + accessToken);
285287

288+
authorization = accessToken;
289+
// config.put("Authorization", accessToken);
286290

287-
@Deprecated
288-
public void login(String email, String password) {
289-
System.out.println("Logging in...");
290-
boolean useProxy = false;
291-
String proxy = null;
292-
if (config.containsKey("proxy")) {
293-
if (!config.get("proxy").equals("")) {
294-
useProxy = true;
295-
proxy = config.get("proxy");
296-
}
297-
}
298-
OpenAIAuth auth = new OpenAIAuth(email, password, useProxy, proxy);
299-
try {
300-
auth.begin();
301-
} catch (Exception e) {
302-
// if RuntimeException with e as "Captcha detected" fail
303-
if (e.getMessage().equals("Captcha detected")) {
304-
System.out.println("Captcha not supported. Use session tokens instead.");
305-
throw new RuntimeException("Captcha detected", e);
306-
}
307-
throw new RuntimeException("Error logging in", e);
308-
}
309-
if (auth.getAccessToken() != null) {
310-
config.put("Authorization", auth.getAccessToken());
311-
if (auth.getSessionToken() != null) {
312-
config.put("session_token", auth.getSessionToken());
313-
} else {
314-
String possibleTokens = auth.getSession().getCookies().get("__Secure-next-auth" +
315-
".session-token");
316-
if (possibleTokens != null) {
317-
if (possibleTokens.length() > 1) {
318-
config.put("session_token", possibleTokens);
319-
// config.put("session_token", possibleTokens[0]);
320-
} else {
321-
try {
322-
config.put("session_token", possibleTokens);
323-
} catch (Exception e) {
324-
throw new RuntimeException("Error logging in", e);
325-
}
326-
}
327-
}
328-
}
329291
this.refreshHeaders();
330-
} else {
331-
throw new RuntimeException("Error logging in");
292+
} catch (Exception e) {
293+
System.out.println("Error refreshing session");
294+
throw new Exception("Error refreshing session", e);
332295
}
333296
}
334297

src/main/java/com/github/plexpt/chatgpt/Config.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
public class Config {
1111
private String email;
1212
private String password;
13+
private String userAgent;
14+
private String cfClearance;
15+
private String session_token;
1316
}

0 commit comments

Comments
 (0)