Skip to content

Commit

Permalink
Merge pull request #24 from GeraudBourdin/main
Browse files Browse the repository at this point in the history
enable query to pixtral model
  • Loading branch information
GeraudBourdin authored Sep 12, 2024
2 parents 957526b + 25a32b6 commit 97ec189
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
77 changes: 76 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,38 @@ function datePlusNdays(\DateTime $date, $n) {
$datePlusNdays->add(new \DateInterval('P'.abs($n).'D'));
```

#### Pixtral request
```php

$contents = [
['type' => 'text', 'text' => 'Describe this image in detail please.'],
['type' => 'image_url', 'image_url' => 'https://s3.amazonaws.com/cms.ipressroom.com/338/files/201808/5b894ee1a138352221103195_A680%7Ejogging-edit/A680%7Ejogging-edit_hero.jpg'],
['type' => 'text', 'text' => 'and this one as well. Answer in French.'],
['type' => 'image_url', 'image_url' => 'https://www.wolframcloud.com/obj/resourcesystem/images/a0e/a0ee3983-46c6-4c92-b85d-059044639928/6af8cfb971db031b.png']
];

$messages = new Messages();
$messages->addMixedContentUserMessage($contents);

try {
$result = $client->chat(
$messages,
[
'model' => 'Pixtral-12B-2409',
'max_tokens' => 1024,
]
);
} catch (MistralClientException $e) {
echo $e->getMessage();
exit(1);
}

print($result->getMessage());
```
Result :
```console
Dans cette image, on voit une famille de cinq personnes posant pour une photo. Ils sont tous vêtus de chemises rouges assorties et sont allongés sur le ventre sur une surface blanche devant un fond blanc. Les membres de la famille sourient et semblent être dans une pose détendue et joyeuse, les mains jointes devant eux. La famille semble former un groupe uni et heureux, et l'ambiance générale de la photo est chaleureuse et accueillante.
```

## Lama.cpp inference
[MistralAi La plateforme](https://console.mistral.ai/) is really cheap you should consider subscribing to it instead of running
Expand Down Expand Up @@ -313,7 +345,7 @@ CHAT_MODEL=mistral-7b-instruct-v0.2.Q4_K_M.gguf
Starting to v0.6.0 vLLM is fully compatible with the tools/tool_choice from mistral's plateforme.
To get an ISO instance locally you will need to specify the template: [examples/tool_chat_template_mistral.jinja](https://github.com/vllm-project/vllm/blob/main/examples/tool_chat_template_mistral.jinja) . Here is a docker-compose you can use for example:


vLLM inference for tool_call
```yaml
services:
mistral:
Expand All @@ -332,6 +364,46 @@ services:
environment:
- HUGGING_FACE_HUB_TOKEN=*****
- NVIDIA_VISIBLE_DEVICES=0,1
- VLLM_WORKER_MULTIPROC_METHOD=spawn
volumes:
- /path_to_config/:/config
- /path_to_cache/.cache/huggingface:/root/.cache/huggingface
ports:
- "40001:8000"
deploy:
resources:
reservations:
devices:
- driver: nvidia
capabilities: [gpu]
runtime: nvidia
ipc: host
networks:
- llm_1x2_network
networks:
llm_1x2_network:
driver: bridge
```
vLLM inference for Pixtral
```yaml
services:
mistral:
image: vllm/vllm-openai:v0.6.1
command: |
--model mistralai/Pixtral-12B-2409
--tokenizer_mode mistral
--limit_mm_per_prompt 'image=4'
--max_num_batched_tokens 16384
--served-model-name Pixtral-12B-2409
--max-model-len 16384
--tensor-parallel-size 2
--gpu-memory-utilization 1
--trust-remote-code
--enforce-eager
environment:
- HUGGING_FACE_HUB_TOKEN=*****
- NVIDIA_VISIBLE_DEVICES=0,1
- VLLM_WORKER_MULTIPROC_METHOD=spawn
volumes:
- /path_to_config/:/config
- /path_to_cache/.cache/huggingface:/root/.cache/huggingface
Expand All @@ -354,6 +426,9 @@ networks:
## About the Response object
`$client->chat()` method return a `Partitech\PhpMistral\Response` object.
Expand Down
37 changes: 37 additions & 0 deletions examples/pixtral.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
require_once __DIR__ . '/../vendor/autoload.php';

use Partitech\PhpMistral\MistralClient;
use Partitech\PhpMistral\MistralClientException;
use Partitech\PhpMistral\Messages;

// export MISTRAL_API_KEY=your_api_key
$apiKey = getenv('MISTRAL_API_KEY');
$client = new MistralClient($apiKey);

$contents = [
['type' => 'text', 'text' => 'Describe this image in detail please.'],
['type' => 'image_url', 'image_url' => 'https://s3.amazonaws.com/cms.ipressroom.com/338/files/201808/5b894ee1a138352221103195_A680%7Ejogging-edit/A680%7Ejogging-edit_hero.jpg'],
['type' => 'text', 'text' => 'and this one as well. Answer in French.'],
['type' => 'image_url', 'image_url' => 'https://www.wolframcloud.com/obj/resourcesystem/images/a0e/a0ee3983-46c6-4c92-b85d-059044639928/6af8cfb971db031b.png']
];


$messages = new Messages();
$messages->addMixedContentUserMessage($contents);
try {
$result = $client->chat(
$messages,
[
'model' => 'Pixtral-12B-2409',
'max_tokens' => 1024,
]
);
} catch (MistralClientException $e) {
echo $e->getMessage();
exit(1);
}

print($result->getMessage());

// Dans cette image, on voit une famille de cinq personnes posant pour une photo. Ils sont tous vêtus de chemises rouges assorties et sont allongés sur le ventre sur une surface blanche devant un fond blanc. Les membres de la famille sourient et semblent être dans une pose détendue et joyeuse, les mains jointes devant eux. La famille semble former un groupe uni et heureux, et l'ambiance générale de la photo est chaleureuse et accueillante.
23 changes: 22 additions & 1 deletion src/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ public function format(string $format=MistralClient::CHAT_ML): string|array|null
if(MistralClient::CHAT_ML === $format) {
$messages = [];
foreach($this->getMessages() as $message) {
$messages[] = $message->toArray();
if(!is_array($message)) {
$messageArray = $message->toArray();
}else{
$messageArray = $message;
}
$messages[] = $messageArray;
}
return $messages;
}
Expand Down Expand Up @@ -113,6 +118,22 @@ public function addUserMessage(string $content): self
return $this;
}

public function addMixedContentUserMessage(array $contents): void
{
$messageContent = [];
foreach ($contents as $content) {
if (isset($content['type']) && $content['type'] === 'text') {
$messageContent[] = ['type' => 'text', 'text' => $content['text']];
} elseif (isset($content['type']) && $content['type'] === 'image_url') {
$messageContent[] = ['type' => 'image_url', 'image_url' => ['url' => $content['image_url']]];
}
}
$this->messages[] = [
'role' => 'user',
'content' => $messageContent
];
}

public function addToolMessage(string $name, array $content, string $toolCallId): self
{
$message = new Message();
Expand Down

0 comments on commit 97ec189

Please sign in to comment.