This PHP library provides a simple wrapper for the Mistral API, allowing you to easily integrate the Mistral API into your PHP projects.
- Easy integration with Mistral API
- Supports all Mistral API endpoints
- Utilizes PSR-17 and PSR-18 compliant HTTP clients, and factories for making API requests
- PHP 7.4 or higher
- A PSR-17 HTTP Factory implementation (e.g., guzzle/psr7 or nyholm/psr7)
- A PSR-18 HTTP Client implementation (e.g., guzzlehttp/guzzle or symfony/http-client)
You can install the library via Composer:
composer require softcreatr/php-mistral-ai-sdk
First, include the library in your project:
<?php
require_once 'vendor/autoload.php';
Then, create an instance of the MistralAI
class with your API key, organization (optional), an HTTP client, an HTTP request factory, and an HTTP stream factory:
use SoftCreatR\MistralAI\MistralAI;
$apiKey = 'your_api_key';
// Replace these lines with your chosen PSR-17 and PSR-18 compatible HTTP client and factories
$httpClient = new YourChosenHttpClient();
$requestFactory = new YourChosenRequestFactory();
$streamFactory = new YourChosenStreamFactory();
$uriFactory = new YourChosenUriFactory();
$mistral = new MistralAI($requestFactory, $streamFactory, $uriFactory, $httpClient, $apiKey);
Now you can call any supported MistralAI API endpoint using the magic method __call
:
$response = $mistral->createChatCompletion([
'model' => 'mistral-tiny',
'messages' => [
[
'role' => 'user',
'content' => 'Who is the most renowned French painter?'
],
],
]);
// Process the API response
if ($response->getStatusCode() === 200) {
$responseObj = json_decode($response->getBody()->getContents(), true);
print_r($responseObj);
} else {
echo "Error: " . $response->getStatusCode();
}
For more details on how to use each endpoint, refer to the Mistral API documentation, and the examples provided in the repository.
- Create Chat Completion - Example
createChatCompletion(array $options = [])
- Create Chat Completion - Example
createEmbedding(array $options = [])
- List Models - Example
listModels()
For a detailed list of changes and updates, please refer to the CHANGELOG.md file. We adhere to Semantic Versioning and document notable changes for each release.
Currently, streaming is not supported. It's planned to address this limitation asap. For now, please be aware that these methods cannot be used for streaming purposes.
If you require streaming functionality, consider using an alternative implementation or keep an eye out for future updates to this library.
This library is licensed under the ISC License. See the LICENSE file for more information.
Sascha Greuel |