GenAI using Kotlin and Spring-AI with ChatGPT

April 18, 2024

Jeff Sheets

Generative AI is all the hype lately, so why not show how to call the OpenAI GPT-4 ChatGPT API from Kotlin using Spring-AI?!? It can be done with a surprisingly small amount of upfront setup. So here is an example REST API that returns either a Quote or a Haiku written by the all-knowing genie of GenAI itself -- GPT-4.


The Goal 🤔

- Create a `localhost:8080/quote` endpoint that can give a random quote from a computer scientist.
- And for fun, another endpoint at `localhost:8080/haiku` to add some joy to your life.

TL&DR; Time is short -- If you only want the code, jump to the src in Github at

The Setup 💻

You can jumpstart the journey by generating a Spring Kotlin project from the Spring Initializr. Add dependencies of `Spring Web` and `OpenAI`. Then download the artifact and open your project in IntelliJ. (Or clone my repo from

This will pull in the Spring-AI library project that is part of Spring Boot, including the adapters utilized for communicating with OpenAI.

OpenAI Key Setup 🔐

  1. You'll need an OpenAI ChatGPT account, so go do the free signup now if you haven't already
  2. Go to the API Keys page at
  3. Create a new secret key, and copy that value to your clipboard
  4. Then either A) store that in a new env variable `export SPRING_AI_OPENAI_API_KEY=<your-key-here>` or B) you could paste it in the file, but that's pretty dangerous as you don't want to commit that file.
  5. Also in, you need to set the model, which I have defaulted to ``

Create a controller to call ChatGPT ⌨️

I'm doing this in Kotlin, but you could use Groovy or Java or any other JVM language that works with Spring Boot here. You can see I've created two endpoints, one for a simple haiku, and one for a quote:

class ChatController(val chatClient: ChatClient) {

   fun haiku(@RequestParam(defaultValue = "write a haiku about software engineering") message: String): String {

   fun quote(): String {
       val message = """
         Give me a random one-line quote from a famous computer scientist or software developer.
         To help make it random, the first letter of the last name should coincide with
           the current second of the current minute of time.
         Include a newline at the end of the quote

Test it! 🚀

- `./gradlew bootRun` to launch the API, be default at localhost port 8080
- `curl localhost:8080/quote` and you'll see some inspirational quote like:

"Premature optimization is the root of all evil." - Donald Knuth

- `curl localhost:8080/haiku` to see something beautiful like:

Code lines intertwine,
Creating digital life,
In software, we find.

That's it???

I know right?!? This simple integration is all it takes to call into the GenAI world and have some genie answer like they are from the future. Hopefully this distills some of the hype. The real magic lies in figuring out how best to harness this in a meaningful way in your own software.

-- Cheers!

(this article was originally posted on Jeff Sheets' personal blog. Also a big thanks to Dan Vega of the SpringBoot team for inspiring this demo from the Spring AI Intro video! Cover image generated in Canva via prompt: "an old timey macintosh in a cartoony style with a spring and cup of java coffee on the display")