A GPT-4 and GPT-3.5 ChatGPT API Client for Your PHP Applications

2 Mar 2023


Since releasing our OpenAI PHP API Client in October last year, we’ve seen thousands of users build some amazing applications using the OpenAI API.

Over that time, OpenAI introduced the DALL·E API, which allows you to generate and edit images from text prompts.

Today OpenAI has released the ChatGPT and Whisper APIs, and we’re proud to announce that our OpenAI PHP client library supports these new endpoints.

Below are some new use cases that are now possible using version 1.4.0 of the tectalic/openai open source package.

ChatGPT Text Completion (GPT-4 & GPT-3.5)

ChatGPT has taken the OpenAI API to the next level, allowing you to create a chatbot that can respond to a series of messages.

Below is an example of how to use the new ChatGPT API, using the latest gpt-4 model.

The input is a series of messages between the system, a user and an assistant (ChatGPT), and the output is a new message from the ChatGPT assistant.

$openaiClient = \Tectalic\OpenAi\Manager::build(new \GuzzleHttp\Client(), new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY')));
/** @var \Tectalic\OpenAi\Models\ChatCompletions\CreateResponse $response */
$response = $openaiClient->chatCompletions()->create(
new \Tectalic\OpenAi\Models\ChatCompletions\CreateRequest([
'model' => 'gpt-4',
'messages' => [
['role' => 'system', 'content' => 'You are a helpful assistant'],
['role' => 'user', 'content' => 'Who won the world series in 2020?'],
['role' => 'assistant', 'content' => 'The Los Angeles Dodgers won the World Series in 2020.'],
['role' => 'user', 'content' => 'Where was it played?'],
echo $response->choices[0]->message->content;
// The 2020 World Series was played at Globe Life Field in Arlington, Texas. It was a neutral site due to COVID-19 pandemic restrictions.

This Chat Completions handler supports both the GPT-4 and GPT-3.5 models, including gpt-4, gpt-3.5-turbo, text-davinci-003, text-davinci-002 and more.

Please see OpenAI’s guide to chat completion for more details.

Speech to Text using Whisper

OpenAI’s Whisper speech to text service is now also available via an API.

This allows you to transcribe audio files into text, and translate audio files into english text.

It currently accepts audio inputs in a variety of file formats, including m4a, mp3, mp4, mpeg, mpga, wav, and webm.

Audio Transcription

The following example takes an audio file and transcribes it into text using the Whisper API.

This transcription is in the source language of the audio file, and OpenAI currently supports 50+ languages.

$openaiClient = \Tectalic\OpenAi\Manager::build(new \GuzzleHttp\Client(), new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY')));
/** @var \Tectalic\OpenAi\Models\AudioTranscriptions\CreateResponse $response */
$response = $openaiClient->audioTranscriptions()->create(
new \Tectalic\OpenAi\Models\AudioTranscriptions\CreateRequest([
'file' => '/full/path/to/audio/file.mp3',
'model' => 'whisper-1',
echo $response->text;
// Your audio transcript in one of 50+ source languages...

Audio Translation

This example is very similar to the previous example, but instead of transcribing the audio into the source language, it transcribes it into English.

This allows you to use the Whisper API to translate the audio from 50+ languages into English text.

$openaiClient = \Tectalic\OpenAi\Manager::build(new \GuzzleHttp\Client(), new \Tectalic\OpenAi\Authentication(getenv('OPENAI_API_KEY')));
/** @var \Tectalic\OpenAi\Models\AudioTranslations\CreateResponse $response */
$response = $openaiClient->audioTranslations()->create(
new \Tectalic\OpenAi\Models\AudioTranslations\CreateRequest([
'file' => '/full/path/to/audio/file.mp3',
'model' => 'whisper-1',
echo $response->text;
// Your audio transcript in English...

Please see OpenAI’s guide to speech to text for more details.

Other Features

The open source tectalic/openai package is a fully featured PHP client for the OpenAI API.

It includes a number of other features, including:

  • Fully typed Data Transfer Objects (DTOs) for all API requests and responses.
  • IDE Autocompletion: All API endpoints, requests and responses are fully documented within advanced IDEs such as Visual Studio Code or PhpStorm.
  • A convenient fluent interface for interacting with the OpenAI API.
  • All 23 API Endpoints Supported: Please see the documentation for a full list of supported API methods.
  • Fully Tested by our developers, with unit and functional integration tests. All test cases are provided.
  • Fully Supported by our Australian-based team of developers.

Getting Started

You can find the tectalic/openai package available on Packagist.org and GitHub.

Please see our comprehensive documentation and getting started guide: how to build a PHP app using the OpenAI API, which includes full code snippets, videos and a full starter (skeleton) app.

Questions or Feedback?

We welcome your feedback, and we’re excited to see what you do with this package.

What do you like about it? What do you think could be improved?

You can reach out to us here, or via our contact form.