Maximilian: So let's explore some code examples.
You could, for example, write code like this
and you find this and a couple
of other examples attached to this lecture,
by the way, you can write code like this,
set up this base URL,
and also set up the API key so
that you don't get any warnings or errors.
Though the value doesn't matter
because this locally running API isn't protected.
So you can set any value here,
but you can set it up like this
and then use this approach that could also be used
with the open AI models if you were communicating with them,
to communicate with your locally running models.
Now what's important is this model identifier
because that tells LM Studio or the server
provided by LM Studio
with which locally running model you want to communicate
or which model should be loaded
if it hasn't been loaded yet.
Of course, it must be a model
that has been downloaded though.
So loaded just means loaded into memory.
If it doesn't exist on your system,
you can't communicate it no matter if it's loaded or not.
But this must be a valid identifier
and you get the value for this identifier from here,
from your developer view in LM Studio
for a given model that you loaded.
So for this model, for example,
you find the identifier here.
You can also find valid identifiers by going to your models,
and then it's these identifiers here
that have to be inserted in your code
that tells the LM Studio server
to which model the request should be sent.
And again, if it hasn't been loaded into memory yet,
if you got just in time model loading enabled,
it will be loaded as soon as a request
tries to reach that model.
Well then here I'm sending a made up chat history
including a system message to that model.
Therefore, here, if I execute this basic PY file
with Python, which can take a while
because streaming is not enabled here,
so I only get the complete response
once it's done and generating that can take a while.
But once I sent this and wait a while,
eventually I will get back the response
of my locally running model.
But now programmatically with help of that server
that's spun up by LM Studio.
And as mentioned, I got some other examples here as well.
For example, a basic chat where I do have streaming enabled
and where I got an infinite loop to ask
for more and more user input.
So if I run that, I got a chat interface here
where I can send messages
and then the responses are streamed in
and I can send up follow up messages.
And then I also got another example
for an application you could build with help of local AI.
I got an image parser application,
which tries to find all jpeg and png images
in a folder named Images.
This folder here, which got 2 demo images here, in my case.
And then for every image file I find,
I in the end convert that to a format that's called base 64,
which is a text representation of that image
that can be converted back
to the actual image on the server I send it to, for example,
and I sent this converted image to the AI model
by setting the content of the message
I'm sending to the model to a combination of text
and an image URL.
And that image URL is that base 64 encoded image.
Of course you have to send that to a model
that supports image input,
but that's the case
for this Gemma 3 model I'm using here.
And with that, I'm sending this lake image,
which I took from a plane
and this image here of me in my recording set up,
I'm sending this to the AI here,
again programmatically by sending it to that server
that was spun up by LM Studio.
Now of course this also takes a while here,
but once it's done, I got a detailed description
of my Max image here, which is correct that it's a selfie,
by a man in a home studio or recording setup.
And I got the thumbs up gesture,
if we take another look at the image.
You see that is correct. Thumbs up.
I got a description of my shirt
and a bunch of other information about the image.
And then the same also for a lake
where it describes a lake, which of course also is true.
It also correctly detected that this was taken from
inside an airplane.
So that's another example
for using a locally running AI model programmatically
by sending a request to that locally running server
that is created and managed by LM Studio,
and that exposes those locally running
AI models through that server.