This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch asf-site
in repository https://gitbox.apache.org/repos/asf/groovy-website.git
The following commit(s) were added to refs/heads/asf-site by this push:
new 27c6c2d add Micronaut and Quarkus examples
27c6c2d is described below
commit 27c6c2d2937dd5af33321b05a65fedb65ad3dc3f
Author: Paul King <[email protected]>
AuthorDate: Fri Nov 14 20:15:57 2025 +1000
add Micronaut and Quarkus examples
---
site/src/site/blog/groovy-ai.adoc | 539 ++++++++++++++++++++++++++++++++++----
1 file changed, 491 insertions(+), 48 deletions(-)
diff --git a/site/src/site/blog/groovy-ai.adoc
b/site/src/site/blog/groovy-ai.adoc
index 3ba00b2..a5471fe 100644
--- a/site/src/site/blog/groovy-ai.adoc
+++ b/site/src/site/blog/groovy-ai.adoc
@@ -1,12 +1,13 @@
= Exploring AI with Groovy™
Paul King <paulk-asert|PMC_Member>
:revdate: 2025-10-15T07:06:20+00:00
-:keywords: groovy, ai, ollama4j, langchain4j, spring-ai, embabel
-:description: A tour of using Groovy with modern AI libraries including
ollama4j, LangChain4j, Spring AI, and Embabel.
+:updated: 2025-11-14T20:05:00+00:00
+:keywords: groovy, ai, ollama4j, langchain4j, spring-ai, embabel,
micronaut-ai, quarkus-ai
+:description: A tour of using Groovy with modern AI libraries including
Ollama4j, LangChain4j, Spring AI, Embabel, Micronaut, and Quarkus.
== Introduction
-> [blue]#In this post, we’ll look at several ways to integrate Groovy with AI
tools, including `ollama4j`, `langchain4j`, `Spring AI`, and `Embabel`.#
+> [blue]#In this post, we’ll look at several ways to integrate Groovy with AI
tools, including `Ollama4j`, `LangChain4j`, `Spring AI`, `Embabel`, `Micronaut
LangChain4j`, and `Quarkus LangChain4j`.#
image:img/bulcock_beach_sunset.jpg[Bulcock Beach at Sunset looking towards
Pumicestone Passage, 400, float="right"]
@@ -19,6 +20,7 @@ The libraries we use here can also connect to remote models
and services.
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 use the `qwen3:8b` model. It seems to give better results
when using tools.
We also used Groovy 5 and JDK 25, but the examples should work on other Groovy
and Java versions.
== Using Ollama4j
@@ -72,7 +74,8 @@ Four things:
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!
----
-We can continue the conversation by including the previous chat history in the
next request:
+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:
[source,groovy]
----
@@ -124,6 +127,256 @@ Response:
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:
+
+[source,groovy]
+----
+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:
+
+[source,groovy]
+----
+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:
+
+[source,groovy]
+----
+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:
+
+[source,groovy]
+----
+@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:
+
+[source,groovy]
+----
+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 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
https://docs.spring.io/spring-ai/reference/[_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
@@ -138,10 +391,10 @@ Our entire script is shown below:
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?")
+ println chatClient
+ .prompt("What are four interesting things to do while I am on
vacation in Caloundra?")
.call()
- println "Response:\n" + response.content()
+ .content()
}
}
----
@@ -152,7 +405,6 @@ to tell Spring AI to use Ollama and our chosen model.
The output might look something like:
----
-Response:
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.
@@ -162,31 +414,19 @@ Response:
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.
-
-5. Go for a scenic flight: For a truly unforgettable experience, consider
taking a scenic flight over the Sunshine Coast. You'll get breathtaking views
of the coastline, hinterland, and the Glass House Mountains.
-
-6. Visit Australia Zoo: Made famous by the Crocodile Hunter, Steve Irwin, this
zoo is home to a wide variety of Australian wildlife. It's a great place for
families and animal lovers.
-
-7. Enjoy local cuisine: Caloundra has a vibrant food scene with numerous
restaurants offering everything from fresh seafood to international cuisines.
Be sure to try some local favorites like Barramundi, Moreton Bay bugs, and
mangoes.
-
-8. Visit the Powerboat Park: If you're a fan of powerboats, this park is a
must-visit. It features a museum dedicated to the history of powerboating in
Australia.
-
-9. Relax at a day spa: After a day of exploring, treat yourself to a relaxing
massage or beauty treatment at one of Caloundra's many day spas.
-
-10. Go fishing: Whether you prefer deep-sea fishing or casting a line from the
shore, Caloundra offers numerous opportunities for anglers. You can even hire a
charter boat if you don't have your own equipment.
----
Spring AI also supports structured outputs — where responses are deserialized
into domain objects.
-Let's create a small domain model for describing activities and lists of
activities (itineraries).
+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:
[source,groovy]
----
-@ToString(includePackage = false)
-record Activity(String activity, String location, String day, String time) {
-}
-
record Itinerary(List<Activity> itinerary) {
+ String display() {
+ itinerary.join('\n')
+ }
}
----
@@ -203,7 +443,7 @@ void main() {
.prompt("What are some interesting things to do while I am on
vacation in Caloundra?")
.call()
.entity(Itinerary)
- println "Response:\n" + response.itinerary.join('\n')
+ println "Response:\n" + response.display()
}
}
----
@@ -220,6 +460,70 @@ 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:
+
+[source,groovy]
+----
+
+@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
https://github.com/embabel[_Embabel_] is a newer JVM library that provides
agent orchestration and LLM integration through a declarative approach.
@@ -235,7 +539,7 @@ void main() {
println context.getBean(OperationContext)
.ai()
.withDefaultLlm()
- .generateText('What are some interesting things to do while I am
on vacation in Caloundra?')
+ .generateText('What are four interesting things to do while I am
on vacation in Caloundra?')
}
}
----
@@ -243,27 +547,13 @@ void main() {
The output might look something like:
----
-Caloundra, located on the Sunshine Coast in Queensland, Australia, offers a
variety of activities for tourists. Here are some suggestions for an enjoyable
vacation:
-
-1. Visit the beautiful beaches: Caloundra has several beautiful beaches, such
as Kings Beach, Shelly Beach, and Moffat Beach, where you can swim, sunbathe,
or surf.
-
-2. Explore the Coastal Walk: Take a leisurely stroll along the Coastal Walk,
which offers stunning views of the ocean, coastal cliffs, and nearby islands.
-
-3. Visit the Underwater World SEA LIFE Mooloolaba Aquarium: Discover an
amazing underwater world filled with sea turtles, sharks, seahorses, and more.
-
-4. Spend a day at Australia Zoo: Home to over 1,200 animals and the late Steve
Irwin's family, this iconic zoo offers up-close encounters with some of
Australia's most famous wildlife.
+ 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.
-5. Visit the Glastonbury Estate Winery: Taste locally produced wines at this
picturesque winery, which also features a restaurant and beautiful gardens.
+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.
-6. Explore the Bulcock Beach Esplanade: This vibrant area offers a variety of
shops, cafes, and restaurants, as well as regular markets on weekends.
+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.
-7. Take a day trip to Fraser Island: Known for its stunning beaches,
crystal-clear waters, and rainforests, Fraser Island is just a short boat ride
away from Caloundra.
-
-8. Enjoy the Pumicestone Passage: Go boating, kayaking, or fishing in this
beautiful waterway that separates Bribie Island from the mainland.
-
-9. Visit the Powerboat Park: Watch high-speed powerboats compete in various
races at this popular watersports venue.
-
-10. Relax at a spa or wellness center: Pamper yourself with a massage, beauty
treatment, or yoga class at one of Caloundra's many wellness centers.
+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:
@@ -352,7 +642,7 @@ class ItineraryAgent {
void main() {
try(var context = SpringApplication.run(Rated)) {
println context.getBean(Autonomy)
- .chooseAndRunAgent('A long-weekend holiday in Caloundra',
ProcessOptions.DEFAULT).output
+ .chooseAndRunAgent('Itinerary for a relaxing long-weekend holiday
in Caloundra', ProcessOptions.DEFAULT).output
}
}
----
@@ -375,13 +665,166 @@ RatedItinerary[
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
+
+https://micronaut-projects.github.io/micronaut-langchain4j/latest/guide/[_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:
+
+[source,groovy]
+----
+@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.
+
+[source,groovy]
+----
+@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
+https://docs.quarkiverse.io/quarkus-langchain4j/dev/index.html[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:
+
+[source,groovy]
+----
+@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:
+
+[source,groovy]
+----
+@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 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, or orchestrating
agents with Embabel, Groovy keeps your code clear and compact.
+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
\ No newline at end of file
+https://github.com/danvega/groovyai
+
+.Update history
+****
+*15/Oct/2023*: Initial version +
+*14/Nov/2025*: Updated with Micronaut, Quarkus, and tools examples.
+****