Skip to content

Commit cd4704e

Browse files
fix: support multimodal requests
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: shenald-dev <245350826+shenald-dev@users.noreply.github.com>
1 parent 5a26f85 commit cd4704e

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function isValidMessagesArray(messages) {
6565
}
6666

6767
function isValidMessage(msg) {
68-
return msg != null && typeof msg.role === 'string' && msg.role !== '' && typeof msg.content === 'string';
68+
return msg != null && typeof msg.role === 'string' && msg.role !== '' && (typeof msg.content === 'string' || Array.isArray(msg.content));
6969
}
7070

7171

tests/api.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ test('POST /v1/chat/completions works with valid data', async () => {
2626
assert.strictEqual(res.body.choices[0].message.content, 'This is a mock response from the unified API.');
2727
});
2828

29+
test('POST /v1/chat/completions works with multimodal data', async () => {
30+
const res = await request(app)
31+
.post('/v1/chat/completions')
32+
.send({
33+
model: 'gpt-4',
34+
messages: [{ role: 'user', content: [{ type: 'text', text: 'Hello!' }] }]
35+
});
36+
37+
assert.strictEqual(res.status, 200);
38+
assert.ok(res.body.id.startsWith('chatcmpl-'));
39+
assert.ok(res.body.id.length > 20);
40+
assert.strictEqual(res.body.object, 'chat.completion');
41+
assert.strictEqual(res.body.model, 'gpt-4');
42+
});
43+
2944
test('POST /v1/chat/completions fails without model', async () => {
3045
const res = await request(app)
3146
.post('/v1/chat/completions')
@@ -126,6 +141,7 @@ test('POST /v1/chat/completions fails with more than 1000 messages', async () =>
126141
test('isValidMessage validation helper', () => {
127142
const { isValidMessage } = require('../src/index.js');
128143
assert.strictEqual(isValidMessage({ role: 'user', content: 'hello' }), true);
144+
assert.strictEqual(isValidMessage({ role: 'user', content: [{ type: 'text', text: 'hello' }] }), true);
129145
assert.strictEqual(isValidMessage(null), false);
130146
assert.strictEqual(isValidMessage([]), false);
131147
assert.strictEqual(isValidMessage({ role: 'user' }), false);

0 commit comments

Comments
 (0)