Making requests to the OpenAI API

Lavorare con l'API di OpenAI

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.

Lavorare con l'API di OpenAI

API endpoints

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

Lavorare con l'API di OpenAI

API endpoints

  • Endpoints → API access point designed for specific interactions

A hospital corridor showing different doors to different departments.

Lavorare con l'API di OpenAI

API authentication

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

A hospital corridor showing different doors to different departments.

Lavorare con l'API di OpenAI

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
Lavorare con l'API di OpenAI

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.

Lavorare con l'API di OpenAI

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)
Lavorare con l'API di OpenAI

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}))
Lavorare con l'API di OpenAI

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}))
Lavorare con l'API di OpenAI

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))]
Lavorare con l'API di OpenAI

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))
Lavorare con l'API di OpenAI

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.
Lavorare con l'API di OpenAI

Let's practice!

Lavorare con l'API di OpenAI

Preparing Video For Download...