Last active
June 14, 2023 15:18
-
-
Save mw3i/a3169086b7569462406c27f174d81a2b to your computer and use it in GitHub Desktop.
Make a single api call with chatgpt, with exponential backoff to mitigate rate limiting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Build with the help of chatgpt | |
''' | |
import openai | |
openai.api_key = 'your_api_key_here' | |
def response(message): | |
retry_count = 0 | |
max_retries = 4 | |
wait_time = 7 # Initial wait time in seconds | |
messages = [ | |
{ | |
'role': 'user', | |
'content': message, | |
} | |
] | |
while retry_count < max_retries: | |
try: | |
completion = openai.ChatCompletion.create( | |
model = "gpt-3.5-turbo", | |
messages = messages, | |
temperature = 0, | |
max_tokens = 1, | |
) | |
# print(completion['usage']['total_tokens']) | |
return completion.choices[0].message.content | |
except Exception as e: | |
print(f"An error occurred: {str(e)[:30]}") | |
print(f"Retrying in {wait_time} seconds...") | |
time.sleep(wait_time) | |
retry_count += 1 | |
wait_time *= 3 # Exponential backoff: double the wait time for each retry | |
print("Exceeded maximum number of retries. Aborting.") | |
return None | |
# Test it out | |
if __name__ == '__main__': | |
print( | |
response("What's your favorite anime?") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment