Saved transcript

Gen AI with Spring AI

Channel: Telusko

Welcome back everyone. My name is Ain

Reddi and in this video we'll talk about

spring AI. Now see we are living in the

AI era. So whatever application you are

building basically you have to enhance

it with the help of AI capabilities. Now

there are two things with genai. One is

how do you use AI tools to enhance or to

improve your productivity and second is

how do you enhance your application. So

let's say if you are building an

application where uh this is for let's

say e-commerce and in the e-commerce

basically for the user side if they are

buying something maybe they they want

some suggestions maybe they want to talk

to a chatbot so AI should be able to

help them uh there are different ways AI

can help for a user but let's say if

you're an admin and if you are adding a

product in that scenario as well maybe

if you want to generate a image or maybe

you want to write a description for a AI

can help you there. So of course based

on your domain, based on what kind of

application you're building, you can

enhance it with the help of AI. So

that's where you you can say it's AI

enabled. Now it has actually become easy

thanks to all these AI providers. So you

don't have to basically build your own

models. We got these amazing models

created by different companies. We got

OpenAI, Google, Antropic and many more.

And they are doing good job. And of

course we don't want to compete with

them in terms of model creation. Second

is you don't have to run those models in

your machine. You can but running a

model in your machine you you need a

high configuration machine and we can do

that. Of course that's a fun of building

your own servers. But what if you can

use a cloud service? Maybe this company

can provide you the AI models running on

their servers and that is actually

happening and that's what you call model

as a service right maybe you can say

mass uh and that's what you can use. So

that's easy right? So let's say you

decided to go for open AAI or maybe

anthropic or maybe Gemini your choice

and you are choosing let's say one

personally I prefer Gemini but since

openAI was the first one to do this or

maybe the first one to get famous

because of this uh so a lot of people

talk about OpenAI but I'm a big follower

of Google so let me just go to Gemini

now if you want to use their model of

course you need to get their API key and

then whenever you want to ask or

whenever you want to talk you need to

connect you you need to hit their APIs.

So if you have a key and if you have the

API link you can hit it and based on

which language you're working with. Of

course this companies provide you SDKs

and they have API where you don't need

to follow any language you can directly

use an API or maybe you can use the

SDKs. Now if you're using a Java SDK

let's say so you got multiple options

you got Python, JavaScript Go, Java, C#

and Appcript. Okay, what is appcript?

um okay never tried it but anyway or

maybe I'm not able to recall now but

let's say if you choose Java or maybe if

you choose let's say Python very famous

in this case you can simply install uh

the library and this is the code in

Python but now since we are talking

about Java let me switch to Java this is

a dependency which you have to add in

your Maven project uh simple dependency

right and it should be available and

then you can write your Java code so it

doesn't matter is it a web application

or a standalone application you can

simply create a client. Uh you have to

once you have dependency you can import

this and you can create a client and you

can say client.mmodels.generate

content and you can pass the prompt it

will give you the answer right so you

can say response.ext text

uh that will work and that's amazing.

Good. So it's easy now. So you can

simply use this code. Of course this is

simple. So the moment you want to do

something complex in that case of course

you need to add some extra code and

there should be some examples as well.

Now let's say you decided to jump from

Gemini to let's say OpenAI mid project.

Okay, in this case or maybe you have

built a project and now you want to move

in this case you will say okay let me go

to open AI and here let me check the

code of course u openai provides SDK for

different languages you can see we have

an option here uh JavaScript python net

java and go and we decided to go with

Java so this is a dependency so you have

to change dependency and that makes

sense dependency will change based on

the SDK but look at the code code is

also changing this is the code for openi

where you have openi client In Gemini it

was the client only. So certain thing

will change and uh you need to create

object of some other class you have to

do this you have to pass the prompt as

well. This is your prompt and then you

say create uh you will say you will get

the response and then you will say

output text. I mean imagine just for a

simple prompt so many lines of code is

changing also if you go to let's say

anthropic you decided to go for claude

in this case again you need to uh so I'm

into different SDK you can see we got

Python SDK JavaScript sorry TypeScript

uh Java go Ruby uh C# PHP I think they

were smart to not have JavaScript there

not a big fan of JavaScript TypeScript

okay uh but let's say you went for uh

Java SDK here and then and reason for

Java is because Most of the enterprise

application is built on Java and now

they want to enhance it. Of course, not

every project for AI will be from

scratch. There are existing application

which you will be using and you'll be

enhancing it. Uh so if it is Java

application

so you will simply go to depend upon

which uh build tool you're using. So

let's say if you're using Maven again

dependency and look let's look at the

code and the code is different again. So

you have to import certain packages but

here it's anthropic client. I think

Gemini was smart to just have a client.

They're not saying Gemini Gemini

everywhere. Good job Gemini. But

Antropic says no you have to go for

anthropic client. Open says you have to

go for open air client and uh things

will change. Okay. So it's good they are

providing you SDKs but every SDK

different code and the problem is if you

want to switch to a different model you

need to change lot of lines of code. Of

course even AI can help you there. You

can use AI and say hey you know or maybe

to co-pilot and say change this code

from Gemini to anthropic and it will do

it for you. Uh that's a good thing right

that's a good addition but uh you're

still changing the code. What if you can

get an abstraction layer somewhere you

use certain tool which will help you to

abstract all these features and as a

user sorry as a developer what is your

job is to use that tool irrespective of

the model behind the scene and the code

will remain same. Yeah, in future if you

want to go from entropic to let's say

open AAI in that case dependency will

change the maven dependency will change

uh everything else remains the same oh

configuration might change where you

will uh specify which model you're using

uh specifically model because claude

openai gemini provide different models

right in fact in openai you you might

have heard about GPT 5 5.1 now we got

5.2 2 even in claude we got different

options sonet opus in gemini we got uh

gemini 3.1 3.2 is 3.2 is released I

think. So those are the things which

will change the code will remain same

and that abstraction we want. Now

there's one way you can build this

abstraction layer by yourself. But then

since you use a framework one of the

famous framework in the Java world is

spring and if you have an application

already built in spring boot you don't

have to worry about creating this

abstraction layer by yourself. What you

can do is you can use something called

Spring AI and Spring AI basically is an

abstraction layer to use the AI models.

So what it does is let's say you have

your application so you got your APIs,

you got your data and you can use a

feature of generative AI. So you can use

genative AI and generative AI can push

some code or some data to you some

features and that's what you can use. uh

of course you will be hitting the models

and one thing one disclaimer when you

want to do this when you want to work

with any of this model you need to add

some credits I think Gemini provides you

some free credits but when I was using

it it was not working properly sometime

it was working sometime it was not

giving you response so it's better to

add some amount of dollars maybe one or

two dollars should be enough if you're

working with image or video generation

which will consume lot of tokens uh

again all these things tokens things

we'll talk about that in sometime may

not be in this video but we'll talk

about it but uh yeah so this you can you

have to add so you can choose any of

this I'll be using anthropic okay so I

was doing openAI in previous videos but

now I'll go with anthropic I feel it's

doing very good in terms of responses

let me know your thoughts in the

comments which model you prefer uh but

as as we as I mentioned you know we are

going to use springi so it doesn't

matter which model you use the code will

remain same the the configuration will

change and dependency will change. Okay.

So here if I go to Spring AI, so this is

what we have talked about. Now Spring AI

got its inspiration from lang chain and

llama index uh but not completely

copied. Inspiration is good but they

have done things bit differently and if

you're not sure about AI concepts again

I'm trying I will try to explain those

things but I would also recommend you to

go to spring AI docs. Uh they have very

good documentation and the best thing is

spring AI 2.0 0 version has launched.

It's still into M2. Uh G will be

available soon. And if you look at this

tab which is AI uh concepts, there are

different uh concepts here. You can go

through it. As I mentioned, we are going

to talk about these concepts step by

step. But there are different concepts

here. Maybe sometime I will refer the

same page to explain you certain

concepts. Now let me take you to a page

which is talks about chat client API.

Again we will not be going in detail.

What I want to show you is the code. Now

irrespective of which model you're

using. So if you look at the code this

is a code a simple code of talking to a

model. So this is your controller simple

controller in which you got chat client.

No way we are mentioning is it uh it is

open AI client or antropic client. We

are simply saying it's a chat client and

then you can simply hit a prompt. Okay.

Yes. In the configuration you have to

mention. So I think they don't have the

configuration mentioned anywhere. No but

I will show you where do you put that.

Is it anthropic or is it OpenAI? Cool.

So this is how it helps you. Now let's

see how do you create a project in

Spring AI and anything I'm missing

before we move to creating a project. So

there are different uh options here. So

you can see it supports all the major

providers. We got uh Antropic, OpenAI,

Microsoft, Amazon, uh Google and Olama.

So yes you can you can use local models

running with the help of Olama and you

can connect it. We got chat completion,

embedding, text to image, uh audio

transcription, text to speech and

moderation. And we get different option.

You get structured outputs. Uh we can

also work with different vector database

providers. Again, what what is vector

database? We'll talk about that. But in

case if you know uh it supports

different providers here as you can see

the list. Uh we also have an option of

tool calling which is a basic of which

is a foundation for agentic AI. Uh so

that is the option. Then you get the

option of observability.

uh then you can evaluate the models and

anything important. Yeah. So yeah we

have we have different options uh AI

concepts will go step by step one by one

but if you go to references and if you

are very specific about the models so if

you expand this the different chat

models you can work with anthropic azure

openai deepse

uh Google has different models you can

check them out and yeah okay so we got

this new option which is openai SDK

never tried this but maybe some of the

video I will try to use this. So we just

have to use the API key. Okay, it's from

the Azure. Okay, there's something I

need to explore.

We'll check that. So we also have the

support of MCP. So we can build MCP

servers with the help of uh Spring AI.

Uh drag is there. Okay. So now let's see

how do we create a Spring AI project.

It's very easy actually. Now if I go to

Spring Initializer, I think that's where

you normally create your Spring Boot

project. Uh I will choose Maven because

some of the uh models the SDK they

provide only supports Maven. So we'll

choose Maven here. The language is Java.

Uh for the spring boot we got Spring

Boot 4. So we'll use that. And uh we can

provide the example here. The group name

which is com.tiscoco

and demo I will say spring AI and then

the packaging will be jarred based on

this will be using Java 25. uh do you

want properties yaml? I will stick to

properties. And now let's add the

dependency. Of course, I want to create

a spring boot pro spring web project. So

we'll add that. Additionally, I want

spring AI as well. Now when I say AI,

you will see a lot of options here. I'm

going to select anic. So let's do that.

In fact, let me just select Gemini

first. Now if you look at Gemini which

is so vortex AI option we have but

Google genai only supports 3.5

springboard 3.5 and less than four and I

don't want to use vortex at this point

so maybe in future they will add this

support if you say open AI you can add

open AI here with supports but since we

want anthropic we'll select that

antropic claude and that's it uh we can

just click on explore just to verify the

things so we are using spring boot 4

which is here spring boot code and then

the spring AI version is this. So 2.0.0

and these are the dependencies we

adding. So we adding spring starter MVC

web MVC and the spring AI starter model

Android andic. Now once you have this

you can click on download it will

download here. I'll just unzip it. So I

got that on desktop. Unzipping done

desktop. Let me open my intellig idea.

You can use community version or you can

use ultimate version your choice

whatever you have. Let me open the

project. So this is the project. I will

click on open. Uh yes, trust. And this

is your project given to you. So if I

expand the pom uh pom xml file, I think

I have to increase the font size a bit.

Appearance. I will keep it 200. Looks

good for recording. And if I go down, so

this is the spring same thing which we

have seen. I'm just verifying things if

things are working or not. And now let

me go to src main

in the application properties. There are

certain things we have to mention here.

I will I will not mention and see what

happens. Uh I will go to the application

simple stuff and let me just run this to

check if things are working or not. The

moment I run this application and we got

an error. It says the simple API key

cannot be null. So that means when you

add the dependency for anthropic it is

looking for an API key which is not

there. So in this basically you have to

set a property for the API key. So I

will say spring AI antropic API key.

This is where you specify the key and AI

should suggest the key. But let's see

how do we get the key. So since we are

using anthropic now so let me search for

the dashboard and anthropic dashboard.

You just need to come on this page and

click on so I'm already logged in. So

you have to basically create uh account

here. So I am logged in. So if I go to

settings billing I have added some

amount here and this is the amount I

already have. So I think $2 should be

enough. And even you can get the API

keys from here or from the homepage. And

how do you create the API key? It's very

simple. You can click on create key and

mention the key name. You can say demo

and click on add. And that's it. You

will get the key. And I hope my editor

will hide this. So you just have to copy

the key and paste it there. Uh remember

when you copy the key just make sure

that you paste it somewhere safe. So

I'll go back here and I'm going to use

my old key because I've not saved it

somewhere somewhere the new key but I'll

stick to my old key. This is this is the

key which I've created before and that's

it. Once you add this key it should

work. Let's let's see what what goes

wrong. So the moment you run this uh no

error. Okay. So at this point there's no

error. Application is running but

nowhere we are using the AI feature

right. So in this basically is we have

to write some code to write a prompt

talk to the AI model. uh but the way I'm

going to do it is we can create a web

application. Okay. Uh and let's try that

in the next part. So in this we

basically created the project not doing

anything fancy here or maybe we should

do that right at least we'll make this

project as a complete project. So let's

say uh at this point in this video I

don't want to use any web feature and I

want to do it from the console. How will

I do it? Uh I have not tried this before

making a video. So this will be a

testing board for you and me in fact for

me and some learning for you hopefully.

So the first thing we need is the chat

model because see ultimately you have to

write a prompt. Okay. So let me just

write a prompt here. So I'll say string

prompt and I will say okay it has

suggested something right let's go with

that. What is the capital of France?

Okay and I want to talk to AI. So the

way I can do it is with the help of call

method. So this chat models provide you

this option of call and you can pass

your prompt here. But from where you

will get the prompt? So you will get the

prompt from the chat model. But which

chat model? Now there are two options.

If you remember when we went to this

page, there's option of chat client. But

in this video, I'm not going to use chat

client. I'm going to use chat model. But

this chat model is not abstracted. So

you have to basically use the entropic

chat model. And I will say this is chat

model equal to. Now we can say new

entropic chat model or you can use

spring container to get that. So I'll

say application

context and with the help of context you

can get a bean for this. Again not a

good way of doing this but since we I

don't want to use web so I'm just using

this technique and with the help of chat

model now you can call the prompt and

this should return you text. So I will

say response and now let's print it and

let's see what happens if we fail. If we

fail I mean if we fail we fail and then

we'll try to debug or maybe with the

help of AI we'll debug this. So let's

see let's run this application or rerun

this application and we got an error.

Okay now the error is in fact ignore the

warning here. It says it's not able to

find this model which is claw 3.7 set.

So one thing you can do is I don't know

why it is not able to find it. One thing

you can do is you can specify the model

which is actually available. Okay,

there's a suggestion. Let's try the

session and let's see if AI can actually

help us there. Okay, this is not the

actual property.

So, it's

anthropic dot chat dot

model.

Okay, it says claude but three is very

old, right? But let's see if the even 3

is working. No, same problem. So, let's

specify the model which I was using. So,

it's claude sonnet port 6. This works.

Okay, for a second I I I got scared

looking at this warning. But anyway, but

you got the answer. The capital of

France is Paris. Okay, this is working,

right? So, if you look at the code, this

is a question we have asked. Or maybe

you can you can ask it to tell me a a

joke on AI. Let's see if we can make

joke on itself. Okay. Uh there's a joke.

There are two jokes actually. Why did AI

break up with internet? because it has

too many connections but nobody really

understood it. Oh, okay. That was deep.

I asked an AI to tell me a joke. Uh, it

says error 404 human not found. Then

charged me $20 someone for that. This

was good. Okay. So, you can this is how

you can talk to the models. But again, I

know you're not happy. You want to use

chat line. You don't want to use

anthropic chat model. Next, you want to

do it with the help of controllers, not

from the main. So let's see that in the

upcoming