PHP code example of aisdk / mistral

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

    

aisdk / mistral example snippets


use AiSdk\Generate;
use AiSdk\Mistral;

$result = Generate::text()
    ->model(Mistral::model('mistral-large-latest'))
    ->instructions('Write short, clear answers.')
    ->prompt('Explain closures in PHP.')
    ->run();

echo $result->text;

Generate::model(Mistral::model('mistral-large-latest'));

$result = Generate::text('Explain closures in PHP.')->run();

$provider = Mistral::create([
    'apiKey' => 'mistral-...',
    'baseUrl' => 'https://api.mistral.ai/v1',
    'headers' => ['X-Custom-Header' => 'value'],
]);

use AiSdk\Generate;
use AiSdk\Mistral;

$stream = Generate::text('Tell me a story.')
    ->model(Mistral::model('mistral-large-latest'))
    ->stream();

foreach ($stream->chunks() as $chunk) {
    echo $chunk;
}

$result = $stream->run();

use AiSdk\Generate;
use AiSdk\Mistral;

$result = Generate::embedding(['Search query', 'Document text'])
    ->model(Mistral::model('mistral-embed'))
    ->providerOptions('mistral', ['user' => 'user-123'])
    ->run();

$queryVector = $result->embeddings[0]->vector;
$documentVector = $result->embeddings[1]->vector;

use AiSdk\Generate;
use AiSdk\Mistral;
use AiSdk\Schema;

$result = Generate::text()
    ->model(Mistral::model('mistral-large-latest'))
    ->prompt('Extract the city and country from: Lahore, Pakistan.')
    ->output(Schema::object(
        name: 'address',
        properties: [
            Schema::string(name: 'city')->

use AiSdk\Generate;
use AiSdk\Mistral;
use AiSdk\Schema;
use AiSdk\Tool;

$weather = Tool::make('weather', 'Get current weather')
    ->input(Schema::string(name: 'city')->er in Lahore?')
    ->tool($weather)
    ->run();

$result = Generate::text('Hello')
    ->model(Mistral::model('mistral-large-latest'))
    ->providerOptions('mistral', [
        'raw' => ['top_k' => 40],
    ])
    ->run();