@@ -110,27 +110,45 @@ public function process(?string $userId, array $input, callable $reportProgress)
110
110
throw new RuntimeException ('Must support chat completion endpoint ' );
111
111
}
112
112
113
- if (!isset ($ input ['image ' ]) || !$ input ['image ' ] instanceof File || !$ input ['image ' ]->isReadable ()) {
114
- throw new RuntimeException ('Invalid input file ' );
115
- }
113
+ $ history = [];
116
114
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 ' );
121
117
}
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 );
131
126
}
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
+ ]);
132
149
}
133
150
151
+
134
152
if (!isset ($ input ['input ' ]) || !is_string ($ input ['input ' ])) {
135
153
throw new RuntimeException ('Invalid prompt ' );
136
154
}
@@ -149,19 +167,7 @@ public function process(?string $userId, array $input, callable $reportProgress)
149
167
150
168
try {
151
169
$ 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 );
165
171
$ completion = $ completion ['messages ' ];
166
172
167
173
if (count ($ completion ) > 0 ) {
0 commit comments