|
| 1 | +package com.thomasvitale.ai.spring; |
| 2 | + |
| 3 | +import org.springframework.ai.chat.prompt.Prompt; |
| 4 | +import org.springframework.ai.mistralai.MistralAiChatModel; |
| 5 | +import org.springframework.ai.mistralai.MistralAiChatOptions; |
| 6 | +import org.springframework.ai.openai.OpenAiChatModel; |
| 7 | +import org.springframework.ai.openai.OpenAiChatOptions; |
| 8 | +import org.springframework.web.bind.annotation.GetMapping; |
| 9 | +import org.springframework.web.bind.annotation.RequestParam; |
| 10 | +import org.springframework.web.bind.annotation.RestController; |
| 11 | + |
| 12 | +@RestController |
| 13 | +class ChatController { |
| 14 | + |
| 15 | + private final MistralAiChatModel mistralAiChatModel; |
| 16 | + private final OpenAiChatModel openAiChatModel; |
| 17 | + |
| 18 | + ChatController(MistralAiChatModel mistralAiChatModel, OpenAiChatModel openAiChatModel) { |
| 19 | + this.mistralAiChatModel = mistralAiChatModel; |
| 20 | + this.openAiChatModel = openAiChatModel; |
| 21 | + } |
| 22 | + |
| 23 | + @GetMapping("/chat/mistral") |
| 24 | + String chatMistralAi(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) { |
| 25 | + return mistralAiChatModel.call(message); |
| 26 | + } |
| 27 | + |
| 28 | + @GetMapping("/chat/openai") |
| 29 | + String chatOpenAi(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) { |
| 30 | + return openAiChatModel.call(message); |
| 31 | + } |
| 32 | + |
| 33 | + @GetMapping("/chat/mistral-options") |
| 34 | + String chatWithMistralAiOptions(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) { |
| 35 | + return mistralAiChatModel.call(new Prompt(message, MistralAiChatOptions.builder() |
| 36 | + .withModel("open-mixtral-8x7b") |
| 37 | + .withTemperature(1.0f) |
| 38 | + .build())) |
| 39 | + .getResult().getOutput().getContent(); |
| 40 | + } |
| 41 | + |
| 42 | + @GetMapping("/chat/openai-options") |
| 43 | + String chatWithOpenAiOptions(@RequestParam(defaultValue = "What did Gandalf say to the Balrog?") String message) { |
| 44 | + return openAiChatModel.call(new Prompt(message, OpenAiChatOptions.builder() |
| 45 | + .withModel("gpt-4-turbo") |
| 46 | + .withTemperature(1.0f) |
| 47 | + .build())) |
| 48 | + .getResult().getOutput().getContent(); |
| 49 | + } |
| 50 | + |
| 51 | +} |
0 commit comments