Skip to content

Commit

Permalink
Merge pull request #741 from lukaspetersson/ratelimit
Browse files Browse the repository at this point in the history
Handle rate limit error with backoff
  • Loading branch information
ATheorell authored Sep 25, 2023
2 parents b2f4f35 + 34d6d26 commit 94e67c4
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions gpt_engineer/ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dataclasses import dataclass
from typing import List, Optional, Union

import backoff
import openai
import tiktoken

Expand Down Expand Up @@ -170,8 +171,8 @@ def next(

logger.debug(f"Creating a new chat completion: {messages}")

callsbacks = [StreamingStdOutCallbackHandler()]
response = self.llm(messages, callbacks=callsbacks) # type: ignore
callbacks = [StreamingStdOutCallbackHandler()]
response = self.backoff_inference(messages, callbacks)

self.update_token_usage_log(
messages=messages, answer=response.content, step_name=step_name
Expand All @@ -181,6 +182,12 @@ def next(

return messages

@backoff.on_exception(
backoff.expo, openai.error.RateLimitError, max_tries=7, max_time=45
)
def backoff_inference(self, messages, callbacks):
return self.llm(messages, callbacks=callbacks) # type: ignore

@staticmethod
def serialize_messages(messages: List[Message]) -> str:
"""
Expand Down

0 comments on commit 94e67c4

Please sign in to comment.