partitech / php-mistral
Connect to MistralAi inference or to Llama.cpp
Installs: 3 773
Dependents: 1
Suggesters: 0
Security: 0
Stars: 15
Watchers: 2
Forks: 6
Open Issues: 0
Requires
- php: >=8.3
- ext-fileinfo: *
- ext-http: *
- ext-zip: *
- czproject/git-php: ^4.4
- knplabs/php-json-schema: ^0.1.0
- nyholm/psr7: ^1.8
- php-http/discovery: ^1.20
- php-http/multipart-stream-builder: ^1.4
- psr/http-client: ^1.0
- psr/http-client-implementation: *
- psr/http-factory-implementation: *
- ramsey/uuid: ^4.7
- symfony/mime: ^7.2
Requires (Dev)
- ext-curl: *
- guzzlehttp/guzzle: ^7.9
- php-http/curl-client: ^2.3
- php-http/message: ^1.16
- phpunit/phpunit: ^9.5
- symfony/http-client: ^7.2
README
Important
📖 Official Documentation is available here: https://php-mistral.partitech.com/
Make sure to visit for in-depth guides, examples, and advanced usage.
PhpMistral is an open-source PHP client designed to interact with various LLM inference servers (like Mistral, Hugging Face TGI, vLLM, Ollama, llama.cpp, xAI, and more), embedding servers, and Hugging Face datasets.
It provides a unified interface for chat completions (streaming & non-streaming), embeddings (dense & sparse), reranking, guided JSON generation, document generation, and Hugging Face dataset management.
Tip
Perfect for developers building AI-driven PHP applications: chatbots, document search, reranking, embeddings, or dataset management for finetuning.
🛠 PSR-18 Compatible HTTP Client
PhpMistral has been fully refactored to comply with PSR-18 recommendations (PHP Standards Recommendation for HTTP clients).
This means you can plug in any HTTP client that implements PSR-18, including:
- Guzzle
- Symfony HttpClient
- cURL-based clients
- Buzz
- Any other compliant client!
Important
The library does not lock you into any specific HTTP client. Choose the one that best fits your framework, performance needs, or preferences.
Whether you're using Symfony, Laravel, or a custom stack, PhpMistral integrates seamlessly into your environment.
This ensures flexibility, interoperability, and future-proofing of your PHP AI integrations.
Key Features
- Open-source:
- Free to use, modify, and contribute to.
- Framework-agnostic:
- Compatible with any PHP framework (Laravel, Symfony, Slim, custom apps, etc.).
- Multi-backend support:
- OpenAI, Mistral Platform, Hugging Face TGI, vLLM, Ollama, llama.cpp, xAI, and more.
- Chat completions:
- Streaming and non-streaming interactions.
- Embeddings:
- Dense embeddings and sparse embeddings (Splade pooling).
- Reranking API:
- Compare and rank multiple documents based on a query.
- Guided JSON generation:
- Ensure structured outputs based on a schema.
- Document generation (Mistral):
- Generate structured documents directly from models.
- Pooling API (vLLM):
- Efficient load balancing across multiple vLLM servers.
- Hugging Face Dataset API:
- Seamlessly interact with Hugging Face datasets, list files, download, manage, and search datasets directly from PHP.
Important
The Hugging Face Dataset API is a must-have for anyone working with finetuning or dataset manipulation. Easily list, fetch, and manage datasets from Hugging Face directly in your PHP applications.
Supported Providers & Features
Core Features
Provider | Chat (stream) | Chat (non-stream) | Embeddings | Sparse Embeddings |
---|---|---|---|---|
Mistral Platform | ✅ | ✅ | ✅ | |
Hugging Face TGI | ✅ | ✅ | ✅ | |
vLLM | ✅ | ✅ | ✅ | |
Ollama | ✅ | ✅ | ✅ | |
llama.cpp | ✅ | ✅ | ✅ | |
xAI | ✅ | ✅ | ✅ | |
Text Embedding Inference | ✅ | ✅ |
Advanced Features
Provider | Rerank | Guided JSON | Documents | Pooling | HF Datasets |
---|---|---|---|---|---|
Mistral Platform | ✅ | ✅ | ✅ | ||
Hugging Face TGI | ✅ | ✅ | ✅ | ||
vLLM | ✅ | ✅ | ✅ | ||
Ollama | ✅ | ✅ | |||
llama.cpp | ✅ | ✅ | |||
xAI | ✅ | ✅ | |||
Text Embedding Inference |
Installation
composer require partitech/php-mistral
Example Usages
Chat Completion (Streaming)
$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); }
Dense Embedding
use Partitech\PhpMistral\Clients\Tei\TeiClient; $client = new TeiClient(apiKey: $apiKey, url: $embeddingUrl); $embedding = $client->embed(inputs: "My name is Olivier and I");
Sparse Embedding (Splade Pooling)
$client = new TeiClient(apiKey: (string) $apiKey, url: $teiUrl); $embedding = $client->embedSparse(inputs: 'What is Deep Learning?'); var_dump($embedding);
Rerank
$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...' ] );
Hugging Face Dataset Management
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' );
Tip
Combine Hugging Face Datasets with Embeddings and Reranking to build advanced search or finetuning pipelines directly in PHP.
Contributing
We welcome contributions!
Help us make PhpMistral safer, richer, and more powerful.
Feel free to:
- Submit issues or ideas 💡
- Improve documentation 📚
- Add support for new providers or features 🚀
- Fix bugs 🐛
Important
Whether you're a beginner or an experienced developer, your help is valuable!
Join us in making PhpMistral the go-to PHP library for AI integrations.
License
MIT License