Skip to content

Instantly share code, notes, and snippets.

@rakia
Created February 5, 2025 15:09
Show Gist options
  • Save rakia/3d0184a3547adf6fb5c396c43e141711 to your computer and use it in GitHub Desktop.
Save rakia/3d0184a3547adf6fb5c396c43e141711 to your computer and use it in GitHub Desktop.
A spring boot REST controller to connect to Gemini API
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.vertexai.gemini.VertexAiGeminiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/gemini")
public class GeminiController {
@Autowired
private VertexAiGeminiChatModel chatModel;
public GeminiController(VertexAiGeminiChatModel 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