From 44544dcd12be9a020a72404f6b341500e690902c Mon Sep 17 00:00:00 2001 From: Geoffrey Kwan Date: Mon, 22 Jan 2024 18:29:21 -0500 Subject: [PATCH 1/2] feat(AI Chat): Implement endpoint to make a request to Chat GPT #251 --- .../web/controllers/ChatGptcontroller.java | 57 +++++++++++++++++++ .../application-dockerdev-sample.properties | 2 + .../resources/application_sample.properties | 2 + 3 files changed, 61 insertions(+) create mode 100644 src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java 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 new file mode 100644 index 000000000..9cfbd8678 --- /dev/null +++ b/src/main/java/org/wise/portal/presentation/web/controllers/ChatGptcontroller.java @@ -0,0 +1,57 @@ +package org.wise.portal.presentation.web.controllers; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.security.access.annotation.Secured; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api") +public class ChatGptcontroller { + + @Autowired + Environment appProperties; + + @ResponseBody + @Secured("ROLE_USER") + @PostMapping("/chat-gpt") + protected String sendChatMessage(@RequestBody String body) { + String openaiApiKey = appProperties.getProperty("OPENAI_API_KEY"); + if (openaiApiKey == null || openaiApiKey.isEmpty()) { + throw new RuntimeException("OPENAI_API_KEY is not set"); + } + try { + 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.setDoOutput(true); + OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); + writer.write(body); + writer.flush(); + writer.close(); + BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String line; + StringBuffer response = new StringBuffer(); + while ((line = br.readLine()) != null) { + response.append(line); + } + br.close(); + return response.toString(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/resources/application-dockerdev-sample.properties b/src/main/resources/application-dockerdev-sample.properties index 809ef8133..bda5ac0f6 100644 --- a/src/main/resources/application-dockerdev-sample.properties +++ b/src/main/resources/application-dockerdev-sample.properties @@ -195,3 +195,5 @@ system-wide-salt=secret # IP Address of Clam AV server for virus scanning # clamav.server.address=127.0.0.1 + +#OPENAI_API_KEY= diff --git a/src/main/resources/application_sample.properties b/src/main/resources/application_sample.properties index f44ceb90c..3207e586c 100644 --- a/src/main/resources/application_sample.properties +++ b/src/main/resources/application_sample.properties @@ -195,3 +195,5 @@ system-wide-salt=secret # IP Address of Clam AV server for virus scanning # clamav.server.address=127.0.0.1 + +#OPENAI_API_KEY= From 50f11dfc9c35a15e6cb298dda01d2cfc4ed57d14 Mon Sep 17 00:00:00 2001 From: Geoffrey Kwan Date: Tue, 16 Apr 2024 10:26:03 -0400 Subject: [PATCH 2/2] feat(AI Chat): Add authoring config param for whether chat gpt is enabled (#271) --- .../web/controllers/author/project/AuthorAPIController.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/wise/portal/presentation/web/controllers/author/project/AuthorAPIController.java b/src/main/java/org/wise/portal/presentation/web/controllers/author/project/AuthorAPIController.java index 8810660b5..6b3b243b0 100644 --- a/src/main/java/org/wise/portal/presentation/web/controllers/author/project/AuthorAPIController.java +++ b/src/main/java/org/wise/portal/presentation/web/controllers/author/project/AuthorAPIController.java @@ -48,6 +48,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; +import org.apache.commons.lang3.StringUtils; import org.json.JSONException; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; @@ -398,6 +399,7 @@ protected HashMap getAuthorProjectConfig(Authentication auth, config.put("projectAssetURL", contextPath + "/api/author/project/asset/" + project.getId()); config.put("projectBaseURL", projectBaseURL); config.put("previewProjectURL", contextPath + "/preview/unit/" + project.getId()); + config.put("chatGptEnabled", !StringUtils.isEmpty(appProperties.getProperty("OPENAI_API_KEY"))); config.put("cRaterRequestURL", contextPath + "/api/c-rater"); config.put("importStepsURL", contextPath + "/api/author/project/importSteps/" + project.getId());