Xử lý lỗi

Phát triển hệ thống AI với OpenAI API

Francesca Donadoni

Curriculum Manager, DataCamp

Lỗi trong ứng dụng AI

 

  • Đơn giản hóa trải nghiệm người dùng
  • Loại bỏ rào cản

Một người đàn ông dùng máy tính để khắc phục lỗi 404

Phát triển hệ thống AI với OpenAI API

Lỗi trong thư viện 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'}}
Phát triển hệ thống AI với OpenAI API

Lỗi kết nối

 

  • Thường do sự cố kết nối từ phía người dùng hoặc dịch vụ

 

  • Ví dụ: InternalServerError, APIConnectionError, APITimeoutError

 

  • Cách khắc phục có thể:
    • Kiểm tra cấu hình kết nối
    • Liên hệ hỗ trợ nếu chưa giải quyết được
Phát triển hệ thống AI với OpenAI API

Lỗi giới hạn tài nguyên

 

  • Thường do giới hạn tần suất yêu cầu hoặc độ dài văn bản gửi lên

 

  • Ví dụ: ConflictError, RateLimitError

 

  • Cách khắc phục có thể:
    • Kiểm tra các giới hạn
    • Bảo đảm yêu cầu nằm trong giới hạn
Phát triển hệ thống AI với OpenAI API

Lỗi xác thực

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.'}}
Phát triển hệ thống AI với OpenAI API

Lỗi yêu cầu không hợp lệ

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}}
Phát triển hệ thống AI với OpenAI API

Xử lý ngoại lệ

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
Phát triển hệ thống AI với OpenAI API

Ayo berlatih!

Phát triển hệ thống AI với OpenAI API

Preparing Video For Download...