From 46a367c8f8f2dc000219d398c0d2d9d08c33fafa Mon Sep 17 00:00:00 2001 From: vesladm1 <41111902e@gapps.ntnu.edu.tw> Date: Tue, 9 Dec 2025 11:03:39 +0800 Subject: [PATCH 1/2] Fix Chinese encoding issue in ChatGptController --- .../web/controllers/ChatGptcontroller.java | 11 ++++-- wise_api_local_changes.patch | 38 +++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 wise_api_local_changes.patch diff --git a/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java b/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java index 9cfbd8678..9cbc0bc8c 100644 --- a/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java +++ b/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java @@ -6,7 +6,10 @@ import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; - +import org.json.JSONObject; +import org.json.JSONException; +import org.json.JSONArray; +import org.wise.portal.dao.ObjectNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.security.access.annotation.Secured; @@ -32,17 +35,19 @@ protected String sendChatMessage(@RequestBody String body) { throw new RuntimeException("OPENAI_API_KEY is not set"); } try { + System.out.println("sendChatMessage from wise api server"); URL url = new URL("https://api.openai.com/v1/chat/completions"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Authorization", "Bearer " + openaiApiKey); - connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); + connection.setRequestProperty("Accept-Charset", "UTF-8"); connection.setDoOutput(true); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(body); writer.flush(); writer.close(); - BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); + BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"ISO-8859-1")); String line; StringBuffer response = new StringBuffer(); while ((line = br.readLine()) != null) { diff --git a/wise_api_local_changes.patch b/wise_api_local_changes.patch new file mode 100644 index 000000000..ee83d8e16 --- /dev/null +++ b/wise_api_local_changes.patch @@ -0,0 +1,38 @@ +diff --git a/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java b/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java +index 9cfbd86..9cbc0bc 100644 +--- a/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java ++++ b/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java +@@ -6,7 +6,10 @@ import java.io.InputStreamReader; + import java.io.OutputStreamWriter; + import java.net.HttpURLConnection; + import java.net.URL; +- ++import org.json.JSONObject; ++import org.json.JSONException; ++import org.json.JSONArray; ++import org.wise.portal.dao.ObjectNotFoundException; + import org.springframework.beans.factory.annotation.Autowired; + import org.springframework.core.env.Environment; + import org.springframework.security.access.annotation.Secured; +@@ -32,17 +35,19 @@ public class ChatGptcontroller { + throw new RuntimeException("OPENAI_API_KEY is not set"); + } + try { ++ System.out.println("sendChatMessage from wise api server"); + URL url = new URL("https://api.openai.com/v1/chat/completions"); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Authorization", "Bearer " + openaiApiKey); +- connection.setRequestProperty("Content-Type", "application/json"); ++ connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); ++ connection.setRequestProperty("Accept-Charset", "UTF-8"); + connection.setDoOutput(true); + OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); + writer.write(body); + writer.flush(); + writer.close(); +- BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); ++ BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"ISO-8859-1")); + String line; + StringBuffer response = new StringBuffer(); + while ((line = br.readLine()) != null) { From ee555364161195168ef008c822881ce3805336eb Mon Sep 17 00:00:00 2001 From: vesladm1 <41111902e@gapps.ntnu.edu.tw> Date: Mon, 22 Dec 2025 22:33:43 +0800 Subject: [PATCH 2/2] Clean up ChatGptcontroller and remove local patch file --- .../web/controllers/ChatGptcontroller.java | 5 --- wise_api_local_changes.patch | 38 ------------------- 2 files changed, 43 deletions(-) delete mode 100644 wise_api_local_changes.patch diff --git a/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java b/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java index 9cbc0bc8c..c60ce3715 100644 --- a/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java +++ b/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java @@ -6,10 +6,6 @@ import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; -import org.json.JSONObject; -import org.json.JSONException; -import org.json.JSONArray; -import org.wise.portal.dao.ObjectNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.security.access.annotation.Secured; @@ -35,7 +31,6 @@ protected String sendChatMessage(@RequestBody String body) { throw new RuntimeException("OPENAI_API_KEY is not set"); } try { - System.out.println("sendChatMessage from wise api server"); URL url = new URL("https://api.openai.com/v1/chat/completions"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); diff --git a/wise_api_local_changes.patch b/wise_api_local_changes.patch deleted file mode 100644 index ee83d8e16..000000000 --- a/wise_api_local_changes.patch +++ /dev/null @@ -1,38 +0,0 @@ -diff --git a/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java b/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java -index 9cfbd86..9cbc0bc 100644 ---- a/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java -+++ b/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java -@@ -6,7 +6,10 @@ import java.io.InputStreamReader; - import java.io.OutputStreamWriter; - import java.net.HttpURLConnection; - import java.net.URL; -- -+import org.json.JSONObject; -+import org.json.JSONException; -+import org.json.JSONArray; -+import org.wise.portal.dao.ObjectNotFoundException; - import org.springframework.beans.factory.annotation.Autowired; - import org.springframework.core.env.Environment; - import org.springframework.security.access.annotation.Secured; -@@ -32,17 +35,19 @@ public class ChatGptcontroller { - throw new RuntimeException("OPENAI_API_KEY is not set"); - } - try { -+ System.out.println("sendChatMessage from wise api server"); - URL url = new URL("https://api.openai.com/v1/chat/completions"); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("POST"); - connection.setRequestProperty("Authorization", "Bearer " + openaiApiKey); -- connection.setRequestProperty("Content-Type", "application/json"); -+ connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); -+ connection.setRequestProperty("Accept-Charset", "UTF-8"); - connection.setDoOutput(true); - OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); - writer.write(body); - writer.flush(); - writer.close(); -- BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); -+ BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"ISO-8859-1")); - String line; - StringBuffer response = new StringBuffer(); - while ((line = br.readLine()) != null) {