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

Add retry support to catch_and_convert_errors #90

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion spice/spice.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ def __init__(
logging_dir: Optional[Path | str] = None,
logging_callback: Optional[Callable[[SpiceResponse, str, str], None]] = None,
default_temperature: Optional[float] = None,
max_retries: int = 0, # Add this line
base_delay: float = 1.0, # Add this line
max_delay: float = 32.0, # Add this line
):
"""
Creates a new Spice client.
Expand Down Expand Up @@ -268,6 +271,11 @@ def __init__(
self._default_embeddings_model = embeddings_model
self._default_temperature = default_temperature

# Initialize retry configuration parameters
self.max_retries = max_retries
self.base_delay = base_delay
self.max_delay = max_delay

# TODO: Should we validate model aliases?
self._model_aliases = model_aliases

Expand Down Expand Up @@ -451,7 +459,7 @@ async def get_response(
elif i > 1 and call_args.temperature is not None:
call_args.temperature = max(0.5, call_args.temperature)

with client.catch_and_convert_errors():
with client.catch_and_convert_errors(max_retries=self.max_retries, base_delay=self.base_delay, max_delay=self.max_delay):
if streaming_callback is not None:
stream = await client.get_chat_completion_or_stream(call_args)
stream = cast(AsyncIterator, stream)
Expand Down
56 changes: 37 additions & 19 deletions spice/wrapped_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import base64
import io
import mimetypes
import time
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You didn't add the implementation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I apologize for the oversight. I'll implement the retry logic in the catch_and_convert_errors method for the WrappedOpenAIClient and WrappedAnthropicClient classes. Thank you for catching that.

from abc import ABC, abstractmethod
from contextlib import contextmanager
from pathlib import Path
Expand Down Expand Up @@ -119,16 +120,24 @@ def extract_text_and_tokens(self, chat_completion, call_args: SpiceCallArgs):

@override
@contextmanager
def catch_and_convert_errors(self):
# TODO: Do we catch all errors? I think we should catch APIStatusError
try:
yield
except openai.APIConnectionError as e:
raise APIConnectionError(f"OpenAI Connection Error: {e.message}") from e
except openai.AuthenticationError as e:
raise AuthenticationError(f"OpenAI Authentication Error: {e.message}") from e
except openai.APIStatusError as e:
raise APIError(f"OpenAI Status Error: {e.message}") from e
def catch_and_convert_errors(self, max_retries: int = 0, base_delay: float = 1.0, max_delay: float = 32.0):
retries = 0
delay = base_delay
while retries <= max_retries:
try:
yield
return
except openai.APIConnectionError as e:
if retries == max_retries:
raise APIConnectionError(f"OpenAI Connection Error: {e.message}") from e
except openai.AuthenticationError as e:
raise AuthenticationError(f"OpenAI Authentication Error: {e.message}") from e
except openai.APIStatusError as e:
if retries == max_retries:
raise APIError(f"OpenAI Status Error: {e.message}") from e
time.sleep(min(delay, max_delay))
delay *= 2
retries += 1

def _get_encoding_for_model(self, model: Model | str) -> tiktoken.Encoding:
from spice.models import Model
Expand Down Expand Up @@ -388,15 +397,24 @@ def extract_text_and_tokens(self, chat_completion, call_args: SpiceCallArgs):

@override
@contextmanager
def catch_and_convert_errors(self):
try:
yield
except anthropic.APIConnectionError as e:
raise APIConnectionError(f"Anthropic Connection Error: {e.message}") from e
except anthropic.AuthenticationError as e:
raise AuthenticationError(f"Anthropic Authentication Error: {e.message}") from e
except anthropic.APIStatusError as e:
raise APIError(f"Anthropic Status Error: {e.message}") from e
def catch_and_convert_errors(self, max_retries: int = 0, base_delay: float = 1.0, max_delay: float = 32.0):
retries = 0
delay = base_delay
while retries <= max_retries:
try:
yield
return
except anthropic.APIConnectionError as e:
if retries == max_retries:
raise APIConnectionError(f"Anthropic Connection Error: {e.message}") from e
except anthropic.AuthenticationError as e:
raise AuthenticationError(f"Anthropic Authentication Error: {e.message}") from e
except anthropic.APIStatusError as e:
if retries == max_retries:
raise APIError(f"Anthropic Status Error: {e.message}") from e
time.sleep(min(delay, max_delay))
delay *= 2
retries += 1

# Anthropic doesn't give us a way to count tokens, so we just use OpenAI's token counting functions and multiply by a pre-determined multiplier
class _FakeWrappedOpenAIClient(WrappedOpenAIClient):
Expand Down
Loading