forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-6355 - OpenAI API with Spring Boot
- Loading branch information
Showing
6 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...-boot-libraries-2/src/main/java/com/baeldung/openapi/config/OpenAIRestTemplateConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...spring-boot-libraries-2/src/main/java/com/baeldung/openapi/controller/ChatController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...t-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/dto/ChatRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/dto/ChatResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/openapi/dto/Message.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters