Skip to content

Commit 77461b9

Browse files
authored
feat: add retry connection to ollama (#2084)
* feat: add retry connection to ollama When Ollama is running in the docker-compose, traefik is not ready sometimes to route the request, and it fails * fix: mypy
1 parent 4262859 commit 77461b9

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

poetry.lock

Lines changed: 27 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

private_gpt/utils/ollama.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,39 @@
33
from collections.abc import Iterator, Mapping
44
from typing import Any
55

6+
from httpx import ConnectError
67
from tqdm import tqdm # type: ignore
78

9+
from private_gpt.utils.retry import retry
10+
811
try:
9-
from ollama import Client # type: ignore
12+
from ollama import Client, ResponseError # type: ignore
1013
except ImportError as e:
1114
raise ImportError(
1215
"Ollama dependencies not found, install with `poetry install --extras llms-ollama or embeddings-ollama`"
1316
) from e
1417

1518
logger = logging.getLogger(__name__)
1619

20+
_MAX_RETRIES = 5
21+
_JITTER = (3.0, 10.0)
22+
1723

24+
@retry(
25+
is_async=False,
26+
exceptions=(ConnectError, ResponseError),
27+
tries=_MAX_RETRIES,
28+
jitter=_JITTER,
29+
logger=logger,
30+
)
1831
def check_connection(client: Client) -> bool:
1932
try:
2033
client.list()
2134
return True
35+
except (ConnectError, ResponseError) as e:
36+
raise e
2237
except Exception as e:
23-
logger.error(f"Failed to connect to Ollama: {e!s}")
38+
logger.error(f"Failed to connect to Ollama: {type(e).__name__}: {e!s}")
2439
return False
2540

2641

private_gpt/utils/retry.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import logging
2+
from collections.abc import Callable
3+
from typing import Any
4+
5+
from retry_async import retry as retry_untyped # type: ignore
6+
7+
retry_logger = logging.getLogger(__name__)
8+
9+
10+
def retry(
11+
exceptions: Any = Exception,
12+
*,
13+
is_async: bool = False,
14+
tries: int = -1,
15+
delay: float = 0,
16+
max_delay: float | None = None,
17+
backoff: float = 1,
18+
jitter: float | tuple[float, float] = 0,
19+
logger: logging.Logger = retry_logger,
20+
) -> Callable[..., Any]:
21+
wrapped = retry_untyped(
22+
exceptions=exceptions,
23+
is_async=is_async,
24+
tries=tries,
25+
delay=delay,
26+
max_delay=max_delay,
27+
backoff=backoff,
28+
jitter=jitter,
29+
logger=logger,
30+
)
31+
return wrapped # type: ignore

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ ollama = {version ="^0.3.0", optional = true}
6666

6767
# Optional HF Transformers
6868
einops = {version = "^0.8.0", optional = true}
69+
retry-async = "^0.1.4"
6970

7071
[tool.poetry.extras]
7172
ui = ["gradio", "ffmpy"]

0 commit comments

Comments
 (0)