Exploring AI with Groovy™
Published: 2025-10-15 07:06AM (Last updated: 2025-11-14 08:05PM)
Introduction
In this post, we’ll look at several ways to integrate Groovy with AI tools, including
Ollama4j,LangChain4j,Spring AI,Embabel,Micronaut LangChain4j, andQuarkus LangChain4j.

We’ll use a simple chat example, perhaps similar to what you might have done yourself when trying out your favourite LLM. We’ll ask for activities to do while on vacation.
To make the examples as accessible as possible, we’ll use Ollama, and use some not-too-large open-source models, which can be run locally. So, no need to get keys, or use up your token limits. But feel free to try out other models and services. Most of the libraries we use here can connect to remote models and services, sometimes just by changing a line or two of config in a properties file.
The examples mostly use the mistral:7b model, which you’ll need to download to run the examples
unchanged, but feel free to try other models and see what results you get.
Some of the examples also use the qwen3:8b model. It seems to give better results when using tools.
See the example repo for how to run the needed services using docker,
or within GitHub actions if you don’t have Ollama already running locally.
We also used Groovy 5 and JDK 25, but the examples should work on other Groovy and Java versions.
Using Ollama4j
Since we are using Ollama, we’ll start with a library aimed directly at that tool. Ollama4j provides a client for interacting with local or remote Ollama models. The examples here are geared towards local models, but see the Ollama documentation if you want to use Ollama cloud models.
We first create an instance of the Ollama class.
We set a generous timeout to allow for longer-running models
but otherwise leave the defaults as is.
While not strictly necessary, we can call the ping method to verify that the Ollama server is reachable.
var ollama = new Ollama(requestTimeoutSeconds: 300)
println "Found ollama: ${ollama.ping()}"
Which gives this output:
Found ollama: true
Now we can send our prompt to the model:
var prompt = 'What are 4 interesting things to do while I am on vacation in Caloundra?'
var builder = OllamaChatRequestBuilder.builder()
.withModel('mistral:7b')
var request = builder
.withMessage(OllamaChatMessageRole.USER, prompt)
.build()
var result = ollama.chat(request, null)
println "Four things:\n$result.responseModel.message.response"
It will respond with something like:
Four things: 1. Visit the beautiful beaches: Caloundra is famous for its stunning beaches, including Kings Beach, Moffat Beach, and Bulcock Beach. Spend your days soaking up the sun, swimming, or surfing. 2. Explore the UnderWater World SeaLife Aquarium: This marine attraction offers a unique opportunity to interact with various sea creatures. You can even have a close encounter with sharks and turtles! 3. Visit the Glastonbury Estate: For those who love history, this beautiful estate is worth a visit. It was built in the 1920s and features a variety of artifacts and memorabilia from World War I and II. 4. Take a day trip to the Australia Zoo: Made famous by the Crocodile Hunter, Steve Irwin, the Australia Zoo is just a short drive from Caloundra. It's home to a wide variety of Australian wildlife, including kangaroos, koalas, and crocodiles. Don't miss the daily wildlife shows!
AI chat calls are stateless, but we can simulate continuing the conversation by including the previous chat history as additional messages in a subsequent request:
var prompt2 = 'If I had half a day and can only go to one, which would you recommend?'
request = builder
.withMessages(result.chatHistory)
.withMessage(OllamaChatMessageRole.USER, prompt2)
.build()
result = ollama.chat(request, null)
println "Best thing:\n$result.responseModel.message.response"
The output might be:
Best thing: If you only have half a day and can choose just one activity, I would recommend visiting the beautiful Kings Beach in Caloundra. It offers a lovely stretch of sandy beach, perfect for swimming, sunbathing, or simply taking a leisurely stroll along the shoreline. The beach also has various amenities like picnic areas, BBQ facilities, and a playground for children. Plus, it provides stunning views of the Pacific Ocean and the Glass House Mountains in the distance. It's the perfect place to relax and soak up the sun on your vacation!
Using LangChain4j
LangChain4j brings LangChain’s composable AI approach to the JVM. Its unified API might be a good option if you want to experiment with different models and providers later.
Here’s the same initial prompt using its OllamaChatModel interface:
var chatModel = OllamaChatModel.builder()
.baseUrl("http://localhost:11434")
.timeout(Duration.ofMinutes(5))
.modelName("mistral:7b")
.build()
String prompt = 'What are 4 interesting things to do while I am on vacation in Caloundra?'
println "Response: " + chatModel.chat(prompt)
The output might look something like:
Response: 1. Visit the beautiful beaches: Caloundra is known for its stunning beaches, with Kings Beach and Moffat Beach being some of the most popular ones. You can spend your days sunbathing, swimming, or surfing. 2. Explore the underwater world: Take a trip to the UnderWater World Sea Life Mooloolaba, an aquarium that houses a variety of marine life including sharks, turtles, and seahorses. It's a great way to learn about and appreciate the ocean's wonders. 3. Visit the Glastonbury Estate: This historic homestead offers a glimpse into Australia's past. The estate features beautiful gardens, a tea room, and often hosts various events throughout the year. 4. Take a day trip to the Glass House Mountains: Just a short drive from Caloundra, these iconic volcanic plugs offer breathtaking views and hiking trails for all levels of fitness. You can also visit the Kondalilla National Park for waterfalls and rainforest walks.
Similarly to Ollama4j, we can manually include previous response messages to have a conversation with memory:
var prompt = 'What are 4 interesting things to do while I am on vacation in Caloundra?'
var response = model.chat(new UserMessage(prompt))
var prompt2 = 'If I had half a day and can only go to one, which would you recommend?'
var response2 = model.chat(response.aiMessage(), new UserMessage(prompt2))
println """
Four things:
${response.aiMessage().text()}
Best thing:
${response2.aiMessage().text()}
"""
The output might be something like this:
Best thing: If you only have half a day and can only choose one attraction, I would recommend visiting the UnderWater World SEA LIFE Mooloolaba. It's an excellent aquarium that offers a fascinating glimpse into the marine life of the region, and it's suitable for people of all ages. The UnderWater World is home to a variety of marine animals, including sharks, turtles, stingrays, seahorses, and many more. You can also participate in interactive experiences such as feeding the sharks or holding a starfish. The aquarium also offers educational programs and behind-the-scenes tours for those interested in learning more about marine conservation. While the Coastal Walk and Glass House Mountains are worth visiting if you have more time, they require more planning and travel time, so I would recommend UnderWater World SEA LIFE Mooloolaba as the best option for a half-day visit.
LangChain4j however, also provides more friendly support to have a conversation with memory using its
AiServices builder. We declare the interface of our chat assistant and provide some
additional configuration information, and the builder will create our service for us:
interface HolidayAssistant {
String chat(String message)
}
var model = OllamaChatModel.builder()
.baseUrl("http://localhost:11434")
.timeout(Duration.ofMinutes(5))
.modelName("mistral:7b")
.build()
var chatMemory = MessageWindowChatMemory.withMaxMessages(10)
var assistant = AiServices.builder(HolidayAssistant)
.chatModel(model)
.chatMemory(chatMemory)
.build()
var prompt = 'What are 4 interesting things to do while I am on vacation in Caloundra?'
var response = assistant.chat(prompt)
var prompt2 = '''
It might rain at some point on the weekend, so can you give me
a very short description of a single backup alternative if it rains?
Make it different to your previous suggestions since I am not
sure which ones I will have already seen by the time it rains.
'''
var response2 = assistant.chat(prompt2)
println """
Four things:
$response
If it rains:
$response2
"""
MessageWindowChatMemory is one of several supported memory implementations.
This ones keeps a window of in-memory messages available. Once the max configured
number of messages is reached, they fall out of the cache.
The output might be something like this:
Four things: 1. Visit the beautiful beaches: Caloundra is known for its stunning beaches, with Mooloolaba Beach and Kings Beach being particularly popular. You can spend your days swimming, sunbathing, or even surfing. 2. Explore the Underwater World SEA LIFE Sunshine Coast: This aquarium offers an amazing opportunity to get up close and personal with a variety of marine life, including sharks, stingrays, turtles, and seals. 3. Visit the Bulcock Beach Esplanade: This is a great spot for shopping, dining, and people-watching. The esplanade offers a range of boutiques, cafes, and restaurants. Don't forget to check out the local markets that are held regularly. 4. Take a day trip to Australia Zoo: Made famous by the Crocodile Hunter, Steve Irwin, this zoo is a must-visit for animal lovers. It's home to a wide variety of Australian wildlife and offers interactive experiences and shows throughout the day. If it rains: If it rains, an indoor activity that you might enjoy is visiting the Queensland Air Museum in Caboolture, which is a short drive from Caloundra. The museum houses one of Australia's largest collections of aircraft and aviation artifacts, including military planes, helicopters, and memorabilia. It offers a fascinating look at the history of Australian aviation and is suitable for all ages.
AiServices also supports structured output if we declare that
when defining our model, as this example shows:
interface HolidayBot {
List<Activity> extractActivitiesFrom(String text)
}
var model = OllamaChatModel.builder()
.baseUrl("http://localhost:11434")
.supportedCapabilities(RESPONSE_FORMAT_JSON_SCHEMA)
.timeout(Duration.ofMinutes(5))
.modelName("mistral:7b")
.build()
var chatMemory = MessageWindowChatMemory.withMaxMessages(10)
var bot = AiServices.builder(HolidayBot)
.chatModel(model)
.chatMemory(chatMemory)
.build()
var prompt = '''
What are 4 interesting things to do for a long weekend vacation in Caloundra?
Provide location, and suggested non-overlapping day and time for each activity.
'''
var response = bot.extractActivitiesFrom(prompt)
var prompt2 = '''
If my only spare time is Sunday morning, and I can only go to one activity, which would you recommend?
'''
var response2 = bot.extractActivitiesFrom(prompt2)
println """
Four things:
${response.join('\n')}
Best thing:
${response2.join('\n')}
"""
Instead of returning a String, our chat service is now returning a List<Activity> where Activity
is a domain record defined as follows:
@ToString(includePackage = false)
record Activity(String activity, String location, String day, String time) {
}
The RESPONSE_FORMAT_JSON_SCHEMA configuration will represent our domain record
in JSON using its record component names and values.
The output might look something like:
Four things: Activity(Visit Australia Zoo, Beerwah, Queensland (Approx. 30 minutes drive from Caloundra), Day 1 - Friday, 9:00 AM - 5:00 PM) Activity(Explore Kings Beach and the Coastal Walk, Caloundra, Queensland, Day 2 - Saturday, 8:00 AM - Afternoon) Activity(Relax at Bulcock Beach Market, Bulcock Street, Caloundra, Day 3 - Sunday, 6:00 AM - 1:00 PM) Activity(Explore the Glass House Mountains, Glass House Mountains, Queensland (Approx. 45 minutes drive from Caloundra), Day 4 - Monday, 9:00 AM - 3:00 PM) Best thing: Activity(Relax at Bulcock Beach Market, Bulcock Street, Caloundra, Sunday, 6:00 AM - 1:00 PM)
AiServices also supports tools. Tools allow the LLM to query for information
different to what it was trained on when the model was built.
We’ll tweak our example to have a tool for finding "next weekend" and a tool for finding the weather forecast given a location and date. We’ll just have fake weather forecasts but we could call a REST service that provided real-time forecasting information.
Our script now includes two tool definitions and might look something like this:
interface HolidayAssistantTools {
String chat(String message)
}
@Tool("The LocalDate of the start of the coming weekend")
LocalDate getWeekend() {
LocalDate.now().with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY))
}
@Field static Integer fakeDay = 0
@Tool('The expected domain.Weather including weather forecast, min and max temperature in Celsius for a given location and LocalDate')
Weather getWeather(String location, LocalDate date) {
var fakeWeather = [0: [Caloundra: new Weather('Sunny and Hot', 30, 37)],
1: [Caloundra: new Weather('Raining', 5, 15)]]
fakeWeather[fakeDay++ % fakeWeather.size()][location]
}
var model = OllamaChatModel.builder()
.baseUrl("http://localhost:11434")
.timeout(Duration.ofMinutes(5))
.modelName("qwen3:8b")
// .logRequests(true)
// .logResponses(true)
.build()
var chatMemory = MessageWindowChatMemory.withMaxMessages(10)
var assistant = AiServices.builder(HolidayAssistantTools)
.chatModel(model)
.chatMemory(chatMemory)
.tools(this)
.build()
var prompt = '''
Recommend an interesting thing to see in Caloundra for each day of this coming weekend.
Factor in expected weather when making recommendations. Do not hallucinate weather or dates.
'''
var response = assistant.chat(prompt)
println """
Preparing recommendations as at: ${LocalDate.now()}
Interesting things:
$response
"""
We switched to the qwen3:8b model. It is slightly larger to download but does
a more reliable job calling tools correctly. The tools are annotation with @Tools
and will be automatically found.
The getWeather tool has return type Weather which is another domain object:
@ToString(includePackage = false)
record Weather(String forecast, int minTemp, int maxTemp) {
}
The output might look something like:
Preparing recommendations as at: 2025-11-12 Interesting things: Here’s a weather-aware recommendation for Caloundra this coming weekend (November 15–16, 2025): **Saturday, November 15 (Sunny & Hot: 30°C–37°C)** ☀️ **Beach Day at Caloundra Spit** - Explore the scenic Caloundra Spit, a 12km stretch of sand with wildlife, birdlife, and picnic spots. - Try snorkeling or swimming in the calm waters (avoid midday sun; visit early morning or late afternoon). - Tip: Stay hydrated, wear sunscreen, and bring a hat. **Sunday, November 16 (Raining & Cool: 5°C–15°C)** 🌧️ **Indoor Cultural Activities** - Visit the **Caloundra Art Gallery** or **Caloundra Library** for indoor browsing and local art exhibits. - Enjoy a cozy café visit (e.g., **The Coffee Bean & Tea Leaf**) with a book or light meal. - Tip: Pack an umbrella, layer clothing, and prioritize dry footwear. Safe travels! 🌊📚
Note that it gave accurate recommendations that include our two fake weather forecasts.
Using Spring AI
Spring AI provides first-class integration with the Spring ecosystem. This would be a good option if you are already using Spring Boot or need its integration and deployment capabilities.
In Groovy, it’s simple to embed AI capabilities into a Spring Boot app. Our entire script is shown below:
@SpringBootApplication
void main() {
try(var context = SpringApplication.run(Holiday)) {
var chatClient = context.getBean(ChatClient.Builder).build()
println chatClient
.prompt("What are four interesting things to do while I am on vacation in Caloundra?")
.call()
.content()
}
}
We also need to set up a few properties, e.g. in application.properties,
to tell Spring AI to use Ollama and our chosen model.
The output might look something like:
Caloundra, located on the Sunshine Coast of Australia, offers a variety of activities that cater to different interests. Here are some suggestions for an enjoyable vacation: 1. Beaches: Caloundry has several beautiful beaches, including Kings Beach, Moffat Beach, and Bulcock Beach. You can swim, sunbathe, surf, or just enjoy the stunning views. 2. Visit the Underwater World SEA LIFE Mooloolaba: This aquarium is home to a diverse range of marine life, including sharks, turtles, and seahorses. It's a great place for both children and adults to learn about and interact with marine creatures. 3. Explore the Glass House Mountains: These are a series of 12 granite peaks that offer stunning views of the surrounding area. You can hike, picnic, or simply enjoy the panoramic vistas. 4. Visit the Eumundi Markets: Open on Saturdays and Wednesdays, these markets feature over 600 stalls selling art, crafts, produce, and food. It's a great place to pick up unique souvenirs and sample local delicacies.
Spring AI also supports structured outputs — where responses are deserialized into domain objects.
We saw the Activity record previously. Rather than just having a list of Activity,
let’s also define a record to capture itineraries of activities:
record Itinerary(List<Activity> itinerary) {
String display() {
itinerary.join('\n')
}
}
These simple records let the AI models return structured data that Groovy can manipulate easily. With our domain records defined, our earlier example can be tweaked as follows:
@SpringBootApplication
void main() {
try(var context = SpringApplication.run(Holiday)) {
var chatClient = context.getBean(ChatClient.Builder).build()
var response = chatClient
.prompt("What are some interesting things to do while I am on vacation in Caloundra?")
.call()
.entity(Itinerary)
println "Response:\n" + response.display()
}
}
The output might look something like:
Response: Activity(Visit Kings Beach, Caloundra, Day 1, Morning) Activity(Explore Bulcock Beach, Caloundra, Day 1, Afternoon) Activity(Sunset at Moffat Headland, Caloundra, Day 1, Evening) Activity(Visit the Australian Zoo, Beerwah, Day 2, Whole Day) Activity(Relax at Shelly Beach, Caloundra, Day 3, Morning) Activity(Explore Pumicestone Passage by boat tour, Caloundra, Day 3, Afternoon)
Spring AI also supports tools. Our script might look like this:
@Component
class WeekendTool {
@Tool(description = 'The LocalDate of the start of the coming weekend')
LocalDate getWeekend() {
LocalDate.now().with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY))
}
}
@Component
class WeatherTool {
static Integer fakeDay = 0
@Tool(description = 'The expected weather including forecast, min and max temperature in Celsius for a given location and LocalDate')
Weather getWeather(String location, LocalDate date) {
var fakeWeather = [0: [Caloundra: new Weather('Sunny and Hot', 30, 37)],
1: [Caloundra: new Weather('Raining', 5, 15)]]
fakeWeather[fakeDay++ % fakeWeather.size()][location]
}
}
@SpringBootApplication
void main() {
var prompt = '''
Recommend an interesting thing to see in Caloundra for each day of this coming weekend.
Factor in expected weather when making recommendations. Do not hallucinate weather or dates.
'''
try(var context = SpringApplication.run(Holiday)) {
var chatClient = context.getBean(ChatClient.Builder).build()
var weekend = context.getBean(WeekendTool)
var weather = context.getBean(WeatherTool)
var options = OllamaChatOptions.builder().model('qwen3:8b').build()
println chatClient
.prompt(prompt)
.options(options)
.tools(weekend, weather)
.call()
.content()
}
}
And the output might look like this:
**Saturday, November 15 (Sunny & Hot: 30°C–37°C)** Perfect for outdoor adventures! - **Caloundra Spit** – Explore the scenic coastal walk with panoramic ocean views. - **Snorkeling at Mooloolaba Beach** – Clear waters and vibrant marine life. - **Glass House Mountains Day Trip** – A short drive offers dramatic landscapes and hiking. **Sunday, November 16 (Raining: 5°C–15°C)** Opt for indoor attractions or sheltered activities: - **Caloundra Cultural Centre** – Discover local art and history. - **Caloundra Regional Gallery** – Enjoy contemporary exhibitions. - **Indoor Water Playground** – A splash-filled escape from the rain. Always check for real-time weather updates closer to the date! 🌞🌧️
Using Embabel
Embabel is a newer JVM library that provides agent orchestration and LLM integration through a declarative approach.
A simple text-generation example:
@SpringBootApplication
@EnableAgents(loggingTheme = LoggingThemes.STAR_WARS)
void main() {
try(var context = SpringApplication.run(Holiday)) {
println context.getBean(OperationContext)
.ai()
.withDefaultLlm()
.generateText('What are four interesting things to do while I am on vacation in Caloundra?')
}
}
The output might look something like:
1. Visit the Bulcock Beach: This is a popular beach in Caloundra, perfect for swimming, sunbathing, and enjoying various water sports. There's also a picturesque esplanade with cafes, shops, and art galleries nearby. 2. Explore the Kings Beach Park: Located next to Kings Beach, this park offers a variety of facilities including picnic areas, BBQ facilities, playgrounds, and beautiful views of the ocean. It's a great spot for families with children. 3. Visit the Australian Zoo: Made famous by Steve Irwin, the Australian Zoo is just a short drive from Caloundra. Here you can see a wide variety of Australian wildlife, including kangaroos, koalas, and crocodiles. 4. Take a day trip to the Glass House Mountains: These are a series of 13 steep sided, volcanic plugs that dominate the local landscape. You can hike some of the mountains, or simply enjoy their unique beauty from various lookout points. Some popular ones include Mount Ngungun and Mount Beerwah.
Similarly to Spring AI, Embabel also supports structured data generation:
@SpringBootApplication
@EnableAgents(loggingTheme = LoggingThemes.STAR_WARS)
void main() {
try(var context = SpringApplication.run(Structured)) {
println context.getBean(OperationContext)
.ai()
.withDefaultLlm()
.createObject('What are some interesting things to do while I am on vacation in Caloundra?', Itinerary)
.display()
}
}
The output might look something like:
Activity(Visit the Kings Beach, Kings Beach, Caloundra, Day 1, Morning) Activity(Explore Bulcock Beach, Bulcock Beach, Caloundra, Day 1, Afternoon) Activity(Dine at Mooloolaba Seafood Market, Mooloolaba Seafood Market, Mooloolaba, Day 1, Evening) Activity(Visit the Aussie World Theme Park, Aussie World, Palmview, Day 2, Whole day) Activity(Relax at Shelly Beach, Shelly Beach, Caloundra, Day 3, Morning) Activity(Try Surfing at Currimundi Beach, Currimundi Beach, Currimundi, Day 3, Afternoon) Activity(Explore the Eumundi Markets, Eumundi Markets, Eumundi, Day 4, Morning to Afternoon)
Autonomous Agents with Embabel
As a final example, let’s look at how Embabel can orchestrate multiple AI calls using its agent model.
Let’s first extend our domain model to be able to support a set of alternative itineraries and allow them to be rated.
record Alternatives(Set<Itinerary> content) {
}
record RatedAlternatives(List<RatedItinerary> content) {
}
record RatedItinerary(Itinerary itinerary, Rating rating) {
}
record Rating(double percentage) { }
Our example uses an @Agent class to coordinate multiple AI actions — generating, rating, and selecting itineraries. The @Action methods that make up the agent’s capabilities are simple to write, with our domain records as inputs and outputs.
@Agent(description = "Creates and ranks itineraries for a holiday at a given location")
class ItineraryAgent {
@Action
Alternatives generateItineraries(UserInput userInput, OperationContext context) {
context.ai()
.withLlm('mistral:7b')
.createObject("Generate 5 sets of: An itinerary of things to do while on $userInput.content?", Alternatives)
}
@Action
RatedAlternatives rateItineraries(Alternatives alternatives, OperationContext context) {
new RatedAlternatives(alternatives.content.collect { itinerary ->
var rating = context.ai()
.withLlm('qwen3:8b')
.createObject("Rate this itinerary on variety and number of activities: $itinerary?", Rating)
new RatedItinerary(itinerary, rating)
})
}
@Action
@AchievesGoal(description = 'Best itinerary')
RatedItinerary bestItinerary(RatedAlternatives ratedAlternatives) {
ratedAlternatives.content.max { it.rating.percentage }
}
}
@SpringBootApplication
@EnableAgents(loggingTheme = LoggingThemes.STAR_WARS)
void main() {
try(var context = SpringApplication.run(Rated)) {
println context.getBean(Autonomy)
.chooseAndRunAgent('Itinerary for a relaxing long-weekend holiday in Caloundra', ProcessOptions.DEFAULT).output
}
}
Here we are using two different models for different tasks — mistral:7b for generating itineraries and qwen3:8b for rating them. Embabel provides richer ways to control and configure the models, but this simple approach works well for our example.
The @AchievesGoal annotation makes use of Embabel’s goal-oriented action planning (GOAP) capabilities.
The output might look something like (slightly formatted here for readability):
RatedItinerary[
itinerary=Itinerary[itinerary=[
Activity(Visit Kings Beach, Caloundra, Friday, All day),
Activity(Sunset at Mooloolaba Beach, Mooloolaba, Friday, Evening),
Activity(Explore Bulcock Beach Markets, Caloundra, Saturday, Morning to Early Afternoon),
Activity(Snorkeling at Dicky Beach, Dicky Beach, Saturday, Afternoon),
Activity(Relax at Shelly Beach, Caloundra, Sunday, All day)]],
rating=Rating[percentage=80.0]]
This demonstrates how Embabel’s agent model and Groovy’s expressive syntax can work together to orchestrate multiple AI calls with minimal boilerplate.
Using Micronaut LangChain4j
Micronaut LangChain4j provides integration between Micronaut and LangChain4j. This module is regarded as somewhat experimental and subject to change, but is already quite feature rich. We’ll just look at some basic capabilities.
First, let’s do a basic chat example. This time asking for recommendations for Auckland. Our code might look like this:
@Singleton
class AppRunner {
@Inject
HolidayAssistant assistant
void run() {
println assistant.activities('What are four good things to see while I am in Auckland?')
}
}
@AiService
interface HolidayAssistant {
@SystemMessage('''
You are knowledgeable about places tourists might like to visit.
Answer using New Zealand slang but keep it family friendly.
''')
String activities(String userMessage)
}
try(var context = ApplicationContext.run()) {
context.getBean(AppRunner).run()
}
Micronaut’s sweet spot is creating microservices.
Here we are just creating a command-line application, so we aren’t use many Micronaut
features, but we will use its dependency injection capabilities.
While not strictly needed, a common convention is to have an AppRunner class with a run method
to run our application.
We saw previously that LangChain4j had an AiServices builder.
Micronaut provides instead the more declarative approach of providing an @AiServices
annotation. The code for our assistant will be generated at compile time.
Note that we can provide a system message as part of that annotation.
Watch out for the NZ slang in some of the responses!
The output might look something like:
Blimey mate, while you're roaming around Auckland, here are four top-notch spots ya gotta check out: 1. The Sky Tower - It's like the tallest bloke in town, with a view that'll make ya heart race. 2. Waitomo Glowworm Caves - It's a magical spot where tiny luminescent critters put on a light show. 3. Waiheke Island - A chilled-out paradise with beaches and vineyards, perfect for a day trip or longer stay. 4. Auckland Zoo - Get up close and personal with some of New Zealand's native critters, as well as exotic animals from around the world. Kiwi, eh?
Structured output is also supported.
@AiService
interface HolidayBot {
@SystemMessage('''
Return holiday activity suggestions as structured JSON matching Itinerary.
Timebox activities if needed to fit within the holiday length and not overlap other
activities while still giving enough time to see all major aspects of each attraction.
Exclude other information.
''')
Itinerary itinerary(String userMessage)
}
try(var context = ApplicationContext.run()) {
println context.getBean(HolidayBot)
.itinerary('Four great things to see in Auckland over a weekend.')
.display()
}
Here we didn’t use the AppRunner convention.
It’s just a single bean that we want to invoke after all.
The output might look like:
Activity(Visit Auckland War Memorial Museum, Auckland, Day 1, 9:00 AM - 5:00 PM) Activity(Explore Viaduct Harbour and Wynyard Quarter, Auckland, Day 1, 6:00 PM - 8:00 PM) Activity(Hike up Mount Eden, Auckland, Day 2, 9:00 AM - 12:00 PM) Activity(Visit Waiheke Island and its vineyards, Waiheke Island, Day 2, 1:00 PM - 6:00 PM)
Using Quarkus LangChain4j
The Quarkus LangChain4j extension integrates Large Language Models (LLMs) into your Quarkus applications. Let’s just build a simple chat example. Quarkus also follows the now familiar declarative approach. We can define an AI service like this:
@RegisterAiService
@ApplicationScoped
interface HolidayAssistant {
@SystemMessage('You are knowledgeable about places tourists might like to visit.')
String ask(@UserMessage String question)
}
Then our main script would be this:
@QuarkusMain
class Holiday implements QuarkusApplication {
@Inject
HolidayAssistant assistant
@Override
int run(String... args) {
def question = 'What are four things to do while visiting Minneapolis?'
println "Asking: $question"
def answer = assistant.ask(question)
println "Answer: $answer"
return 0
}
}
Running the example, and note we are asking about Minneapolis this time, might give output like:
Asking: What are four things to do while visiting Minneapolis? Answer: 1. Explore the Minneapolis Sculpture Garden: This 11-acre outdoor museum located in downtown Minneapolis is home to over 40 works of art, including the iconic "Spoonbridge and Cherry" sculpture. The garden also features walking trails, picnic areas, and a conservatory. 2. Visit the Mill City Museum: Located in the former Washburn A Mill, this museum tells the story of Minneapolis' milling heritage. You can explore exhibits on the city's flour-milling past, take a tour of the restored flour mill elevators, and enjoy panoramic views of the Mississippi River from the rooftop observation deck. 3. Stroll through the Minnehaha Park: This beautiful urban park features hiking trails, a waterfall, and breathtaking views of the Mississippi River. You can also visit Minnehaha Falls, a 53-foot waterfall that is one of the most popular attractions in Minneapolis. 4. Attend a concert or sporting event: Minneapolis is home to several major sports teams, including the Minnesota Vikings (NFL), Minnesota Timberwolves (NBA), and Minnesota Twins (MLB). The city also has a thriving music scene, with venues like First Avenue and the Orpheum Theatre hosting concerts by popular artists. Additionally, the Walker Art Center offers free outdoor performances during the summer months at its Sculpture Garden.
Conclusion
Groovy’s interoperability, concise syntax, and powerful DSL capabilities make it an excellent language for prototyping and composing AI workflows on the JVM. Whether you’re chatting with Ollama, integrating via Spring, Micronaut, or Quarkus, or orchestrating agents with Embabel, Groovy keeps your code clear and compact. Feel free to experiment with different models and prompts to see what interesting results you can achieve!
You can find the full source for all these examples at:
https://github.com/paulk-asert/groovy-ai
Other examples of using Groovy with Spring AI can be found at:
https://github.com/danvega/groovyai
