AWS Bedrock + Claude SSE流式传输
Bedrock SSE 스트리밍
AWS Bedrock에서 Claude API를 실시간 스트리밍으로 연동합니다.
왜 스트리밍?
- UX 향상 — 사용자가 즉시 응답을 확인
- TTFB 단축 — 첫 토큰까지 1-2초
- 긴 응답 — 타임아웃 없이 전체 응답 수신
서버 구현
const { BedrockRuntimeClient, ConverseStreamCommand } = require('@aws-sdk/client-bedrock-runtime');
const client = new BedrockRuntimeClient({ region: 'us-east-1' });
app.post('/api/interpret', async (req, res) => {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
const command = new ConverseStreamCommand({
modelId: 'global.anthropic.claude-sonnet-4-6',
messages: [{ role: 'user', content: [{ text: req.body.prompt }] }],
inferenceConfig: { temperature: 0.2 }
});
const response = await client.send(command);
for await (const event of response.stream) {
if (event.contentBlockDelta) {
res.write(`data: ${JSON.stringify({ text: event.contentBlockDelta.delta.text })}\n\n`);
}
}
res.write('data: [DONE]\n\n');
res.end();
});
클라이언트
const evtSource = new EventSource('/api/interpret');
evtSource.onmessage = (e) => {
if (e.data === '[DONE]') return evtSource.close();
const { text } = JSON.parse(e.data);
outputDiv.textContent += text;
};