PHP code example of partitech / php-mistral

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

    

partitech / php-mistral example snippets


$client = new MistralClient($apiKey);
$messages = $client->getMessages()->addUserMessage('What is the best French cheese?');

$params = [
    'model' => 'mistral-large-latest',
        'temperature' => 0.7,
        'top_p' => 1,
        'max_tokens' => null,
        'safe_prompt' => false,
        'random_seed' => 0
];

try {
    /** @var Message $chunk */
    foreach ($client->chat(messages: $messages, params: $params, stream: true) as $chunk) {
        echo $chunk->getChunk();
    }
} catch (MistralClientException|DateMalformedStringException $e) {
    echo $e->getMessage();
    exit(1);
}

use Partitech\PhpMistral\Clients\Tei\TeiClient;

$client = new TeiClient(apiKey: $apiKey, url: $embeddingUrl);
$embedding = $client->embed(inputs: "My name is Olivier and I");

$client = new TeiClient(apiKey: (string) $apiKey, url: $teiUrl);
$embedding = $client->embedSparse(inputs: 'What is Deep Learning?');
var_dump($embedding);

$client = new TeiClient(apiKey: $apiKey, url: $teiUrl);
$results = $client->rerank(
        query: 'What is the difference between Deep Learning and Machine Learning?',
        texts: [
            'Deep learning is...',
            'cheese is made of',
            'Deep Learning is not...'
        ]

    );

use Partitech\PhpMistral\Clients\HuggingFace\HuggingFaceDatasetClient;

$client = new HuggingFaceDatasetClient(apiKey: $apiKey);

// Commit large dataset to huggingface repository
$commit = $client->commit(
    repository: $hfUser . '/test2',
    dir: realpath('mon_dataset'),
    files: $client->listFiles('./dir'),
    summary: 'commit title',
    commitMessage: 'commit message',
    branch: 'main'
);

// get specific dataset page
$paginatedRows = $client->rows(
        dataset: 'nvidia/OpenCodeReasoning', 
        split: 'split_0', 
        config: 'split_0', 
        offset: 3, 
        length: 2 
    );

// Download Dataset locally
$dest = $client->downloadDatasetFiles(
    'google/civil_comments',
    revision: 'main',
    destination: '/tmp/downloaded_datasets/civil_comments'
);
bash
composer