From 5f0f58273ca47a8acbd701659afacded726f4295 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Mon, 17 Apr 2023 23:33:07 +0530 Subject: [PATCH 1/2] BAEL-6355 - OpenAI API with Spring Boot --- .../config/OpenAIRestTemplateConfig.java | 25 +++++++++++ .../openapi/controller/ChatController.java | 44 +++++++++++++++++++ .../com/baeldung/openapi/dto/ChatRequest.java | 32 ++++++++++++++ .../baeldung/openapi/dto/ChatResponse.java | 38 ++++++++++++++++ .../com/baeldung/openapi/dto/Message.java | 31 +++++++++++++ .../src/main/resources/application.properties | 6 ++- 6 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/config/OpenAIRestTemplateConfig.java create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/controller/ChatController.java create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/dto/ChatRequest.java create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/dto/ChatResponse.java create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/dto/Message.java diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/config/OpenAIRestTemplateConfig.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/config/OpenAIRestTemplateConfig.java new file mode 100644 index 0000000000..b832ee4264 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/config/OpenAIRestTemplateConfig.java @@ -0,0 +1,25 @@ +package com.baeldung.openapi.config; + +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class OpenAIRestTemplateConfig { + + @Value("${openai.api.key}") + private String openaiApiKey; + + @Bean + @Qualifier("openaiRestTemplate") + public RestTemplate openaiRestTemplate() { + RestTemplate restTemplate = new RestTemplate(); + restTemplate.getInterceptors().add((request, body, execution) -> { + request.getHeaders().add("Authorization", "Bearer " + openaiApiKey); + return execution.execute(request, body); + }); + return restTemplate; + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/controller/ChatController.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/controller/ChatController.java new file mode 100644 index 0000000000..4cd65580dc --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/controller/ChatController.java @@ -0,0 +1,44 @@ +package com.baeldung.openapi.controller; + +import com.baeldung.openapi.dto.ChatRequest; +import com.baeldung.openapi.dto.ChatResponse; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +@RestController +public class ChatController { + + @Qualifier("openaiRestTemplate") + @Autowired + private RestTemplate restTemplate; + + @Value("${openai.model}") + private String model; + + @Value("${openai.api.url}") + private String apiUrl; + + @GetMapping("/chat") + public String chat(@RequestParam String prompt) { + // create a request + ChatRequest request = new ChatRequest(model, prompt); + + // call the API + ChatResponse response = restTemplate.postForObject( + apiUrl, + request, + ChatResponse.class); + + if (response == null || response.getChoices() == null || response.getChoices().isEmpty()) { + return "No response"; + } + + // return the first response + return response.getChoices().get(0).getMessage().getContent(); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/dto/ChatRequest.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/dto/ChatRequest.java new file mode 100644 index 0000000000..1e32b937ce --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/dto/ChatRequest.java @@ -0,0 +1,32 @@ +package com.baeldung.openapi.dto; + +import java.util.ArrayList; +import java.util.List; + +public class ChatRequest { + private String model; + private List messages; + + public ChatRequest(String model, String prompt) { + this.model = model; + + this.messages = new ArrayList<>(); + this.messages.add(new Message("user", prompt)); + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public List getMessages() { + return messages; + } + + public void setMessages(List messages) { + this.messages = messages; + } +} diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/dto/ChatResponse.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/dto/ChatResponse.java new file mode 100644 index 0000000000..937e2d87db --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/dto/ChatResponse.java @@ -0,0 +1,38 @@ +package com.baeldung.openapi.dto; + +import java.util.List; + +public class ChatResponse { + + private List choices; + + public static class Choice { + + private int index; + private Message message; + + public int getIndex() { + return index; + } + + public void setIndex(int index) { + this.index = index; + } + + public Message getMessage() { + return message; + } + + public void setMessage(Message message) { + this.message = message; + } + } + + public List getChoices() { + return choices; + } + + public void setChoices(List choices) { + this.choices = choices; + } +} diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/dto/Message.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/dto/Message.java new file mode 100644 index 0000000000..242cf4e89f --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/dto/Message.java @@ -0,0 +1,31 @@ +package com.baeldung.openapi.dto; + +public class Message { + + private String role; + private String content; + + Message(String role, String content) { + this.role = role; + this.content = content; + } + + Message() { + } + + public String getRole() { + return role; + } + + public void setRole(String role) { + this.role = role; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/resources/application.properties b/spring-boot-modules/spring-boot-libraries-2/src/main/resources/application.properties index a18eda349a..47b2973fca 100644 --- a/spring-boot-modules/spring-boot-libraries-2/src/main/resources/application.properties +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/resources/application.properties @@ -47,4 +47,8 @@ resilience4j.ratelimiter.instances.rateLimiterApi.limit-refresh-period=60s resilience4j.ratelimiter.instances.rateLimiterApi.timeout-duration=0s resilience4j.ratelimiter.instances.rateLimiterApi.allow-health-indicator-to-fail=true resilience4j.ratelimiter.instances.rateLimiterApi.subscribe-for-events=true -resilience4j.ratelimiter.instances.rateLimiterApi.event-consumer-buffer-size=50 \ No newline at end of file +resilience4j.ratelimiter.instances.rateLimiterApi.event-consumer-buffer-size=50 + +openai.model=gpt-3.5-turbo +openai.api.url=https://api.openai.com/v1/chat/completions +openai.api.key=your-api-key \ No newline at end of file From 44f0f0ea5a2083392f77bbda9470a31dcd5c741f Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Sun, 23 Apr 2023 14:02:10 +0530 Subject: [PATCH 2/2] BAEL-6355 - OpenAI API with Spring Boot - review comment incorporated --- .../baeldung/openapi/controller/ChatController.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/controller/ChatController.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/controller/ChatController.java index 4cd65580dc..129d233582 100644 --- a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/controller/ChatController.java +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/controller/ChatController.java @@ -23,12 +23,17 @@ public class ChatController { @Value("${openai.api.url}") private String apiUrl; + /** + * Creates a chat request and sends it to the OpenAI API + * Returns the first message from the API response + * + * @param prompt the prompt to send to the API + * @return first message from the API response + */ @GetMapping("/chat") public String chat(@RequestParam String prompt) { - // create a request ChatRequest request = new ChatRequest(model, prompt); - // call the API ChatResponse response = restTemplate.postForObject( apiUrl, request, @@ -38,7 +43,6 @@ public class ChatController { return "No response"; } - // return the first response return response.getChoices().get(0).getMessage().getContent(); } } \ No newline at end of file