// Slide 07: Streaming -- .stream() yields chunks progressively // Code slide: how to consume the model output as a stream. const ds = require('./design-system'); function createSlide(pres, theme) { const slide = pres.addSlide(); ds.helpers.slideBase(slide, pres, theme); ds.helpers.addHeader(slide, pres, theme, { eyebrow: 'STAGE 1: CHAINS', section: 'Streaming', title: 'Потоковый вывод -- token за токеном', sectionNumber: 1, }); ds.helpers.addCodeBlock(slide, pres, theme, { x: 0.5, y: 1.5, w: 9.0, h: 3.25, language: 'python', filePath: 'examples/streaming.py', code: [ 'from langchain.chat_models import init_chat_model', '', 'model = init_chat_model("openai:gpt-4.1-mini")', '', '# .stream() -> итератор по AIMessageChunk', 'for chunk in model.stream("Write a haiku about Python"):', ' # У каждого chunk-а есть .content (текст) и .response_metadata', ' print(chunk.content, end="", flush=True)', '', 'print() # перевод строки после потока', '', '# Async-вариант: model.astream() -- то же самое в async-контексте', 'import asyncio', '', 'async def main():', ' async for chunk in model.astream("Write a haiku about async"):', ' print(chunk.content, end="", flush=True)', ].join('\n'), }); ds.helpers.addCallout(slide, pres, theme, { x: 0.5, y: 4.85, w: 9.0, h: 0.3, kind: 'success', text: 'AIMessageChunk можно складывать через оператор + -- для буферизации и метрик.', }); ds.helpers.addSourceLine(slide, pres, theme, { source: 'python.langchain.com/docs/how_to/streaming/', }); ds.helpers.addPageNumber(slide, pres, theme, 7); } module.exports = { createSlide };