PHP code example of elliottlawson / converse-prism

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

    

elliottlawson / converse-prism example snippets


// Extract messages from Converse 😓
$messages = [];
foreach ($conversation->messages as $message) {
    $messages[] = [
        'role' => $message->role,
        'content' => $message->content
    ];
}

// Manually configure Prism
$prism = Prism::text()
    ->using(Provider::OpenAI, 'gpt-4')
    ->withMessages($messages)
    ->withMaxTokens(500);

// Make the call
$response = $prism->generate();

// Figure out metadata storage...
$conversation->messages()->create([
    'role' => 'assistant',
    'content' => $response->text,
    // What about tokens? Model info? 🤷
]);

// Everything flows automatically ✨
$response = $conversation
    ->toPrismText()
    ->using(Provider::OpenAI, 'gpt-4')
    ->withMaxTokens(500)
    ->asText();

// Store response with all metadata
$conversation->addPrismResponse($response->text);

use ElliottLawson\ConversePrism\Concerns\HasAIConversations;

class User extends Authenticatable
{
    use HasAIConversations; // Replaces the base Converse trait
}

use Prism\Enums\Provider;

// Build the conversation context
$conversation = $user->startConversation(['title' => 'My Chat'])
    ->addSystemMessage('You are a helpful assistant')
    ->addUserMessage('Hello! What is Laravel?');

// Make your AI call with automatic message passing
$response = $conversation
    ->toPrismText()
    ->using(Provider::OpenAI, 'gpt-4')
    ->withMaxTokens(500)
    ->asText();

// Store the AI's response with metadata
$conversation->addPrismResponse($response->text);
bash
php artisan migrate