Tectalic OpenAI REST API Client
Introduction
The Tectalic OpenAI REST API Client offers a seamless and effective way for developers to interact with the OpenAI API directly from their PHP applications. Although unofficial and unaffiliated with OpenAI, this package provides extensive support for various models, including ChatGPT, GPT-4, GPT-3.5, Codex, DALL·E, and Whisper. It simplifies the interaction through fully typed Data Transfer Objects (DTOs) and offers IDE autocomplete, making it both developer-friendly and efficient.
Features
The package supports a wide array of functionalities across different OpenAI models:
- Text and Chat Completion: With models like GPT-3.5 and GPT-4, developers can generate text completions, enabling features like sophisticated chatbots and natural language understanding in applications.
- Code Completion: Codex models provide tools for smart code suggestions and completions ideal for development environments.
- Image Generation: Using the DALL·E model, images can be generated based on textual descriptions, enhancing creative and design applications.
- Speech to Text and Translation: The Whisper model offers features for transcribing audio to text and translating audio into English, supporting over 50 languages.
Code Integration Examples
Chat Completion Example
Developers can easily initiate a chat completion using simple PHP code, as illustrated below with GPT-4:
$response = $openaiClient->chatCompletions()->create(
new \Tectalic\OpenAi\Models\ChatCompletions\CreateRequest([
'model' => 'gpt-4',
'messages' => [
[
'role' => 'user',
'content' => 'Will using a well designed and supported third party package save time?'
],
],
])
)->toModel();
Image Generation Example
For generating images, the following code leverages the DALL·E model to create images with specific prompts:
$response = $openaiClient->imagesGenerations()->create(
new \Tectalic\OpenAi\Models\ImagesGenerations\CreateRequest([
'prompt' => 'A cute baby sea otter wearing a hat',
'size' => '256x256',
'n' => 5
])
)->toModel();
Installation
To integrate it into your project, make sure your PHP version is 7.2.5 or newer. Install through Composer using the following command:
composer require tectalic/openai
It's also important to have a compatible PSR-18 HTTP client like Guzzle or Symfony HTTP Client configured in your project.
Usage Guide
After installation, you can use the Tectalic OpenAI REST API Client
by building it with your HTTP client and authentication key:
use Symfony\Component\HttpClient\Psr18Client;
use Tectalic\OpenAi\Authentication;
use Tectalic\OpenAi\Manager;
$auth = new Authentication(getenv('OPENAI_API_KEY'));
$httpClient = new Psr18Client();
Manager::build($httpClient, $auth);
Error Handling
This client uses exceptions to handle errors. For instance, if an unsatisfactory HTTP status code is returned, a ClientException
will be thrown, guiding developers to manage API errors effectively:
try {
$model = $handler->create()->toModel();
} catch (ClientException $e) {
// Handle error...
}
Conclusion
Overall, the Tectalic OpenAI REST API Client makes it remarkably easy to incorporate advanced AI capabilities into PHP applications. From text completion to image generation, it encapsulates a diverse set of tools that can expand the potential of any project utilizing artificial intelligence. Despite the package no longer being actively maintained, it represents a robust solution for those seeking to leverage OpenAI's services through PHP.