1. Go to this page and download the library: Download texhub/openai library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
texhub / openai example snippets
use TexHub\OpenAI\OpenAI;
$ai = OpenAI::make('sk-...'); // or with org/project: OpenAI::make('sk-...', 'org-...', 'proj-...')
// Simplest possible — just text in, text out:
echo $ai->chat()->text('Объясни квантовую запутанность одним предложением.');
// With a system prompt and options:
$response = $ai->chat()->ask(
'Напиши слоган для кофейни',
system: 'Ты креативный копирайтер.',
options: ['model' => 'gpt-4o', 'temperature' => 0.9],
);
echo $response->content();
echo $response->totalTokens();
use TexHub\OpenAI\Builders\Tool;
use TexHub\OpenAI\Builders\Message;
$response = $ai->chat()->create([
'messages' => [Message::user('Какая погода в Душанбе?')],
'tools' => [
Tool::function('get_weather', 'Получить погоду по городу', Tool::objectSchema(
properties: ['city' => ['type' => 'string']],
Душанбе?'),
$response->message(),
Message::toolResult($call['id'], json_encode($result)),
],
'tools' => [/* same tools */],
]);
echo $final->content();
}
}
use TexHub\OpenAI\Builders\Message;
// Remote URL:
$ai->chat()->vision('Что на картинке?', ['https://example.com/photo.jpg']);
// Local file as a data URL:
$ai->chat()->vision('Опиши фото', [Message::imageDataUrl('/path/to/photo.jpg')]);
$json = $ai->chat()->json([Message::user('Верни {"city","country"} для Душанбе')]);
$ai->chat()->streamText('Расскажи историю', function (string $delta) {
echo $delta; // tokens arrive live
});
use TexHub\OpenAI\Builders\Tool;
// 1) Create a reusable assistant
$assistant = $ai->assistants()->create('gpt-4o', [
'name' => 'Data Analyst',
'instructions' => 'Ты аналитик. Отвечай кратко, считай через code interpreter.',
'tools' => [Tool::codeInterpreter()],
]);
// 2) Create a thread, add a message, run, and wait for completion
$thread = $ai->threads()->create();
$ai->threads()->addMessage($thread->id(), 'Посчитай факториал 10');
$run = $ai->threads()->runAndPoll($thread->id(), $assistant->id());
// 3) Read the assistant's reply
foreach ($ai->threads()->messages($thread->id())->data() as $message) {
// newest first
}
// Per-request token usage is on every response:
$ai->chat()->ask('hi')->usage(); // ['prompt_tokens'=>..,'completion_tokens'=>..,'total_tokens'=>..]
// Org-wide analytics (