Search by

xudid / mistral-client

xudid

Package info

github.com/xudid/mistral-client

pkg:composer/xudid/mistral-client

Statistics

Installs: 7

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.1.2 2026-11-11 16:24 UTC

This package is auto-updated.

Last update: 2026-11-11 16:28:23 UTC


README

A small PHP client for Mistral chat completions.

<?php

require __DIR__ . '/vendor/autoload.php';

use Xudid\MistralClient\Mistral;
use Xudid\MistralClient\Message\AssistantMessage;
use Xudid\MistralClient\Message\SystemMessage;
use Xudid\MistralClient\Message\ToolMessage;
use Xudid\MistralClient\Message\UserMessage;
use Xudid\MistralClient\ResponseFormat;

$client = new Mistral(apiKey: getenv('MISTRAL_API_KEY'));
$response = $client->chat->complete(
    model: 'mistral-large-latest',
    messages: [new UserMessage('Hello')]
);

echo $response['choices'][0]['message']['content'];

complete() sends a synchronous request to the chat-completions endpoint and returns the full decoded JSON response as an associative array. HTTP and network errors are raised by Guzzle, and invalid JSON responses raise JsonException.

Configure a response format to enable text, JSON, or JSON Schema output. The configured format is reused across calls until you replace or clear it:

$client->chat->responseFormat(new ResponseFormat('json_object'));
$response = $client->chat->complete(
    model: 'mistral-large-latest',
    messages: [new UserMessage('Return a short answer as JSON.')]
);

$client->chat->responseFormat(new ResponseFormat('json_schema', [
    'name' => 'result',
    'schema' => ['type' => 'object', 'properties' => ['value' => ['type' => 'string']]],
]));
$response = $client->chat->complete(
    model: 'mistral-large-latest',
    messages: [new UserMessage('Respond using the requested schema.')]
);
$client->chat->responseFormat(null); // Clear the configured format.

For JSON mode, prompt the model to produce JSON. The configured ResponseFormat is sent as Mistral's response_format field; see the chat completion API documentation for supported formats and JSON Schema options.

SystemMessage, AssistantMessage, and ToolMessage can also be used to construct conversation messages. Assistant tool calls and tool results retain their metadata when serialized:

$messages = [
    new SystemMessage('You are a helpful assistant.'),
    new UserMessage('What is 1 + 1?'),
    new AssistantMessage(null, [[
        'id' => 'call_123',
        'type' => 'function',
        'function' => ['name' => 'calculator', 'arguments' => '{"expression":"1 + 1"}'],
    ]]),
    new ToolMessage('2', 'call_123', 'calculator'),
    new AssistantMessage('The result is 2.'),
    new AssistantMessage('Continue from this prefix', prefix: true),
];

License

This project is licensed under the GNU General Public License v3.0 or later. See LICENSE for the full text.