Merge pull request #13857 from abh1navv/BAEL-6355

BAEL-6355 - OpenAI API with Spring Boot
This commit is contained in:
doodoroma 2023-04-26 21:19:14 +02:00 committed by GitHub
commit 3c29c79dc8
6 changed files with 179 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -0,0 +1,48 @@
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;
/**
* 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) {
ChatRequest request = new ChatRequest(model, prompt);
ChatResponse response = restTemplate.postForObject(
apiUrl,
request,
ChatResponse.class);
if (response == null || response.getChoices() == null || response.getChoices().isEmpty()) {
return "No response";
}
return response.getChoices().get(0).getMessage().getContent();
}
}

View File

@ -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<Message> 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<Message> getMessages() {
return messages;
}
public void setMessages(List<Message> messages) {
this.messages = messages;
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.openapi.dto;
import java.util.List;
public class ChatResponse {
private List<Choice> 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<Choice> getChoices() {
return choices;
}
public void setChoices(List<Choice> choices) {
this.choices = choices;
}
}

View File

@ -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;
}
}

View File

@ -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
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