エラーの処理

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 システムの開発

接続エラー

  • ユーザー側またはサービス側の接続問題が原因であることが多い
  • 例: InternalServerErrorAPIConnectionErrorAPITimeoutError
  • 考えられる解決策:
    • 接続設定の確認
    • それでも失敗した場合はサポートに連絡する
OpenAI API を使った AI システムの開発

リソース制限エラー

  • リクエストの頻度または渡されるテキスト量の制限によるものが多い
  • 例: ConflictErrorRateLimitError
  • 考えられる解決策:
    • 制限の確認
    • リクエストが上限を超えていないか確認
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 システムの開発

Bad requestエラー

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 システムの開発

練習しましょう!

OpenAI API を使った AI システムの開発

Preparing Video For Download...