|
| 1 | +import * as Sentry from '@sentry/node'; |
| 2 | +import express from 'express'; |
| 3 | +import OpenAI from 'openai'; |
| 4 | + |
| 5 | +function startMockServer() { |
| 6 | + const app = express(); |
| 7 | + app.use(express.json({ limit: '10mb' })); |
| 8 | + |
| 9 | + app.post('/openai/chat/completions', (req, res) => { |
| 10 | + res.send({ |
| 11 | + id: 'chatcmpl-mock123', |
| 12 | + object: 'chat.completion', |
| 13 | + created: 1677652288, |
| 14 | + model: req.body.model, |
| 15 | + choices: [ |
| 16 | + { |
| 17 | + index: 0, |
| 18 | + message: { role: 'assistant', content: 'Hello!' }, |
| 19 | + finish_reason: 'stop', |
| 20 | + }, |
| 21 | + ], |
| 22 | + usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 }, |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + app.post('/openai/responses', (req, res) => { |
| 27 | + res.send({ |
| 28 | + id: 'resp_mock456', |
| 29 | + object: 'response', |
| 30 | + created_at: 1677652290, |
| 31 | + model: req.body.model, |
| 32 | + output: [ |
| 33 | + { |
| 34 | + type: 'message', |
| 35 | + id: 'msg_mock_output_1', |
| 36 | + status: 'completed', |
| 37 | + role: 'assistant', |
| 38 | + content: [{ type: 'output_text', text: 'Response text', annotations: [] }], |
| 39 | + }, |
| 40 | + ], |
| 41 | + output_text: 'Response text', |
| 42 | + status: 'completed', |
| 43 | + usage: { input_tokens: 5, output_tokens: 3, total_tokens: 8 }, |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + return new Promise(resolve => { |
| 48 | + const server = app.listen(0, () => { |
| 49 | + resolve(server); |
| 50 | + }); |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +async function run() { |
| 55 | + const server = await startMockServer(); |
| 56 | + |
| 57 | + await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { |
| 58 | + const client = new OpenAI({ |
| 59 | + baseURL: `http://localhost:${server.address().port}/openai`, |
| 60 | + apiKey: 'mock-api-key', |
| 61 | + }); |
| 62 | + |
| 63 | + // Single long message for chat completions |
| 64 | + const longContent = 'A'.repeat(50_000); |
| 65 | + await client.chat.completions.create({ |
| 66 | + model: 'gpt-4', |
| 67 | + messages: [{ role: 'user', content: longContent }], |
| 68 | + }); |
| 69 | + |
| 70 | + // Responses API with long string input |
| 71 | + const longStringInput = 'B'.repeat(50_000); |
| 72 | + await client.responses.create({ |
| 73 | + model: 'gpt-4', |
| 74 | + input: longStringInput, |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + // Flush is required when span streaming is enabled to ensure streamed spans are sent before the process exits |
| 79 | + await Sentry.flush(); |
| 80 | + server.close(); |
| 81 | +} |
| 82 | + |
| 83 | +run(); |
0 commit comments