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

no head request for create blob #277

Merged
merged 1 commit into from
Nov 8, 2024
Merged
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
34 changes: 11 additions & 23 deletions ollama/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,14 +523,8 @@ def _create_blob(self, path: Union[str, Path]) -> str:

digest = f'sha256:{sha256sum.hexdigest()}'

try:
self._request_raw('HEAD', f'/api/blobs/{digest}')
except ResponseError as e:
if e.status_code != 404:
raise

with open(path, 'rb') as r:
self._request_raw('POST', f'/api/blobs/{digest}', content=r)
with open(path, 'rb') as r:
self._request_raw('POST', f'/api/blobs/sha256:{digest}', content=r)

return digest

Expand Down Expand Up @@ -1007,21 +1001,15 @@ async def _create_blob(self, path: Union[str, Path]) -> str:

digest = f'sha256:{sha256sum.hexdigest()}'

try:
await self._request_raw('HEAD', f'/api/blobs/{digest}')
except ResponseError as e:
if e.status_code != 404:
raise

async def upload_bytes():
with open(path, 'rb') as r:
while True:
chunk = r.read(32 * 1024)
if not chunk:
break
yield chunk

await self._request_raw('POST', f'/api/blobs/{digest}', content=upload_bytes())
async def upload_bytes():
with open(path, 'rb') as r:
while True:
chunk = r.read(32 * 1024)
if not chunk:
break
yield chunk

await self._request_raw('POST', f'/api/blobs/{digest}', content=upload_bytes())

return digest

Expand Down
26 changes: 12 additions & 14 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def generate():


def test_client_create_path(httpserver: HTTPServer):
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(
'/api/create',
method='POST',
Expand All @@ -316,7 +316,7 @@ def test_client_create_path(httpserver: HTTPServer):


def test_client_create_path_relative(httpserver: HTTPServer):
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(
'/api/create',
method='POST',
Expand Down Expand Up @@ -348,7 +348,7 @@ def userhomedir():


def test_client_create_path_user_home(httpserver: HTTPServer, userhomedir):
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(
'/api/create',
method='POST',
Expand All @@ -371,7 +371,7 @@ def test_client_create_path_user_home(httpserver: HTTPServer, userhomedir):


def test_client_create_modelfile(httpserver: HTTPServer):
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(
'/api/create',
method='POST',
Expand All @@ -390,7 +390,7 @@ def test_client_create_modelfile(httpserver: HTTPServer):


def test_client_create_modelfile_roundtrip(httpserver: HTTPServer):
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(
'/api/create',
method='POST',
Expand Down Expand Up @@ -455,7 +455,6 @@ def test_client_create_from_library(httpserver: HTTPServer):


def test_client_create_blob(httpserver: HTTPServer):
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=404))
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=201))

client = Client(httpserver.url_for('/'))
Expand All @@ -466,7 +465,7 @@ def test_client_create_blob(httpserver: HTTPServer):


def test_client_create_blob_exists(httpserver: HTTPServer):
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))

client = Client(httpserver.url_for('/'))

Expand Down Expand Up @@ -774,7 +773,7 @@ def generate():

@pytest.mark.asyncio
async def test_async_client_create_path(httpserver: HTTPServer):
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(
'/api/create',
method='POST',
Expand All @@ -798,7 +797,7 @@ async def test_async_client_create_path(httpserver: HTTPServer):

@pytest.mark.asyncio
async def test_async_client_create_path_relative(httpserver: HTTPServer):
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(
'/api/create',
method='POST',
Expand All @@ -822,7 +821,7 @@ async def test_async_client_create_path_relative(httpserver: HTTPServer):

@pytest.mark.asyncio
async def test_async_client_create_path_user_home(httpserver: HTTPServer, userhomedir):
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(
'/api/create',
method='POST',
Expand All @@ -846,7 +845,7 @@ async def test_async_client_create_path_user_home(httpserver: HTTPServer, userho

@pytest.mark.asyncio
async def test_async_client_create_modelfile(httpserver: HTTPServer):
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(
'/api/create',
method='POST',
Expand All @@ -866,7 +865,7 @@ async def test_async_client_create_modelfile(httpserver: HTTPServer):

@pytest.mark.asyncio
async def test_async_client_create_modelfile_roundtrip(httpserver: HTTPServer):
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(
'/api/create',
method='POST',
Expand Down Expand Up @@ -933,7 +932,6 @@ async def test_async_client_create_from_library(httpserver: HTTPServer):

@pytest.mark.asyncio
async def test_async_client_create_blob(httpserver: HTTPServer):
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=404))
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=201))

client = AsyncClient(httpserver.url_for('/'))
Expand All @@ -945,7 +943,7 @@ async def test_async_client_create_blob(httpserver: HTTPServer):

@pytest.mark.asyncio
async def test_async_client_create_blob_exists(httpserver: HTTPServer):
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='HEAD').respond_with_response(Response(status=200))
httpserver.expect_ordered_request(PrefixPattern('/api/blobs/'), method='POST').respond_with_response(Response(status=200))

client = AsyncClient(httpserver.url_for('/'))

Expand Down