Skip to content

Commit 25dbbff

Browse files
committed
📝
1 parent e26d43c commit 25dbbff

File tree

3 files changed

+12
-101
lines changed

3 files changed

+12
-101
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ https://mirror.xyz/boxchen.eth/9O9CSqyKDj4BKUIil7NC1Sa1LJM-3hsPqaeW_QjfFBc
6262
1. 下载
6363
2. 编辑 config.json 里的sessionToken
6464
3. 运行 run.bat
65+
4. 注意:输入之后需要回车两次
6566

6667
# Awesome ChatGPT
6768
[My list](https://github.com/stars/acheong08/lists/awesome-chatgpt)

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.alibaba.fastjson2.JSON;
44
import com.alibaba.fastjson2.TypeReference;
5-
import com.google.gson.Gson;
65

76
import java.io.BufferedReader;
87
import java.io.File;
@@ -19,7 +18,6 @@
1918

2019

2120
public class ChatGTP {
22-
private static final Gson gson = new Gson();
2321

2422
@SneakyThrows
2523
public static String getInput(String prompt) {
@@ -105,7 +103,6 @@ public static void main(String[] args) {
105103

106104
try {
107105
System.out.println("Chatbot: ");
108-
List<String> formattedParts = new ArrayList<>();
109106
Map<String, Object> message = chatbot.getChatResponse(prompt, "stream");
110107
// Split the message by newlines
111108
String[] messageParts = message.get("message").toString().split("\n");

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

Lines changed: 11 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,11 @@ public Map<String, Object> getChatText(Map<String, Object> data) {
136136
session.setHeaders(this.headers);
137137

138138
// Set multiple cookies
139-
session.getCookies().put("__Secure-next-auth.session-token", config.get(
140-
"session_token"));
139+
session.getCookies().put("__Secure-next-auth.session-token", config.get("session_token"));
141140
session.getCookies().put("__Secure-next-auth.callback-url", "https://chat.openai.com/");
142141

143142
// Set proxies
144-
if (config.get("proxy") != null && !config.get("proxy").equals("")) {
145-
Map<String, String> proxies = new HashMap<>();
146-
proxies.put("http", config.get("proxy"));
147-
proxies.put("https", config.get("proxy"));
148-
session.setProxies(proxies);
149-
}
143+
setupProxy(session);
150144

151145
HttpResponse response = session.post2("https://chat.openai.com/backend-api/conversation",
152146
data);
@@ -193,81 +187,15 @@ public Map<String, Object> getChatText(Map<String, Object> data) {
193187

194188
}
195189
return chatData;
190+
}
196191

197-
// try {
198-
// JSONObject responseObject = JSON.parseObject(body);
199-
//
200-
//
201-
// this.parentId = (String) responseObject.getJSONObject("message").get("id");
202-
// this.conversationId = (String) responseObject.get("conversation_id");
203-
//
204-
// JSONArray jsonArray = responseObject.getJSONObject(
205-
// "message")
206-
// .getJSONObject("content")
207-
// .getJSONArray("parts");
208-
// if (jsonArray.size() == 0) {
209-
//// continue;
210-
// }
211-
// Map<String, Object> message = (Map<String, Object>) jsonArray
212-
// .get(0);
213-
// Map<String, Object> result = new HashMap<>();
214-
// result.put("message", message);
215-
// result.put("conversation_id", this.conversationId);
216-
// result.put("parent_id", this.parentId);
217-
// return result;
218-
//
219-
// } catch (Exception e) {
220-
// e.printStackTrace();
221-
// try {
222-
// // Get the title text
223-
// System.out.println(body);
224-
//
225-
// String titleText = Pattern.compile("<title>(.*)</title>")
226-
// .matcher(response.toString())
227-
// .group();
228-
//
229-
//// // Find all div elements and capture the id attribute and the contents of the
230-
//// // element
231-
//// String divPattern = "<div[^>]*id=\"([^\"]*)\">(.*)</div>";
232-
//// Matcher matcher = Pattern.compile(divPattern)
233-
//// .matcher(response.toString());
234-
//// List<String[]> divElements = (List<String[]>) matcher;
235-
////
236-
////
237-
//// for (int i = 1; i <= matcher.groupCount(); i++) {
238-
//// String group = matcher.group(i);
239-
//// // 对匹配的组进行操作
240-
////
241-
//// }
242-
////
243-
//// .results()
244-
//// .map(m -> new String[]{m.group(1), m.group(2)})
245-
//// .collect(Collectors.toList());
246-
////
247-
//// // Loop through the div elements and find the one with the "message" id
248-
//// String messageText = "";
249-
//// for (String[] div : divElements) {
250-
//// String divId = div[0];
251-
//// String divContent = div[1];
252-
//// if (divId.equals("message")) {
253-
//// messageText = divContent;
254-
//// break;
255-
//// }
256-
//// }
257-
// // Concatenate the title and message text
258-
// errorDesc = titleText;
259-
// } catch (Exception ex) {
260-
// ex.printStackTrace();
261-
//// errorDesc = (String) ((Map) JSON.parse(response.toString())).get("detail");
262-
//// if (errorDesc.containsKey("message")) {
263-
//// errorDesc = (String) errorDesc.get("message");
264-
//// }
265-
// } finally {
266-
// System.out.println(response.toString());
267-
// throw new RuntimeException("Response is not in the correct format " + errorDesc);
268-
// }
269-
// }
270-
192+
private void setupProxy(Session session) {
193+
if (config.get("proxy") != null && !config.get("proxy").equals("")) {
194+
Map<String, String> proxies = new HashMap<>();
195+
proxies.put("http", config.get("proxy"));
196+
proxies.put("https", config.get("proxy"));
197+
session.setProxies(proxies);
198+
}
271199
}
272200

273201
public Map<String, Object> getChatResponse(String prompt, String output) {
@@ -316,12 +244,7 @@ public void refreshSession() {
316244
Session session = new Session();
317245

318246
// Set proxies
319-
if (config.get("proxy") != null && !config.get("proxy").equals("")) {
320-
Map<String, String> proxies = new HashMap<>();
321-
proxies.put("http", config.get("proxy"));
322-
proxies.put("https", config.get("proxy"));
323-
session.setProxies(proxies);
324-
}
247+
setupProxy(session);
325248

326249
// Set cookies
327250
session.getCookies().put("__Secure-next-auth.session-token", config.get(
@@ -350,19 +273,9 @@ public void refreshSession() {
350273

351274
config.put("Authorization", accessToken);
352275

353-
354-
// List<Cookie> cookies = Cookie.parseAll(HttpUrl.parse(urlSession),
355-
// response.headers());
356-
// for (Cookie cookie1 : cookies) {
357-
// if (cookie1.name().equals(name)) {
358-
// config.put("session_token", response.getCookieValue(name));
359-
// }
360-
// }
361-
362276
this.refreshHeaders();
363277
} catch (Exception e) {
364278
System.out.println("Error refreshing session");
365-
// System.out.println(response.toString());
366279
throw new Exception("Error refreshing session", e);
367280
}
368281
} else if (config.containsKey("email") && config.containsKey("password")) {

0 commit comments

Comments
 (0)