Sunday 12 February 2023

PHP - Access ChatGPT API using cURL

To access OpenAI's ChatGPT API using PHP, you can use any HTTP client library that supports making HTTP requests with JSON payloads, such as Guzzle or cURL. You may find it a bit ridiculous, because ChatGPT hasn't officially announced the API yet. But the fact that OpenAI already has APIs available to provide full task processing like ChatGPT. This REST API can answer your questions or handle your requests with the same results as ChatGPT.

How GPT Chat Works

As a general-purpose language model, ChatGPT is likely to use a combination of OpenAI's available models and techniques to generate its responses, depending on the context and nature of the question. Additionally, OpenAI may update or modify its models over time, so the specific models used by ChatGPT may change as well. In other words, if you use the OpenAI API with the right Model, you can get the same results as ChatGPT. Of course ChatGPT will have more features and especially the ability to link topics with your messages.

How to access OpenAI ChatGPT API using Curl

You can study the documents and examples at the following links:

OpenAI Docs: https://platform.openai.com/docs/introduction
Models: https://platform.openai.com/docs/models/gpt-3
Features and examples: https://platform.openai.com/examples
Get API Key: https://platform.openai.com/account/api-keys

ChatGPT API Sample:

<?php
$data = array(
  "prompt" => "Is Climate Change worrisome?", //Your question or request
  "temperature" => 0.5,
  "max_tokens" => 500
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/engines/davinci-codex/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer YOUR_API_KEY';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo $response;
/**
 * Output Response :
 * Yes, climate change is dangerous and has significant consequences
 * for the planet and its inhabitants. The warming of the Earth's climate
 * due to increased greenhouse gas emissions, primarily from human
 * activities such as burning fossil fuels, is causing more frequent
 * and severe natural disasters, such as floods, heatwaves, wildfires, and storms.
 * */

Note:

- Unlike ChatGPT, OpenAI API is not free but charges based on the number of input and output characters each of your APIs

- A programmer can use OpenAI's API to integrate various natural language processing capabilities into their applications, such as text generation, question answering, language translation, sentiment analysis, and more. With the API, You can leverage the power of OpenAI's advanced machine learning models and algorithms without having to build them from scratch. However, note that OpenAI has some usage restrictions for their API, so make sure to review their documentation before integrating it into your project.


Friday 10 February 2023

How to fix error "system is deadlocked on memory" on Vultr VPS

The error "system is deadlocked on memory" "end Kernel panic - not syncing" usually indicates that the system has run out of available memory and processes are unable to allocate additional memory. This error comes from the VPS kernel, so it can be encountered in both Linux or Windows VPS operating systems. To resolve the issue on your Vultr VPS or any other service provider (Linode, Digitalocean ...) you can try the following steps:

Note: The first thing you need to do when you encounter this error is to quickly backup or snapshot VPS. Because the wrong operations at this time can cause you to lose the data in your VPS. 

Case 1 : Error on startup (can not start VPS)

  1. Restart the server: If the issue persists, you can restart your Vultr VPS to clear the memory and resolve the deadlock.

  2. Turn off the startup scripts with VPS (in VPS management) that you added (if exist)

  3. Upgrading to a VPS droplet with more memory: If other solutions don't work, upgrading to a more configurable VPS plan might solve the situation. Upgrading VPS to a higher droplet at Vultr is easy, safe and does not affect your data or applications.

  4. Contact Vultr for support: The last resort or if you are too worried about the current data in the VPS. Experts from Vultr will be able to help you fix the problem

Case 2: Error during operation (sometimes encounter)

  1. Monitor resource usage: Use the 'top',' htop' command or "Task Manager' to monitor the resource usage of your system. Identify the processes that are consuming large amounts of memory and determine if they can be terminated or if their resource usage can be optimized.

  2. Kill processes: If a specific process is consuming a large amount of memory and is not responding, terminate it.

  3. Increase swap space(Linux): If your system does not have enough physical memory, you can increase the amount of swap space to temporarily address the issue.

  4. Optimize application performance: If the issue recurs frequently, you may need to optimize the performance of your applications

It's important to monitor your system and its resource usage to identify and resolve issues before they become severe. Consider using a system monitoring tool to automatically alert you to any issues.