-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathscenario-vision.mjs
More file actions
101 lines (91 loc) · 2.38 KB
/
scenario-vision.mjs
File metadata and controls
101 lines (91 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import * as Sentry from '@sentry/node';
import express from 'express';
import OpenAI from 'openai';
function startMockServer() {
const app = express();
app.use(express.json({ limit: '10mb' }));
app.post('/openai/chat/completions', (req, res) => {
res.send({
id: 'chatcmpl-vision-123',
object: 'chat.completion',
created: 1677652288,
model: req.body.model,
choices: [
{
index: 0,
message: {
role: 'assistant',
content: 'I see a red square in the image.',
},
finish_reason: 'stop',
},
],
usage: {
prompt_tokens: 50,
completion_tokens: 10,
total_tokens: 60,
},
});
});
return new Promise(resolve => {
const server = app.listen(0, () => {
resolve(server);
});
});
}
// Small 10x10 red PNG image encoded as base64
const RED_PNG_BASE64 =
'iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAFUlEQVR42mP8z8BQDwADhQGAWjR9awAAAABJRU5ErkJggg==';
async function run() {
const server = await startMockServer();
await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
const client = new OpenAI({
baseURL: `http://localhost:${server.address().port}/openai`,
apiKey: 'mock-api-key',
});
// Vision request with inline base64 image
await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'What is in this image?' },
{
type: 'image_url',
image_url: {
url: `data:image/png;base64,${RED_PNG_BASE64}`,
},
},
],
},
],
});
// Vision request with multiple images (one inline, one URL)
await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Compare these images' },
{
type: 'image_url',
image_url: {
url: `data:image/png;base64,${RED_PNG_BASE64}`,
},
},
{
type: 'image_url',
image_url: {
url: 'https://example.com/image.png',
},
},
],
},
],
});
});
server.close();
}
run();