{"id":483695,"date":"2026-06-15T13:30:15","date_gmt":"2026-06-15T13:30:15","guid":{"rendered":"https:\/\/savepearlharbor.com\/?p=483695"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=483695","title":{"rendered":"\u041e\u0434\u0438\u043d SSE \u0434\u043b\u044f \u0447\u0435\u0442\u044b\u0440\u0451\u0445 LLM: \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433 OpenAI, Anthropic, DeepSeek \u0438 Kimi \u0447\u0435\u0440\u0435\u0437 \u043e\u0434\u0438\u043d \u0431\u044d\u043a\u0435\u043d\u0434"},"content":{"rendered":"<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u0447\u0430\u0442-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440, \u0433\u0434\u0435 \u0432 \u043e\u0434\u043d\u043e\u043c \u043e\u043a\u043d\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b GPT, Claude, Kimi \u0438 DeepSeek. \u0424\u0440\u043e\u043d\u0442\u0435\u043d\u0434\u0443 \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u2014 \u0442\u043e\u043a\u0435\u043d \u0437\u0430 \u0442\u043e\u043a\u0435\u043d\u043e\u043c, \u043a\u0430\u043a \u0432 ChatGPT. \u0411\u044d\u043a\u0435\u043d\u0434 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0445\u043e\u0434\u0438\u0442 \u043a \u0447\u0435\u0442\u044b\u0440\u0451\u043c \u0440\u0430\u0437\u043d\u044b\u043c API, \u0438 \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433 \u0443 \u043d\u0438\u0445 \u0443\u0441\u0442\u0440\u043e\u0435\u043d \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443. \u0420\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u043a\u0430\u043a \u043c\u044b \u0441\u0432\u0435\u043b\u0438 \u044d\u0442\u043e \u043a \u0435\u0434\u0438\u043d\u043e\u043c\u0443 SSE-\u043f\u043e\u0442\u043e\u043a\u0443 \u043d\u0430\u0440\u0443\u0436\u0443, \u0438 \u043f\u0440\u043e \u0434\u0432\u0435 \u0433\u0440\u0430\u0431\u043b\u0438, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u043b\u0438: \u0440\u0432\u0430\u043d\u044b\u0435 UTF-8 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0438 \u043f\u0430\u0440\u0441\u0438\u043d\u0433 \u0447\u0443\u0436\u0438\u0445 SSE.<\/p>\n<p>\u0421\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0432\u0441\u0435\u043c, \u043a\u0442\u043e \u043f\u0440\u043e\u043a\u0441\u0438\u0440\u0443\u0435\u0442 LLM \u0447\u0435\u0440\u0435\u0437 \u0441\u0432\u043e\u0439 \u0441\u0435\u0440\u0432\u0435\u0440.<\/p>\n<p>\u0417\u0430\u0447\u0435\u043c \u0432\u043e\u043e\u0431\u0449\u0435 \u0441\u0432\u043e\u0439 \u043f\u0440\u043e\u043a\u0441\u0438<\/p>\n<p>\u0424\u0440\u043e\u043d\u0442\u0435\u043d\u0434 \u043d\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0437\u043d\u0430\u0442\u044c \u043a\u043b\u044e\u0447\u0438 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u043e\u0432 \u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0445\u043e\u0434\u0438\u0442\u044c \u043a \u043d\u0438\u043c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e. \u0412\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438\u0434\u0443\u0442 \u0447\u0435\u0440\u0435\u0437 \u043d\u0430\u0448 Node.js-\u0431\u044d\u043a\u0435\u043d\u0434: \u043e\u043d \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043a\u043b\u044e\u0447, \u0434\u0451\u0440\u0433\u0430\u0435\u0442 \u043d\u0443\u0436\u043d\u044b\u0439 API \u0441 stream: true, \u043f\u0430\u0440\u0441\u0438\u0442 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u043f\u043e\u0442\u043e\u043a \u0438 \u043e\u0442\u0434\u0430\u0451\u0442 \u0444\u0440\u043e\u043d\u0442\u0443 \u0443\u043d\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f. \u041f\u043b\u044e\u0441 \u043d\u0430 \u0431\u044d\u043a\u0435\u043d\u0434\u0435 \u0436\u0438\u0432\u0443\u0442 \u043b\u0438\u043c\u0438\u0442\u044b, \u0443\u0447\u0451\u0442 \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u0438 \u043f\u043e\u0434\u043c\u0435\u043d\u0430 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430.<\/p>\n<p>\u0417\u0430\u0434\u0430\u0447\u0430: \u00ab\u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u0442\u043e\u043a \u043e\u0442 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430 X \u2192 \u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0438\u0442\u044c \u2192 \u043e\u0442\u0434\u0430\u0442\u044c \u0444\u0440\u043e\u043d\u0442\u0443 \u0432 \u0435\u0434\u0438\u043d\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435\u00bb.<\/p>\n<p>\u0414\u0432\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433\u0430<\/p>\n<p>\u041f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u044b \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0430 \u043b\u0430\u0433\u0435\u0440\u044f.<\/p>\n<ol>\n<li>\n<p>OpenAI-\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0435 (GPT, DeepSeek, Kimi). SSE, \u0433\u0434\u0435 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u0438 \u043b\u0435\u0436\u0438\u0442 delta:<\/p>\n<p>data: {\u201cchoices\u201d:[{\u201cdelta\u201d:{\u201ccontent\u201d:\u201c\u041f\u0440\u0438\u201d}}]} data: {\u201cchoices\u201d:[{\u201cdelta\u201d:{\u201ccontent\u201d:\u201c\u0432\u0435\u0442\u201d}}]} data: [DONE]<\/p>\n<\/li>\n<li>\n<p>Anthropic (Claude). \u0421\u0432\u043e\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u0441 \u0442\u0438\u043f\u0430\u043c\u0438:<\/p>\n<p>data: {\u201ctype\u201d:\u201cmessage_start\u201d,\u201cmessage\u201d:{\u201cusage\u201d:{\u201cinput_tokens\u201d:10}}} data: {\u201ctype\u201d:\u201ccontent_block_delta\u201d,\u201cdelta\u201d:{\u201ctype\u201d:\u201ctext_delta\u201d,\u201ctext\u201d:\u201c\u041f\u0440\u0438\u201d}} data: {\u201ctype\u201d:\u201cmessage_delta\u201d,\u201cusage\u201d:{\u201coutput_tokens\u201d:5}}<\/p>\n<\/li>\n<\/ol>\n<p>\u0422\u0435\u043a\u0441\u0442 \u043b\u0435\u0436\u0438\u0442 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445, \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443, \u0442\u043e\u043a\u0435\u043d\u044b usage \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445 \u043f\u043e\u0442\u043e\u043a\u0430. \u041d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u0432\u0441\u0451 \u043a \u043e\u0434\u043d\u043e\u043c\u0443 \u0432\u0438\u0434\u0443.<\/p>\n<p>\u0415\u0434\u0438\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u043d\u0430\u0440\u0443\u0436\u0443<\/p>\n<p>\u0414\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438\u0441\u044c \u0441 \u0444\u0440\u043e\u043d\u0442\u043e\u043c \u043e \u043f\u0440\u043e\u0441\u0442\u043e\u043c \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0435 \u043f\u043e\u0432\u0435\u0440\u0445 SSE:<\/p>\n<p>data: {\u201ct\u201d:\u201c\u043a\u0443\u0441\u043e\u043a \u0442\u0435\u043a\u0441\u0442\u0430\u201d}   \/\/ \u0434\u0435\u043b\u044c\u0442\u0430 data: {\u201cdone\u201d:true,\u201cfull\u201d:\u201c\u0432\u0435\u0441\u044c \u0442\u0435\u043a\u0441\u0442\u201d} data: [DONE]<\/p>\n<p>\u0414\u0430\u043b\u044c\u0448\u0435 \u2014 \u0434\u0432\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430, \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u043b\u0430\u0433\u0435\u0440\u044c.<\/p>\n<p>\u041f\u0430\u0440\u0441\u0438\u043d\u0433 OpenAI-\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430<\/p>\n<p>\u0427\u0430\u043d\u043a\u0438 \u0438\u0437 \u0441\u043e\u043a\u0435\u0442\u0430 \u043d\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442 \u0441 \u0433\u0440\u0430\u043d\u0438\u0446\u0430\u043c\u0438 SSE-\u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0443\u0435\u043c \u0438 \u0440\u0435\u0436\u0435\u043c \u043f\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044e \\n\\n:<\/p>\n<p>let buf = \u201c\u201d; proxyRes.setEncoding(\u201cutf-8\u201d); proxyRes.on(\u201cdata\u201d, (chunk) =&gt; { buf += chunk; const parts = buf.split(\u201c\\n\\n\u201d); buf = parts.pop() <span class=\"habrahidden\"> \u201c\u201d;          \/\/ \u0445\u0432\u043e\u0441\u0442 \u2014 \u043d\u0435\u0434\u043e\u0441\u043e\u0431\u0440\u0430\u043d\u043d\u043e\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 for (const part of parts) { for (const line of part.split(\u201c\\n\u201d)) { const s = line.trim(); if (!s.startsWith(&#171;data: &#171;) <\/span> s === \u201cdata: [DONE]\u201d) continue; const evt = JSON.parse(s.slice(6)); const delta = evt.choices?.[0]?.delta?.content; if (delta) sseWrite(res, { t: delta }); } } });<\/p>\n<p>\u0413\u043b\u0430\u0432\u043d\u043e\u0435 \u0437\u0434\u0435\u0441\u044c \u2014 \u043d\u0435 \u043f\u0430\u0440\u0441\u0438\u0442\u044c buf \u0446\u0435\u043b\u0438\u043a\u043e\u043c \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0447\u0430\u043d\u043a\u0435, \u0430 \u043e\u0442\u0440\u0435\u0437\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0451\u043d\u043d\u044b\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f (\u0434\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \\n\\n), \u0430 \u043d\u0435\u0434\u043e\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u0445\u0432\u043e\u0441\u0442 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0432 \u0431\u0443\u0444\u0435\u0440\u0435 \u0434\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0447\u0430\u043d\u043a\u0430.<\/p>\n<p>Anthropic \u043f\u0430\u0440\u0441\u0438\u0442\u0441\u044f \u0442\u0430\u043a \u0436\u0435, \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c text \u0438\u0437 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0441 \u0442\u0438\u043f\u043e\u043c content_block_delta, \u0430 usage \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0438\u0437 message_start \u0438 message_delta.<\/p>\n<p>\u0413\u0440\u0430\u0431\u043b\u044f \u21161: data += chunk \u043b\u043e\u043c\u0430\u0435\u0442 \u0440\u0443\u0441\u0441\u043a\u0438\u0435 \u0431\u0443\u043a\u0432\u044b<\/p>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0442\u0435\u043b\u043e \u043e\u0442\u0432\u0435\u0442\u0430 \u043c\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u043b\u0438 \u043d\u0430\u0438\u0432\u043d\u043e:<\/p>\n<p>let data = \u201c\u201d; proxyRes.on(\u201cdata\u201d, chunk =&gt; data += chunk);   \/\/ \u274c<\/p>\n<p>\u0418 \u0432 \u043e\u0442\u0432\u0435\u0442\u0430\u0445 \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u044f\u0432\u043b\u044f\u043b\u0438\u0441\u044c \u00ab\u0431\u0438\u0442\u044b\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b\u00bb \u2014 \u043a\u0440\u0430\u043a\u043e\u0437\u044f\u0431\u0440\u044b \u0432\u043c\u0435\u0441\u0442\u043e \u0447\u0430\u0441\u0442\u0438 \u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u044b \u0438\u043b\u0438 \u044d\u043c\u043e\u0434\u0437\u0438. \u041f\u0440\u0438\u0447\u0451\u043c \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430, \u0430 \u0431\u0443\u0434\u0442\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e.<\/p>\n<p>\u041f\u0440\u0438\u0447\u0438\u043d\u0430: chunk \u2014 \u044d\u0442\u043e Buffer, \u0430 \u043d\u0435 \u0441\u0442\u0440\u043e\u043a\u0430. \u041a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f data += chunk \u043d\u0435\u044f\u0432\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 chunk.toString() \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u043a\u0443\u0441\u043a\u0435 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e. \u041c\u043d\u043e\u0433\u043e\u0431\u0430\u0439\u0442\u043d\u044b\u0435 UTF-8 \u0441\u0438\u043c\u0432\u043e\u043b\u044b (\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430 \u2014 2 \u0431\u0430\u0439\u0442\u0430, \u044d\u043c\u043e\u0434\u0437\u0438 \u2014 4) \u043c\u043e\u0433\u0443\u0442 \u0440\u0430\u0437\u043e\u0440\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0435 \u0441\u0435\u0442\u0435\u0432\u043e\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u0430: \u043f\u0435\u0440\u0432\u044b\u0439 \u0431\u0430\u0439\u0442 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0443\u0435\u0434\u0435\u0442 \u0432 \u043a\u043e\u043d\u0435\u0446 \u043e\u0434\u043d\u043e\u0433\u043e \u0447\u0430\u043d\u043a\u0430, \u0432\u0442\u043e\u0440\u043e\u0439 \u2014 \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e. toString() \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0447\u0430\u043d\u043a\u0430 \u043f\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u043a\u0443 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0432 U+FFFD \u2014 \u0442\u043e\u0442 \u0441\u0430\u043c\u044b\u0439 \u00ab\u0440\u043e\u043c\u0431\u0438\u043a \u0441 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c\u00bb.<\/p>\n<p>\u0427\u0435\u043c \u0432\u044b\u0448\u0435 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438 \u0434\u043b\u0438\u043d\u043d\u0435\u0435 \u043e\u0442\u0432\u0435\u0442, \u0442\u0435\u043c \u0447\u0430\u0449\u0435 \u043f\u0430\u043a\u0435\u0442\u044b \u0431\u044c\u044e\u0442\u0441\u044f \u043d\u0435 \u043f\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u043c \u2014 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u0430\u0433 \u0438 \u043a\u0430\u0437\u0430\u043b\u0441\u044f \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0438\u043c.<\/p>\n<p>\u0414\u0432\u0430 \u0440\u0430\u0431\u043e\u0447\u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u044f:<\/p>\n<ol>\n<li>\n<p>\u041d\u0430\u043a\u0430\u043f\u043b\u0438\u0432\u0430\u0442\u044c \u0431\u0430\u0439\u0442\u044b, \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0432 \u043a\u043e\u043d\u0446\u0435:<\/p>\n<p>const chunks = []; proxyRes.on(\u201cdata\u201d, c =&gt; chunks.push\u00a9); proxyRes.on(\u201cend\u201d, () =&gt; { const data = Buffer.concat(chunks).toString(\u201cutf-8\u201d);  \/\/ \u2705 });<\/p>\n<\/li>\n<li>\n<p>\u0414\u043b\u044f \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433\u0430, \u0433\u0434\u0435 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u043e \u043f\u043e \u0445\u043e\u0434\u0443, \u2014 \u043f\u0435\u0440\u0435\u043b\u043e\u0436\u0438\u0442\u044c \u0441\u043a\u043b\u0435\u0439\u043a\u0443 \u0431\u0430\u0439\u0442\u043e\u0432 \u043d\u0430 \u0441\u0430\u043c \u043f\u043e\u0442\u043e\u043a:<\/p>\n<p>proxyRes.setEncoding(\u201cutf-8\u201d);   \/\/ \u2705 \u0442\u0435\u043f\u0435\u0440\u044c chunk \u2014 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430, \/\/ \u043f\u043e\u0442\u043e\u043a \u0441\u0430\u043c \u0434\u0435\u0440\u0436\u0438\u0442 \u00ab\u0445\u0432\u043e\u0441\u0442\u00bb \u043d\u0435\u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430<\/p>\n<\/li>\n<\/ol>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043c\u044b \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0432 \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433\u043e\u0432\u044b\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u0445 \u0432\u044b\u0448\u0435 \u2014 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 setEncoding(\u201cutf-8\u201d) \u043f\u0435\u0440\u0435\u0434 on(\u201cdata\u201d).<\/p>\n<p>\u0412\u044b\u0432\u043e\u0434 \u043f\u0440\u043e\u0441\u0442\u043e\u0439, \u043d\u043e \u0435\u0433\u043e \u043b\u0435\u0433\u043a\u043e \u0437\u0430\u0431\u044b\u0442\u044c \u043f\u043e\u0434 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u043e\u0439: \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0441\u043a\u043b\u0435\u0438\u0432\u0430\u0439\u0442\u0435 \u0441\u0435\u0442\u0435\u0432\u044b\u0435 \u0447\u0430\u043d\u043a\u0438 \u043a\u0430\u043a \u0441\u0442\u0440\u043e\u043a\u0438. \u041b\u0438\u0431\u043e Buffer.concat, \u043b\u0438\u0431\u043e setEncoding \u043d\u0430 \u043f\u043e\u0442\u043e\u043a\u0435.<\/p>\n<p>\u0413\u0440\u0430\u0431\u043b\u044f \u21162: usage \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0447\u0430\u043d\u043a\u0435<\/p>\n<p>\u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0442\u043e\u043a\u0435\u043d\u043e\u0432 (\u0434\u043b\u044f \u0443\u0447\u0451\u0442\u0430 \u0438 \u0431\u0438\u043b\u043b\u0438\u043d\u0433\u0430) \u0443 OpenAI \u043f\u0440\u0438\u043b\u0435\u0442\u0430\u0435\u0442 \u0432 \u0441\u0430\u043c\u043e\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u0438 \u043f\u0435\u0440\u0435\u0434 [DONE], \u0430 \u0443 Anthropic \u2014 \u0440\u0430\u0437\u0434\u0435\u043b\u044c\u043d\u043e: input \u0432 message_start, output \u0432 message_delta. \u0415\u0441\u043b\u0438 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c \u043f\u043e\u0442\u043e\u043a \u043b\u0435\u043d\u0438\u0432\u043e \u0438 \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u043f\u043e \u043f\u0435\u0440\u0432\u043e\u043c\u0443 [DONE], \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0442\u0435\u0440\u044f\u0442\u044c usage. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 usage \u0430\u043a\u043a\u0443\u043c\u0443\u043b\u0438\u0440\u0443\u0435\u043c \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043f\u043e \u0445\u043e\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430 \u0438 \u0444\u0438\u043a\u0441\u0438\u0440\u0443\u0435\u043c \u0432 on(\u201cend\u201d), \u0442\u0430\u043c \u0436\u0435 \u043e\u0442\u0434\u0430\u0451\u043c \u0444\u0440\u043e\u043d\u0442\u0443 \u0438\u0442\u043e\u0433\u043e\u0432\u043e\u0435 {done:true,full}.<\/p>\n<p>\u041c\u0435\u043b\u043a\u0438\u0435, \u043d\u043e \u0432\u0430\u0436\u043d\u044b\u0435 \u0434\u0435\u0442\u0430\u043b\u0438<\/p>\n<p>\u2014 \u0422\u0430\u0439\u043c\u0430\u0443\u0442 \u043d\u0430 \u0437\u0430\u043f\u0440\u043e\u0441 \u043a \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0443 (\u043c\u044b \u0441\u0442\u0430\u0432\u0438\u043c 120\u0441) + \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u0430\u044f \u043e\u0442\u0434\u0430\u0447\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432 \u0442\u043e\u043c \u0436\u0435 SSE, \u0430 \u043d\u0435 \u043e\u0431\u0440\u044b\u0432 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f. \u2014 \u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440 \u0432\u0435\u0440\u043d\u0443\u043b \u043d\u0435-200 \u2014 \u0447\u0438\u0442\u0430\u0435\u043c \u0442\u0435\u043b\u043e \u043e\u0448\u0438\u0431\u043a\u0438 \u0446\u0435\u043b\u0438\u043a\u043e\u043c (\u0447\u0435\u0440\u0435\u0437 Buffer.concat, \u0441\u043c. \u0433\u0440\u0430\u0431\u043b\u044f \u21161) \u0438 \u043e\u0442\u0434\u0430\u0451\u043c \u0444\u0440\u043e\u043d\u0442\u0443 \u0447\u0435\u043b\u043e\u0432\u0435\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435. \u2014 \u0424\u0440\u043e\u043d\u0442 \u0442\u043e\u0436\u0435 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0443\u0435\u0442 \u043f\u043e \\n\\n: \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e\u0435 SSE-\u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u043d\u0435\u043b\u044c\u0437\u044f JSON.parse\u2019\u0438\u0442\u044c.<\/p>\n<p>\u0418\u0442\u043e\u0433<\/p>\n<p>\u0421\u0432\u0435\u0441\u0442\u0438 \u0447\u0435\u0442\u044b\u0440\u0435 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433\u043e\u0432\u044b\u0445 API \u043a \u043e\u0434\u043d\u043e\u043c\u0443 SSE-\u043f\u043e\u0442\u043e\u043a\u0443 \u2014 \u044d\u0442\u043e \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u0431\u0443\u0444\u0435\u0440\u0430\u043c\u0438 \u0438 \u0437\u043d\u0430\u043d\u0438\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u043e\u0432 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430. \u0414\u0432\u0435 \u0433\u043b\u0430\u0432\u043d\u044b\u0435 \u043b\u043e\u0432\u0443\u0448\u043a\u0438 \u2014 \u0440\u0432\u0430\u043d\u044b\u0435 UTF-8 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0430\u0445 \u0447\u0430\u043d\u043a\u043e\u0432 \u0438 \u043f\u043e\u0442\u0435\u0440\u044f\u043d\u043d\u044b\u0439 usage \u2014 \u0441\u0442\u043e\u0438\u043b\u0438 \u043d\u0430\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0445\u043e\u0442\u044f \u0447\u0438\u043d\u044f\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u043e\u043a\u0443.<\/p>\n<p>\u0412\u0441\u0451 \u044d\u0442\u043e \u043a\u0440\u0443\u0442\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u0435 Nomi, \u043d\u043e \u043a\u043e\u0434 \u0438 \u0433\u0440\u0430\u0431\u043b\u0438 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b \u0434\u043b\u044f \u043b\u044e\u0431\u043e\u0433\u043e LLM-\u043f\u0440\u043e\u043a\u0441\u0438. \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u043c\u043e\u0433\u0443 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c unified-\u0444\u043e\u0440\u043c\u0430\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043e\u0442\u043c\u0435\u043d\u044b (abort) \u043d\u0430 \u0441\u0442\u0440\u0438\u043c\u0435.<\/p>\n<p>\u041f\u0438\u0448\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445, \u043a\u0442\u043e \u043a\u0430\u043a \u0440\u0435\u0448\u0430\u043b UTF-8 \u043d\u0430 \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433\u0435 \u2014 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b\u0438 \u043b\u0438 setEncoding-\u0441\u044e\u0440\u043f\u0440\u0438\u0437\u044b?<\/p>\n<\/div>\n<p>\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/1047740\/\">https:\/\/habr.com\/ru\/articles\/1047740\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u0447\u0430\u0442-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440, \u0433\u0434\u0435 \u0432 \u043e\u0434\u043d\u043e\u043c \u043e\u043a\u043d\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b GPT, Claude, Kimi \u0438 DeepSeek. \u0424\u0440\u043e\u043d\u0442\u0435\u043d\u0434\u0443 \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u2014 \u0442\u043e\u043a\u0435\u043d \u0437\u0430 \u0442\u043e\u043a\u0435\u043d\u043e\u043c, \u043a\u0430\u043a \u0432 ChatGPT. \u0411\u044d\u043a\u0435\u043d\u0434 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0445\u043e\u0434\u0438\u0442 \u043a \u0447\u0435\u0442\u044b\u0440\u0451\u043c \u0440\u0430\u0437\u043d\u044b\u043c API, \u0438 \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433 \u0443 \u043d\u0438\u0445 \u0443\u0441\u0442\u0440\u043e\u0435\u043d \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443. \u0420\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u043a\u0430\u043a \u043c\u044b \u0441\u0432\u0435\u043b\u0438 \u044d\u0442\u043e \u043a \u0435\u0434\u0438\u043d\u043e\u043c\u0443 SSE-\u043f\u043e\u0442\u043e\u043a\u0443 \u043d\u0430\u0440\u0443\u0436\u0443, \u0438 \u043f\u0440\u043e \u0434\u0432\u0435 \u0433\u0440\u0430\u0431\u043b\u0438, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u043b\u0438: \u0440\u0432\u0430\u043d\u044b\u0435 UTF-8 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0438 \u043f\u0430\u0440\u0441\u0438\u043d\u0433 \u0447\u0443\u0436\u0438\u0445 SSE.\u0421\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0432\u0441\u0435\u043c, \u043a\u0442\u043e \u043f\u0440\u043e\u043a\u0441\u0438\u0440\u0443\u0435\u0442 LLM \u0447\u0435\u0440\u0435\u0437 \u0441\u0432\u043e\u0439 \u0441\u0435\u0440\u0432\u0435\u0440.\u0417\u0430\u0447\u0435\u043c \u0432\u043e\u043e\u0431\u0449\u0435 \u0441\u0432\u043e\u0439 \u043f\u0440\u043e\u043a\u0441\u0438\u0424\u0440\u043e\u043d\u0442\u0435\u043d\u0434 \u043d\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0437\u043d\u0430\u0442\u044c \u043a\u043b\u044e\u0447\u0438 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u043e\u0432 \u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0445\u043e\u0434\u0438\u0442\u044c \u043a \u043d\u0438\u043c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e. \u0412\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438\u0434\u0443\u0442 \u0447\u0435\u0440\u0435\u0437 \u043d\u0430\u0448 Node.js-\u0431\u044d\u043a\u0435\u043d\u0434: \u043e\u043d \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043a\u043b\u044e\u0447, \u0434\u0451\u0440\u0433\u0430\u0435\u0442 \u043d\u0443\u0436\u043d\u044b\u0439 API \u0441 stream: true, \u043f\u0430\u0440\u0441\u0438\u0442 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u043f\u043e\u0442\u043e\u043a \u0438 \u043e\u0442\u0434\u0430\u0451\u0442 \u0444\u0440\u043e\u043d\u0442\u0443 \u0443\u043d\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f. \u041f\u043b\u044e\u0441 \u043d\u0430 \u0431\u044d\u043a\u0435\u043d\u0434\u0435 \u0436\u0438\u0432\u0443\u0442 \u043b\u0438\u043c\u0438\u0442\u044b, \u0443\u0447\u0451\u0442 \u0442\u043e\u043a\u0435\u043d\u043e\u0432 \u0438 \u043f\u043e\u0434\u043c\u0435\u043d\u0430 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430.\u0417\u0430\u0434\u0430\u0447\u0430: \u00ab\u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u0442\u043e\u043a \u043e\u0442 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430 X \u2192 \u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0438\u0442\u044c \u2192 \u043e\u0442\u0434\u0430\u0442\u044c \u0444\u0440\u043e\u043d\u0442\u0443 \u0432 \u0435\u0434\u0438\u043d\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435\u00bb.\u0414\u0432\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433\u0430\u041f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u044b \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0430 \u043b\u0430\u0433\u0435\u0440\u044f.OpenAI-\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0435 (GPT, DeepSeek, Kimi). SSE, \u0433\u0434\u0435 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u0438 \u043b\u0435\u0436\u0438\u0442 delta:data: {\u201cchoices\u201d:[{\u201cdelta\u201d:{\u201ccontent\u201d:\u201c\u041f\u0440\u0438\u201d}}]} data: {\u201cchoices\u201d:[{\u201cdelta\u201d:{\u201ccontent\u201d:\u201c\u0432\u0435\u0442\u201d}}]} data: [DONE]Anthropic (Claude). \u0421\u0432\u043e\u044f \u0441\u043e\u0431\u044b\u0442\u0438\u0439\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u0441 \u0442\u0438\u043f\u0430\u043c\u0438:data: {\u201ctype\u201d:\u201cmessage_start\u201d,\u201cmessage\u201d:{\u201cusage\u201d:{\u201cinput_tokens\u201d:10}}} data: {\u201ctype\u201d:\u201ccontent_block_delta\u201d,\u201cdelta\u201d:{\u201ctype\u201d:\u201ctext_delta\u201d,\u201ctext\u201d:\u201c\u041f\u0440\u0438\u201d}} data: {\u201ctype\u201d:\u201cmessage_delta\u201d,\u201cusage\u201d:{\u201coutput_tokens\u201d:5}}\u0422\u0435\u043a\u0441\u0442 \u043b\u0435\u0436\u0438\u0442 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445, \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443, \u0442\u043e\u043a\u0435\u043d\u044b usage \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445 \u043f\u043e\u0442\u043e\u043a\u0430. \u041d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u0432\u0441\u0451 \u043a \u043e\u0434\u043d\u043e\u043c\u0443 \u0432\u0438\u0434\u0443.\u0415\u0434\u0438\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u043d\u0430\u0440\u0443\u0436\u0443\u0414\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438\u0441\u044c \u0441 \u0444\u0440\u043e\u043d\u0442\u043e\u043c \u043e \u043f\u0440\u043e\u0441\u0442\u043e\u043c \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0435 \u043f\u043e\u0432\u0435\u0440\u0445 SSE:data: {\u201ct\u201d:\u201c\u043a\u0443\u0441\u043e\u043a \u0442\u0435\u043a\u0441\u0442\u0430\u201d}   \/\/ \u0434\u0435\u043b\u044c\u0442\u0430 data: {\u201cdone\u201d:true,\u201cfull\u201d:\u201c\u0432\u0435\u0441\u044c \u0442\u0435\u043a\u0441\u0442\u201d} data: [DONE]\u0414\u0430\u043b\u044c\u0448\u0435 \u2014 \u0434\u0432\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430, \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u043b\u0430\u0433\u0435\u0440\u044c.\u041f\u0430\u0440\u0441\u0438\u043d\u0433 OpenAI-\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430\u0427\u0430\u043d\u043a\u0438 \u0438\u0437 \u0441\u043e\u043a\u0435\u0442\u0430 \u043d\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442 \u0441 \u0433\u0440\u0430\u043d\u0438\u0446\u0430\u043c\u0438 SSE-\u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0443\u0435\u043c \u0438 \u0440\u0435\u0436\u0435\u043c \u043f\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044e \\n\\n:let buf = \u201c\u201d; proxyRes.setEncoding(\u201cutf-8\u201d); proxyRes.on(\u201cdata\u201d, (chunk) =&gt; { buf += chunk; const parts = buf.split(\u201c\\n\\n\u201d); buf = parts.pop()  \u201c\u201d;          \/\/ \u0445\u0432\u043e\u0441\u0442 \u2014 \u043d\u0435\u0434\u043e\u0441\u043e\u0431\u0440\u0430\u043d\u043d\u043e\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 for (const part of parts) { for (const line of part.split(\u201c\\n\u201d)) { const s = line.trim(); if (!s.startsWith(&#171;data: &#171;)  s === \u201cdata: [DONE]\u201d) continue; const evt = JSON.parse(s.slice(6)); const delta = evt.choices?.[0]?.delta?.content; if (delta) sseWrite(res, { t: delta }); } } });\u0413\u043b\u0430\u0432\u043d\u043e\u0435 \u0437\u0434\u0435\u0441\u044c \u2014 \u043d\u0435 \u043f\u0430\u0440\u0441\u0438\u0442\u044c buf \u0446\u0435\u043b\u0438\u043a\u043e\u043c \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0447\u0430\u043d\u043a\u0435, \u0430 \u043e\u0442\u0440\u0435\u0437\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0451\u043d\u043d\u044b\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f (\u0434\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \\n\\n), \u0430 \u043d\u0435\u0434\u043e\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u0445\u0432\u043e\u0441\u0442 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0432 \u0431\u0443\u0444\u0435\u0440\u0435 \u0434\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0447\u0430\u043d\u043a\u0430.Anthropic \u043f\u0430\u0440\u0441\u0438\u0442\u0441\u044f \u0442\u0430\u043a \u0436\u0435, \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u0442\u0430\u0441\u043a\u0438\u0432\u0430\u0435\u043c text \u0438\u0437 \u0441\u043e\u0431\u044b\u0442\u0438\u0439 \u0441 \u0442\u0438\u043f\u043e\u043c content_block_delta, \u0430 usage \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0438\u0437 message_start \u0438 message_delta.\u0413\u0440\u0430\u0431\u043b\u044f \u21161: data += chunk \u043b\u043e\u043c\u0430\u0435\u0442 \u0440\u0443\u0441\u0441\u043a\u0438\u0435 \u0431\u0443\u043a\u0432\u044b\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0442\u0435\u043b\u043e \u043e\u0442\u0432\u0435\u0442\u0430 \u043c\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u043b\u0438 \u043d\u0430\u0438\u0432\u043d\u043e:let data = \u201c\u201d; proxyRes.on(\u201cdata\u201d, chunk =&gt; data += chunk);   \/\/ \u274c\u0418 \u0432 \u043e\u0442\u0432\u0435\u0442\u0430\u0445 \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u044f\u0432\u043b\u044f\u043b\u0438\u0441\u044c \u00ab\u0431\u0438\u0442\u044b\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b\u00bb \u2014 \u043a\u0440\u0430\u043a\u043e\u0437\u044f\u0431\u0440\u044b \u0432\u043c\u0435\u0441\u0442\u043e \u0447\u0430\u0441\u0442\u0438 \u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u044b \u0438\u043b\u0438 \u044d\u043c\u043e\u0434\u0437\u0438. \u041f\u0440\u0438\u0447\u0451\u043c \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430, \u0430 \u0431\u0443\u0434\u0442\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e.\u041f\u0440\u0438\u0447\u0438\u043d\u0430: chunk \u2014 \u044d\u0442\u043e Buffer, \u0430 \u043d\u0435 \u0441\u0442\u0440\u043e\u043a\u0430. \u041a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f data += chunk \u043d\u0435\u044f\u0432\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 chunk.toString() \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u043a\u0443\u0441\u043a\u0435 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e. \u041c\u043d\u043e\u0433\u043e\u0431\u0430\u0439\u0442\u043d\u044b\u0435 UTF-8 \u0441\u0438\u043c\u0432\u043e\u043b\u044b (\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430 \u2014 2 \u0431\u0430\u0439\u0442\u0430, \u044d\u043c\u043e\u0434\u0437\u0438 \u2014 4) \u043c\u043e\u0433\u0443\u0442 \u0440\u0430\u0437\u043e\u0440\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0435 \u0441\u0435\u0442\u0435\u0432\u043e\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u0430: \u043f\u0435\u0440\u0432\u044b\u0439 \u0431\u0430\u0439\u0442 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0443\u0435\u0434\u0435\u0442 \u0432 \u043a\u043e\u043d\u0435\u0446 \u043e\u0434\u043d\u043e\u0433\u043e \u0447\u0430\u043d\u043a\u0430, \u0432\u0442\u043e\u0440\u043e\u0439 \u2014 \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e. toString() \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0447\u0430\u043d\u043a\u0430 \u043f\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u0443\u0435\u0442 \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u043a\u0443 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0432 U+FFFD \u2014 \u0442\u043e\u0442 \u0441\u0430\u043c\u044b\u0439 \u00ab\u0440\u043e\u043c\u0431\u0438\u043a \u0441 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c\u00bb.\u0427\u0435\u043c \u0432\u044b\u0448\u0435 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438 \u0434\u043b\u0438\u043d\u043d\u0435\u0435 \u043e\u0442\u0432\u0435\u0442, \u0442\u0435\u043c \u0447\u0430\u0449\u0435 \u043f\u0430\u043a\u0435\u0442\u044b \u0431\u044c\u044e\u0442\u0441\u044f \u043d\u0435 \u043f\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u043c \u2014 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u0430\u0433 \u0438 \u043a\u0430\u0437\u0430\u043b\u0441\u044f \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0438\u043c.\u0414\u0432\u0430 \u0440\u0430\u0431\u043e\u0447\u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u044f:\u041d\u0430\u043a\u0430\u043f\u043b\u0438\u0432\u0430\u0442\u044c \u0431\u0430\u0439\u0442\u044b, \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0432 \u043a\u043e\u043d\u0446\u0435:const chunks = []; proxyRes.on(\u201cdata\u201d, c =&gt; chunks.push\u00a9); proxyRes.on(\u201cend\u201d, () =&gt; { const data = Buffer.concat(chunks).toString(\u201cutf-8\u201d);  \/\/ \u2705 });\u0414\u043b\u044f \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433\u0430, \u0433\u0434\u0435 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u043e \u043f\u043e \u0445\u043e\u0434\u0443, \u2014 \u043f\u0435\u0440\u0435\u043b\u043e\u0436\u0438\u0442\u044c \u0441\u043a\u043b\u0435\u0439\u043a\u0443 \u0431\u0430\u0439\u0442\u043e\u0432 \u043d\u0430 \u0441\u0430\u043c \u043f\u043e\u0442\u043e\u043a:proxyRes.setEncoding(\u201cutf-8\u201d);   \/\/ \u2705 \u0442\u0435\u043f\u0435\u0440\u044c chunk \u2014 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430, \/\/ \u043f\u043e\u0442\u043e\u043a \u0441\u0430\u043c \u0434\u0435\u0440\u0436\u0438\u0442 \u00ab\u0445\u0432\u043e\u0441\u0442\u00bb \u043d\u0435\u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u0412\u0442\u043e\u0440\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043c\u044b \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0432 \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433\u043e\u0432\u044b\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u0445 \u0432\u044b\u0448\u0435 \u2014 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 setEncoding(\u201cutf-8\u201d) \u043f\u0435\u0440\u0435\u0434 on(\u201cdata\u201d).\u0412\u044b\u0432\u043e\u0434 \u043f\u0440\u043e\u0441\u0442\u043e\u0439, \u043d\u043e \u0435\u0433\u043e \u043b\u0435\u0433\u043a\u043e \u0437\u0430\u0431\u044b\u0442\u044c \u043f\u043e\u0434 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u043e\u0439: \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0441\u043a\u043b\u0435\u0438\u0432\u0430\u0439\u0442\u0435 \u0441\u0435\u0442\u0435\u0432\u044b\u0435 \u0447\u0430\u043d\u043a\u0438 \u043a\u0430\u043a \u0441\u0442\u0440\u043e\u043a\u0438. \u041b\u0438\u0431\u043e Buffer.concat, \u043b\u0438\u0431\u043e setEncoding \u043d\u0430 \u043f\u043e\u0442\u043e\u043a\u0435.\u0413\u0440\u0430\u0431\u043b\u044f \u21162: usage \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0447\u0430\u043d\u043a\u0435\u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0442\u043e\u043a\u0435\u043d\u043e\u0432 (\u0434\u043b\u044f \u0443\u0447\u0451\u0442\u0430 \u0438 \u0431\u0438\u043b\u043b\u0438\u043d\u0433\u0430) \u0443 OpenAI \u043f\u0440\u0438\u043b\u0435\u0442\u0430\u0435\u0442 \u0432 \u0441\u0430\u043c\u043e\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0441\u043e\u0431\u044b\u0442\u0438\u0438 \u043f\u0435\u0440\u0435\u0434 [DONE], \u0430 \u0443 Anthropic \u2014 \u0440\u0430\u0437\u0434\u0435\u043b\u044c\u043d\u043e: input \u0432 message_start, output \u0432 message_delta. \u0415\u0441\u043b\u0438 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c \u043f\u043e\u0442\u043e\u043a \u043b\u0435\u043d\u0438\u0432\u043e \u0438 \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u043f\u043e \u043f\u0435\u0440\u0432\u043e\u043c\u0443 [DONE], \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0442\u0435\u0440\u044f\u0442\u044c usage. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 usage \u0430\u043a\u043a\u0443\u043c\u0443\u043b\u0438\u0440\u0443\u0435\u043c \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043f\u043e \u0445\u043e\u0434\u0443 \u043f\u043e\u0442\u043e\u043a\u0430 \u0438 \u0444\u0438\u043a\u0441\u0438\u0440\u0443\u0435\u043c \u0432 on(\u201cend\u201d), \u0442\u0430\u043c \u0436\u0435 \u043e\u0442\u0434\u0430\u0451\u043c \u0444\u0440\u043e\u043d\u0442\u0443 \u0438\u0442\u043e\u0433\u043e\u0432\u043e\u0435 {done:true,full}.\u041c\u0435\u043b\u043a\u0438\u0435, \u043d\u043e \u0432\u0430\u0436\u043d\u044b\u0435 \u0434\u0435\u0442\u0430\u043b\u0438\u2014 \u0422\u0430\u0439\u043c\u0430\u0443\u0442 \u043d\u0430 \u0437\u0430\u043f\u0440\u043e\u0441 \u043a \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0443 (\u043c\u044b \u0441\u0442\u0430\u0432\u0438\u043c 120\u0441) + \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u0430\u044f \u043e\u0442\u0434\u0430\u0447\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432 \u0442\u043e\u043c \u0436\u0435 SSE, \u0430 \u043d\u0435 \u043e\u0431\u0440\u044b\u0432 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f. \u2014 \u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440 \u0432\u0435\u0440\u043d\u0443\u043b \u043d\u0435-200 \u2014 \u0447\u0438\u0442\u0430\u0435\u043c \u0442\u0435\u043b\u043e \u043e\u0448\u0438\u0431\u043a\u0438 \u0446\u0435\u043b\u0438\u043a\u043e\u043c (\u0447\u0435\u0440\u0435\u0437 Buffer.concat, \u0441\u043c. \u0433\u0440\u0430\u0431\u043b\u044f \u21161) \u0438 \u043e\u0442\u0434\u0430\u0451\u043c \u0444\u0440\u043e\u043d\u0442\u0443 \u0447\u0435\u043b\u043e\u0432\u0435\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435. \u2014 \u0424\u0440\u043e\u043d\u0442 \u0442\u043e\u0436\u0435 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0443\u0435\u0442 \u043f\u043e \\n\\n: \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e\u0435 SSE-\u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u043d\u0435\u043b\u044c\u0437\u044f JSON.parse\u2019\u0438\u0442\u044c.\u0418\u0442\u043e\u0433\u0421\u0432\u0435\u0441\u0442\u0438 \u0447\u0435\u0442\u044b\u0440\u0435 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433\u043e\u0432\u044b\u0445 API \u043a \u043e\u0434\u043d\u043e\u043c\u0443 SSE-\u043f\u043e\u0442\u043e\u043a\u0443 \u2014 \u044d\u0442\u043e \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u0431\u0443\u0444\u0435\u0440\u0430\u043c\u0438 \u0438 \u0437\u043d\u0430\u043d\u0438\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u043e\u0432 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u043e\u0432\u0430\u0439\u0434\u0435\u0440\u0430. \u0414\u0432\u0435 \u0433\u043b\u0430\u0432\u043d\u044b\u0435 \u043b\u043e\u0432\u0443\u0448\u043a\u0438 \u2014 \u0440\u0432\u0430\u043d\u044b\u0435 UTF-8 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0430\u0445 \u0447\u0430\u043d\u043a\u043e\u0432 \u0438 \u043f\u043e\u0442\u0435\u0440\u044f\u043d\u043d\u044b\u0439 usage \u2014 \u0441\u0442\u043e\u0438\u043b\u0438 \u043d\u0430\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0445\u043e\u0442\u044f \u0447\u0438\u043d\u044f\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u043e\u043a\u0443.\u0412\u0441\u0451 \u044d\u0442\u043e \u043a\u0440\u0443\u0442\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u0435 Nomi, \u043d\u043e \u043a\u043e\u0434 \u0438 \u0433\u0440\u0430\u0431\u043b\u0438 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b \u0434\u043b\u044f \u043b\u044e\u0431\u043e\u0433\u043e LLM-\u043f\u0440\u043e\u043a\u0441\u0438. \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u043c\u043e\u0433\u0443 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c unified-\u0444\u043e\u0440\u043c\u0430\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043e\u0442\u043c\u0435\u043d\u044b (abort) \u043d\u0430 \u0441\u0442\u0440\u0438\u043c\u0435.\u041f\u0438\u0448\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445, \u043a\u0442\u043e \u043a\u0430\u043a \u0440\u0435\u0448\u0430\u043b UTF-8 \u043d\u0430 \u0441\u0442\u0440\u0438\u043c\u0438\u043d\u0433\u0435 \u2014 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b\u0438 \u043b\u0438 setEncoding-\u0441\u044e\u0440\u043f\u0440\u0438\u0437\u044b?\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 https:\/\/habr.com\/ru\/articles\/1047740\/<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-483695","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/483695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=483695"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/483695\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=483695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=483695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=483695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}