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

client: expose resource cleanup methods #444

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 14 additions & 1 deletion ollama/_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import ipaddress
import json
import os
Expand Down Expand Up @@ -70,7 +71,7 @@
T = TypeVar('T')


class BaseClient:
class BaseClient(contextlib.AbstractContextManager, contextlib.AbstractAsyncContextManager):
def __init__(
self,
client,
Expand Down Expand Up @@ -105,6 +106,12 @@ def __init__(
**kwargs,
)

def __exit__(self, exc_type, exc_val, exc_tb):
self.close()

async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.close()


CONNECTION_ERROR_MESSAGE = 'Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download'

Expand All @@ -113,6 +120,9 @@ class Client(BaseClient):
def __init__(self, host: Optional[str] = None, **kwargs) -> None:
super().__init__(httpx.Client, host, **kwargs)

def close(self):
self._client.close()

def _request_raw(self, *args, **kwargs):
try:
r = self._client.request(*args, **kwargs)
Expand Down Expand Up @@ -617,6 +627,9 @@ class AsyncClient(BaseClient):
def __init__(self, host: Optional[str] = None, **kwargs) -> None:
super().__init__(httpx.AsyncClient, host, **kwargs)

async def close(self):
await self._client.aclose()

async def _request_raw(self, *args, **kwargs):
try:
r = await self._client.request(*args, **kwargs)
Expand Down
30 changes: 30 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,3 +1140,33 @@ async def test_async_client_connection_error():
with pytest.raises(ConnectionError) as exc_info:
await client.show('model')
assert str(exc_info.value) == 'Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download'


def test_client_close():
client = Client()
client.close()
assert client._client.is_closed


@pytest.mark.asyncio
async def test_async_client_close():
client = AsyncClient()
await client.close()
assert client._client.is_closed


def test_client_context_manager():
with Client() as client:
assert isinstance(client, Client)
assert not client._client.is_closed

assert client._client.is_closed


@pytest.mark.asyncio
async def test_async_client_context_manager():
async with AsyncClient() as client:
assert isinstance(client, AsyncClient)
assert not client._client.is_closed

assert client._client.is_closed