WEBVTT

1
00:00:00.660 --> 00:00:02.670
<v Instructor>One other extremely useful</v>

2
00:00:02.670 --> 00:00:05.310
thing you can do with Ollama,

3
00:00:05.310 --> 00:00:07.500
which you could also do with LM Studio,

4
00:00:07.500 --> 00:00:11.610
as you learned there, is request structured data,

5
00:00:11.610 --> 00:00:16.610
or request the response to be structured in a certain way.

6
00:00:16.830 --> 00:00:19.290
Specifically, you can ask for JSON data

7
00:00:19.290 --> 00:00:22.380
and define a json_schema to which Ollama

8
00:00:22.380 --> 00:00:25.293
and the underlying model then will adhere.

9
00:00:26.220 --> 00:00:27.900
So here, in this example script,

10
00:00:27.900 --> 00:00:30.840
I again, wanna talk to the gemma3 model,

11
00:00:30.840 --> 00:00:33.120
and I have a hard coded prompt here.

12
00:00:33.120 --> 00:00:35.190
And of course you could also build an application

13
00:00:35.190 --> 00:00:37.560
where you are dynamically fetching that prompt.

14
00:00:37.560 --> 00:00:40.623
Here, it's just hard coded for demo purposes.

15
00:00:41.460 --> 00:00:46.050
And I'm asking for a JSON list of three fictional users

16
00:00:46.050 --> 00:00:47.370
where every user should have

17
00:00:47.370 --> 00:00:50.580
a first name, last name, birth date, and country.

18
00:00:50.580 --> 00:00:52.920
And then I again, make it really clear

19
00:00:52.920 --> 00:00:57.510
that I want JSON data, and then I define a json_schema.

20
00:00:57.510 --> 00:01:00.480
And here in Python, that can be done as a dictionary

21
00:01:00.480 --> 00:01:04.380
where I set up the type of the root data object,

22
00:01:04.380 --> 00:01:07.110
so to say, that it should be an array full of items,

23
00:01:07.110 --> 00:01:10.890
and that every item is an object which has four properties,

24
00:01:10.890 --> 00:01:14.010
first name, last name, birth date, country,

25
00:01:14.010 --> 00:01:16.230
where every property is of "type": "string",

26
00:01:16.230 --> 00:01:19.890
so a text value, where I describe every property,

27
00:01:19.890 --> 00:01:22.230
which is important for the model to understand

28
00:01:22.230 --> 00:01:25.083
which kind of value should go into that property,

29
00:01:26.010 --> 00:01:29.670
and that all properties are required and must be set.

30
00:01:29.670 --> 00:01:33.030
Then, for the request that's about to be sent to the model,

31
00:01:33.030 --> 00:01:35.520
I insert the model name, my hard coded prompt,

32
00:01:35.520 --> 00:01:39.330
and this schema under this format parameter.

33
00:01:39.330 --> 00:01:41.580
And that is something you learn in the official docs.

34
00:01:41.580 --> 00:01:43.890
You can set such a format parameter

35
00:01:43.890 --> 00:01:46.350
on the request you're sending to the model

36
00:01:46.350 --> 00:01:51.090
to request data to be returned in a certain JSON schema.

37
00:01:51.090 --> 00:01:52.440
There also is a more detailed

38
00:01:52.440 --> 00:01:55.950
structured outputs example in those docs.

39
00:01:55.950 --> 00:01:59.760
Here, you see something similar as I'm setting it up here.

40
00:01:59.760 --> 00:02:01.260
Again, I don't wanna stream that data,

41
00:02:01.260 --> 00:02:03.123
I want to get it in one block.

42
00:02:04.350 --> 00:02:07.563
And then, I sent my request here and handle the response.

43
00:02:08.580 --> 00:02:12.270
And as a result, when running that script here,

44
00:02:12.270 --> 00:02:14.760
it will send that prompt to the model

45
00:02:14.760 --> 00:02:18.390
that is being executed, or that is being hosted by Ollama.

46
00:02:18.390 --> 00:02:20.310
And indeed then, I get back

47
00:02:20.310 --> 00:02:23.340
data formatted in that format I requested.

48
00:02:23.340 --> 00:02:24.780
And that's of course, super useful,

49
00:02:24.780 --> 00:02:26.460
because here it's just a demo,

50
00:02:26.460 --> 00:02:30.390
but you can use this feature for any use case you have.

51
00:02:30.390 --> 00:02:31.917
Whenever you are building some kind

52
00:02:31.917 --> 00:02:35.040
of automated AI-powered system,

53
00:02:35.040 --> 00:02:37.283
chances are high that you want the AI

54
00:02:37.283 --> 00:02:42.000
to generate data that adheres to a certain structure,

55
00:02:42.000 --> 00:02:44.760
and that's how you can get the model

56
00:02:44.760 --> 00:02:48.090
to return that data in that format you want.

57
00:02:48.090 --> 00:02:51.780
So that's another very useful feature offered by Ollama,

58
00:02:51.780 --> 00:02:54.063
which you can use programmatically.

