Created
February 5, 2025 15:05
-
-
Save rakia/09ae8394889995a2717c7000d9524a76 to your computer and use it in GitHub Desktop.
A spring boot REST controller to connect to OpenAI API
This file contains hidden or 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
import org.springframework.ai.chat.prompt.Prompt; | |
import org.springframework.ai.openai.OpenAiChatModel; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestParam; | |
import org.springframework.web.bind.annotation.RestController; | |
@RestController | |
@RequestMapping("/api/openai") | |
public class OpenAIController { | |
@Autowired | |
private OpenAiChatModel chatModel; | |
public OpenAIController(OpenAiChatModel chatModel) { | |
this.chatModel = chatModel; | |
} | |
@GetMapping("/chat") | |
public String chat(@RequestParam String message) { | |
Prompt prompt = new Prompt(message); | |
return chatModel.call(prompt).getResult().getOutput().getContent(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment