Leveraging OpenAI API Endpoints Over SDKs for Enhanced Control and Stability
Rany ElHousieny, PhD???
Senior Software Engineering Manager (EX-Microsoft) | Generative AI Leader @ Clearwater Analytics | Generative AI, Conversational AI Solutions Architect
In the fast-evolving landscape of AI technologies, particularly with services like OpenAI's language models, developers often need robust and flexible methods to integrate these capabilities into their applications. Directly interfacing with API endpoints using HTTP requests, rather than relying on SDKs (Software Development Kits), can offer significant advantages, especially in maintaining long-term stability and compatibility in your projects. Below, I'll explain why using endpoints can be beneficial, how to use the Python requests library to send these HTTP requests, and provide a step-by-step breakdown of how you might implement this in your code.
Using API Endpoints Directly
Advantages of Direct Endpoint Use:
Here are the APIs for OpenAI
If you deploy OpenAI on Azure, you will have a different URL per deployment. The Azure URL might look as follows:
{AZURE_BASE_URL}/openai/deployments/{DEPLOYMENT_NAME}/chat/completions
Using the requests Library
The Python requests library is a popular choice for handling HTTP requests due to its simplicity and the powerful features it offers. Here’s how we can use it to chat with OpenAI:
Making an HTTP POST Request:
response = requests.post(url, headers=headers, params=params, json=request)
Step-by-Step Guide for Chat Completion Request
Step 1: Setting Up Your Request
First, you'll need to import the requests library. Make sure you have it installed (pip install requests), then prepare the necessary elements for your request, such as the API key, headers, and the request payload.
import requests
# API Key and Headers
api_key = MY_OPENAI_API_KEY # Replace with your actual OpenAI API key
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}'
}
Headers in an HTTP request play a critical role in ensuring that the request is formatted and authenticated properly. Let’s take a closer look at the two headers we have above:
领英推荐
1. Content-Type
The Content-Type header is used to indicate the type of data being sent to the server. Here’s what it means in this case:
2. Authorization
The Authorization header is used to authenticate a request to an API that requires security credentials, ensuring that the request is allowed to perform the action it’s attempting. Here’s how it’s used in this example:
# URL for the API endpoint
url = 'https://api.openai.com/v1/chat/completions'
# Request payload
payload = {
"model": "gpt-3.5-turbo", # Model selection can vary based on availability and your needs
"messages": [
{"role": "user",
"content": "What is the capital of France?"}
]
}
Step 2: Sending the Request
Now, use the requests.post method to send the request to the OpenAI API.
try:
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status() # Raises an exception for HTTP errors
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
else:
completion = response.json() # Parsing the response JSON to a dictionary
print(completion)
{'id': 'chatcmpl-9Mcltc2ASCuRiut1KnP48jvmHxKtf',
'object': 'chat.completion',
'created': 1715179281,
'model': 'gpt-3.5-turbo-0125',
'choices': [
{'index': 0,
'message': {
'role': 'assistant',
'content': 'The capital of France is Paris.'
},
'logprobs': None,
'finish_reason': 'stop'
}
],
'usage': {'prompt_tokens': 14,
'completion_tokens': 7,
'total_tokens': 21},
'system_fingerprint': None
}
Step 3: Handling the Response and Printing the Message
The response from the OpenAI API will be in JSON format. Here's how to handle and print the response data:
if response.status_code == 200:
print("Chat completion was successful!")
# Accessing the assistant's message
completion = response.json() # Convert the response JSON to a Python dictionary
# Accessing the assistant's message
assistant_message = completion['choices'][0]['message']['content']
print("Assistant's response:", assistant_message)
else:
print("Failed to get a valid response:", response.status_code)
print(response.text)
Chat completion was successful!
Assistant's response: The capital of France is Paris.