오류 처리

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 시스템 개발하기

연습해 봅시다!

OpenAI API로 AI 시스템 개발하기

Preparing Video For Download...