I’m using the openai.ChatCompletion.create endpoint and on an absolutely random basis when running the function in a loop it will halt execution without returning a response.
I can not even shut off the process via KeyboardInterrupt.
This is a new issue and has not happened before even though using the basically same function and API key. If it were due to a rate limit I believe I would get some response at least.
Please evaluate my functions’s logic. ChatGPT suggests adding a timeout but this doesn’t seem to be available to simply add to the call.
Any suggestions?
def call_gpt(api_key, gpt_model=3, prompt="How are you?", input_text=""):
logger = get_logger()
if api_key:
# Set your OpenAI API key
openai.api_key = api_key
else:
return "No OpenAI API key..."
if gpt_model == 3:
gpt_model = "3.5-turbo"
if isinstance(gpt_model, int):
gpt_model = str(gpt_model)
# Concatenate the prompt and input input_text
full_prompt = prompt + str(input_text)
attempts = 0
while attempts < 5:
try:
# Send the request to the OpenAI API
logger.debug(f"About to make the GPT-{gpt_model} OpenAI API call - attempt {attempts}")
response = openai.ChatCompletion.create(
model=f"gpt-{gpt_model}",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": full_prompt},
],
)
# Extract the generated summary from the API response
output_text = response.choices[0].message.content
# print(f"\n{output_text}")
# Remove non-ASCII characters from the output_text
output_text = output_text.encode("ascii", "ignore").decode()
return output_text
except (
openai.error.RateLimitError,
requests.exceptions.ConnectionError,
openai.error.APIError,
openai.error.ServiceUnavailableError,
openai.error.APIConnectionError,
requests.exceptions.ReadTimeout,
) as e:
logger.warning(f"{type(e).__name__} encountered. New API call attempt in {(2**attempts)} seconds...\n{e}")
time.sleep((2**attempts))
attempts += 1
return f"No valid response from OpenAI API after {attempts} attempts!"