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:
Create an OpenAI account at https://beta.openai.com/
Create an API key at https://beta.openai.com/account/api-keys
Make a variable to store your API key in, let's say $apiKey
Create a new PHPOpenAIChat object, passing in your API key, like this:
$openAIChat = new PHPOpenAIChat($apiKey);
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 />";
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.
You can also estimate the token count of the messages to send to the model, and subtracts from max_tokens.
You should be able to get the text from the response by using $response['choices'][0]['message']['content']
You can append the text from the response to the messages to send to the model
You can also use the add_prompt_to_messages() function to add a prompt to the current conversation
Then you can repeat the cycle again by sending the messages to the model and getting another response.
Download: https://helurl.com/drive/s/dnZ9JBKWDSy4blbuLLHhRJTMyt0zCG
Cre: PHPClasses