Skip to content

feat: add support for mistral #291

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ OPENAI_API_KEY=
# For using Claude on Anthropic
ANTHROPIC_API_KEY=

# For using Mistral
MISTRAL_API_KEY=

# For using Voyage
VOYAGE_API_KEY=

Expand Down
25 changes: 25 additions & 0 deletions examples/chat-mistral.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use PhpLlm\LlmChain\Bridge\Mistral\Mistral;
use PhpLlm\LlmChain\Bridge\Mistral\PlatformFactory;
use PhpLlm\LlmChain\Chain;
use PhpLlm\LlmChain\Model\Message\Message;
use PhpLlm\LlmChain\Model\Message\MessageBag;
use Symfony\Component\Dotenv\Dotenv;

require_once dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env');

if (empty($_ENV['MISTRAL_API_KEY'])) {
echo 'Please set the REPLICATE_API_KEY environment variable.'.PHP_EOL;
exit(1);
}

$platform = PlatformFactory::create($_ENV['MISTRAL_API_KEY']);
$llm = new Mistral();

$chain = new Chain($platform, $llm);
$messages = new MessageBag(Message::ofUser('What is the best French cheese?'));
$response = $chain->call($messages);

echo $response->getContent().PHP_EOL;
33 changes: 33 additions & 0 deletions src/Bridge/Mistral/Embeddings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Bridge\Mistral;

use PhpLlm\LlmChain\Model\EmbeddingsModel;

final readonly class Embeddings implements EmbeddingsModel
{
public const MISTRAL_EMBED = 'mistral-embed';

public function __construct(

Check failure on line 13 in src/Bridge/Mistral/Embeddings.php

View workflow job for this annotation

GitHub Actions / qa

Method PhpLlm\LlmChain\Bridge\Mistral\Embeddings::__construct() has parameter $options with no value type specified in iterable type array.
private string $name = self::MISTRAL_EMBED,
private array $options = [],
) {
}

public function getName(): string
{
return $this->name;
}

public function getOptions(): array
{
return $this->options;
}

public function supportsMultipleInputs(): bool
{
return true;
}
}
71 changes: 71 additions & 0 deletions src/Bridge/Mistral/Mistral.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Bridge\Mistral;

use PhpLlm\LlmChain\Model\LanguageModel;

final readonly class Mistral implements LanguageModel
{
public const CODESTRAL = 'codestral-latest';
public const CODESTRAL_MAMBA = 'open-codestral-mamba';
public const MISTRAL_LARGE = 'mistral-large-latest';
public const MISTRAL_SMALL = 'mistral-small-latest';
public const MISTRAL_NEMO = 'open-mistral-nemo';
public const MISTRAL_SABA = 'mistral-saba-latest';
public const MINISTRAL_3B = 'mistral-3b-latest';
public const MINISTRAL_8B = 'mistral-8b-latest';
public const PIXSTRAL_LARGE = 'pixstral-large-latest';
public const PIXSTRAL = 'pixstral-12b-latest';

public function __construct(

Check failure on line 22 in src/Bridge/Mistral/Mistral.php

View workflow job for this annotation

GitHub Actions / qa

Method PhpLlm\LlmChain\Bridge\Mistral\Mistral::__construct() has parameter $options with no value type specified in iterable type array.
private string $name = self::MISTRAL_LARGE,
private array $options = [],
) {
}

public function getName(): string
{
return $this->name;
}

public function getOptions(): array
{
return $this->options;
}

public function supportsAudioInput(): bool
{
return false;
}

public function supportsImageInput(): bool
{
return in_array($this->name, [self::PIXSTRAL, self::PIXSTRAL_LARGE, self::MISTRAL_SMALL], true);
}

public function supportsStreaming(): bool
{
return true;
}

public function supportsStructuredOutput(): bool
{
return true;
}

public function supportsToolCalling(): bool
{
return in_array($this->name, [
self::CODESTRAL,
self::MISTRAL_LARGE,
self::MISTRAL_SMALL,
self::MISTRAL_NEMO,
self::MINISTRAL_3B,
self::MINISTRAL_8B,
self::PIXSTRAL,
self::PIXSTRAL_LARGE,
], true);
}
}
94 changes: 94 additions & 0 deletions src/Bridge/Mistral/ModelHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Bridge\Mistral;

use PhpLlm\LlmChain\Exception\RuntimeException;
use PhpLlm\LlmChain\Model\Message\MessageBagInterface;
use PhpLlm\LlmChain\Model\Model;
use PhpLlm\LlmChain\Model\Response\Choice;
use PhpLlm\LlmChain\Model\Response\ChoiceResponse;
use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse;
use PhpLlm\LlmChain\Model\Response\TextResponse;
use PhpLlm\LlmChain\Model\Response\ToolCall;
use PhpLlm\LlmChain\Model\Response\ToolCallResponse;
use PhpLlm\LlmChain\Platform\ModelClient;
use PhpLlm\LlmChain\Platform\ResponseConverter;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

final readonly class ModelHandler implements ModelClient, ResponseConverter
{
private EventSourceHttpClient $httpClient;

public function __construct(
HttpClientInterface $httpClient,
#[\SensitiveParameter]
private string $apiKey,
) {
$this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
}

public function supports(Model $model, object|array|string $input): bool
{
return $model instanceof Mistral && $input instanceof MessageBagInterface;
}

public function request(Model $model, object|array|string $input, array $options = []): ResponseInterface
{
return $this->httpClient->request('POST', 'https://api.mistral.ai/v1/chat/completions', [
'auth_bearer' => $this->apiKey,
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
],
'json' => array_merge($options, [
'model' => $model->getName(),
'messages' => $input,
]),
]);
}

public function convert(ResponseInterface $response, array $options = []): LlmResponse
{
$data = $response->toArray();
if (!isset($data['choices'])) {
throw new RuntimeException('Response does not contain choices');
}

/** @var Choice[] $choices */
$choices = \array_map($this->convertChoice(...), $data['choices']);

if (1 !== count($choices)) {
return new ChoiceResponse(...$choices);
}

if ($choices[0]->hasToolCall()) {
return new ToolCallResponse(...$choices[0]->getToolCalls());
}

return new TextResponse($choices[0]->getContent());
}

private function convertChoice(array $choice): Choice

Check failure on line 75 in src/Bridge/Mistral/ModelHandler.php

View workflow job for this annotation

GitHub Actions / qa

Method PhpLlm\LlmChain\Bridge\Mistral\ModelHandler::convertChoice() has parameter $choice with no value type specified in iterable type array.
{
if ('tool_calls' === $choice['finish_reason']) {
return new Choice(toolCalls: \array_map($this->convertToolCall(...), $choice['message']['tool_calls']));
}

if ('stop' === $choice['finish_reason']) {
return new Choice($choice['message']['content']);
}

throw new RuntimeException(sprintf('Unsupported finish reason "%s".', $choice['finish_reason']));
}

private function convertToolCall(array $toolCall): ToolCall

Check failure on line 88 in src/Bridge/Mistral/ModelHandler.php

View workflow job for this annotation

GitHub Actions / qa

Method PhpLlm\LlmChain\Bridge\Mistral\ModelHandler::convertToolCall() has parameter $toolCall with no value type specified in iterable type array.
{
$arguments = json_decode((string) $toolCall['function']['arguments'], true, JSON_THROW_ON_ERROR);

return new ToolCall($toolCall['id'], $toolCall['function']['name'], $arguments);
}
}
23 changes: 23 additions & 0 deletions src/Bridge/Mistral/PlatformFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Bridge\Mistral;

use PhpLlm\LlmChain\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final class PlatformFactory
{
public static function create(
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
): Platform {
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
$handler = new ModelHandler($httpClient, $apiKey);

return new Platform([$handler], [$handler]);
}
}
Loading