2
2
3
3
The Ollama Python library provides the easiest way to integrate Python 3.8+ projects with [ Ollama] ( https://github.com/ollama/ollama ) .
4
4
5
+ ## Prerequisites
6
+
7
+ - [ Ollama] ( https://ollama.com/download ) should be installed and running
8
+ - Pull a model to use with the library: ` ollama pull <model> ` e.g. ` ollama pull llama3.2 `
9
+ - See [ Ollama.com] ( https://ollama.com/search ) for more information on the models available.
10
+
5
11
## Install
6
12
7
13
``` sh
@@ -11,25 +17,34 @@ pip install ollama
11
17
## Usage
12
18
13
19
``` python
14
- import ollama
15
- response = ollama.chat(model = ' llama3.1' , messages = [
20
+ from ollama import chat
21
+ from ollama import ChatResponse
22
+
23
+ response: ChatResponse = chat(model = ' llama3.2' , messages = [
16
24
{
17
25
' role' : ' user' ,
18
26
' content' : ' Why is the sky blue?' ,
19
27
},
20
28
])
21
29
print (response[' message' ][' content' ])
30
+ # or access fields directly from the response object
31
+ print (response.message.content)
22
32
```
23
33
34
+ See [ _ types.py] ( ollama/_types.py ) for more information on the response types.
35
+
24
36
## Streaming responses
25
37
26
- Response streaming can be enabled by setting ` stream=True ` , modifying function calls to return a Python generator where each part is an object in the stream.
38
+ Response streaming can be enabled by setting ` stream=True ` .
39
+
40
+ > [ !NOTE]
41
+ > Streaming Tool/Function calling is not yet supported.
27
42
28
43
``` python
29
- import ollama
44
+ from ollama import chat
30
45
31
- stream = ollama. chat(
32
- model = ' llama3.1 ' ,
46
+ stream = chat(
47
+ model = ' llama3.2 ' ,
33
48
messages = [{' role' : ' user' , ' content' : ' Why is the sky blue?' }],
34
49
stream = True ,
35
50
)
@@ -38,20 +53,68 @@ for chunk in stream:
38
53
print (chunk[' message' ][' content' ], end = ' ' , flush = True )
39
54
```
40
55
56
+ ## Custom client
57
+ A custom client can be created by instantiating ` Client ` or ` AsyncClient ` from ` ollama ` .
58
+
59
+ All extra keyword arguments are passed into the [ ` httpx.Client ` ] ( https://www.python-httpx.org/api/#client ) .
60
+
61
+ ``` python
62
+ from ollama import Client
63
+ client = Client(
64
+ host = ' http://localhost:11434' ,
65
+ headers = {' x-some-header' : ' some-value' }
66
+ )
67
+ response = client.chat(model = ' llama3.2' , messages = [
68
+ {
69
+ ' role' : ' user' ,
70
+ ' content' : ' Why is the sky blue?' ,
71
+ },
72
+ ])
73
+ ```
74
+
75
+ ## Async client
76
+
77
+ The ` AsyncClient ` class is used to make asynchronous requests. It can be configured with the same fields as the ` Client ` class.
78
+
79
+ ``` python
80
+ import asyncio
81
+ from ollama import AsyncClient
82
+
83
+ async def chat ():
84
+ message = {' role' : ' user' , ' content' : ' Why is the sky blue?' }
85
+ response = await AsyncClient().chat(model = ' llama3.2' , messages = [message])
86
+
87
+ asyncio.run(chat())
88
+ ```
89
+
90
+ Setting ` stream=True ` modifies functions to return a Python asynchronous generator:
91
+
92
+ ``` python
93
+ import asyncio
94
+ from ollama import AsyncClient
95
+
96
+ async def chat ():
97
+ message = {' role' : ' user' , ' content' : ' Why is the sky blue?' }
98
+ async for part in await AsyncClient().chat(model = ' llama3.2' , messages = [message], stream = True ):
99
+ print (part[' message' ][' content' ], end = ' ' , flush = True )
100
+
101
+ asyncio.run(chat())
102
+ ```
103
+
41
104
## API
42
105
43
106
The Ollama Python library's API is designed around the [ Ollama REST API] ( https://github.com/ollama/ollama/blob/main/docs/api.md )
44
107
45
108
### Chat
46
109
47
110
``` python
48
- ollama.chat(model = ' llama3.1 ' , messages = [{' role' : ' user' , ' content' : ' Why is the sky blue?' }])
111
+ ollama.chat(model = ' llama3.2 ' , messages = [{' role' : ' user' , ' content' : ' Why is the sky blue?' }])
49
112
```
50
113
51
114
### Generate
52
115
53
116
``` python
54
- ollama.generate(model = ' llama3.1 ' , prompt = ' Why is the sky blue?' )
117
+ ollama.generate(model = ' llama3.2 ' , prompt = ' Why is the sky blue?' )
55
118
```
56
119
57
120
### List
@@ -63,14 +126,14 @@ ollama.list()
63
126
### Show
64
127
65
128
``` python
66
- ollama.show(' llama3.1 ' )
129
+ ollama.show(' llama3.2 ' )
67
130
```
68
131
69
132
### Create
70
133
71
134
``` python
72
135
modelfile= '''
73
- FROM llama3.1
136
+ FROM llama3.2
74
137
SYSTEM You are mario from super mario bros.
75
138
'''
76
139
@@ -80,37 +143,37 @@ ollama.create(model='example', modelfile=modelfile)
80
143
### Copy
81
144
82
145
``` python
83
- ollama.copy(' llama3.1 ' , ' user/llama3.1 ' )
146
+ ollama.copy(' llama3.2 ' , ' user/llama3.2 ' )
84
147
```
85
148
86
149
### Delete
87
150
88
151
``` python
89
- ollama.delete(' llama3.1 ' )
152
+ ollama.delete(' llama3.2 ' )
90
153
```
91
154
92
155
### Pull
93
156
94
157
``` python
95
- ollama.pull(' llama3.1 ' )
158
+ ollama.pull(' llama3.2 ' )
96
159
```
97
160
98
161
### Push
99
162
100
163
``` python
101
- ollama.push(' user/llama3.1 ' )
164
+ ollama.push(' user/llama3.2 ' )
102
165
```
103
166
104
167
### Embed
105
168
106
169
``` python
107
- ollama.embed(model = ' llama3.1 ' , input = ' The sky is blue because of rayleigh scattering' )
170
+ ollama.embed(model = ' llama3.2 ' , input = ' The sky is blue because of rayleigh scattering' )
108
171
```
109
172
110
173
### Embed (batch)
111
174
112
175
``` python
113
- ollama.embed(model = ' llama3.1 ' , input = [' The sky is blue because of rayleigh scattering' , ' Grass is green because of chlorophyll' ])
176
+ ollama.embed(model = ' llama3.2 ' , input = [' The sky is blue because of rayleigh scattering' , ' Grass is green because of chlorophyll' ])
114
177
```
115
178
116
179
### Ps
@@ -119,50 +182,6 @@ ollama.embed(model='llama3.1', input=['The sky is blue because of rayleigh scatt
119
182
ollama.ps()
120
183
```
121
184
122
- ## Custom client
123
-
124
- A custom client can be created with the following fields:
125
-
126
- - ` host ` : The Ollama host to connect to
127
- - ` timeout ` : The timeout for requests
128
-
129
- ``` python
130
- from ollama import Client
131
- client = Client(host = ' http://localhost:11434' )
132
- response = client.chat(model = ' llama3.1' , messages = [
133
- {
134
- ' role' : ' user' ,
135
- ' content' : ' Why is the sky blue?' ,
136
- },
137
- ])
138
- ```
139
-
140
- ## Async client
141
-
142
- ``` python
143
- import asyncio
144
- from ollama import AsyncClient
145
-
146
- async def chat ():
147
- message = {' role' : ' user' , ' content' : ' Why is the sky blue?' }
148
- response = await AsyncClient().chat(model = ' llama3.1' , messages = [message])
149
-
150
- asyncio.run(chat())
151
- ```
152
-
153
- Setting ` stream=True ` modifies functions to return a Python asynchronous generator:
154
-
155
- ``` python
156
- import asyncio
157
- from ollama import AsyncClient
158
-
159
- async def chat ():
160
- message = {' role' : ' user' , ' content' : ' Why is the sky blue?' }
161
- async for part in await AsyncClient().chat(model = ' llama3.1' , messages = [message], stream = True ):
162
- print (part[' message' ][' content' ], end = ' ' , flush = True )
163
-
164
- asyncio.run(chat())
165
- ```
166
185
167
186
## Errors
168
187
0 commit comments