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

import ollama error when os.environ["HTTP_PROXY"] = "socks5h://xxx" #424

Closed
wugifer opened this issue Jan 17, 2025 · 3 comments
Closed

import ollama error when os.environ["HTTP_PROXY"] = "socks5h://xxx" #424

wugifer opened this issue Jan 17, 2025 · 3 comments

Comments

@wugifer
Copy link

wugifer commented Jan 17, 2025

I use ollama in a proxy environment, so os.environ["HTTP_PROXY"] = "socks5h://xxx", But

  1. proxy is only used by other requests functions
  2. ollama connect to http://127.0.0.1 without proxy

I use this code to create ollama client, so I can control the proxy:
client = ollama.Client(host="http://127.0.0.1:11434", trust_env = False)

But in ollama/__init__.py, this code cause error:
_client = Client()

the error is:
ValueError: Unknown scheme for proxy URL URL('socks5h://

cause by this judge:
if url.scheme not in ("http", "https", "socks5"):

So, my question is, could:

  1. ollama support socks5h? or
  2. remove "_client = Client()" by default?
@zdposter
Copy link

If you do not need to use proxy for reaching ollama, set evn. variable for ignoring localhost:

import os
os.environ['NO_PROXY'] = 'localhost,127.0.0.1,::1'

@wugifer
Copy link
Author

wugifer commented Feb 7, 2025

If you do not need to use proxy for reaching ollama, set evn. variable for ignoring localhost:

import os
os.environ['NO_PROXY'] = 'localhost,127.0.0.1,::1'

Thank you, but not work. My Problem is not "not need to use proxy", but "import ollama" error, below is error stack:

File my_code.py:7
----> 7 import ollama

File /usr/local/lib/python3.10/site-packages/ollama/__init__.py:36
      2 from ollama._types import (
      3   GenerateResponse,
      4   ChatResponse,
   (...)
      9   ResponseError,
     10 )
     12 __all__ = [
     13   'Client',
     14   'AsyncClient',
   (...)
     33   'ps',
     34 ]
---> 36 _client = Client()
        #### CODE1 ############################################################################
     38 generate = _client.generate
     39 chat = _client.chat

File /usr/local/lib/python3.10/site-packages/ollama/_client.py:67, in Client.__init__(self, host, **kwargs)
     66 def __init__(self, host: Optional[str] = None, **kwargs) -> None:
---> 67   super().__init__(httpx.Client, host, **kwargs)

File /usr/local/lib/python3.10/site-packages/ollama/_client.py:56, in BaseClient.__init__(self, client, host, follow_redirects, timeout, **kwargs)
     53 headers['Accept'] = 'application/json'
     54 headers['User-Agent'] = f'ollama-python/{__version__} ({platform.machine()} {platform.system().lower()}) Python/{platform.python_version()}'
---> 56 self._client = client(
     57   base_url=_parse_host(host or os.getenv('OLLAMA_HOST')),
     58   follow_redirects=follow_redirects,
     59   timeout=timeout,
     60   headers=headers,
     61   **kwargs,
     62 )

File /usr/local/lib/python3.10/site-packages/httpx/_client.py:683, in Client.__init__(self, auth, params, headers, cookies, verify, cert, http1, http2, proxy, proxies, mounts, timeout, follow_redirects, limits, max_redirects, event_hooks, base_url, transport, app, trust_env, default_encoding)
    680     warnings.warn(message, DeprecationWarning)
    682 allow_env_proxies = trust_env and app is None and transport is None
--> 683 proxy_map = self._get_proxy_map(proxies or proxy, allow_env_proxies)
    685 self._transport = self._init_transport(
    686     verify=verify,
    687     cert=cert,
   (...)
    693     trust_env=trust_env,
    694 )
    695 self._mounts: dict[URLPattern, BaseTransport | None] = {
    696     URLPattern(key): None
    697     if proxy is None
   (...)
    707     for key, proxy in proxy_map.items()
    708 }

File /usr/local/lib/python3.10/site-packages/httpx/_client.py:217, in BaseClient._get_proxy_map(self, proxies, allow_env_proxies)
    215 if proxies is None:
    216     if allow_env_proxies:
--> 217         return {
    218             key: None if url is None else Proxy(url=url)
        #### CODE2 ############################################################################
    219             for key, url in get_environment_proxies().items()
    220         }
    221     return {}
    222 if isinstance(proxies, dict):

File /usr/local/lib/python3.10/site-packages/httpx/_client.py:218, in <dictcomp>(.0)
    215 if proxies is None:
    216     if allow_env_proxies:
    217         return {
--> 218             key: None if url is None else Proxy(url=url)
    219             for key, url in get_environment_proxies().items()
    220         }
    221     return {}
    222 if isinstance(proxies, dict):

File /usr/local/lib/python3.10/site-packages/httpx/_config.py:336, in Proxy.__init__(self, url, ssl_context, auth, headers)
    333 headers = Headers(headers)
    335 if url.scheme not in ("http", "https", "socks5"):
--> 336     raise ValueError(f"Unknown scheme for proxy URL {url!r}")
    338 if url.username or url.password:
    339     # Remove any auth credentials from the URL.
    340     auth = (url.username, url.password)

ValueError: Unknown scheme for proxy URL URL('socks5h://127.0.0.1:1080')

CODE1 call Client() without any args, and CODE2 call Proxy(url="socks5h://xxx") if get_environment_proxies() found one(whether used or not used), then ValueError

CODE1 is only used to set variables generate, chat, ...

_client = Client()

generate = _client.generate
chat = _client.chat
embed = _client.embed
embeddings = _client.embeddings
pull = _client.pull
push = _client.push
create = _client.create
delete = _client.delete
list = _client.list
copy = _client.copy
show = _client.show
ps = _client.ps

change it to

_client = Client(trust_env = False)

will set variables to same result, and no ValueError

thanks

@wugifer
Copy link
Author

wugifer commented Feb 7, 2025

httpx >= 0.28.0 support socks5h

done

@wugifer wugifer closed this as completed Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants