api.md 10.1 KB
Newer Older
1
2
3
4
[Documentation Home](./README.md)

# API

Matt Williams's avatar
Matt Williams committed
5
6
7
8
9
10
11
- [Generate a Prompt](#generate-a-prompt)
- [Create a Model](#create-a-model)
- [List Local Models](#list-local-models)
- [Copy a Model](#copy-a-model)
- [Delete a Model](#delete-a-model)
- [Pull a Model](#pull-a-model)

Matt Williams's avatar
Matt Williams committed
12
13
14
15
## Things to keep in mind when using the API

### Model name format

Matt Williams's avatar
Matt Williams committed
16
The model name format is `site/namespace/model:tag`. **Site** and **namespace** are optional, but will default to `registry.ollama.ai/library`. You will see this format used in `~/.ollama/models/manifests/`.
Matt Williams's avatar
Matt Williams committed
17
18
19
20
21

### Durations

All durations are in nanoseconds.

22
## Generate a Prompt
Matt Williams's avatar
Matt Williams committed
23

Matt Williams's avatar
Matt Williams committed
24
**POST /api/generate**
25

Matt Williams's avatar
Matt Williams committed
26
27
### Description

Matt Williams's avatar
Matt Williams committed
28
**Generate** is the main endpoint that you will use when working with Ollama. This is used to generate a response to a prompt sent to a model. This is a streaming endpoint, so will be a series of responses. The final response will include the context and what is usually seen in the output from verbose mode.
Matt Williams's avatar
Matt Williams committed
29

Matt Williams's avatar
Matt Williams committed
30
31
### Request

Matt Williams's avatar
Matt Williams committed
32
33
The **Generate** endpoint takes a JSON object with the following fields:

Matt Williams's avatar
Matt Williams committed
34
```JSON
Matt Williams's avatar
Matt Williams committed
35
{
Matt Williams's avatar
Matt Williams committed
36
  "model": "site/namespace/model:tag",
Matt Williams's avatar
Matt Williams committed
37
38
39
40
  "prompt": "You are a software engineer working on building docs for Ollama.",
  "options": {
    "temperature": 0.7,
  }
Matt Williams's avatar
Matt Williams committed
41
42
43
}
```

Matt Williams's avatar
Matt Williams committed
44
45
**Options** can include any of the parameters listed in the [Modelfile](./modelfile.mdvalid-parameters-and-values) documentation. The only required parameter is **model**. If no **prompt** is provided, the model will generate a response to an empty prompt. If no **options** are provided, the model will use the default options from the Modelfile of the parent model.

Matt Williams's avatar
Matt Williams committed
46
47
### Response

Matt Williams's avatar
Matt Williams committed
48
49
The response is a stream of JSON objects with the following fields:

Matt Williams's avatar
Matt Williams committed
50
```JSON
Matt Williams's avatar
Matt Williams committed
51
52
53
54
55
56
57
58
{
  "model": "modelname",
  "created_at": "2023-08-04T08:52:19.385406455-07:00"
  "response": "the current token",
  "done": false
}
```

Matt Williams's avatar
Matt Williams committed
59
60
The final response in the stream also includes the context and what is usually seen in the output from verbose mode. For example:

Matt Williams's avatar
Matt Williams committed
61
```JSON
Matt Williams's avatar
Matt Williams committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
{
  "model":"orca",
  "created_at":"2023-08-04T19:22:45.499127Z",
  "done":true,
  "total_duration":5589157167,
  "load_duration":3013701500,
  "sample_count":114,
  "sample_duration":81442000,
  "prompt_eval_count":46,
  "prompt_eval_duration":1160282000,
  "eval_count":113,
  "eval_duration":1325948000
}
```

Matt Williams's avatar
Matt Williams committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
| field                | description                                             |
| -------------------- | ------------------------------------------------------- |
| model                | the name of the model                                   |
| created_at           | the time the response was generated                     |
| response             | the current token                                       |
| done                 | whether the response is complete                        |
| total_duration       | total time in nanoseconds spent generating the response |
| load_duration        | time spent in nanoseconds loading the model             |
| sample_count         | number of samples generated                             |
| sample_duration      | time spent generating samples                           |
| prompt_eval_count    | number of times the prompt was evaluated                |
| prompt_eval_duration | time spent in nanoseconds evaluating the prompt         |
| eval_count           | number of times the response was evaluated              |
| eval_duration        | time in nanoseconds spent evaluating the response       |
Matt Williams's avatar
Matt Williams committed
91
92
93
94
95
96

### Example

#### Request

```shell
Matt Williams's avatar
Matt Williams committed
97
98
curl -X POST 'http://localhost:11434/api/generate' -d \
'{
Matt Williams's avatar
Matt Williams committed
99
100
101
102
103
    "model": "orca",
    "prompt": "why is the sky blue"
}'
```

Matt Williams's avatar
Matt Williams committed
104
#### Response
Matt Williams's avatar
Matt Williams committed
105
106
107
108
109
110
111
112
113

```json
{"model":"orca","created_at":"2023-08-04T19:22:44.085127Z","response":" The","done":false}
{"model":"orca","created_at":"2023-08-04T19:22:44.176425Z","response":" sky","done":false}
{"model":"orca","created_at":"2023-08-04T19:22:44.18883Z","response":" appears","done":false}
{"model":"orca","created_at":"2023-08-04T19:22:44.200852Z","response":" blue","done":false}
{"model":"orca","created_at":"2023-08-04T19:22:44.213644Z","response":" because","done":false}
{"model":"orca","created_at":"2023-08-04T19:22:44.225706Z","response":" of","done":false}
{"model":"orca","created_at":"2023-08-04T19:22:44.237686Z","response":" a","done":false}
Matt Williams's avatar
Matt Williams committed
114
...
Matt Williams's avatar
Matt Williams committed
115
{"model":"orca","created_at":"2023-08-04T19:22:45.487113Z","response":".","done":false}
Matt Williams's avatar
Matt Williams committed
116
{"model":"orca","created_at":"2023-08-04T19:22:45.499127Z","done":true,"total_duration":5589157167,"load_duration":3013701500,"sample_count":114,"sample_duration":81442000,"prompt_eval_count":46,"prompt_eval_duration":1160282000,"eval_count":113,"eval_duration":1325948000}
Matt Williams's avatar
Matt Williams committed
117
118
```

119
## Create a Model
Matt Williams's avatar
Matt Williams committed
120

Matt Williams's avatar
Matt Williams committed
121
**POST /api/create**
122

Matt Williams's avatar
Matt Williams committed
123
124
125
126
127
128
129
130
131
132
133
### Description

**Create** takes a path to a Modelfile and creates a model. The Modelfile is documented [here](./modelfile.md).

### Request

The **Create** endpoint takes a JSON object with the following fields:

```JSON
{
  "name": "modelname",
Matt Williams's avatar
Matt Williams committed
134
  "path": "absolute path to Modelfile"
Matt Williams's avatar
Matt Williams committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
}
```

### Response

The response is a stream of JSON objects that have a single key/value pair for status. For example:

```JSON
{
  "status": "parsing modelfile"
}
```

### Example

#### Request

```shell
curl --location --request POST 'http://localhost:11434/api/create' \
--header 'Content-Type: text/plain' \
--data-raw '{
    "name": "myCoolModel",
    "path": "/Users/matt/ollamamodelfiles/sentiments"
}'
```

#### Response

```JSON
{"status":"parsing modelfile"}
{"status":"looking for model"}
{"status":"creating model template layer"}
{"status":"creating config layer"}
{"status":"using already created layer sha256:e84705205f71dd55be7b24a778f248f0eda9999a125d313358c087e092d83148"}
{"status":"using already created layer sha256:93ca9b3d83dc541f11062c0b994ae66a7b327146f59a9564aafef4a4c15d1ef5"}
{"status":"writing layer sha256:d3fe6fb39620a477da7720c5fa00abe269a018a9675a726320e18122b7142ee7"}
{"status":"writing layer sha256:16cc83359b0395026878b41662f7caef433f5260b5d49a3257312b6417b7d8a8"}
{"status":"writing manifest"}
{"status":"success"}
```

176
## List Local Models
Matt Williams's avatar
Matt Williams committed
177

Matt Williams's avatar
Matt Williams committed
178
179
**GET /api/tags**

Matt Williams's avatar
Matt Williams committed
180
### Description
Matt Williams's avatar
Matt Williams committed
181

Matt Williams's avatar
Matt Williams committed
182
**List** will list out the models that are available locally.
Matt Williams's avatar
Matt Williams committed
183
184
185
186
187
188
189
190
191
192

### Request

The **List** endpoint takes no parameters and is a simple GET request.

### Response

The response is a JSON object with a single key/value pair for models. For example:

```JSON
Matt Williams's avatar
Matt Williams committed
193
194
195
196
197
{
  "models": [
    {
      "name": "modelname:tags",
      "modified_at": "2023-08-04T08:52:19.385406455-07:00",
Matt Williams's avatar
Matt Williams committed
198
      "size": 7323310500
Matt Williams's avatar
Matt Williams committed
199
200
    }
  ]
Matt Williams's avatar
Matt Williams committed
201
202
203
204
}
```

### Example
Matt Williams's avatar
Matt Williams committed
205

Matt Williams's avatar
Matt Williams committed
206
207
208
#### Request

```shell
Matt Williams's avatar
Matt Williams committed
209
curl 'http://localhost:11434/api/tags'
Matt Williams's avatar
Matt Williams committed
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
```

#### Response

```JSON
{
    "models": [
        {
            "name": "llama2:70b",
            "modified_at": "2023-08-04T08:52:19.385406455-07:00",
            "size": 38871966966
        },
        {
            "name": "llama2:70b-chat-q4_0",
            "modified_at": "2023-08-04T09:21:27.703371485-07:00",
            "size": 38871974480
        },
        {
            "name": "midjourney-prompter:latest",
            "modified_at": "2023-08-04T08:45:46.399609053-07:00",
            "size": 7323311708
        },
        {
            "name": "raycast_orca:3b",
            "modified_at": "2023-08-04T06:23:20.10832636-07:00",
            "size": 1928446602
        },
        {
            "name": "stablebeluga:13b-q4_K_M",
            "modified_at": "2023-08-04T09:48:26.416547463-07:00",
            "size": 7865679045
        }
    ]
Matt Williams's avatar
Matt Williams committed
243
244
}
```
245
246

## Copy a Model
Matt Williams's avatar
Matt Williams committed
247

Matt Williams's avatar
Matt Williams committed
248
249
250
251
252
253
254
255
256
257
258
259
**POST /api/copy**

### Description

**Copy** will copy a model from one name to another. This is useful for creating a new model from an existing model. It is often used as the first step to renaming a model.

### Request

The **Copy** endpoint takes a JSON object with the following fields:

```JSON
{
Matt Williams's avatar
Matt Williams committed
260
261
  "source": "modelname",
  "destination": "newmodelname"
Matt Williams's avatar
Matt Williams committed
262
263
264
265
266
267
268
269
270
271
272
273
}
```

### Response

There is no response other than a 200 status code.

### Example

#### Request

```shell
Matt Williams's avatar
Matt Williams committed
274
275
curl -X POST 'http://localhost:11434/api/copy' -d \
'{
Matt Williams's avatar
Matt Williams committed
276
277
278
279
280
281
282
283
    "source": "MyCoolModel",
    "destination": "ADifferentModel"
}'
```

#### Response

No response is returned other than a 200 status code.
284

Matt Williams's avatar
Matt Williams committed
285
## Delete a Model
Matt Williams's avatar
Matt Williams committed
286

Matt Williams's avatar
Matt Williams committed
287
**DELETE /api/delete**
Matt Williams's avatar
Matt Williams committed
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311

### Description

**Delete** will delete a model from the local machine. This is useful for cleaning up models that are no longer needed.

### Request

The **Delete** endpoint takes a JSON object with a single key/value pair for modelname. For example:

```JSON
{
  "model": "modelname"
}
```

### Response

No response is returned other than a 200 status code.

### Example

#### Request

```shell
Matt Williams's avatar
Matt Williams committed
312
313
curl -X DELETE 'http://localhost:11434/api/delete' -d \
'{
Matt Williams's avatar
Matt Williams committed
314
315
316
317
318
319
320
    "name": "adifferentModel"
}'
```

#### Response

No response is returned other than a 200 status code.
Matt Williams's avatar
Matt Williams committed
321

322
## Pull a Model
Matt Williams's avatar
Matt Williams committed
323

Matt Williams's avatar
Matt Williams committed
324
325
326
327
328
329
330
331
332
333
334
335
**POST /api/pull**

### Description

**Pull** will pull a model from a remote registry. This is useful for getting a model from the Ollama registry and in the future from alternate registries.

### Request

The **Pull** endpoint takes a JSON object with the following fields:

```JSON
{
Matt Williams's avatar
Matt Williams committed
336
  "name": "modelname"
Matt Williams's avatar
Matt Williams committed
337
338
339
340
}
```

### Response
341

Matt Williams's avatar
Matt Williams committed
342
The response is a stream of JSON objects with the following format:
Matt Williams's avatar
Matt Williams committed
343

Matt Williams's avatar
Matt Williams committed
344
345
346
347
348
349
350
```JSON
{
  "status":"downloading digestname",
  "digest":"digestname",
  "total":2142590208
}
```
Matt Williams's avatar
Matt Williams committed
351

Matt Williams's avatar
Matt Williams committed
352
### Example
Matt Williams's avatar
Matt Williams committed
353

Matt Williams's avatar
Matt Williams committed
354
355
356
#### Request

```shell
Matt Williams's avatar
Matt Williams committed
357
358
curl -X POST 'http://localhost:11434/api/pull' -d \
'{
Matt Williams's avatar
Matt Williams committed
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
    "name": "orca:3b-q4_1"
}'
```

#### Response

```JSON
{"status":"pulling manifest"}
{"status":"downloading sha256:63151c63f792939bb4a40b35f37ea06e047c02486399d1742113aaefd0d33e29","digest":"sha256:63151c63f792939bb4a40b35f37ea06e047c02486399d1742113aaefd0d33e29","total":2142590208}
{"status":"downloading sha256:63151c63f792939bb4a40b35f37ea06e047c02486399d1742113aaefd0d33e29","digest":"sha256:63151c63f792939bb4a40b35f37ea06e047c02486399d1742113aaefd0d33e29","total":2142590208,"completed":1048576}
...
{"status":"downloading sha256:20714f2ebe4be44313358bfa58556d783652398ed47f12178914c706c4ad12c4","digest":"sha256:20714f2ebe4be44313358bfa58556d783652398ed47f12178914c706c4ad12c4","total":299}
{"status":"downloading sha256:20714f2ebe4be44313358bfa58556d783652398ed47f12178914c706c4ad12c4","digest":"sha256:20714f2ebe4be44313358bfa58556d783652398ed47f12178914c706c4ad12c4","total":299,"completed":299}
{"status":"verifying sha256 digest"}
{"status":"writing manifest"}
{"status":"success"}

```