11package com .github .plexpt .chatgpt ;
22
33import com .alibaba .fastjson2 .JSON ;
4+ import com .alibaba .fastjson2 .JSONArray ;
45import com .alibaba .fastjson2 .JSONObject ;
56import com .google .gson .Gson ;
67
78import java .util .Collections ;
89import java .util .HashMap ;
910import java .util .Map ;
1011import java .util .UUID ;
11- import java .util .regex .Pattern ;
1212
1313import cn .hutool .http .HttpResponse ;
1414import 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
0 commit comments