エラー処理

OpenAI API で開発する AI システム

Francesca Donadoni

Curriculum Manager, DataCamp

AIアプリのエラー

 

  • ユーザー体験を簡素化
  • 障壁を取り除く

404エラーをトラブルシュートする男性

OpenAI API で開発する AI システム

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'}}
OpenAI API で開発する AI システム

接続エラー

 

  • 一般に、ユーザー側またはサービス側の接続問題が原因

 

  • 例: InternalServerError, APIConnectionError, APITimeoutError

 

  • 対処案:
    • 接続設定を確認
    • 解決しなければサポートに連絡
OpenAI API で開発する AI システム

リソース上限制限エラー

 

  • 一般に、リクエスト頻度やテキスト量の上限が原因

 

  • 例: ConflictError, RateLimitError

 

  • 対処案:
    • 上限制約を確認
    • リクエストが上限内か確認
OpenAI API で開発する AI システム

認証エラー

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.'}}
OpenAI API で開発する AI システム

不正リクエストエラー

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}}
OpenAI API で開発する AI システム

例外処理

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
OpenAI API で開発する AI システム

Passons à la pratique !

OpenAI API で開発する AI システム

Preparing Video For Download...