I’m using a chat completion function that receives a list of items, and I need the function to run once for each item in the list. The list I need it to iterate through is 12 items long.
Here is the code:
class RoundupGenerator(BaseGenerator):
def generate_roundup(self):
completions = []
for type in self.type_notes:
try:
if all(messages) == True:
completion = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "system", "content": f''' Briefly introduce yourself '''}]
)
completions.append(completion.choices[0].message.content)
except openai.error.APIError as err:
completions.append('API Error')
except openai.error.APIConnectionError as err:
completions.append('API Connection Error')
except openai.error.RateLimitError as err:
completions.append('Rate Limit Error')
return '\n'.join(completions)
When I run this function, it takes 10-20 minutes before returning a ‘502 Bad Gateway’ error.
Debugging and fixes tried:
- Introducing a 0.5 second time.sleep delay in the for loop
- Instantiating the class/function with only 1 item in the list
- Adding the error handling seen at the end of my code as recommended from OpenAI’s documentation here
- Removing the for loop and instantiating the class/function with 1 item in the list to ensure the function itself works
- Asking it nicely
I found an older forum thread here about this issue, but my step 3 seemed to fix it for them. Any ideas?