1. Go to this page and download the library: Download in2code/texter 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/ */
in2code / texter example snippets
declare(strict_types=1);
namespace Vendor\MyExtension\Domain\Repository\Llm;
use In2code\Texter\Domain\Repository\Llm\AbstractRepository;
use In2code\Texter\Domain\Repository\Llm\RepositoryInterface;
use In2code\Texter\Domain\Service\ConversationHistory;
use In2code\Texter\Exception\ApiException;
use In2code\Texter\Exception\ConfigurationException;
use TYPO3\CMS\Core\Http\RequestFactory;
class ChatGptRepository extends AbstractRepository implements RepositoryInterface
{
private string $apiKey = '';
private string $apiUrl = 'https://api.openai.com/v1/chat/completions';
public function __construct(
protected RequestFactory $requestFactory,
protected ConversationHistory $conversationHistory,
) {
parent::__construct($requestFactory, $conversationHistory);
$this->apiKey = getenv('OPENAI_API_KEY') ?: '';
}
public function checkApiKey(): void
{
if ($this->apiKey === '') {
throw new ConfigurationException('OpenAI API key not configured', 1735646000);
}
}
public function getApiUrl(): string
{
return $this->apiUrl;
}
public function getText(string $prompt, string $pageId = '0'): string
{
$this->checkApiKey();
$history = $this->conversationHistory->getHistory($pageId);
$this->conversationHistory->addUserMessage($history, $this->extendPrompt($prompt));
$response = $this->connectToChatGpt($history);
$this->conversationHistory->addModelResponse($history, $response);
$this->conversationHistory->saveHistory($history, $pageId);
return $response;
}
protected function connectToChatGpt(array $conversationHistory): string
{
// Convert Gemini format to ChatGPT format
$messages = $this->convertHistoryToChatGptFormat($conversationHistory);
$payload = [
'model' => 'gpt-4o',
'messages' => $messages,
'temperature' => 0.7,
'max_tokens' => 8192,
];
$options = [
'headers' => [
'Authorization' => 'Bearer ' . $this->apiKey,
'Content-Type' => 'application/json',
],
'body' => json_encode($payload),
];
$response = $this->requestFactory->request($this->getApiUrl(), $this->requestMethod, $options);
if ($response->getStatusCode() !== 200) {
throw new ApiException(
'Failed to generate text with ChatGPT: ' . $response->getBody()->getContents(),
1735646001
);
}
$responseData = json_decode($response->getBody()->getContents(), true);
if (isset($responseData['choices'][0]['message']['content']) === false) {
throw new ApiException('Invalid ChatGPT API response structure', 1735646002);
}
return $responseData['choices'][0]['message']['content'];
}
protected function convertHistoryToChatGptFormat(array $geminiHistory): array
{
$messages = [];
foreach ($geminiHistory as $entry) {
$role = $entry['role'] === 'model' ? 'assistant' : $entry['role'];
$content = $entry['parts'][0]['text'] ?? '';
$messages[] = [
'role' => $role,
'content' => $content,
];
}
return $messages;
}
}