Developing AI Systems with the OpenAI API
Francesca Donadoni
Curriculum Manager, DataCamp
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'}}
InternalServerError
, APIConnectionError
, APITimeoutError
ConflictError
, RateLimitError
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.'}}
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}}
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
Developing AI Systems with the OpenAI API