BAEL-6355 - OpenAI API with Spring Boot - review comment incorporated

This commit is contained in:
Abhinav Pandey 2023-04-23 14:02:10 +05:30
parent 8d5f39b720
commit c4ccc31008

View File

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