Skip to content

Commit 52f799a

Browse files
committed
support multiple images
Signed-off-by: Lukas Schaefer <[email protected]>
1 parent 9ed215a commit 52f799a

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

lib/TaskProcessing/AnalyzeImageProvider.php

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,45 @@ public function process(?string $userId, array $input, callable $reportProgress)
110110
throw new RuntimeException('Must support chat completion endpoint');
111111
}
112112

113-
if (!isset($input['image']) || !$input['image'] instanceof File || !$input['image']->isReadable()) {
114-
throw new RuntimeException('Invalid input file');
115-
}
113+
$history = [];
116114

117-
$inputFile = base64_encode(stream_get_contents($input['image']->fopen('rb')));
118-
$fileType = $input['image']->getMimeType();
119-
if (!str_starts_with($fileType, 'image/')) {
120-
throw new RuntimeException('Invalid input file type ' . $fileType);
115+
if (!isset($input['image']) || !is_array($input['image'])) {
116+
throw new RuntimeException('Invalid file list');
121117
}
122-
if ($this->openAiAPIService->isUsingOpenAi()) {
123-
$validFileTypes = [
124-
'image/jpeg',
125-
'image/png',
126-
'image/gif',
127-
'image/webp',
128-
];
129-
if (!in_array($fileType, $validFileTypes)) {
130-
throw new RuntimeException('Invalid input file type for OpenAI ' . $fileType);
118+
foreach ($input['image'] as $image) {
119+
if (!$image instanceof File || !$image->isReadable()) {
120+
throw new RuntimeException('Invalid input file');
121+
}
122+
$inputFile = base64_encode(stream_get_contents($image->fopen('rb')));
123+
$fileType = $image->getMimeType();
124+
if (!str_starts_with($fileType, 'image/')) {
125+
throw new RuntimeException('Invalid input file type ' . $fileType);
131126
}
127+
if ($this->openAiAPIService->isUsingOpenAi()) {
128+
$validFileTypes = [
129+
'image/jpeg',
130+
'image/png',
131+
'image/gif',
132+
'image/webp',
133+
];
134+
if (!in_array($fileType, $validFileTypes)) {
135+
throw new RuntimeException('Invalid input file type for OpenAI ' . $fileType);
136+
}
137+
}
138+
$history[] = json_encode([
139+
'role' => 'user',
140+
'content' => [
141+
[
142+
'type' => 'image_url',
143+
'image_url' => [
144+
'url' => 'data:' . $fileType . ';base64,' . $inputFile,
145+
],
146+
],
147+
],
148+
]);
132149
}
133150

151+
134152
if (!isset($input['input']) || !is_string($input['input'])) {
135153
throw new RuntimeException('Invalid prompt');
136154
}
@@ -149,19 +167,7 @@ public function process(?string $userId, array $input, callable $reportProgress)
149167

150168
try {
151169
$systemPrompt = 'Take the user\'s question and answer it based on the provided image. Ensure that the answer matches the language of the user\'s question.';
152-
$completion = $this->openAiAPIService->createChatCompletion($userId, $model, $prompt, $systemPrompt, [
153-
json_encode([
154-
'role' => 'user',
155-
'content' => [
156-
[
157-
'type' => 'image_url',
158-
'image_url' => [
159-
'url' => 'data:' . $fileType . ';base64,' . $inputFile,
160-
],
161-
],
162-
],
163-
])
164-
], 1, $maxTokens);
170+
$completion = $this->openAiAPIService->createChatCompletion($userId, $model, $prompt, $systemPrompt, $history, 1, $maxTokens);
165171
$completion = $completion['messages'];
166172

167173
if (count($completion) > 0) {

lib/TaskProcessing/AnalyzeImageTaskType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public function getId(): string {
5050
public function getInputShape(): array {
5151
return [
5252
'image' => new ShapeDescriptor(
53-
$this->l->t('Image'),
54-
$this->l->t('Image to ask a question about'),
55-
EShapeType::Image,
53+
$this->l->t('Images'),
54+
$this->l->t('Images to ask a question about'),
55+
EShapeType::ListOfImages,
5656
),
5757
'input' => new ShapeDescriptor(
5858
$this->l->t('Question'),

0 commit comments

Comments
 (0)