PHP code example of jonaspoelmans / laravel-gpt

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

    

jonaspoelmans / laravel-gpt example snippets


// Create your prompt for the LLM
$prompt = "Give me the list of 10 best PHP frameworks."

// Instantiate the Laravel GPT service
$laravelGPT = new LaravelGPTService();

// Retrieve a response from ChatGPT
$response = $laravelGPT->generateOpenAIResponse($prompt);


// Prior messages are generated by a user, the system or the chat assistant
$priorMessageUser = new OpenAIMessage('user', 'I love Laravel but exclude it from the list.');
$priorMessageAssistant = new OpenAIMessage('assistant', 'Which list?');
$history = [$priorMessageUser, $priorMessageAssistant];

// The prompt can be fed to ChatGPT alongside the chat history
$response = $laravelGPT->generateOpenAIResponse($prompt, $history);


// The base URI for the OpenAI API.
// This is the endpoint where all API requests will be sent.
'openai_base_uri' => 'https://api.openai.com/v1/',

// The default model to be used for generating responses.
// You can change this to any valid model identifier provided by OpenAI,
// such as 'gpt-3.5-turbo' or 'gpt-4-1106-preview'.
'openai_model' => 'gpt-4-1106-preview',

// The maximum number of tokens to generate in the response.
// Tokens can be thought of as pieces of words. The maximum number
// of tokens allowed is determined by the model you are using.
'openai_max_tokens' => 4000,

// The temperature setting for the response generation.
// Temperature controls the randomness of the output.
// A value closer to 0 makes the output more deterministic and repetitive,
// while a value closer to 1 makes it more random.
'openai_temperature' => 0.7,

// Enable or disable logging of errors.
// When set to true, any errors encountered while using the API
// will be logged using Laravel's built-in logging system.
'openai_logging' => true,