การจัดการข้อผิดพลาด

การพัฒนาระบบ AI ด้วย OpenAI API

Francesca Donadoni

Curriculum Manager, DataCamp

ข้อผิดพลาดในแอปพลิเคชัน AI

 

  • ทำให้ประสบการณ์ผู้ใช้เรียบง่ายขึ้น
  • ขจัดอุปสรรค

ชายกำลังแก้ไขข้อผิดพลาด 404 บนคอมพิวเตอร์

การพัฒนาระบบ AI ด้วย OpenAI API

ข้อผิดพลาดใน OpenAI API library

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

การจัดการ exception

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...