Instructor: One other extremely useful
thing you can do with Ollama,
which you could also do with LM Studio,
as you learned there, is request structured data,
or request the response to be structured in a certain way.
Specifically, you can ask for JSON data
and define a json_schema to which Ollama
and the underlying model then will adhere.
So here, in this example script,
I again, wanna talk to the gemma3 model,
and I have a hard coded prompt here.
And of course you could also build an application
where you are dynamically fetching that prompt.
Here, it's just hard coded for demo purposes.
And I'm asking for a JSON list of three fictional users
where every user should have
a first name, last name, birth date, and country.
And then I again, make it really clear
that I want JSON data, and then I define a json_schema.
And here in Python, that can be done as a dictionary
where I set up the type of the root data object,
so to say, that it should be an array full of items,
and that every item is an object which has four properties,
first name, last name, birth date, country,
where every property is of "type": "string",
so a text value, where I describe every property,
which is important for the model to understand
which kind of value should go into that property,
and that all properties are required and must be set.
Then, for the request that's about to be sent to the model,
I insert the model name, my hard coded prompt,
and this schema under this format parameter.
And that is something you learn in the official docs.
You can set such a format parameter
on the request you're sending to the model
to request data to be returned in a certain JSON schema.
There also is a more detailed
structured outputs example in those docs.
Here, you see something similar as I'm setting it up here.
Again, I don't wanna stream that data,
I want to get it in one block.
And then, I sent my request here and handle the response.
And as a result, when running that script here,
it will send that prompt to the model
that is being executed, or that is being hosted by Ollama.
And indeed then, I get back
data formatted in that format I requested.
And that's of course, super useful,
because here it's just a demo,
but you can use this feature for any use case you have.
Whenever you are building some kind
of automated AI-powered system,
chances are high that you want the AI
to generate data that adheres to a certain structure,
and that's how you can get the model
to return that data in that format you want.
So that's another very useful feature offered by Ollama,
which you can use programmatically.