PHP code example of blinq / synth

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

    

blinq / synth example snippets


use Blinq\Synth\Modules\Module;

/**
 * Class MyModule
 * 
 * @propery \Blinq\Synth\Commands\SynthCommand $cmd
 */
class MyModule extends Module
{
    public function name(): string
    {
        return 'MyModule';
    }

    public function register(): array
    {
        return [
            'action1' => 'Perform Action 1',
            'action2' => 'Perform Action 2',
        ];
    }

    public function onSelect(?string $key = null)
    {
        $this->cmd->info("You selected: {$key}");

        $synth = $this->cmd->synth;

        if ($key === 'action1') {
            // Handle Action 1
            while (true) {
                $input = $this->cmd->ask("You");

                // Send the input to GPT
                $synth->chat($input, [
                    // ... The OpenAI Chat options

                    // If you want a function to be called by GPT
                    'function_call' => ['name' => 'some_function'], // Forces the function call
                    'functions' => [
                        [
                            'name' => 'some_function',
                            'description' => 'Description of the function',
                            'parameters' => [
                                // ..schema
                            ]
                        ]
                    ]
                ]);

                Functions::register('some_function', function (SynthCommand $cmd, $args, $asSpecified, $inSchema) { // etc..
                    // Do something with the call
                });

                // This will parse the json result and call the function if needed
                $synth->handleFunctionsForLastMessage();

                // Just retrieve the last message
                $lastMessage = $synth->getLastMessage();

                // Echo it's contents
                echo $lastMessage->content;

                // Or it's raw function_call
                dump($lastMessage->function_call);

                if (!$input || $input == 'exit') {
                    break;
                }
            }
        }
        if ($key === 'action2') {
            // Handle Action 2
        }
    }
}

use Blinq\Synth\Modules;

Modules::register(MyModule::class);
bash
   php artisan vendor:publish --tag=synth-config
   
bash
php artisan synth