-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathscenario-span-streaming.mjs
More file actions
83 lines (73 loc) · 2.14 KB
/
scenario-span-streaming.mjs
File metadata and controls
83 lines (73 loc) · 2.14 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
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-mock123',
object: 'chat.completion',
created: 1677652288,
model: req.body.model,
choices: [
{
index: 0,
message: { role: 'assistant', content: 'Hello!' },
finish_reason: 'stop',
},
],
usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 },
});
});
app.post('/openai/responses', (req, res) => {
res.send({
id: 'resp_mock456',
object: 'response',
created_at: 1677652290,
model: req.body.model,
output: [
{
type: 'message',
id: 'msg_mock_output_1',
status: 'completed',
role: 'assistant',
content: [{ type: 'output_text', text: 'Response text', annotations: [] }],
},
],
output_text: 'Response text',
status: 'completed',
usage: { input_tokens: 5, output_tokens: 3, total_tokens: 8 },
});
});
return new Promise(resolve => {
const server = app.listen(0, () => {
resolve(server);
});
});
}
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',
});
// Single long message for chat completions
const longContent = 'A'.repeat(50_000);
await client.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: longContent }],
});
// Responses API with long string input
const longStringInput = 'B'.repeat(50_000);
await client.responses.create({
model: 'gpt-4',
input: longStringInput,
});
});
// Flush is required when span streaming is enabled to ensure streamed spans are sent before the process exits
await Sentry.flush();
server.close();
}
run();