thojou/prompt-template

PHP library for creating AI interaction prompts.

dev-master 2023-10-20 12:37 UTC

This package is auto-updated.

Last update: 2024-05-20 13:48:17 UTC


README

Static Badge CI Coverage License

The PHP Prompt Template is a library designed to simplify dynamic text generation in AI projects. This library empowers you to effortlessly incorporate placeholders into your text and render them with dynamic values. Thanks to yethee/tiktoken-php, this library also provides a straightforward way to count and retrieve the tokens of a prompt.

Requirements

  • PHP version >= 8.1

Installation

You can effortlessly install the PHP Prompt Template using the popular package manager composer.

composer require thojou/php-prompt-template

Usage

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Thojou\PromptTemplate\Prompt;
use Thojou\PromptTemplate\PromptTemplate;

// Create a new prompt
$prompt = new Prompt('Hello World!');

// Render a prompt template
$template = new PromptTemplate('Hello {{name}}!');
$renderedPrompt = $template->render(['name' => 'Alice']);

echo $prompt;           // Outputs: "Hello World!"
echo $renderedPrompt;   // Outputs: "Hello Alice!"

echo $prompt->getTokenCount('gpt-3.5-turbo'); // Outputs: 3
echo join(', ', $prompt->getTokens('gpt-3.5-turbo')); // Outputs: 15496, 2159, 0

For more practical examples, please refer to the examples folder.

License

This project is licensed under the generous and permissive MIT license.