ollama / ollama-python

Ollama Python library
https://ollama.com
MIT License
4.09k stars 344 forks source link

httpx.ConnectError: [Errno 111] Connection refused #12

Closed asmith26 closed 8 months ago

asmith26 commented 8 months ago

Hi there,

I'm trying to run simple-chat-stream example, but unfortunately I'm geeting

Traceback (most recent call last):
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpcore/_exceptions.py", line 10, in map_exceptions
    yield
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpcore/_backends/sync.py", line 206, in connect_tcp
    sock = socket.create_connection(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/mambaforge/envs/panel-apps/lib/python3.11/socket.py", line 851, in create_connection
    raise exceptions[0]
  File "~/mambaforge/envs/panel-apps/lib/python3.11/socket.py", line 836, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpx/_transports/default.py", line 66, in map_httpcore_exceptions
    yield
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpx/_transports/default.py", line 228, in handle_request
    resp = self._pool.handle_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py", line 268, in handle_request
    raise exc
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py", line 251, in handle_request
    response = connection.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpcore/_sync/connection.py", line 99, in handle_request
    raise exc
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpcore/_sync/connection.py", line 76, in handle_request
    stream = self._connect(request)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpcore/_sync/connection.py", line 124, in _connect
    stream = self._network_backend.connect_tcp(**kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpcore/_backends/sync.py", line 205, in connect_tcp
    with map_exceptions(exc_map):
  File "~/mambaforge/envs/panel-apps/lib/python3.11/contextlib.py", line 158, in __exit__
    self.gen.throw(typ, value, traceback)
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.ConnectError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "~/git/panel-apps/main.py", line 11, in <module>
    for part in chat('mistral', messages=messages, stream=True):
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/ollama/_client.py", line 64, in _stream
    with self._client.stream(method, url, **kwargs) as r:
  File "~/mambaforge/envs/panel-apps/lib/python3.11/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpx/_client.py", line 857, in stream
    response = self.send(
               ^^^^^^^^^^
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpx/_client.py", line 901, in send
    response = self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpx/_client.py", line 929, in _send_handling_auth
    response = self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpx/_client.py", line 966, in _send_handling_redirects
    response = self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpx/_client.py", line 1002, in _send_single_request
    response = transport.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpx/_transports/default.py", line 227, in handle_request
    with map_httpcore_exceptions():
  File "~/mambaforge/envs/panel-apps/lib/python3.11/contextlib.py", line 158, in __exit__
    self.gen.throw(typ, value, traceback)
  File "~/mambaforge/envs/panel-apps/lib/python3.11/site-packages/httpx/_transports/default.py", line 83, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ConnectError: [Errno 111] Connection refused

I'm using ollama = 0.1.2 and have tried running ollama.pull("mistral").

Many thanks for any help!

asmith26 commented 8 months ago

Ah, I've just realised I need to run ollama in the background.

I'll close this ticket - let me know if I can with something like add this to the README or enhancing the error message with this info.

Many thanks for these amazing libs!

abisheka-pitumpe commented 6 months ago

How do I run ollama in the background in a python environment? I know that ollama serve works on CLI, I am getting the same Error 111 in an environment where I can only use ollama-python and not CLI.

ValeWalker27 commented 5 months ago

How do I run ollama in the background in a python environment? I know that ollama serve works on CLI, I am getting the same Error 111 in an environment where I can only use ollama-python and not CLI.

I'm not an expert but may be you can try running a Custom client and refer to it in your said environment. Ref: https://ollama.com/blog/python-javascript-libraries

ollama = Client(host='my.ollama.host')

Hope it helps.