const response = await fetch("http://localhost:3000/v1/chat/completions", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ model: "gpt-5.2", messages: [ { role: "user", content: "What is 2+2? Answer in one sentence." }, ], stream: true, }), }); console.log("Status:", response.status); const reader = response.body.getReader(); const decoder = new TextDecoder(); let buffer = ""; let count = 0; let fullContent = ""; while (true) { const { done, value } = await reader.read(); if (done) break; buffer += decoder.decode(value, { stream: true }); const lines = buffer.split("\n"); buffer = lines.pop() || ""; for (const line of lines) { if (line.startsWith("data: ")) { count++; const data = line.substring(6); if (data.trim() !== "[DONE]") { try { const parsed = JSON.parse(data); if (parsed.choices && parsed.choices[0]) { const delta = parsed.choices[0].delta; if (delta.content) { fullContent += delta.content; } } console.log( `Chunk ${count}: ${JSON.stringify(parsed).substring(0, 150)}`, ); } catch (e) { console.log(`Raw chunk ${count}:`, data.substring(0, 100)); } } } } } console.log("\n=== Full Content ==="); console.log(fullContent); console.log("===================");