I do this for all of my OpenAI api calls:
// Define the OpenAI API endpoint and parameters
$url = $this->urlOpenAI;
// Initialize the $messages array with the system message
$message = array(
array("role" => "system", "content" => $systemMessage)
);
// Define the new user message
$userMessage = array("role" => "user", "content" => $text);
// Append the new user message to the end of the $messages array
$message[] = $userMessage;
// Define the data for the API request
$data = array(
'messages' => $message, // No need to wrap $message in an array if it is set as array above
'model' => $model,
'temperature' => $temperature
);
// Define the request headers and data
$headers = [
'Authorization: Bearer ' . $this->apiOpenAI,
'Content-Type: application/json'
];
// Call the GPT API
$responseBody = $this->solrai_executeCurlRequest($url, $headers, $data);
Then call this curl method:
/**
* Executes a cURL request.
*
* @param string $url
* The URL to make the request to.
* @param array $headers
* An array of HTTP headers.
* @param array $data
* The data to send in the request body.
*
* @return array
* The response from the server, decoded from JSON to an array.
*/
public function solrai_executeCurlRequest($url, $headers, $data) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, '');
curl_setopt($curl, CURLOPT_MAXREDIRS, 10); // Was set to 10. Set to 0
$startTime = microtime(true);
# Debug
# Generate a unique identifier for this request
$uniqueId = uniqid();
# Debug
# Log the request details
# $this->loggerFactory->get('solrai')->info("(solrai_executeCurlRequest) Unique ID: $uniqueId | Request Details: " . print_r([
# 'url' => $url,
# 'headers' => $headers,
# 'body' => $data,
# ], true));
$response = curl_exec($curl);
# Debug
# Log the raw response details
# $this->loggerFactory->get('solrai')->info("(solrai_executeCurlRequest) Unique ID: $uniqueId | Raw Response: " . $response);
# Debug
# Log the decoded response details
# $decodedResponse = json_decode($response, true);
# $this->loggerFactory->get('solrai')->info("(solrai_executeCurlRequest) Unique ID: $uniqueId | Decoded Response: " . print_r($decodedResponse, true));
# Debug
# Get and log detailed cURL info
# $curlInfo = curl_getinfo($curl);
# $this->loggerFactory->get('solrai')->info("(solrai_executeCurlRequest) Unique ID: $uniqueId | cURL Info: " . print_r($curlInfo, true));
$endTime = microtime(true);
$elapsedTime = $endTime - $startTime;
// Add to global variable
$this->elapsedTime = $elapsedTime;
$error_msg = null;
try {
if (curl_errno($curl)) {
$error_msg = curl_error($curl);
throw new \Exception('cURL error: ' . $error_msg);
}
} catch (\Exception $e) {
$this->loggerFactory->get('solrai')->error($e->getMessage());
}
curl_close($curl);
if ($this->logElapsedTime === true) {
// Extract the base domain from the URL
$host = parse_url($url, PHP_URL_HOST);
$baseDomain = implode('.', array_slice(explode('.', $host), -2));
// Extract the model from the data array
$model = isset($data['model']) ? $data['model'] : 'Not provided';
// Log the information
$this->loggerFactory->get('solrai')->info('API: ' . $baseDomain . ' | Elapsed time: ' . $elapsedTime . ' seconds' . ' | Model: ' . $model . ' | Method: ' . __FUNCTION__ . ' | Endpoint: ' . $url);
}
if ($error_msg) {
return ['error' => $error_msg];
}
$responseBody = json_decode($response, true);
// Append elapsed time to the response body
$responseBody['_elapsed_time'][$url] = $elapsedTime;
return $responseBody;
}
So, I’m able to use this anywhere in my code whenever I want to call an OpenAI api (actually, use it for my Weaviate vector store calls as well).