Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull Main #1

Merged
merged 82 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
09ae36a
doc(README) : add prerequisites
adriens Jan 31, 2024
70e3763
Update README.md
adriens Feb 1, 2024
e28f43f
Update README.md
adriens Feb 15, 2024
42f9f70
modified embeddings return annotation
N-Vlahovic Apr 5, 2024
dc9543b
fix 'content' check on messages in chat()
DSLstandard Apr 15, 2024
426b30e
Bump ruff from 0.3.4 to 0.4.1
dependabot[bot] Apr 19, 2024
d38fe12
Merge pull request #114 from N-Vlahovic/n-vlahovic/20240405-annotatio…
mxyng Apr 25, 2024
5b94a8d
Merge pull request #118 from DSLstandard/fix-content-check
mxyng Apr 25, 2024
029b080
Merge pull request #124 from ollama/dependabot/pip/ruff-0.4.1
dependabot[bot] Apr 25, 2024
e1b0600
Bump pillow from 10.2.0 to 10.3.0
dependabot[bot] Apr 25, 2024
30090c5
Add py.typed to mark the library as typed
davep Apr 25, 2024
e403d74
Merge pull request #110 from ollama/dependabot/pip/pillow-10.3.0
dependabot[bot] Apr 25, 2024
fed2b2e
Update README.md link still point legacy url
veinkr Apr 27, 2024
e5c4799
Update README.md
TitanStar73 Apr 28, 2024
30f762a
Bump pytest from 8.1.1 to 8.2.0
dependabot[bot] Apr 29, 2024
c4931b2
Ensure that py.typed gets packaged and distributed
davep Apr 30, 2024
2ac751f
fix: annotation in AsyncClient.embedding
jingfelix May 3, 2024
5cf83dc
Bump ruff from 0.4.1 to 0.4.3
dependabot[bot] May 6, 2024
85f5263
Merge pull request #45 from adriens/patch-1
mxyng May 8, 2024
8b91188
Merge pull request #148 from ollama/dependabot/pip/ruff-0.4.3
dependabot[bot] May 8, 2024
cc1fca0
Merge pull request #145 from jingfelix/fix/async-client-embed-annotation
mxyng May 8, 2024
2b66ade
Merge pull request #140 from ollama/dependabot/pip/pytest-8.2.0
dependabot[bot] May 8, 2024
a8cb34e
Merge pull request #138 from TitanStar73/patch-1
mxyng May 8, 2024
00eafed
Merge pull request #135 from veinkr/main
mxyng May 8, 2024
96214c0
remove old options
mxyng May 9, 2024
eaab477
add done reason to generate type responses
BruceMacD May 9, 2024
3b69774
Merge pull request #152 from ollama/mxyng/rm-options
mxyng May 10, 2024
309007d
add quantization to create requests
mxyng May 7, 2024
5dc857e
Merge pull request #150 from ollama/mxyng/quantization
mxyng May 10, 2024
cb81f52
Merge pull request #154 from ollama/done-reason
mxyng May 10, 2024
0f8c20a
Avoid side effects if chat message contains images
tillfalko May 18, 2024
0824044
---
dependabot[bot] May 20, 2024
4fb909d
Bump ruff from 0.4.3 to 0.4.7
dependabot[bot] May 31, 2024
b45848a
Bump pytest from 8.2.0 to 8.2.2
dependabot[bot] Jun 4, 2024
14d7f8c
Merge pull request #172 from ollama/dependabot/pip/ruff-0.4.7
dependabot[bot] Jun 5, 2024
d5316d0
Python API PS (#177)
royjhan Jun 5, 2024
74db547
Merge pull request #162 from ollama/dependabot/pip/pytest-asyncio-0.23.7
dependabot[bot] Jun 5, 2024
57c597b
Merge pull request #158 from tillfalko/main
mxyng Jun 5, 2024
04d102b
Merge pull request #178 from ollama/dependabot/pip/pytest-8.2.2
dependabot[bot] Jun 5, 2024
fa7bf7c
Revert explicit inclusion of py.typed
davep Jun 5, 2024
d25c4aa
Merge pull request #129 from davep/mark-as-typed
mxyng Jun 5, 2024
982d65f
Simple Example (#179)
royjhan Jun 18, 2024
ce56f27
Add type overloads to methods (#181)
royjhan Jun 19, 2024
1a15742
Update README.md
jmorganca Jun 22, 2024
49568d5
Bump pillow from 10.3.0 to 10.4.0
dependabot[bot] Jul 1, 2024
222c207
Bump ruff from 0.4.7 to 0.5.2
dependabot[bot] Jul 15, 2024
359c63d
integrate tool calls (#213)
joshyan1 Jul 17, 2024
2cb796c
Bump pytest-asyncio from 0.23.7 to 0.23.8
dependabot[bot] Jul 17, 2024
b0ea6d9
Support `api/embed` (#208)
royjhan Jul 18, 2024
33c4b61
add insert support to generate endpoint (#215)
royjhan Jul 18, 2024
a49c986
Merge pull request #203 from ollama/dependabot/pip/pillow-10.4.0
dependabot[bot] Jul 18, 2024
b498ab3
Merge pull request #211 from ollama/dependabot/pip/ruff-0.5.2
dependabot[bot] Jul 18, 2024
f62eb97
Merge pull request #217 from ollama/dependabot/pip/pytest-asyncio-0.23.8
dependabot[bot] Jul 18, 2024
6025c36
Bump pytest-httpserver from 1.0.10 to 1.0.12
dependabot[bot] Jul 22, 2024
8e3a8fe
Bump ruff from 0.5.2 to 0.5.5
dependabot[bot] Jul 25, 2024
ab7e600
Bump pytest from 8.2.2 to 8.3.2
dependabot[bot] Jul 25, 2024
60d292a
update to `llama3.1` (#237)
jmorganca Jul 29, 2024
ae2bdbf
Merge pull request #231 from ollama/dependabot/pip/pytest-8.3.2
dependabot[bot] Jul 29, 2024
d2da64f
Merge pull request #230 from ollama/dependabot/pip/ruff-0.5.5
dependabot[bot] Jul 29, 2024
cd4fbfc
Merge pull request #224 from ollama/dependabot/pip/pytest-httpserver-…
dependabot[bot] Jul 29, 2024
cbf0887
fix: update name of toolcallfunction parameter (#236)
zeelrupapara Jul 29, 2024
8b694bb
Update `Message` annotations to support tool calls (#227)
Shulyaka Jul 29, 2024
10d0ff2
Bump pytest-httpserver from 1.0.12 to 1.1.0
dependabot[bot] Aug 12, 2024
9e6726e
Bump pytest-asyncio from 0.23.8 to 0.24.0
dependabot[bot] Aug 22, 2024
dfdeb7c
Add URL path to client URL in in Client._parse_host() (#170)
bplunkert Aug 23, 2024
e220e46
Merge pull request #252 from ollama/dependabot/pip/pytest-httpserver-…
dependabot[bot] Aug 27, 2024
9f2832d
Merge pull request #260 from ollama/dependabot/pip/pytest-asyncio-0.24.0
dependabot[bot] Aug 27, 2024
9c34d81
Bump ruff from 0.5.5 to 0.6.2
dependabot[bot] Aug 27, 2024
981015c
Merge pull request #261 from ollama/dependabot/pip/ruff-0.6.2
dependabot[bot] Aug 27, 2024
d98f646
IPv6 support (#262)
jbinder Aug 27, 2024
5f51129
Bump ruff from 0.6.2 to 0.6.3
dependabot[bot] Aug 29, 2024
81edab1
_stream in async client raises RuntimeError processing HTTP errors (#…
Oneirag Aug 29, 2024
89e8b74
Merge pull request #267 from ollama/dependabot/pip/ruff-0.6.3
dependabot[bot] Aug 30, 2024
ccf8af3
Bump pytest from 8.3.2 to 8.3.3
dependabot[bot] Sep 10, 2024
eff2afd
update docs
pdevine Sep 12, 2024
a26537c
update docs
royjhan Jul 17, 2024
1ec88ed
Merge pull request #216 from ollama/royh/embed-docs
pdevine Sep 13, 2024
7fda5c9
Bump ruff from 0.6.3 to 0.6.5
dependabot[bot] Sep 13, 2024
61c8d0d
add basic delete/copy tests (#275)
mxyng Sep 18, 2024
f3e72b6
update pyproject.toml (#284)
mxyng Sep 18, 2024
9bcd0d6
Merge pull request #278 from ollama/dependabot/pip/pytest-8.3.3
dependabot[bot] Sep 19, 2024
ebe332b
Merge pull request #281 from ollama/dependabot/pip/ruff-0.6.5
dependabot[bot] Sep 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Ollama Python Library

The Ollama Python library provides the easiest way to integrate Python 3.8+ projects with [Ollama](https://github.com/jmorganca/ollama).
The Ollama Python library provides the easiest way to integrate Python 3.8+ projects with [Ollama](https://github.com/ollama/ollama).

## Install

Expand All @@ -12,7 +12,7 @@ pip install ollama

```python
import ollama
response = ollama.chat(model='llama2', messages=[
response = ollama.chat(model='llama3.1', messages=[
{
'role': 'user',
'content': 'Why is the sky blue?',
Expand All @@ -29,7 +29,7 @@ Response streaming can be enabled by setting `stream=True`, modifying function c
import ollama

stream = ollama.chat(
model='llama2',
model='llama3.1',
messages=[{'role': 'user', 'content': 'Why is the sky blue?'}],
stream=True,
)
Expand All @@ -40,18 +40,18 @@ for chunk in stream:

## API

The Ollama Python library's API is designed around the [Ollama REST API](https://github.com/jmorganca/ollama/blob/main/docs/api.md)
The Ollama Python library's API is designed around the [Ollama REST API](https://github.com/ollama/ollama/blob/main/docs/api.md)

### Chat

```python
ollama.chat(model='llama2', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}])
ollama.chat(model='llama3.1', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}])
```

### Generate

```python
ollama.generate(model='llama2', prompt='Why is the sky blue?')
ollama.generate(model='llama3.1', prompt='Why is the sky blue?')
```

### List
Expand All @@ -63,14 +63,14 @@ ollama.list()
### Show

```python
ollama.show('llama2')
ollama.show('llama3.1')
```

### Create

```python
modelfile='''
FROM llama2
FROM llama3.1
SYSTEM You are mario from super mario bros.
'''

Expand All @@ -80,31 +80,43 @@ ollama.create(model='example', modelfile=modelfile)
### Copy

```python
ollama.copy('llama2', 'user/llama2')
ollama.copy('llama3.1', 'user/llama3.1')
```

### Delete

```python
ollama.delete('llama2')
ollama.delete('llama3.1')
```

### Pull

```python
ollama.pull('llama2')
ollama.pull('llama3.1')
```

### Push

```python
ollama.push('user/llama2')
ollama.push('user/llama3.1')
```

### Embeddings
### Embed

```python
ollama.embeddings(model='llama2', prompt='The sky is blue because of rayleigh scattering')
ollama.embed(model='llama3.1', input='The sky is blue because of rayleigh scattering')
```

### Embed (batch)

```python
ollama.embed(model='llama3.1', input=['The sky is blue because of rayleigh scattering', 'Grass is green because of chlorophyll'])
```

### Ps

```python
ollama.ps()
```

## Custom client
Expand All @@ -117,7 +129,7 @@ A custom client can be created with the following fields:
```python
from ollama import Client
client = Client(host='http://localhost:11434')
response = client.chat(model='llama2', messages=[
response = client.chat(model='llama3.1', messages=[
{
'role': 'user',
'content': 'Why is the sky blue?',
Expand All @@ -133,7 +145,7 @@ from ollama import AsyncClient

async def chat():
message = {'role': 'user', 'content': 'Why is the sky blue?'}
response = await AsyncClient().chat(model='llama2', messages=[message])
response = await AsyncClient().chat(model='llama3.1', messages=[message])

asyncio.run(chat())
```
Expand All @@ -146,7 +158,7 @@ from ollama import AsyncClient

async def chat():
message = {'role': 'user', 'content': 'Why is the sky blue?'}
async for part in await AsyncClient().chat(model='llama2', messages=[message], stream=True):
async for part in await AsyncClient().chat(model='llama3.1', messages=[message], stream=True):
print(part['message']['content'], end='', flush=True)

asyncio.run(chat())
Expand Down
6 changes: 3 additions & 3 deletions examples/fill-in-middle/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from ollama import generate

prefix = '''def remove_non_ascii(s: str) -> str:
prompt = '''def remove_non_ascii(s: str) -> str:
""" '''

suffix = """
return result
"""


response = generate(
model='codellama:7b-code',
prompt=f'<PRE> {prefix} <SUF>{suffix} <MID>',
prompt=prompt,
suffix=suffix,
options={
'num_predict': 128,
'temperature': 0,
Expand Down
31 changes: 31 additions & 0 deletions examples/ps/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from ollama import ps, pull, chat

response = pull('mistral', stream=True)
progress_states = set()
for progress in response:
if progress.get('status') in progress_states:
continue
progress_states.add(progress.get('status'))
print(progress.get('status'))

print('\n')

response = chat('mistral', messages=[{'role': 'user', 'content': 'Hello!'}])
print(response['message']['content'])

print('\n')

response = ps()

name = response['models'][0]['name']
size = response['models'][0]['size']
size_vram = response['models'][0]['size_vram']

if size == size_vram:
print(f'{name}: 100% GPU')
elif not size_vram:
print(f'{name}: 100% CPU')
else:
size_cpu = size - size_vram
cpu_percent = round(size_cpu / size * 100)
print(f'{name}: {cpu_percent}% CPU/{100 - cpu_percent}% GPU')
3 changes: 3 additions & 0 deletions examples/tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# tools

This example demonstrates how to utilize tool calls with an asynchronous Ollama client and the chat endpoint.
87 changes: 87 additions & 0 deletions examples/tools/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import json
import ollama
import asyncio


# Simulates an API call to get flight times
# In a real application, this would fetch data from a live database or API
def get_flight_times(departure: str, arrival: str) -> str:
flights = {
'NYC-LAX': {'departure': '08:00 AM', 'arrival': '11:30 AM', 'duration': '5h 30m'},
'LAX-NYC': {'departure': '02:00 PM', 'arrival': '10:30 PM', 'duration': '5h 30m'},
'LHR-JFK': {'departure': '10:00 AM', 'arrival': '01:00 PM', 'duration': '8h 00m'},
'JFK-LHR': {'departure': '09:00 PM', 'arrival': '09:00 AM', 'duration': '7h 00m'},
'CDG-DXB': {'departure': '11:00 AM', 'arrival': '08:00 PM', 'duration': '6h 00m'},
'DXB-CDG': {'departure': '03:00 AM', 'arrival': '07:30 AM', 'duration': '7h 30m'},
}

key = f'{departure}-{arrival}'.upper()
return json.dumps(flights.get(key, {'error': 'Flight not found'}))


async def run(model: str):
client = ollama.AsyncClient()
# Initialize conversation with a user query
messages = [{'role': 'user', 'content': 'What is the flight time from New York (NYC) to Los Angeles (LAX)?'}]

# First API call: Send the query and function description to the model
response = await client.chat(
model=model,
messages=messages,
tools=[
{
'type': 'function',
'function': {
'name': 'get_flight_times',
'description': 'Get the flight times between two cities',
'parameters': {
'type': 'object',
'properties': {
'departure': {
'type': 'string',
'description': 'The departure city (airport code)',
},
'arrival': {
'type': 'string',
'description': 'The arrival city (airport code)',
},
},
'required': ['departure', 'arrival'],
},
},
},
],
)

# Add the model's response to the conversation history
messages.append(response['message'])

# Check if the model decided to use the provided function
if not response['message'].get('tool_calls'):
print("The model didn't use the function. Its response was:")
print(response['message']['content'])
return

# Process function calls made by the model
if response['message'].get('tool_calls'):
available_functions = {
'get_flight_times': get_flight_times,
}
for tool in response['message']['tool_calls']:
function_to_call = available_functions[tool['function']['name']]
function_response = function_to_call(tool['function']['arguments']['departure'], tool['function']['arguments']['arrival'])
# Add function response to the conversation
messages.append(
{
'role': 'tool',
'content': function_response,
}
)

# Second API call: Get final response from the model
final_response = await client.chat(model=model, messages=messages)
print(final_response['message']['content'])


# Run the async function
asyncio.run(run('mistral'))
4 changes: 4 additions & 0 deletions ollama/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'ResponseError',
'generate',
'chat',
'embed',
'embeddings',
'pull',
'push',
Expand All @@ -29,12 +30,14 @@
'list',
'copy',
'show',
'ps',
]

_client = Client()

generate = _client.generate
chat = _client.chat
embed = _client.embed
embeddings = _client.embeddings
pull = _client.pull
push = _client.push
Expand All @@ -43,3 +46,4 @@
list = _client.list
copy = _client.copy
show = _client.show
ps = _client.ps
Loading