Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
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.beans.factory.annotation.Value;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -16,33 +15,36 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class ChatGptcontroller {
@RequestMapping("/api/chat-gpt")
public class ChatGptController {

@Autowired
Environment appProperties;
@Value("${openai.api.key:}")
private String openAiApiKey;

@Value("${openai.chat.api.url:https://api.openai.com/v1/chat/completions}")
private String openAiChatApiUrl;

@ResponseBody
@Secured("ROLE_USER")
@PostMapping("/chat-gpt")
@PostMapping
protected String sendChatMessage(@RequestBody String body) {
String openaiApiKey = appProperties.getProperty("OPENAI_API_KEY");
if (openaiApiKey == null || openaiApiKey.isEmpty()) {
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");
URL url = new URL(openAiChatApiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Bearer " + openaiApiKey);
connection.setRequestProperty("Authorization", "Bearer " + openAiApiKey);
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(),"ISO-8859-1"));
BufferedReader br = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
StringBuffer response = new StringBuffer();
while ((line = br.readLine()) != null) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application-dockerdev-sample.properties
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ system-wide-salt=secret
#speech-to-text.aws.identity-pool-id=

# OpenAI and AWS Bedrock Chat endpoints (optional)
#OPENAI_API_KEY=
#openai.api.key=
#openai.chat.api.url=
#aws.bedrock.api.key=
#aws.bedrock.runtime.endpoint=

3 changes: 2 additions & 1 deletion src/main/resources/application_sample.properties
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ system-wide-salt=secret
#speech-to-text.aws.identity-pool-id=

# OpenAI and AWS Bedrock Chat endpoints (optional)
#OPENAI_API_KEY=
#openai.api.key=
#openai.chat.api.url=
#aws.bedrock.api.key=
#aws.bedrock.runtime.endpoint=
Loading