Forgive the somewhat cutesy title. I hate vague titles that only serve to be clickbait, but given the subject, I couldn't help it. This post is about AI, specifically Generative AI, with BL, AKA BoxLang. This has always been possible with BoxLang and any GenAI service that had a REST API, but recently the team released an impressive AI Module that makes this a lot easier. So what does it do?
Much like LangChain, the BoxLang AI module provides a unified interface to work with multiple different AI providers. This makes it a bit easier to switch from one service to another. I don't necessarily see people doing that willy nilly. Each service has it's unique strengths and weaknesses and pricing schemes. But the module at least makes the technical aspect of switching easier. And heck, even if you don't plan on switching, it still greatly simplifies making use of those APIs in your code. Let's consider an example.
The quickest and simplest way to use the module is via the aiChat
BIF:
answer = aiChat(
messages="why are cats better than dogs?",
options={
provider:"gemini",
apiKey:server.system.environment.GEMINI_API_KEY
}
);
This returns the response as a simple string. Want to use OpenAI?
answer = aiChat(
messages="why are cats better than dogs?",
options={
provider:"openai",
apiKey:server.system.environment.OPENAI_API_KEY
}
);
Literally the only change was to the provider
and apiKey
values. The docs cover this and you should check them for the latest updates, but currently you get support for Claude, DeepSeek, Gemini, Grok (eww), OpenAI, and Perplexity out of the box.
While aiChat
supports a simple string for messages
, you can also pass an array which is a handy way to handle system messages:
answer = aiChat(
messages=[
{ "role":"system", "content":"You are a teacher for young children." },
{ "role":"user", "content":"Where do stars come from?"}
],
options={
provider:"gemini",
apiKey:server.system.environment.GEMINI_API_KEY
}
);
println(answer);
println('-'.repeat(80));
answer = aiChat(
messages=[
{ "role":"system", "content":"You are a teacher for adults with some scientific knowledge." },
{ "role":"user", "content":"Where do stars come from?"}
],
options={
provider:"gemini",
apiKey:server.system.environment.GEMINI_API_KEY
}
);
println(answer);
In this example I've used the same prompt with two different system messages. This has a wild impact on the output as you can imagine (the line in the output below delineates between the two versions):
That's a wonderful question! Stars are like giant, sparkly babies that come from something called a nebula.
Imagine a big, fluffy cloud in space, much bigger than the Earth! This cloud is made of gas and dust, like tiny little specks of stardust. This cloud is called a nebula.
Over a very, very long time, gravity (which is like a big hug that pulls things together!) starts to pull the gas and dust in the nebula closer and closer.
As the gas and dust squish together, it gets hotter and hotter in the middle. Imagine rubbing your hands together really fast - they get warm, right? It's kind of like that!
Eventually, the middle gets so hot that it starts to glow, like a tiny spark. This spark gets bigger and bigger, until it becomes a bright, shining star!
So, to recap, stars come from:
- A big, fluffy cloud of gas and dust called a nebula.
- Gravity pulls the gas and dust together.
- The middle gets hotter and hotter until it starts to glow.
- The glowing ball of hot gas becomes a star!
Isn't that amazing? Space is a magical place! Do you have any other questions about stars or space?
Alright, let's talk about where stars come from. It's a pretty grand story, spanning billions of years and involving vast cosmic processes.
Essentially, stars are born from giant clouds of gas and dust called nebulae. Think of these nebulae as the raw ingredients of the universe – mostly hydrogen and helium, with traces of other elements.
Here's a more detailed breakdown:
-
The Nebula Exists: These nebulae are vast, diffuse regions in space. They're cold and not very dense, just hanging around.
-
Something Triggers Collapse: This is the crucial step. Something needs to disrupt the equilibrium of the nebula and start the process of gravitational collapse. Possible triggers include:
- Shockwaves from a nearby supernova: A massive star exploding can send out shockwaves that compress regions of the nebula.
- Collisions between nebulae: Two nebulae bumping into each other can create dense regions.
- Density fluctuations: There could be some inherent random density fluctuations in the nebula. If a region is slightly denser than its surroundings, its gravity becomes stronger, attracting more material.
-
Gravitational Collapse: Once a region within the nebula becomes dense enough, gravity takes over. The gas and dust start to collapse inward, pulled by their own mutual gravitational attraction. As the cloud collapses, it fragments into smaller and smaller clumps.
-
Formation of a Protostar: As a fragment collapses, it heats up. This hot, dense core is called a protostar. The protostar is not yet a true star because it's not undergoing nuclear fusion. It's just a ball of hot gas contracting under gravity.
-
Accretion Disk and Jets: The protostar is often surrounded by a rotating disk of gas and dust called an accretion disk. Material from the disk spirals inward onto the protostar, feeding its growth. Powerful jets of gas are often ejected from the poles of the protostar, perpendicular to the accretion disk. These jets help to carry away excess angular momentum, allowing the protostar to continue collapsing.
-
Ignition of Nuclear Fusion: As the protostar continues to collapse, the core gets hotter and denser. Eventually, the temperature and pressure in the core reach a critical point where nuclear fusion can begin. This is where hydrogen atoms fuse together to form helium, releasing enormous amounts of energy.
-
Birth of a Star: The onset of nuclear fusion marks the birth of a true star. The energy released by fusion creates outward pressure that balances the inward pull of gravity, stabilizing the star. At this point the star joins the main sequence. The length of the star's life on the main sequence is determined by its mass, the more massive, the shorter the life.
In Summary:
Nebula -> Trigger -> Gravitational Collapse -> Protostar -> Nuclear Fusion -> Star
Important Considerations:
- Mass is key: The mass of the initial fragment determines the star's properties – its temperature, luminosity, lifespan, and eventual fate. Massive stars have short, dramatic lives and end in supernovae. Smaller stars like our sun have much longer lifespans and eventually become white dwarfs.
- Star Clusters: Stars often form in groups called star clusters. This is because a large nebula can fragment into many smaller pieces, each of which can form a star.
- Ongoing Process: Star formation is an ongoing process in galaxies, especially in spiral galaxies where there's plenty of gas and dust.
So, there you have it! Stars are born from the gravitational collapse of giant clouds of gas and dust, powered by nuclear fusion in their cores. Any questions about that process, or related topics such as the different types of stars, the end stages of stars, or the lifecycle of elements in the universe?
So far I've only shown aiChat
, but there's actually multiple different BIFs added when you install the module. As an example, aiService
lets you create a re-usable wrapper to a particular service:
aiService = aiService("gemini").configure(server.system.environment.GEMINI_API_KEY);
answer = aiService.invoke(aiChatRequest("honestly, why ARE cats so awesome???"));
Or in a more complex example:
aiService = aiService("gemini").configure(server.system.environment.GEMINI_API_KEY);
answer2 = aiservice.invoke(aiChatRequest(
[
{ "role":"system", "content":"You are a teacher for adults with some scientific knowledge." },
{ "role":"user", "content":"Where do stars come from?"}
]
));
I'd imagine in a web app type scenario with repeated calls, it would make much more sense to use a service object. You can also use aiChatReqest
for more complex integrations.
Unsurprisingly, there's a lot to this. I definitely suggest perusing the documentation for more information, and if you would like, you can hear my lovely voice in the video version below. Enjoy!