Skip to content

Commit 4e9e4eb

Browse files
committed
🎉 fix all
1 parent 206bd81 commit 4e9e4eb

File tree

4 files changed

+132
-83
lines changed

4 files changed

+132
-83
lines changed

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"email":"[email protected]",
33
"password": "xxx",
4-
"session_token" : " "
4+
"session_token" : "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..pDMxLMivBYXB17Ro.9D4en7rDjvTrWuK3nBEyPKpB5Cr1lYmfTVyc9dLiSQRkwVwcqgC2kxoZ5KWk9tw-xRXCTwNftQDbPPpSyvNaTzzW44Fr0e0W31_x7U4tYTtq_wwhakLiCJG_iWmErEiX7ILE3uD47LwwIKEudAwFgJnVY1e5LvkLVr1xJ7fdVJpGG8SucljbMeZA70Q0HI3tx0oIeT3wUb8LR6RTWs8XoLXdDByQVbaogCcODBexSO8Cu2L_IYfzDiUphW2wiRvSl7ac1Eda-k8MhOGDH0Ti4brUxFqegoSCvIN_vst3KT49sjL7Exi6zS7I8Al_YAoHVFAKblSnigdEXso1nezwDIUzi9_rWpXNaLZpCVy3tNROOn7jwaFKHh09f6fwAoTs3xKlGQwho80HlHjGV_MnVOG7BzWwZSu9qXdkEUpQiaIupKHOI7DVAW4iJHXxOs8hjgH8_fr61c7gPqd_Vvh2U_ZgGwejG9zWAay2kgA06neiBEm8-4t3KhLWP4alsbUe0VsNZpDfUQbiQzE_SjXmkyfvfLNaL8quTpiMTzdVHz8lgv7i88oupV4tYi7250qxk9R1YjWWcQuR8ey-PjWjikmIVZM3axRwuV8LVOHYvE_zm2ezUfg8lXz_9kvdyowmHuXmSP5V9NJJwsULENQMJS0PBwkElkuNZK4usbSwiphnPjPdwRlh60SOFWALq4jGNF4FFFA3iKhRMwCOIL1w13roOiCye6n5FEhE5RaNQifpL9eiRXkaMygwp_w6UkhQ64PLsLGWVF7GByb1rP2cLfWzi-FkegixnWaWY2E9NyxIerc8RaF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id2HFKbo_zlZR6liFVFyfuWQCUI7U9RZHdnasJO0EaazRDtsZOn25iOKWwBNWz6xc_aAe-djvxFIg5o6xFcD2ICPFFiItgVP9ZKbaSMPt5VmAXzcfDk-y29kHBULGdlS69J18uIa2vwbPKhHZGGSk9saVVP2FoCCZWBGTYyxWuvVy-S6VfqMaK90d_jFcJnzyOYzj1o2dzehNloLTFTtp1XupOk1mWgA-kPTWUwCZPon8vnfHMsCtNHz7g49F5ct0H0rN4ebxLFu6-UMZdNAS9w7mZdvHskrUXLHRWALh4CAIxRVXjDl25Wz4sm2qqdoZnAZmYM2hiVZHkkq5gSan7akP60iK_sM8L5s9-NNY6hjQoUfDrADGGurw2iBIbiyoDyfIZG51LFsD3kZC12YsRrGw6POpy4SMzq7N7QMOOIJETGxVWXQ.J3g7qTqK295YtTdBxhSboQ"
55
}

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ public static void main(String[] args) {
101101
System.out.println(e);
102102
}
103103
} else {
104-
List<String> messages = new ArrayList<>();
105-
int linesPrinted = 0;
106104

107105
try {
108106
System.out.println("Chatbot: ");
@@ -118,19 +116,6 @@ public static void main(String[] args) {
118116
}
119117
}
120118

121-
// Wrap each part separately
122-
// formattedParts.clear();
123-
// for (String part : messageParts) {
124-
// formattedParts.addAll(Arrays.asList(TextWrapper.wrap(part, width
125-
// = 80)));
126-
// for (String formattedLine : formattedParts) {
127-
// if (formattedParts.size() > linesPrinted + 1) {
128-
// System.out.println(formattedParts.get(linesPrinted));
129-
// linesPrinted++;
130-
// }
131-
// }
132-
// }
133-
// System.out.println(formattedParts.get(linesPrinted));
134119
} catch (Exception e) {
135120
System.out.println("Something went wrong!");
136121
e.printStackTrace();

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

Lines changed: 117 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.github.plexpt.chatgpt;
22

33
import com.alibaba.fastjson2.JSON;
4+
import com.alibaba.fastjson2.JSONArray;
45
import com.alibaba.fastjson2.JSONObject;
56
import com.google.gson.Gson;
67

78
import java.util.Collections;
89
import java.util.HashMap;
910
import java.util.Map;
1011
import java.util.UUID;
11-
import java.util.regex.Pattern;
1212

1313
import cn.hutool.http.HttpResponse;
1414
import cn.hutool.http.HttpUtil;
@@ -73,15 +73,15 @@ public void refreshHeaders() {
7373
Map<String, Object> getChatStream(Map<String, Object> data) {
7474
String url = "https://chat.openai.com/backend-api/conversation";
7575

76-
String responseBody = HttpUtil.createPost(url)
76+
String body = HttpUtil.createPost(url)
7777
.headerMap(headers, true)
7878
.body(JSON.toJSONString(data), "application/json")
7979
.execute()
8080
.body();
8181

8282
String message = "";
8383
Map<String, Object> chatData = new HashMap<>();
84-
for (String s : responseBody.split("\n")) {
84+
for (String s : body.split("\n")) {
8585
if ((s == null) || "".equals(s)) {
8686
continue;
8787
}
@@ -94,10 +94,14 @@ Map<String, Object> getChatStream(Map<String, Object> data) {
9494

9595
try {
9696

97-
message = lineData.getJSONObject("message")
97+
JSONArray jsonArray = lineData.getJSONObject("message")
9898
.getJSONObject("content")
99-
.getJSONArray("parts")
100-
.getString(0);
99+
.getJSONArray("parts");
100+
101+
if (jsonArray.size() == 0) {
102+
continue;
103+
}
104+
message = jsonArray.getString(0);
101105

102106
conversationId = lineData.getString("conversation_id");
103107
parentId = (lineData.getJSONObject("message")).getString("id");
@@ -138,79 +142,125 @@ public Map<String, Object> getChatText(Map<String, Object> data) {
138142
session.setProxies(proxies);
139143
}
140144

141-
Response response = session.post("https://chat.openai.com/backend-api/conversation", data);
145+
HttpResponse response = session.post2("https://chat.openai.com/backend-api/conversation",
146+
data);
147+
String body = response.body();
142148

143149
String errorDesc = "";
144150

145-
try {
146-
JSONObject responseObject = JSON.parseObject(response.toString());
147-
148151

149-
this.parentId = (String) responseObject.getJSONObject("message").get("id");
150-
this.conversationId = (String) responseObject.get("conversation_id");
152+
String message = "";
153+
Map<String, Object> chatData = new HashMap<>();
154+
for (String s : body.split("\n")) {
155+
if ((s == null) || "".equals(s)) {
156+
continue;
157+
}
158+
if (s.contains("data: [DONE]")) {
159+
continue;
160+
}
151161

152-
Map<String, Object> message = (Map<String, Object>) responseObject.getJSONObject(
153-
"message")
154-
.getJSONObject("content")
155-
.getJSONArray("parts")
156-
.get(0);
157-
Map<String, Object> result = new HashMap<>();
158-
result.put("message", message);
159-
result.put("conversation_id", this.conversationId);
160-
result.put("parent_id", this.parentId);
161-
return result;
162+
String part = s.substring(5);
163+
JSONObject lineData = JSON.parseObject(part);
162164

163-
} catch (Exception e) {
164-
e.printStackTrace();
165165
try {
166-
// Get the title text
167-
System.out.println(response.toString());
168-
169-
String titleText = Pattern.compile("<title>(.*)</title>")
170-
.matcher(response.toString())
171-
.group();
172-
173-
// // Find all div elements and capture the id attribute and the contents of the
174-
// // element
175-
// String divPattern = "<div[^>]*id=\"([^\"]*)\">(.*)</div>";
176-
// Matcher matcher = Pattern.compile(divPattern)
177-
// .matcher(response.toString());
178-
// List<String[]> divElements = (List<String[]>) matcher;
166+
167+
JSONArray jsonArray = lineData.getJSONObject("message")
168+
.getJSONObject("content")
169+
.getJSONArray("parts");
170+
171+
if (jsonArray.size() == 0) {
172+
continue;
173+
}
174+
message = jsonArray.getString(0);
175+
176+
conversationId = lineData.getString("conversation_id");
177+
parentId = (lineData.getJSONObject("message")).getString("id");
178+
179+
chatData.put("message", message);
180+
chatData.put("conversation_id", conversationId);
181+
chatData.put("parent_id", parentId);
182+
} catch (Exception e) {
183+
System.out.println("getChatStream Exception: " + part);
184+
// e.printStackTrace();
185+
continue;
186+
}
187+
188+
}
189+
return chatData;
190+
191+
// try {
192+
// JSONObject responseObject = JSON.parseObject(body);
179193
//
180194
//
181-
// for (int i = 1; i <= matcher.groupCount(); i++) {
182-
// String group = matcher.group(i);
183-
// // 对匹配的组进行操作
195+
// this.parentId = (String) responseObject.getJSONObject("message").get("id");
196+
// this.conversationId = (String) responseObject.get("conversation_id");
184197
//
185-
// }
198+
// JSONArray jsonArray = responseObject.getJSONObject(
199+
// "message")
200+
// .getJSONObject("content")
201+
// .getJSONArray("parts");
202+
// if (jsonArray.size() == 0) {
203+
//// continue;
204+
// }
205+
// Map<String, Object> message = (Map<String, Object>) jsonArray
206+
// .get(0);
207+
// Map<String, Object> result = new HashMap<>();
208+
// result.put("message", message);
209+
// result.put("conversation_id", this.conversationId);
210+
// result.put("parent_id", this.parentId);
211+
// return result;
186212
//
187-
// .results()
188-
// .map(m -> new String[]{m.group(1), m.group(2)})
189-
// .collect(Collectors.toList());
213+
// } catch (Exception e) {
214+
// e.printStackTrace();
215+
// try {
216+
// // Get the title text
217+
// System.out.println(body);
190218
//
191-
// // Loop through the div elements and find the one with the "message" id
192-
// String messageText = "";
193-
// for (String[] div : divElements) {
194-
// String divId = div[0];
195-
// String divContent = div[1];
196-
// if (divId.equals("message")) {
197-
// messageText = divContent;
198-
// break;
199-
// }
200-
// }
201-
// Concatenate the title and message text
202-
errorDesc = titleText;
203-
} catch (Exception ex) {
204-
ex.printStackTrace();
205-
// errorDesc = (String) ((Map) JSON.parse(response.toString())).get("detail");
206-
// if (errorDesc.containsKey("message")) {
207-
// errorDesc = (String) errorDesc.get("message");
208-
// }
209-
} finally {
210-
System.out.println(response.toString());
211-
throw new RuntimeException("Response is not in the correct format " + errorDesc);
212-
}
213-
}
219+
// String titleText = Pattern.compile("<title>(.*)</title>")
220+
// .matcher(response.toString())
221+
// .group();
222+
//
223+
//// // Find all div elements and capture the id attribute and the contents of the
224+
//// // element
225+
//// String divPattern = "<div[^>]*id=\"([^\"]*)\">(.*)</div>";
226+
//// Matcher matcher = Pattern.compile(divPattern)
227+
//// .matcher(response.toString());
228+
//// List<String[]> divElements = (List<String[]>) matcher;
229+
////
230+
////
231+
//// for (int i = 1; i <= matcher.groupCount(); i++) {
232+
//// String group = matcher.group(i);
233+
//// // 对匹配的组进行操作
234+
////
235+
//// }
236+
////
237+
//// .results()
238+
//// .map(m -> new String[]{m.group(1), m.group(2)})
239+
//// .collect(Collectors.toList());
240+
////
241+
//// // Loop through the div elements and find the one with the "message" id
242+
//// String messageText = "";
243+
//// for (String[] div : divElements) {
244+
//// String divId = div[0];
245+
//// String divContent = div[1];
246+
//// if (divId.equals("message")) {
247+
//// messageText = divContent;
248+
//// break;
249+
//// }
250+
//// }
251+
// // Concatenate the title and message text
252+
// errorDesc = titleText;
253+
// } catch (Exception ex) {
254+
// ex.printStackTrace();
255+
//// errorDesc = (String) ((Map) JSON.parse(response.toString())).get("detail");
256+
//// if (errorDesc.containsKey("message")) {
257+
//// errorDesc = (String) errorDesc.get("message");
258+
//// }
259+
// } finally {
260+
// System.out.println(response.toString());
261+
// throw new RuntimeException("Response is not in the correct format " + errorDesc);
262+
// }
263+
// }
214264

215265
}
216266

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.plexpt.chatgpt;
22

3+
import com.alibaba.fastjson2.JSON;
4+
35
import net.dreamlu.mica.http.HttpRequest;
46

57
import java.util.HashMap;
@@ -30,6 +32,18 @@ public Response post(String url, Map<String, Object> data) {
3032
.response();
3133
}
3234

35+
public HttpResponse post2(String url, Map<String, Object> data) {
36+
getCookiesString();
37+
38+
39+
return HttpUtil.createPost(url)
40+
.addHeaders(headers)
41+
.cookie(getCookiesString())
42+
.body(JSON.toJSONString(data), "application/json")
43+
.execute();
44+
45+
}
46+
3347
public Response post(String url, Map<String, Object> data, boolean followRedirects) {
3448
getCookiesString();
3549

0 commit comments

Comments
 (0)