ollama / ollama-python

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

Add an error message when Ollama server isn't running #120

Open shivakanthsujit opened 2 months ago

shivakanthsujit commented 2 months ago

The Ollama app returns an error message if the connection is refused pointing out that the server might not be running.

https://github.com/ollama/ollama/blob/8645076a71941d78a996e52cff65c794df6cdbcb/cmd/cmd.go#L893

This package does not have a similar error handling. Might be good to add it. Can be done by adding a check after initialising self._client as follows

try:
  response = self._client.head(self._client.base_url)
  response.raise_for_status()
except httpx.ConnectError as e:
  raise ConnectionError("could not connect to ollama app, is it running?")
except Exception as e:
  raise e

I can open a PR for this if you think it is worthwhile.