Skip to content

Instantly share code, notes, and snippets.

@aialenti
Created June 28, 2023 13:26
Show Gist options
  • Save aialenti/81bd58dfbe74bfd4ffc5bfc7430cf824 to your computer and use it in GitHub Desktop.
Save aialenti/81bd58dfbe74bfd4ffc5bfc7430cf824 to your computer and use it in GitHub Desktop.
# 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