Обробка помилок

Розроблення AI‑систем з OpenAI API

Francesca Donadoni

Curriculum Manager, DataCamp

Помилки в застосунках ШІ

 

  • Спрощення взаємодії з продуктом
  • Усунення барʼєрів

Чоловік за комп'ютером усуває помилку 404

Розроблення AI‑систем з OpenAI API

Помилки в бібліотеці OpenAI API

response = client.chat.completions.create(
 model="text-davinci-001",
 messages=[
   {
    "role": "user", 
    "content": "List two data science professions with related skills in json format."
   }
 ],
 response_format={"type": "json_object"}
)
NotFoundError: Error code: 404 - {'error': {'message': 'The model `text-davinci-001` 
has been deprecated, learn more here: https://platform.openai.com/docs/deprecations'}}
Розроблення AI‑систем з OpenAI API

Помилки зʼєднання

 

  • Зазвичай через проблеми зі зʼєднанням на боці користувача або сервісу

 

  • Приклади: InternalServerError, APIConnectionError, APITimeoutError

 

  • Можливе рішення:
    • Перевірити налаштування зʼєднання
    • Звернутися в підтримку, якщо не допомогло
Розроблення AI‑систем з OpenAI API

Помилки лімітів ресурсів

 

  • Зазвичай через обмеження частоти запитів або обсягу тексту

 

  • Приклади: ConflictError, RateLimitError

 

  • Можливе рішення:
    • Перевірити ліміти
    • Слідкувати, щоб запити були в межах лімітів
Розроблення AI‑систем з OpenAI API

Помилки автентифікації

client = OpenAI(api_key="This is an Invalid Key")

response = client.chat.completions.create( model="gpt-4o-mini", messages=[ { "role": "user", "content": "List two data science professions with related skills in json format." } ], response_format={"type": "json_object"} )
AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: 
ThisIsNo*AKey. You can find your API key at https://platform.openai.com/account/api-keys.'}}
Розроблення AI‑систем з OpenAI API

Помилки некоректного запиту

response = client.chat.completions.create(
 model="gpt-4o-mini",
 messages=[
   {"role": "This is not a Valid Role", "content": "List two data science 
   professions with related skills in json format."}
 ],
 response_format={"type": "json_object"}
)
BadRequestError: Error code: 400 - {'error': {'message': "'NotARole' is not one of 
['system', 'assistant', 'user', 'function'] - 'messages.0.role'", 
'type': 'invalid_request_error', 'param': None, 'code': None}}
Розроблення AI‑систем з OpenAI API

Обробка винятків

try: 
    response = client.chat.completions.create(
     model="gpt-4o-mini",
     messages=[{"role": "user", 
      "content": "List five data science professions."}])

except openai.AuthenticationError as e: print(f"OpenAI API failed to authenticate: {e}") pass except openai.RateLimitError as e: print(f"OpenAI API request exceeded rate limit: {e}") pass
except Exception as e: print(f"Unable to generate a response. Exception: {e}") pass
Розроблення AI‑систем з OpenAI API

Давайте потренуємось!

Розроблення AI‑систем з OpenAI API

Preparing Video For Download...