From 8b929ab4964a58a90360d0a6d2d527f245f58891 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Thu, 8 Feb 2024 11:02:24 -0800 Subject: [PATCH 1/2] python user agent add a user agent to ollama-python requests --- ollama/_client.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ollama/_client.py b/ollama/_client.py index c5aa1bc..4db5ab5 100644 --- a/ollama/_client.py +++ b/ollama/_client.py @@ -3,6 +3,7 @@ import json import httpx import binascii +import platform import urllib.parse from os import PathLike from pathlib import Path @@ -18,6 +19,13 @@ else: from collections.abc import Iterator, AsyncIterator +from importlib import metadata + +try: + __version__ = metadata.version('ollama') +except metadata.PackageNotFoundError: + __version__ = '0.0.0' + from ollama._types import Message, Options, RequestError, ResponseError @@ -37,10 +45,15 @@ def __init__( - `timeout`: None `kwargs` are passed to the httpx client. """ + + headers = kwargs.pop('headers', {}) + headers['user-agent'] = f'ollama-python/{__version__} ({platform.machine()} {platform.system().lower()}) Python/{platform.python_version()}' + self._client = client( base_url=_parse_host(host or os.getenv('OLLAMA_HOST')), follow_redirects=follow_redirects, timeout=timeout, + headers=headers, **kwargs, ) From ec8bf88c2b6e62db73b4b4b99c03f968299d7ba2 Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Thu, 8 Feb 2024 11:59:39 -0800 Subject: [PATCH 2/2] add content-type and accept mirrors header values set by ollama cli --- ollama/_client.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ollama/_client.py b/ollama/_client.py index 4db5ab5..607f12c 100644 --- a/ollama/_client.py +++ b/ollama/_client.py @@ -47,7 +47,9 @@ def __init__( """ headers = kwargs.pop('headers', {}) - headers['user-agent'] = f'ollama-python/{__version__} ({platform.machine()} {platform.system().lower()}) Python/{platform.python_version()}' + headers['Content-Type'] = 'application/json' + headers['Accept'] = 'application/json' + headers['User-Agent'] = f'ollama-python/{__version__} ({platform.machine()} {platform.system().lower()}) Python/{platform.python_version()}' self._client = client( base_url=_parse_host(host or os.getenv('OLLAMA_HOST')),