PHP code example of noah-medra / prompt-builder

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;

$builder = PromptBuilder::make();

use NoahMedra\PromptBuilder\Drivers\HuggingFaceDriver;

$builder->driver(HuggingFaceDriver::class);

$builder->instruction("### Financial History")
        ->instruction("We will also review your financial history to identify trends.")
        ->when(
            true,  // This condition is true, so the sub-instructions will be applied
            function($builder) {
                $builder->instruction("Here is a summary of your financial history for the last three months.");
                     ->when(false, fn($ist) => $ist->add('It seems there are no significant changes in your financial behavior this month. We’ll continue to monitor this trend for future insights.'))
                $builder->instruction("Your balance has fluctuated between X and Y. It seems like you had some unexpected expenses.");
            }
        );

$builder->withParams(['key' => 'value']);

$builder->context("You are a virtual assistant with expertise in financial analysis.");

$builder->ask("What are the main trends in the past three months of my financial data?");

$builder->useHistory(true);

$builder->process();
$output = $builder->getOutput();
echo $output->('message.content'); // Display the generated response
echo $output->('model'); // Display the model's response

$builder->expectResponseFormat('{"resume": "Summary of the response", "response": "Your response here"}');

namespace App\Drivers;

use Exception;
use Illuminate\Support\Facades\Http;
use NoahMedra\PromptBuilder\BuilderInput;
use NoahMedra\PromptBuilder\BuilderOutput;
use NoahMedra\PromptBuilder\Drivers\DriverInterface;

class OllamaDriver implements DriverInterface
{
    public function process(BuilderInput $input) : BuilderOutput
    {
        return new BuilderOutput($this->executePrompt($input));
    }

    private function executePrompt(BuilderInput $input) : string
    {
            $response = Http::withHeaders([
                'Content-Type' => 'application/x-www-form-urlencoded',
            ])->post('http://localhost:11434/api/chat', [
                'model' => 'llama3.1',
                'stream' => false,
                'messages' => [
                    [
                        'role' => 'user',
                        'content' => $input->getPromptText(),
                    ]
                ],
                ...($input->getParams() ?? [])
            ]);

            if ($response->failed()) {
                throw new Exception($response->body());
            }

            return $response->body();
    }
}

use NoahMedra\PromptBuilder\PromptBuilder;

$builder = PromptBuilder::make();

// Add parameters, instructions, context, and a question
$builder->withParams(['lang' => 'en'])
        ->instruction("Provide a clear and concise answer.")
        ->instruction("The response should be in JSON format.")
        ->instruction("Include the following sub-instructions:")
        ->when(true, function($builder){
                $builder
                    ->instruction("Ensure the answer is clear and easy to understand.")
                    ->instruction("Be concise and avoid unnecessary details.")
        })
        ->context("You are an expert in financial analysis.")
        ->ask("What are the main trends in the past three months of my financial data?");

// Generate and get the response
$builder->process();
$output = $builder->getOutput();

echo $output->get('message.content');