|
| 1 | +import Anthropic from '@anthropic-ai/sdk'; |
| 2 | +import * as Sentry from '@sentry/node'; |
| 3 | +import express from 'express'; |
| 4 | + |
| 5 | +function streamEvents(id, model) { |
| 6 | + return [ |
| 7 | + { |
| 8 | + type: 'message_start', |
| 9 | + message: { id, type: 'message', role: 'assistant', model, content: [], usage: { input_tokens: 10 } }, |
| 10 | + }, |
| 11 | + { type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } }, |
| 12 | + { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'Hello!' } }, |
| 13 | + { type: 'content_block_stop', index: 0 }, |
| 14 | + { type: 'message_delta', delta: { stop_reason: 'end_turn', stop_sequence: null }, usage: { output_tokens: 15 } }, |
| 15 | + { type: 'message_stop' }, |
| 16 | + ]; |
| 17 | +} |
| 18 | + |
| 19 | +function startMockAnthropicServer() { |
| 20 | + const app = express(); |
| 21 | + app.use(express.json()); |
| 22 | + |
| 23 | + app.post('/anthropic/v1/messages', (req, res) => { |
| 24 | + // The prompt names the response id, so each call's span can be told apart. |
| 25 | + const id = req.body.messages[0].content; |
| 26 | + |
| 27 | + if (!req.body.stream) { |
| 28 | + res.json({ |
| 29 | + id, |
| 30 | + type: 'message', |
| 31 | + role: 'assistant', |
| 32 | + model: req.body.model, |
| 33 | + content: [{ type: 'text', text: 'Hello!' }], |
| 34 | + stop_reason: 'end_turn', |
| 35 | + stop_sequence: null, |
| 36 | + usage: { input_tokens: 10, output_tokens: 15 }, |
| 37 | + }); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', Connection: 'keep-alive' }); |
| 42 | + const events = streamEvents(id, req.body.model); |
| 43 | + events.forEach((event, index) => { |
| 44 | + setTimeout(() => { |
| 45 | + res.write(`event: ${event.type}\n`); |
| 46 | + res.write(`data: ${JSON.stringify(event)}\n\n`); |
| 47 | + if (index === events.length - 1) { |
| 48 | + res.end(); |
| 49 | + } |
| 50 | + }, index * 10); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + app.post('/anthropic/v1/complete', (req, res) => { |
| 55 | + res.json({ |
| 56 | + id: req.body.prompt.includes('asResponse') ? 'compl_v0_as_response' : 'compl_v0', |
| 57 | + type: 'completion', |
| 58 | + model: req.body.model, |
| 59 | + completion: 'Hello!', |
| 60 | + stop_reason: 'stop_sequence', |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + return new Promise(resolve => { |
| 65 | + const server = app.listen(0, () => { |
| 66 | + resolve(server); |
| 67 | + }); |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +async function run() { |
| 72 | + const server = await startMockAnthropicServer(); |
| 73 | + |
| 74 | + await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { |
| 75 | + const client = new Anthropic({ |
| 76 | + apiKey: 'mock-api-key', |
| 77 | + baseURL: `http://localhost:${server.address().port}/anthropic`, |
| 78 | + }); |
| 79 | + const model = 'claude-3-haiku-20240307'; |
| 80 | + |
| 81 | + await client.messages.create({ model, max_tokens: 100, messages: [{ role: 'user', content: 'msg_v0_create' }] }); |
| 82 | + await client.beta.messages.create({ |
| 83 | + model, |
| 84 | + max_tokens: 100, |
| 85 | + messages: [{ role: 'user', content: 'msg_v0_beta_create' }], |
| 86 | + }); |
| 87 | + |
| 88 | + const stream = await client.messages.create({ |
| 89 | + model, |
| 90 | + max_tokens: 100, |
| 91 | + stream: true, |
| 92 | + messages: [{ role: 'user', content: 'msg_v0_stream' }], |
| 93 | + }); |
| 94 | + for await (const _ of stream) { |
| 95 | + // consume |
| 96 | + } |
| 97 | + |
| 98 | + await client.messages |
| 99 | + .stream({ model, max_tokens: 100, messages: [{ role: 'user', content: 'msg_v0_stream_helper' }] }) |
| 100 | + .finalMessage(); |
| 101 | + |
| 102 | + await client.completions.create({ |
| 103 | + model: 'claude-2', |
| 104 | + max_tokens_to_sample: 100, |
| 105 | + prompt: '\n\nHuman: Hello\n\nAssistant:', |
| 106 | + }); |
| 107 | + const response = await client.completions |
| 108 | + .create({ model: 'claude-2', max_tokens_to_sample: 100, prompt: '\n\nHuman: asResponse\n\nAssistant:' }) |
| 109 | + .asResponse(); |
| 110 | + await response.json(); |
| 111 | + }); |
| 112 | + |
| 113 | + await Sentry.flush(2000); |
| 114 | + |
| 115 | + server.close(); |
| 116 | +} |
| 117 | + |
| 118 | +run(); |
0 commit comments