1. Go to this page and download the library: Download noah-medra/prompt-builder 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/ */
noah-medra / prompt-builder example snippets
use NoahMedra\PromptBuilder\PromptBuilder;
use NoahMedra\PromptBuilder\Drivers\OllamaDriver;
$builder = PromptBuilder::make()
->persona('Tu es un professeur de mathématiques bienveillant et rigoureux.')
->context("L'élève est en terminale et prépare son bac.")
->must('Réponds en français')
->mustNot('Ne donne jamais la réponse finale sans expliquer le raisonnement')
->example('Résous x + 2 = 5', 'On isole x : x = 5 - 2 = 3.')
->withParams(['sujet' => 'les suites numériques', 'ton' => 'encourageant'])
->instruction('Adopte un ton {ton} en abordant {sujet}.')
->language('fr') // section labels in French (default: English)
->ask('Explique ce qu\'est une suite arithmétique.');
// 1. Preview the composed prompt WITHOUT any network call:
echo $builder->toPrompt();
// 2. Execute it against a model:
$output = $builder->driver(new OllamaDriver())->process()->getOutput();
echo $output->get('message.content');
$builder->instruction('Structure your answer', function ($ist) {
$ist->add('Start with a recap')
->add('Give a worked example')
->add('End with an exercise', callback: function ($sub) {
$sub->add('Include its solution');
});
});
$builder->withParams(['tone' => 'formal', 'user' => ['name' => 'Sam']])
->instruction('Answer in a {tone} tone to {user.name}.');
// -> "Answer in a formal tone to Sam."
$builder->when($user->isPremium(),
fn ($b) => $b->must('Include an in-depth analysis'),
fn ($b) => $b->must('Keep it short'),
);
use NoahMedra\PromptBuilder\Rendering\TextRenderer;
use NoahMedra\PromptBuilder\Translation\ArrayTranslator;
// Custom catalog directory, laid out as {locale}/labels.php
$renderer = new TextRenderer(new ArrayTranslator('es', '/path/to/lang'));
use NoahMedra\PromptBuilder\Rendering\TextRenderer;
use NoahMedra\PromptBuilder\Translation\Laravel\LaravelTranslator;
$renderer = new TextRenderer(new LaravelTranslator(app('translator')));
$builder->driver($driver) // a PromptDriverInterface instance or class-string
->process(); // renders the PromptSpec and sends it
$output = $builder->getOutput(); // ?BuilderOutput (null before process())
$output->get('message.content'); // dotted-path access into decoded JSON
$output->getRaw(); // the raw response string
use NoahMedra\PromptBuilder\Drivers\OllamaDriver;
use NoahMedra\PromptBuilder\Rendering\ChatMessagesRenderer;
$driver = new OllamaDriver(
model: 'llama3.1',
endpoint: 'http://localhost:11434/api/chat',
renderer: new ChatMessagesRenderer(),
timeoutSeconds: 30,
);
use NoahMedra\PromptBuilder\BuilderOutput;
use NoahMedra\PromptBuilder\Drivers\PromptDriverInterface;
use NoahMedra\PromptBuilder\PromptSpec;
use NoahMedra\PromptBuilder\Rendering\ChatMessagesRenderer;
class MyDriver implements PromptDriverInterface
{
public function process(PromptSpec $spec): BuilderOutput
{
$messages = (new ChatMessagesRenderer())->render($spec);
// ...send $messages to your API...
return new BuilderOutput($responseBody);
}
}
use NoahMedra\PromptBuilder\History\InMemoryHistoryStore;
$store = new InMemoryHistoryStore(); // default store; works anywhere
PromptBuilder::make()->useHistory($store)->driver($driver)->ask('Bonjour')->process();
PromptBuilder::make()->useHistory($store)->driver($driver)->ask('Et ensuite ?')->process();
// The second call is sent the first question + answer as chat history.
use NoahMedra\PromptBuilder\Facades\PromptBuilder;
$prompt = PromptBuilder::make()
->context('...')
->ask('...')
->toPrompt();
text
# Rôle
Tu es un professeur de mathématiques bienveillant et rigoureux.
# Contexte
L'élève est en terminale et prépare son bac.
# Exemples
Exemple 1 :
Entrée : Résous x + 2 = 5
Sortie attendue : On isole x : x = 5 - 2 = 3.
# Instructions
- [Obligatoire] Réponds en français
- [Interdit] Ne donne jamais la réponse finale sans expliquer le raisonnement
- Adopte un ton encourageant en abordant les suites numériques.
# Question
Explique ce qu'est une suite arithmétique.