BAEL-6355 - OpenAI API with Spring Boot - review comment incorporated
This commit is contained in:
parent
8d5f39b720
commit
c4ccc31008
@ -23,12 +23,17 @@ public class ChatController {
|
|||||||
@Value("${openai.api.url}")
|
@Value("${openai.api.url}")
|
||||||
private String apiUrl;
|
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")
|
@GetMapping("/chat")
|
||||||
public String chat(@RequestParam String prompt) {
|
public String chat(@RequestParam String prompt) {
|
||||||
// create a request
|
|
||||||
ChatRequest request = new ChatRequest(model, prompt);
|
ChatRequest request = new ChatRequest(model, prompt);
|
||||||
|
|
||||||
// call the API
|
|
||||||
ChatResponse response = restTemplate.postForObject(
|
ChatResponse response = restTemplate.postForObject(
|
||||||
apiUrl,
|
apiUrl,
|
||||||
request,
|
request,
|
||||||
@ -38,7 +43,6 @@ public class ChatController {
|
|||||||
return "No response";
|
return "No response";
|
||||||
}
|
}
|
||||||
|
|
||||||
// return the first response
|
|
||||||
return response.getChoices().get(0).getMessage().getContent();
|
return response.getChoices().get(0).getMessage().getContent();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user