Making requests to the OpenAI API

Working with the OpenAI API

James Chapman

Curriculum Manager, DataCamp

API endpoints

A computer connecting to the OpenAI via one of several access points, which are in-turn connected to different servers.

Working with the OpenAI API

API endpoints

A computer connecting to the OpenAI via one of several endpoints, which are in-turn connected to different servers.

Working with the OpenAI API

API endpoints

  • Endpoints → API access point designed for specific interactions

A hospital corridor showing different doors to different departments.

Working with the OpenAI API

API authentication

  • Authentication → Controls on access to API endpoint services (often unique key)

A hospital corridor showing different doors to different departments.

Working with the OpenAI API

API usage costs

 

  • For OpenAI API:
    • Model
    • Larger inputs + outputs = Greater cost

 

  • No additional costs for this course 🎉

A toll road.

1 https://openai.com/pricing
Working with the OpenAI API

Creating an API key

  • Isn't required in this course
  1. Create your account → https://platform.openai.com/signup

  2. Go to the API keys page → https://platform.openai.com/account/api-keys

  3. Create a new secret key and copy it

The button to create a new secret key.

Working with the OpenAI API

Making a request

from openai import OpenAI


client = OpenAI(api_key="ENTER YOUR KEY HERE")
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "What is the OpenAI API?"}]
)
print(response)
Working with the OpenAI API

The response

ChatCompletion(id='chatcmpl-AEcQbQekIzxcxVAKYVAjgUAXokgrl', choices=[Choice(finish_reason='length', index=0,
logprobs=None, message=ChatCompletionMessage(content='The OpenAI API is a cloud-based service provided by
OpenAI that allows developers to integrate advanced AI models into their applications.', refusal=None,
role='assistant', function_call=None, tool_calls=None))], created=1728047673, model='gpt-4o-mini-2024-07-18',
object='chat.completion', service_tier=None, system_fingerprint='fp_f85bea6784',
usage=CompletionUsage(completion_tokens=30, prompt_tokens=14, total_tokens=44,
prompt_tokens_details={'cached_tokens': 0}, completion_tokens_details={'reasoning_tokens': 0}))
Working with the OpenAI API

The response

ChatCompletion(id='chatcmpl-AEcQbQekIzxcxVAKYVAjgUAXokgrl',
               choices=[Choice(finish_reason='length', index=0, logprobs=None,
                               message=ChatCompletionMessage(content='The OpenAI API is a cloud-based service
                               provided by OpenAI that allows developers to integrate advanced AI models
                               into their applications.',
                               refusal=None, role='assistant', function_call=None, tool_calls=None))],
               created=1728047673,
               model='gpt-4o-mini-2024-07-18',
               object='chat.completion', service_tier=None, system_fingerprint='fp_f85bea6784',
               usage=CompletionUsage(completion_tokens=25, prompt_tokens=14, total_tokens=39,
                                     prompt_tokens_details={'cached_tokens': 0},
                                     completion_tokens_details={'reasoning_tokens': 0}))
Working with the OpenAI API

Interpreting the response

print(response.choices)
[Choice(finish_reason='length', index=0, logprobs=None,
 message=ChatCompletionMessage(content='The OpenAI API is a cloud-based service provided by
 OpenAI that allows developers to integrate advanced AI models into their applications.',
 refusal=None, role='assistant', function_call=None, tool_calls=None))]
Working with the OpenAI API

Interpreting the response

print(response.choices[0])
Choice(finish_reason='length', index=0, logprobs=None,
 message=ChatCompletionMessage(content='The OpenAI API is a cloud-based service provided by
 OpenAI that allows developers to integrate advanced AI models into their applications.',
 refusal=None, role='assistant', function_call=None, tool_calls=None))
Working with the OpenAI API

Interpreting the response

print(response.choices[0].message)
ChatCompletionMessage(content='The OpenAI API is a cloud-based service provided by
 OpenAI that allows developers to integrate advanced AI models into their applications.',
 refusal=None, role='assistant', function_call=None, tool_calls=None)
print(response.choices[0].message.content)
The OpenAI API is a cloud-based service provided by OpenAI that allows developers to
integrate advanced AI models into their applications.
Working with the OpenAI API

Let's practice!

Working with the OpenAI API

Preparing Video For Download...