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

Feature / Suggestion #237

Open
jonny7737 opened this issue May 10, 2024 · 4 comments
Open

Feature / Suggestion #237

jonny7737 opened this issue May 10, 2024 · 4 comments

Comments

@jonny7737
Copy link

jonny7737 commented May 10, 2024

In phi/llm/anthropic/claude.py, I think the invoke and invoke_stream methods would benifit from a try-except around the return statement to catch rate limit errors with a backoff mechanism. Something like this might work [WARNING: UNTESTED CODE]

class ExponentialBackoff:
    def __init__(self, base: float = 2, max_retries: int = 5, max_backoff: float = 60):
        self.base = base
        self.max_retries = max_retries
        self.max_backoff = max_backoff
        self.retry_count = 0

    def backoff(self):
        backoff = self.base ** self.retry_count
        self.retry_count += 1
        return min(backoff, self.max_backoff)

def invoke(self, messages: List[Message]) -> AnthropicMessage:
    api_kwargs: Dict[str, Any] = self.api_kwargs
    api_messages: List[dict] = []

    for m in messages:
        if m.role == "system":
            api_kwargs["system"] = m.content
        else:
            api_messages.append({"role": m.role, "content": m.content or ""})

    backoff = ExponentialBackoff()
    while True:
        try:
            return self.client.messages.create(
                model=self.model,
                messages=api_messages,
                **api_kwargs,
            )
        except RateLimitError as e:
            if backoff.retry_count > backoff.max_retries:
                raise e  # Maximum retries exceeded, raise the exception
            
            delay = backoff.backoff()
            print(f"Rate limit exceeded. Retrying in {delay} seconds...")
            time.sleep(delay)
@ashpreetbedi
Copy link
Contributor

Really good idea, will test and probably release this week.

@jonny7737
Copy link
Author

Another possible approach is to apply a decorator to invoke and invoke_stream functions that enforces a rate limit. This approach is more proactive but might require changes elsewhere to enable configuring (setting) the rate limit to enforce.

Great work on this repo.
Everybody says it but it is worth repeating.

@ashpreetbedi
Copy link
Contributor

@jonny7737 decorators are a great idea (maybe using tenacity).
Thank you for your help in making this better. im working on this :)

as for a timeline, im tinkering with a new concept and after putting that out will work on the retry logic as that seems to be a p0 for a number of use-cases

@jonny7737
Copy link
Author

Hope I have contributed in some small way. Thanks for listening.

Keep up the great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants