Created
June 28, 2023 13:26
-
-
Save aialenti/81bd58dfbe74bfd4ffc5bfc7430cf824 to your computer and use it in GitHub Desktop.
This file contains 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
# Function to do the API call. Function returns the completition message | |
def complete(messages, max_retries=5, delay=5): | |
for i in range(max_retries): | |
try: | |
return openai.ChatCompletion.create( | |
model="gpt-3.5-turbo-0613", # The deployment name you chose when you deployed the ChatGPT model. | |
messages=messages, | |
temperature=0.5, | |
max_tokens=400, | |
top_p=1.0, | |
frequency_penalty=0, | |
presence_penalty=0, | |
stop=['<|im_end|>'] | |
).choices[0]["message"]["content"] | |
except openai.OpenAIError as e: | |
if i < max_retries - 1: # i is zero indexed | |
time.sleep(delay) # wait before trying again | |
continue | |
else: | |
raise e # rethrow the last exception if max_retries is exceeded |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment