From e48ce1360317d9602098e92ec75d44cad4375001 Mon Sep 17 00:00:00 2001 From: royjhan Date: Thu, 11 Jul 2024 13:46:28 -0700 Subject: [PATCH 1/4] api/embed --- examples/ps/show.py | 20 ++++++++++++++++++ ollama/__init__.py | 8 ++++++-- ollama/_client.py | 50 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 examples/ps/show.py diff --git a/examples/ps/show.py b/examples/ps/show.py new file mode 100644 index 00000000..41ebc5a2 --- /dev/null +++ b/examples/ps/show.py @@ -0,0 +1,20 @@ +from ollama import show, pull + +import json + +def prettify_json(data): + pretty_json = json.dumps(data, indent=4) + return pretty_json + +response = pull('llava', 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 = show('llava') +print(prettify_json(response)) diff --git a/ollama/__init__.py b/ollama/__init__.py index 80b08585..c9c3a497 100644 --- a/ollama/__init__.py +++ b/ollama/__init__.py @@ -21,7 +21,7 @@ 'ResponseError', 'generate', 'chat', - 'embeddings', + 'embed', 'pull', 'push', 'create', @@ -30,13 +30,14 @@ 'copy', 'show', 'ps', + 'embeddings', ] _client = Client() generate = _client.generate chat = _client.chat -embeddings = _client.embeddings +embed = _client.embed pull = _client.pull push = _client.push create = _client.create @@ -45,3 +46,6 @@ copy = _client.copy show = _client.show ps = _client.ps + +# Legacy +embeddings = _client.embeddings diff --git a/ollama/_client.py b/ollama/_client.py index 1109aeed..2a8afbc1 100644 --- a/ollama/_client.py +++ b/ollama/_client.py @@ -245,6 +245,30 @@ def chat( stream=stream, ) + def embed( + self, + model: str = '', + input: Union[str, Sequence[AnyStr]] = '', + truncate: bool = True, + options: Optional[Options] = None, + keep_alive: Optional[Union[float, str]] = None, + ) -> Mapping[str, Any]: + + if not model: + raise RequestError('must provide a model') + + return self._request( + 'POST', + '/api/embed', + json={ + 'model': model, + 'input': input, + 'truncate': truncate, + 'options': options or {}, + 'keep_alive': keep_alive, + }, + ).json() + def embeddings( self, model: str = '', @@ -637,6 +661,32 @@ async def chat( }, stream=stream, ) + + async def embed( + self, + model: str = '', + input: Union[str, Sequence[AnyStr]] = '', + truncate: bool = True, + options: Optional[Options] = None, + keep_alive: Optional[Union[float, str]] = None, + ) -> Mapping[str, Any]: + + if not model: + raise RequestError('must provide a model') + + response = await self._request( + 'POST', + '/api/embed', + json={ + 'model': model, + 'input': input, + 'truncate': truncate, + 'options': options or {}, + 'keep_alive': keep_alive, + }, + ) + + return response.json() async def embeddings( self, From f68ecc583a6809eba3101bc85253f0827ba73a87 Mon Sep 17 00:00:00 2001 From: royjhan Date: Thu, 11 Jul 2024 13:58:14 -0700 Subject: [PATCH 2/4] api/embed --- examples/ps/show.py | 6 ++++-- ollama/_client.py | 4 +--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/ps/show.py b/examples/ps/show.py index 41ebc5a2..da875bd9 100644 --- a/examples/ps/show.py +++ b/examples/ps/show.py @@ -2,9 +2,11 @@ import json + def prettify_json(data): - pretty_json = json.dumps(data, indent=4) - return pretty_json + pretty_json = json.dumps(data, indent=4) + return pretty_json + response = pull('llava', stream=True) progress_states = set() diff --git a/ollama/_client.py b/ollama/_client.py index 2a8afbc1..ffb06129 100644 --- a/ollama/_client.py +++ b/ollama/_client.py @@ -253,7 +253,6 @@ def embed( options: Optional[Options] = None, keep_alive: Optional[Union[float, str]] = None, ) -> Mapping[str, Any]: - if not model: raise RequestError('must provide a model') @@ -661,7 +660,7 @@ async def chat( }, stream=stream, ) - + async def embed( self, model: str = '', @@ -670,7 +669,6 @@ async def embed( options: Optional[Options] = None, keep_alive: Optional[Union[float, str]] = None, ) -> Mapping[str, Any]: - if not model: raise RequestError('must provide a model') From 2a4e5ec07e0a028b3b7ea0fa1464afc1dd8e8511 Mon Sep 17 00:00:00 2001 From: royjhan Date: Thu, 11 Jul 2024 14:02:53 -0700 Subject: [PATCH 3/4] api/embed --- examples/ps/show.py | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 examples/ps/show.py diff --git a/examples/ps/show.py b/examples/ps/show.py deleted file mode 100644 index da875bd9..00000000 --- a/examples/ps/show.py +++ /dev/null @@ -1,22 +0,0 @@ -from ollama import show, pull - -import json - - -def prettify_json(data): - pretty_json = json.dumps(data, indent=4) - return pretty_json - - -response = pull('llava', 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 = show('llava') -print(prettify_json(response)) From 35174ea9757f9d54145585f42fcbfec0c908a52f Mon Sep 17 00:00:00 2001 From: royjhan Date: Wed, 17 Jul 2024 10:34:45 -0700 Subject: [PATCH 4/4] rm legacy --- ollama/__init__.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ollama/__init__.py b/ollama/__init__.py index c9c3a497..c452f710 100644 --- a/ollama/__init__.py +++ b/ollama/__init__.py @@ -22,6 +22,7 @@ 'generate', 'chat', 'embed', + 'embeddings', 'pull', 'push', 'create', @@ -30,7 +31,6 @@ 'copy', 'show', 'ps', - 'embeddings', ] _client = Client() @@ -38,6 +38,7 @@ generate = _client.generate chat = _client.chat embed = _client.embed +embeddings = _client.embeddings pull = _client.pull push = _client.push create = _client.create @@ -46,6 +47,3 @@ copy = _client.copy show = _client.show ps = _client.ps - -# Legacy -embeddings = _client.embeddings