PHP Classes

OpenAI Chat API PHP Completions Class: Chat application like ChatGPT using OpenAI GPT API

Recommend this page to a friend!

  Author Author  
Picture of JImmy Bo
Name: JImmy Bo is available for providing paid consulting. Contact JImmy Bo .
Classes: 14 packages by
Country: United States United States
Age: ???
All time rank: 1254180 in United States United States
Week rank: 3 Up1 in United States United States Up
Innovation award
Innovation award
Nominee: 8x

Winner: 1x


  Detailed description   Download Download .zip .tar.gz   Install with Composer Install with Composer  
This package implements a chat application like ChatGPT using OpenAI GPT API.

It provides a class that can access the OpenAI GPT API to send requests to implement chat interactions.

The package also provides a Web application to let users chat with GPT artificial intelligence engine, similar to what is possible with the ChatGPT application.


Instructions for use:

1. Create an OpenAI account at https://beta.openai.com/

2. Create an API key at https://beta.openai.com/account/api-keys

3. Make a variable to store your API key in, let's say $apiKey

4. Create a new PHPOpenAIChat object, passing in your API key, like this:
$openAIChat = new PHPOpenAIChat($apiKey);

5. Send a message to the model and get a response like this:
$messages = []; # create a conversation
// set agent
$messages = $openAIChat->set_agent($messages, "You are an assistant cat that can speak english and is named Henry.");
// add prompt to messages conversation
$messages = $openAIChat->add_prompt_to_messages($messages, "What is your name?");
$response = $openAIChat->sendMessage($messages);
print_r($response); # show entire returned array
// print text
echo "<br />response text: " . $openAIChat->get_response_text($response) ."<br />";

6. You can also set the model to use (gpt-3.5-turbo or gpt-4), the max_tokens to use, the temperature to use, the frequency_penalty to use, and the presence_penalty to use.

7. You can also estimate the token count of the messages to send to the model, and subtracts from max_tokens.

8. You should be able to get the text from the response by using $response['choices'][0]['message']['content']

9. You can append the text from the response to the messages to send to the model

10. You can also use the add_prompt_to_messages() function to add a prompt to the current conversation

11. Then you can repeat the cycle again by sending the messages to the model and getting another response.

  Classes of JImmy Bo  >  OpenAI Chat API PHP Completions Class  >  Download Download .zip .tar.gz  >  Support forum Support forum (2)  >  Blog Blog (1)  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: OpenAI Chat API PHP Completions Class
Base name: phpopenaichat
Description: Chat application like ChatGPT using OpenAI GPT API
Version: 1.0.1
PHP version: 5
License: BSD License
All time users: 308 users
All time rank: 7312
Week users: 4 users
Week rank: 25 Up
 
  Groups   Screenshots Screenshots   Rate classes User ratings   Applications   Files Files  

  Groups  
Group folder image PHP 5 Classes using PHP 5 specific features View top rated classes
Group folder image Web services Web data clipping, SOAP or XML-RPC clients and servers View top rated classes
Group folder image Chat Chating and instant messaging View top rated classes
Group folder image Artificial intelligence Automation of tasks using human-like intelligence View top rated classes
Group folder image Applications Full applications that perform useful tasks View top rated classes

  Files folder image Screenshots  
/opt2/mlemos/httpd/htdocs/application/repository_browse.php 2937 screenshot.phpopenaichat.png
File Role Description
Accessible without login Image file screenshot.phpopenaichat.png Screen Demonstration of using the example.


  Innovation Award  
PHP Programming Innovation award winner
May 2023
Winner
ChatGPT became a very famous artificial intelligence application due to its ability to provide a chat application that allows the users to interact with a robot in a way that feels like the ChatGPT users are talking with other humans.

This package provides a PHP application that works similarly to ChatGPT.

It uses the GPT API to benefit from the same artificial intelligence engine of OpenAI that ChatGPT uses.

Manuel Lemos

  User ratings  
RatingsUtility Consistency Documentation Examples Tests Videos Overall Rank
All time: Sufficient (75%) Good (83%) - Good (83%) - - Not sure (54%) 1986
Month: Not yet rated by the users

  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  Files folder image Files  
File Role Description
Plain text file class.phpopenaichat.php Class A php class to use the OpenAI API chat completions to chat with a GPT-3.5 or a GPT-4 model.
Accessible without login Plain text file example.phpopenaichat.php Example An example using the class PHPOpenAIChat

Install with Composer Install with Composer - Download Download all files: phpopenaichat.tar.gz phpopenaichat.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
  Files folder image Files  
File Role Description
Plain text file class.phpopenaichat.php Class A php class to use the OpenAI API chat completions to chat with a GPT-3.5 or a GPT-4 model.
Accessible without login Plain text file example.phpopenaichat.php Example An example using the class PHPOpenAIChat

Install with Composer Install with Composer - Download Download all files: phpopenaichat.tar.gz phpopenaichat.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.