Tales from the jar side: Validating records, Generating an opera libretto using LangChain4j chat memory, and the usual Xcrements and toots
Did you know there are no canaries on the Canary Islands? Same as with the Virgin Islands ... no canaries there, either. (rimshot, h/t to @MostlyHarmless on Mastodon)
(Yeah, somebody suggested that tweets should now be called Xcrements, and I’m going with it.)
Welcome, fellow jarheads, to Tales from the jar side, the Kousen IT newsletter, for the week of August 18 - 25, 2024. This week I taught an Upgrade to Modern Java course. I also published a new video on validating Java records on the Tales from the jar side YouTube channel.
Regular readers of, listeners to, and video viewers of this newsletter are affectionately known as jarheads, and are far more intelligent, sophisticated, and attractive than the average newsletter reader or listener or viewer. If you wish to become a jarhead, please subscribe using this form:
As a reminder, when this message is truncated in email, click on the title to open it in a browser tab formatted properly for both the web and mobile.
Java Records Unleashed
I published my video on validating Java records:
The title is Java Records Unleashed: Validation Best Practices, which admittedly is a click-baity way of trying to make the idea of validating input variables sound exciting.
The video is doing reasonably well. It certainly got a lot of comments, which was great. The channel as a whole is also doing well. It recently passed 55,000 total views, and over the weekend it reached 3000 views in the past year. That milestone qualifies the channel for limited membership into the YouTube Partner Program, though that’s not official until they review and approve everything.
I don’t expect to make a significant income from the channel. I just wanted to reach that milestone as a goal. The next goal is to get to 4000 views in the last year, which gets you full membership, but something will probably have to go viral in order for that to happen. We’ll see.
Whispers of the Lost Hartford
That’s the AI-generated name of the opera libretto I got GPT-4o and Claude to generate for me. Last week in the newsletter I talked about how to hold conversations with AI tools using LangChain4j. Doing that with a single tool is easy:
With LangChain4j’s AI Services, like the one I’m using here, all you have to do is to call the chatMemory
method in the builder and you’re good.
If I want to share a set of messages between two or more AI tools, however, I need to do some extra work. There you have to instantiate a ChatMemory
object, add the user messages and AI response messages to it manually, and send the messages with each request. I therefore created two models:
My original plan was to have three models (I was going to add Gemini) each play a role in a debate and send the arguments back and forth. Eventually I settled on just these two, and had them write the libretto (book) for an opera.
Here is the system message (the global instructions) I sent them:
They say that all operas are about a soprano who wants to sleep with the tenor, but the baritone won't let her. See, for example, La Traviata, Rigoletto, or Carmen.
You are composing the libretto for such an opera. The setting is the wild jungles of Connecticut, in the not-so-distant future after global warming has reclaimed the land. The soprano is an intrepid explorer searching for the lost city of Hartford. The tenor is a native poet who has been living in the jungle for years, writing sonnets to the trees and composing symphonies for the monkeys.
The baritone is a government agent who has been sent to stop the soprano from finding the lost city. He has a secret weapon: a giant robot that can sing Verdi arias in three different languages.
The soprano and the tenor meet in the jungle and fall in love. They decide to join forces and find the lost city together. But the baritone is always one step behind them, and his giant robot is getting closer and closer.
Part of that is mine, and part was generated by GitHub Copilot. You can probably guess which parts. I mean, sure, I like the idea of the jungles of Hartford, but it did not occur to me to add a giant singing robot to the mix.
And we’re off. Back and forth the two models went, generating scenes 2 (?) through 6, going in directions that were completely unexpected. I’m not sure why it started with scene 2, though I suspect it’s because I gave so many instructions in the system message which is sent with each request.
I saved the results to a Markdown file (the chat tools love to generate Markdown). Then I wrote code (with some AI assistance) to read in each scene and send it to the DALL-E 3 image generator.
Here’s the first scene:
### Scene 2: The Heart of the Jungle
*The curtain rises to reveal a lush, overgrown jungle setting. The sound of exotic birds and distant waterfalls fills the air. In the middle of the stage, a clearing is visible with ancient, vine-covered ruins hinting at the lost city of Hartford. The soprano, **Elena**, dressed in rugged explorer's attire, is carefully examining an old map. The tenor, **Rafael**, barefoot and adorned with natural elements, is composing music on a makeshift wooden instrument. They are completely absorbed in their tasks.*
**Elena**: *(Singing, with a sense of determination and curiosity)*
In this verdant maze, we seek the past,
Where whispers of a city long have cast
Their shadows on our hearts and dreams,
In Hartford's ruins, find what seems.
**Rafael**: *(Responding, with a poetic and melodic air)*
Oh Elena, your courage lights the way,
Through tangled vines and night and day.
In sonnets, I have penned this quest,
To find the city where our hearts may rest.
*They join hands and sing together, their voices blending in harmony.*
**Elena & Rafael**: *(Duet)*
Together, we shall brave the wild,
With love and hope, our hearts are mild.
In Hartford's ancient, hidden halls,
We'll write our story on its walls.
*Suddenly, the ground trembles slightly, and a mechanical hum is heard in the distance. Elena and Rafael exchange worried glances.*
**Elena**: *(Whispering)*
It's him... the agent sent to thwart our dreams.
We must move quickly, for his machine is near.
**Rafael**: *(Nodding)*
We'll outsmart him with the forest's lore,
And find the path that leads to Hartford's door.
*They begin to gather their belongings, but before they can leave, the baritone, **Agent Victor**, enters the scene. He is dressed in a sleek, futuristic uniform and carries a remote control device. Behind him, the giant robot looms, its eyes glowing menacingly.*
**Victor**: *(Singing, with a powerful and commanding voice)*
Elena, cease this foolish quest,
For Hartford's secrets must not be professed.
Return with me, abandon your flight,
Or face the wrath of metallic might.
*The robot takes a step forward, its voice booming as it sings an aria from Verdi's "Rigoletto" in flawless Italian.*
**Robot**: *(In Italian)*
Questa è la fine del tuo viaggio,
Rinuncia ora, o affronta il mio coraggio.
**Elena**: *(Defiantly)*
We will not surrender, Victor, no!
Love and dreams are stronger than your show.
**Rafael**: *(Echoing her resolve)*
Let the jungle be our shield and guide,
In Hartford's ruins, we shall confide.
*Elena and Rafael make a dash into the dense foliage, disappearing from sight. Victor clenches his fist in frustration and commands the robot to follow.*
**Victor**: *(Singing)*
Pursue them, my mechanical friend,
Their journey to Hartford we shall end.
*The robot's mechanical hum grows louder as it moves into the jungle, its glowing eyes scanning for any sign of the fleeing lovers. The stage darkens as the scene shifts to the next part of their adventure.*
*Curtain falls.*
If you want to see the whole thing, I committed the entire project to GitHub. Specifically, here is the libretto itself. If you look in the src/main/resources
folder (which is also where you’ll find that libretto), you’ll see the images generated for each scene, which are somewhat uncomfortable given their biases. I didn’t give the image generator any information other than the scenes themselves, so it made all the awkward “native” character decisions. I’m glad that at least one image (I think it’s scene 4) contains a robot in it.
Needless to say, and resemblance between the characters in the libretto and any real people or places is probably coincidental, though I guess with AI tools you never really know. At any rate, nothing in this story bears any similarity to Hartford, CT in any way, shape, or form. I mean, what secrets would Hartford ever have? That they let the Whalers go for nothing? That’s hardly a secret. Everybody knows that.
I should also say that if you feel the urge to actually set this to music, I strongly recommend you lie down in a dark room until that feeling goes away.
Tweets and Toots
Out of breath
Hey, it’s a big gym!
What could go wrong?
Could be true, right?
Grok again
The Grok image generator is almost, but not quite, enough to make me consider signing up for a paid Twitter account.
Nah, nothing could get me to give my credit card info to Elon.
What about Ivan the Terrible, or Winnie the Pooh
Seriously, what are the odds?
Others: Jabba the Hutt, Conan the Barbarian, Vlad the Impaler.
Even more: Smokey the Bear. Billy the Kid. Attila the Hun. Catherine the Great.
That’s got to be enough.
Frosty the Snowman. Mack the Knife. Gandalf the Grey/White.
(I’ve got to stop now.)
(Richard the Lionheart)
Doppler shifts
Ooh, a physics joke. :)
A memory gag
That hits a bit too close to home.
It’s everywhere
And on that farm he had some image generators…
Good advice
That seems … logical.
Have a great week, everybody!
Last week:
Java Upgrade course as an NFJS Virtual Workshop
This week:
My first NFJS Virtual Workshop on LangChain4j :)