PHP code example of la-87 / aipromptbuilder

1. Go to this page and download the library: Download la-87/aipromptbuilder 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/ */

    

la-87 / aipromptbuilder example snippets


return [
    'api_key' => env('OPENAI_API_KEY', ''),
    'cache_ttl' => env('AI_PROMPT_CACHE_TTL', 600),
];

use LA87\AIPromptBuilder\Services\AIPromptBuilderService;
use LA87\AIPromptBuilder\Enums\AIModelEnum;
use OpenAI\Client;

$client = app(Client::class);

$service = new AIPromptBuilderService($client);

$response = $service->setModel(AIModelEnum::GPT4)
                    ->setPrompt('What is the capital of France?')
                    ->setRole('You are a knowledgeable assistant.')
                    ->ask();

echo $response->completion;

use LA87\AIPromptBuilder\Contracts\AIFunctionInterface;
use stdClass;

class GetWeatherFunction implements AIFunctionInterface
{
    public function getName(): string
    {
        return 'getWeather';
    }

    public function getDescription(): string
    {
        return 'Fetches the current weather for a given location.';
    }

    public function getParametersSchema(): array
    {
        return [
            'type' => 'object',
            'properties' => [
                'location' => [
                    'type' => 'string',
                    'description' => 'The location to get the weather for.'
                ]
            ],
            '

$service->setFunctionCalls([new GetWeatherFunction()])
        ->askAndExecute();

$result = $service->getFunctionResult('getWeather');
echo $result;

$models = $service->listModels();
print_r($models);

try {
    $service->askAndExecute();
    $result = $service->getFunctionResult('getWeather');
    echo $result;
} catch (MissingFunctionCallException $e) {
    echo "Function call missing!";
} catch (MissingFunctionResultException $e) {
    echo "Function result missing!";
}
bash
php artisan vendor:publish --provider="LA87\AIPromptBuilder\AIPromptBuilderServiceProvider"