Skip to content

Commit

Permalink
Fix pr #6095: Chore: clean up LLM (prompt caching, supports fn callin…
Browse files Browse the repository at this point in the history
…g), leftover renames
  • Loading branch information
openhands-agent committed Jan 31, 2025
1 parent 5b795d1 commit 92227dc
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
14 changes: 14 additions & 0 deletions openhands/llm/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from litellm.exceptions import APIError


class CloudflareBlockError(APIError):
"""Exception raised when Cloudflare blocks the request."""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def __instancecheck__(self, instance):
return (
super().__instancecheck__(instance)
and 'Attention Required! | Cloudflare' in str(instance)
)
2 changes: 2 additions & 0 deletions openhands/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from openhands.core.logger import openhands_logger as logger
from openhands.core.message import Message
from openhands.llm.debug_mixin import DebugMixin
from openhands.llm.exceptions import CloudflareBlockError
from openhands.llm.fn_call_converter import (
STOP_WORDS,
convert_fncall_messages_to_non_fncall_messages,
Expand Down Expand Up @@ -147,6 +148,7 @@ def __init__(
@self.retry_decorator(
num_retries=self.config.num_retries,
retry_exceptions=LLM_RETRY_EXCEPTIONS,
exclude_exceptions=(CloudflareBlockError,),
retry_min_wait=self.config.retry_min_wait,
retry_max_wait=self.config.retry_max_wait,
retry_multiplier=self.config.retry_multiplier,
Expand Down
10 changes: 5 additions & 5 deletions openhands/llm/retry_mixin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from litellm.exceptions import APIError
from tenacity import (
retry,
retry_if_exception,
Expand All @@ -19,23 +18,24 @@ def retry_decorator(self, **kwargs):
Args:
**kwargs: Keyword arguments to override default retry behavior.
Keys: num_retries, retry_exceptions, retry_min_wait, retry_max_wait, retry_multiplier
Keys: num_retries, retry_exceptions, exclude_exceptions, retry_min_wait, retry_max_wait, retry_multiplier
Returns:
A retry decorator with the parameters customizable in configuration.
"""
num_retries = kwargs.get('num_retries')
retry_exceptions: tuple = kwargs.get('retry_exceptions', ())
exclude_exceptions: tuple = kwargs.get('exclude_exceptions', ())
retry_min_wait = kwargs.get('retry_min_wait')
retry_max_wait = kwargs.get('retry_max_wait')
retry_multiplier = kwargs.get('retry_multiplier')

def _filter_exceptions(e):
# For Cloudflare blocks, don't retry - just return False
if isinstance(e, APIError) and 'Attention Required! | Cloudflare' in str(e):
# First check if the exception is in the exclude list
if isinstance(e, exclude_exceptions):
return False

# Otherwise, return True if we want to retry, which means e is in retry_exceptions
# Then check if it's in the retry list
return isinstance(e, retry_exceptions)

return retry(
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ reportlab = "*"
[tool.coverage.run]
concurrency = ["gevent"]


[tool.poetry.group.runtime.dependencies]
jupyterlab = "*"
notebook = "*"
Expand Down Expand Up @@ -129,6 +130,7 @@ ignore = ["D1"]
[tool.ruff.lint.pydocstyle]
convention = "google"


[tool.poetry.group.evaluation.dependencies]
streamlit = "*"
whatthepatch = "*"
Expand Down

0 comments on commit 92227dc

Please sign in to comment.