PHP code example of creativecrafts / laravel-ai-assistant

1. Go to this page and download the library: Download creativecrafts/laravel-ai-assistant 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/ */

    

creativecrafts / laravel-ai-assistant example snippets


    'api_key' => env('OPENAI_API_KEY', null),
    'organization' => env('OPENAI_ORGANIZATION', null),

    'model' => env('OPENAI_CHAT_MODEL', 'gpt-3.5-turbo'),
    'chat_model' => env('OPENAI_CHAT_MODEL', 'gpt-3.5-turbo'),
    'edit_model' => 'gpt-4o',
    'audio_model' => 'whisper-1',

    'temperature' => 0.3,
    'top_p' => 1,
    'max_completion_tokens' => 400,
    'stream' => false,
    'n' => 1,
    'stop' => null,
    'suffix' => null,
    'presence_penalty' => 0,
    'frequency_penalty' => 0,
    'best_of' => 1,

    'ai_role' => 'assistant',
    'user_role' => 'user',

    use CreativeCrafts\LaravelAiAssistant\Tasks\AiAssistant;
    $assistant = AiAssistant::init();

    $assistant->setModelName('gpt-3.5-turbo')
          ->adjustTemperature(0.7)
          ->setDeveloperMessage('Please maintain a friendly tone.')
          ->setUserMessage('What is the weather like today?');

    $response = $assistant->sendChatMessage();

    $assistant->setFilePath('/path/to/audio.mp3');
    $transcription = $assistant->transcribeTo('en', 'Transcribe the following audio:');

    // Adding a custom function tool
    $assistant->includeFunctionCallTool(
        'calculateSum',
        'Calculates the sum of two numbers',
        ['num1' => 'number', 'num2' => 'number'],
        isStrict: true,
        

    return [
        'api_key' => env('OPENAI_API_KEY', null),
        'organization' => env('OPENAI_ORGANIZATION', null),
        'model' => env('OPENAI_CHAT_MODEL', 'gpt-3.5-turbo'),
        'temperature' => 0.3,
        'top_p' => 1,
        'max_completion_tokens' => 400,
        'stream' => false,
        'n' => 1,
        'stop' => null,
        'chat_model' => env('OPENAI_CHAT_MODEL', 'gpt-3.5-turbo'),
        'ai_role' => 'assistant',
        'user_role' => 'user',
        'edit_model' => 'gpt-4o',
        'audio_model' => 'whisper-1',
    ];

    use CreativeCrafts\LaravelAiAssistant\AiAssistant;

    $assistant = AiAssistant::init()
        ->setModelName('gpt-3.5-turbo')
        ->adjustTemperature(0.7)
        ->setDeveloperMessage('Maintain a formal tone.')
        ->setUserMessage('Tell me a joke.')
        ->sendChatMessage();

    use CreativeCrafts\LaravelAiAssistant\AiAssistant;
    
    $transcription = AiAssistant::init()
        ->setFilePath('/path/to/audio.mp3');
        ->transcribeTo('en', 'Transcribe this audio:');

    use CreativeCrafts\LaravelAiAssistant\AiAssistant;
    
    $response = AiAssistant::init()
        ->includeFunctionCallTool(
            'calculateSum',
            'Calculates the sum of two numbers',
            ['num1' => 'number', 'num2' => 'number'],
            isStrict: true,
            

use CreativeCrafts\LaravelAiAssistant\AiAssistant;

$assistant = AiAssistant::init()
    ->setModelName('gpt-4') // Optional, defaults to config default model
    ->adjustTemperature(0.5) // Optional defaults to 0.7
    ->setAssistantName('My Assistant') // Optional, defaults to '' and Open Ai will assign random name
    ->setAssistantDescription('An assistant for handling tasks') // Optional, defaults to ''
    ->setInstructions('Be as helpful as possible.') // Optional, defaults to ''
    ->create();

    use CreativeCrafts\LaravelAiAssistant\AiAssistant;
    
    $response = \CreativeCrafts\LaravelAiAssistant\AiAssistant::init()
        ->setAssistantId($assistantId) // Required
        ->createTask() // Can optionally pass a list of tasks as an array, defaults to []
        ->askQuestion('Translate this text to French: "Hello, how are you?"')
        ->process()
        ->response(); // returns the response from the assistant as a string
bash
php artisan vendor:publish --tag="ai-assistant-config"