-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathscenario-separate-scope-1.mjs
More file actions
74 lines (64 loc) · 1.81 KB
/
scenario-separate-scope-1.mjs
File metadata and controls
74 lines (64 loc) · 1.81 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
import * as Sentry from '@sentry/node';
import express from 'express';
import OpenAI from 'openai';
function startMockServer() {
const app = express();
app.use(express.json());
// Chat completions endpoint
app.post('/openai/chat/completions', (req, res) => {
const { model } = req.body;
res.send({
id: 'chatcmpl-mock123',
object: 'chat.completion',
created: 1677652288,
model: model,
choices: [
{
index: 0,
message: {
role: 'assistant',
content: 'Mock response from OpenAI',
},
finish_reason: 'stop',
},
],
usage: {
prompt_tokens: 10,
completion_tokens: 15,
total_tokens: 25,
},
});
});
return new Promise(resolve => {
const server = app.listen(0, () => {
resolve(server);
});
});
}
async function run() {
const server = await startMockServer();
const client = new OpenAI({
baseURL: `http://localhost:${server.address().port}/openai`,
apiKey: 'mock-api-key',
});
// First request/conversation scope
await Sentry.withScope(async scope => {
// Set conversation ID for this request scope BEFORE starting the span
scope.setConversationId('conv_user1_session_abc');
await Sentry.startSpan({ op: 'http.server', name: 'GET /chat/conversation-1' }, async () => {
// First message in conversation 1
await client.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello from conversation 1' }],
});
// Second message in conversation 1
await client.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Follow-up in conversation 1' }],
});
});
});
server.close();
await Sentry.flush(2000);
}
run();