mirror of
https://github.com/ollama/ollama-python.git
synced 2026-10-29 12:03:34 +00:00
readme: update to use gemma 4 and format (#740)
This commit is contained in:
@@ -5,7 +5,7 @@ The Ollama Python library provides the easiest way to integrate Python 3.8+ proj
|
|||||||
## Prerequisites
|
## Prerequisites
|
||||||
|
|
||||||
- [Ollama](https://ollama.com/download) should be installed and running
|
- [Ollama](https://ollama.com/download) should be installed and running
|
||||||
- Pull a model to use with the library: `ollama pull <model>` e.g. `ollama pull gemma3`
|
- Pull a model to use with the library: `ollama pull <model>` e.g. `ollama pull gemma4`
|
||||||
- See [Ollama.com](https://ollama.com/search) for more information on the models available.
|
- See [Ollama.com](https://ollama.com/search) for more information on the models available.
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
@@ -20,12 +20,15 @@ pip install ollama
|
|||||||
from ollama import chat
|
from ollama import chat
|
||||||
from ollama import ChatResponse
|
from ollama import ChatResponse
|
||||||
|
|
||||||
response: ChatResponse = chat(model='gemma3', messages=[
|
response: ChatResponse = chat(
|
||||||
{
|
model='gemma4',
|
||||||
'role': 'user',
|
messages=[
|
||||||
'content': 'Why is the sky blue?',
|
{
|
||||||
},
|
'role': 'user',
|
||||||
])
|
'content': 'Why is the sky blue?',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
)
|
||||||
print(response['message']['content'])
|
print(response['message']['content'])
|
||||||
# or access fields directly from the response object
|
# or access fields directly from the response object
|
||||||
print(response.message.content)
|
print(response.message.content)
|
||||||
@@ -41,9 +44,9 @@ Response streaming can be enabled by setting `stream=True`.
|
|||||||
from ollama import chat
|
from ollama import chat
|
||||||
|
|
||||||
stream = chat(
|
stream = chat(
|
||||||
model='gemma3',
|
model='gemma4',
|
||||||
messages=[{'role': 'user', 'content': 'Why is the sky blue?'}],
|
messages=[{'role': 'user', 'content': 'Why is the sky blue?'}],
|
||||||
stream=True,
|
stream=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
for chunk in stream:
|
for chunk in stream:
|
||||||
@@ -110,10 +113,7 @@ curl https://ollama.com/api/tags
|
|||||||
import os
|
import os
|
||||||
from ollama import Client
|
from ollama import Client
|
||||||
|
|
||||||
client = Client(
|
client = Client(host='https://ollama.com', headers={'Authorization': 'Bearer ' + os.environ.get('OLLAMA_API_KEY')})
|
||||||
host='https://ollama.com',
|
|
||||||
headers={'Authorization': 'Bearer ' + os.environ.get('OLLAMA_API_KEY')}
|
|
||||||
)
|
|
||||||
|
|
||||||
messages = [
|
messages = [
|
||||||
{
|
{
|
||||||
@@ -133,16 +133,17 @@ All extra keyword arguments are passed into the [`httpx.Client`](https://www.pyt
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
from ollama import Client
|
from ollama import Client
|
||||||
client = Client(
|
|
||||||
host='http://localhost:11434',
|
client = Client(host='http://localhost:11434', headers={'x-some-header': 'some-value'})
|
||||||
headers={'x-some-header': 'some-value'}
|
response = client.chat(
|
||||||
|
model='gemma4',
|
||||||
|
messages=[
|
||||||
|
{
|
||||||
|
'role': 'user',
|
||||||
|
'content': 'Why is the sky blue?',
|
||||||
|
},
|
||||||
|
],
|
||||||
)
|
)
|
||||||
response = client.chat(model='gemma3', messages=[
|
|
||||||
{
|
|
||||||
'role': 'user',
|
|
||||||
'content': 'Why is the sky blue?',
|
|
||||||
},
|
|
||||||
])
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Async client
|
## Async client
|
||||||
@@ -153,9 +154,11 @@ The `AsyncClient` class is used to make asynchronous requests. It can be configu
|
|||||||
import asyncio
|
import asyncio
|
||||||
from ollama import AsyncClient
|
from ollama import AsyncClient
|
||||||
|
|
||||||
|
|
||||||
async def chat():
|
async def chat():
|
||||||
message = {'role': 'user', 'content': 'Why is the sky blue?'}
|
message = {'role': 'user', 'content': 'Why is the sky blue?'}
|
||||||
response = await AsyncClient().chat(model='gemma3', messages=[message])
|
response = await AsyncClient().chat(model='gemma4', messages=[message])
|
||||||
|
|
||||||
|
|
||||||
asyncio.run(chat())
|
asyncio.run(chat())
|
||||||
```
|
```
|
||||||
@@ -166,11 +169,13 @@ Setting `stream=True` modifies functions to return a Python asynchronous generat
|
|||||||
import asyncio
|
import asyncio
|
||||||
from ollama import AsyncClient
|
from ollama import AsyncClient
|
||||||
|
|
||||||
|
|
||||||
async def chat():
|
async def chat():
|
||||||
message = {'role': 'user', 'content': 'Why is the sky blue?'}
|
message = {'role': 'user', 'content': 'Why is the sky blue?'}
|
||||||
async for part in await AsyncClient().chat(model='gemma3', messages=[message], stream=True):
|
async for part in await AsyncClient().chat(model='gemma4', messages=[message], stream=True):
|
||||||
print(part['message']['content'], end='', flush=True)
|
print(part['message']['content'], end='', flush=True)
|
||||||
|
|
||||||
|
|
||||||
asyncio.run(chat())
|
asyncio.run(chat())
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -181,13 +186,13 @@ The Ollama Python library's API is designed around the [Ollama REST API](https:/
|
|||||||
### Chat
|
### Chat
|
||||||
|
|
||||||
```python
|
```python
|
||||||
ollama.chat(model='gemma3', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}])
|
ollama.chat(model='gemma4', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}])
|
||||||
```
|
```
|
||||||
|
|
||||||
### Generate
|
### Generate
|
||||||
|
|
||||||
```python
|
```python
|
||||||
ollama.generate(model='gemma3', prompt='Why is the sky blue?')
|
ollama.generate(model='gemma4', prompt='Why is the sky blue?')
|
||||||
```
|
```
|
||||||
|
|
||||||
### List
|
### List
|
||||||
@@ -199,49 +204,49 @@ ollama.list()
|
|||||||
### Show
|
### Show
|
||||||
|
|
||||||
```python
|
```python
|
||||||
ollama.show('gemma3')
|
ollama.show('gemma4')
|
||||||
```
|
```
|
||||||
|
|
||||||
### Create
|
### Create
|
||||||
|
|
||||||
```python
|
```python
|
||||||
ollama.create(model='example', from_='gemma3', system="You are Mario from Super Mario Bros.")
|
ollama.create(model='example', from_='gemma4', system='You are Mario from Super Mario Bros.')
|
||||||
```
|
```
|
||||||
|
|
||||||
### Copy
|
### Copy
|
||||||
|
|
||||||
```python
|
```python
|
||||||
ollama.copy('gemma3', 'user/gemma3')
|
ollama.copy('gemma4', 'user/gemma4')
|
||||||
```
|
```
|
||||||
|
|
||||||
### Delete
|
### Delete
|
||||||
|
|
||||||
```python
|
```python
|
||||||
ollama.delete('gemma3')
|
ollama.delete('gemma4')
|
||||||
```
|
```
|
||||||
|
|
||||||
### Pull
|
### Pull
|
||||||
|
|
||||||
```python
|
```python
|
||||||
ollama.pull('gemma3')
|
ollama.pull('gemma4')
|
||||||
```
|
```
|
||||||
|
|
||||||
### Push
|
### Push
|
||||||
|
|
||||||
```python
|
```python
|
||||||
ollama.push('user/gemma3')
|
ollama.push('user/gemma4')
|
||||||
```
|
```
|
||||||
|
|
||||||
### Embed
|
### Embed
|
||||||
|
|
||||||
```python
|
```python
|
||||||
ollama.embed(model='gemma3', input='The sky is blue because of rayleigh scattering')
|
ollama.embed(model='gemma4', input='The sky is blue because of rayleigh scattering')
|
||||||
```
|
```
|
||||||
|
|
||||||
### Embed (batch)
|
### Embed (batch)
|
||||||
|
|
||||||
```python
|
```python
|
||||||
ollama.embed(model='gemma3', input=['The sky is blue because of rayleigh scattering', 'Grass is green because of chlorophyll'])
|
ollama.embed(model='gemma4', input=['The sky is blue because of rayleigh scattering', 'Grass is green because of chlorophyll'])
|
||||||
```
|
```
|
||||||
|
|
||||||
### Ps
|
### Ps
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ async def main():
|
|||||||
]
|
]
|
||||||
|
|
||||||
client = AsyncClient()
|
client = AsyncClient()
|
||||||
response = await client.chat('gemma3', messages=messages)
|
response = await client.chat('gemma4', messages=messages)
|
||||||
print(response['message']['content'])
|
print(response['message']['content'])
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import ollama
|
|||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
client = ollama.AsyncClient()
|
client = ollama.AsyncClient()
|
||||||
response = await client.generate('gemma3', 'Why is the sky blue?')
|
response = await client.generate('gemma4', 'Why is the sky blue?')
|
||||||
print(response['response'])
|
print(response['response'])
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ messages = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
response = ollama.chat(
|
response = ollama.chat(
|
||||||
model='gemma3',
|
model='gemma4',
|
||||||
messages=messages,
|
messages=messages,
|
||||||
logprobs=True,
|
logprobs=True,
|
||||||
top_logprobs=3,
|
top_logprobs=3,
|
||||||
|
|||||||
@@ -7,5 +7,5 @@ messages = [
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
for part in chat('gemma3', messages=messages, stream=True):
|
for part in chat('gemma4', messages=messages, stream=True):
|
||||||
print(part['message']['content'], end='', flush=True)
|
print(part['message']['content'], end='', flush=True)
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ messages = [
|
|||||||
while True:
|
while True:
|
||||||
user_input = input('Chat with history: ')
|
user_input = input('Chat with history: ')
|
||||||
response = chat(
|
response = chat(
|
||||||
'gemma3',
|
'gemma4',
|
||||||
messages=[*messages, {'role': 'user', 'content': user_input}],
|
messages=[*messages, {'role': 'user', 'content': user_input}],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -7,5 +7,5 @@ messages = [
|
|||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
response = chat('gemma3', messages=messages)
|
response = chat('gemma4', messages=messages)
|
||||||
print(response['message']['content'])
|
print(response['message']['content'])
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@ from ollama import Client
|
|||||||
client = Client()
|
client = Client()
|
||||||
response = client.create(
|
response = client.create(
|
||||||
model='my-assistant',
|
model='my-assistant',
|
||||||
from_='gemma3',
|
from_='gemma4',
|
||||||
system='You are mario from Super Mario Bros.',
|
system='You are mario from Super Mario Bros.',
|
||||||
stream=False,
|
stream=False,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ def print_logprobs(logprobs: Iterable[dict], label: str) -> None:
|
|||||||
|
|
||||||
|
|
||||||
response = ollama.generate(
|
response = ollama.generate(
|
||||||
model='gemma3',
|
model='gemma4',
|
||||||
prompt='hi! be concise.',
|
prompt='hi! be concise.',
|
||||||
logprobs=True,
|
logprobs=True,
|
||||||
top_logprobs=3,
|
top_logprobs=3,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from ollama import generate
|
from ollama import generate
|
||||||
|
|
||||||
for part in generate('gemma3', 'Why is the sky blue?', stream=True):
|
for part in generate('gemma4', 'Why is the sky blue?', stream=True):
|
||||||
print(part['response'], end='', flush=True)
|
print(part['response'], end='', flush=True)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from ollama import generate
|
from ollama import generate
|
||||||
|
|
||||||
response = generate('gemma3', 'Why is the sky blue?')
|
response = generate('gemma4', 'Why is the sky blue?')
|
||||||
print(response['response'])
|
print(response['response'])
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ path = input('Please enter the path to the image: ')
|
|||||||
# img = Path(path).read_bytes()
|
# img = Path(path).read_bytes()
|
||||||
|
|
||||||
response = chat(
|
response = chat(
|
||||||
model='gemma3',
|
model='gemma4',
|
||||||
messages=[
|
messages=[
|
||||||
{
|
{
|
||||||
'role': 'user',
|
'role': 'user',
|
||||||
|
|||||||
+2
-2
@@ -1,7 +1,7 @@
|
|||||||
from ollama import ProcessResponse, chat, ps, pull
|
from ollama import ProcessResponse, chat, ps, pull
|
||||||
|
|
||||||
# Ensure at least one model is loaded
|
# Ensure at least one model is loaded
|
||||||
response = pull('gemma3', stream=True)
|
response = pull('gemma4', stream=True)
|
||||||
progress_states = set()
|
progress_states = set()
|
||||||
for progress in response:
|
for progress in response:
|
||||||
if progress.get('status') in progress_states:
|
if progress.get('status') in progress_states:
|
||||||
@@ -12,7 +12,7 @@ for progress in response:
|
|||||||
print('\n')
|
print('\n')
|
||||||
|
|
||||||
print('Waiting for model to load... \n')
|
print('Waiting for model to load... \n')
|
||||||
chat(model='gemma3', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}])
|
chat(model='gemma4', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}])
|
||||||
|
|
||||||
|
|
||||||
response: ProcessResponse = ps()
|
response: ProcessResponse = ps()
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@ from tqdm import tqdm
|
|||||||
from ollama import pull
|
from ollama import pull
|
||||||
|
|
||||||
current_digest, bars = '', {}
|
current_digest, bars = '', {}
|
||||||
for progress in pull('gemma3', stream=True):
|
for progress in pull('gemma4', stream=True):
|
||||||
digest = progress.get('digest', '')
|
digest = progress.get('digest', '')
|
||||||
if digest != current_digest and current_digest in bars:
|
if digest != current_digest and current_digest in bars:
|
||||||
bars[current_digest].close()
|
bars[current_digest].close()
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
from ollama import ShowResponse, show
|
from ollama import ShowResponse, show
|
||||||
|
|
||||||
response: ShowResponse = show('gemma3')
|
response: ShowResponse = show('gemma4')
|
||||||
print('Model Information:')
|
print('Model Information:')
|
||||||
print(f'Modified at: {response.modified_at}')
|
print(f'Modified at: {response.modified_at}')
|
||||||
print(f'Template: {response.template}')
|
print(f'Template: {response.template}')
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ if not path.exists():
|
|||||||
|
|
||||||
# Set up chat as usual
|
# Set up chat as usual
|
||||||
response = chat(
|
response = chat(
|
||||||
model='gemma3',
|
model='gemma4',
|
||||||
format=ImageDescription.model_json_schema(), # Pass in the schema for the response
|
format=ImageDescription.model_json_schema(), # Pass in the schema for the response
|
||||||
messages=[
|
messages=[
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user