// Slide 17: RunnableParallel + RunnableBranch -- fan-out / conditional routing // Code slide: parallel execution and if/else logic in a chain. 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: 'Runnable helpers', title: 'Parallel + Branch -- fan-out и роутинг', sectionNumber: 1, }); ds.helpers.addCodeBlock(slide, pres, theme, { x: 0.5, y: 1.5, w: 9.0, h: 3.25, language: 'python', filePath: 'examples/runnable_parallel.py', code: [ 'from langchain_core.runnables import RunnableParallel, RunnableBranch', '', '# Parallel -- один вход, несколько параллельных Runnable-ов, dict на выходе', 'joke_chain = ChatPromptTemplate.from_template("Tell a joke about {topic}") | model', 'poem_chain = ChatPromptTemplate.from_template("Write a poem about {topic}") | model', '', 'parallel = RunnableParallel(joke=joke_chain, poem=poem_chain)', 'result = parallel.invoke({"topic": "cats"})', 'print(result.keys()) # dict_keys(["joke", "poem"])', '', '# Branch -- if/else роутинг по условию', 'branch = RunnableBranch(', ' (lambda x: "code" in x["topic"].lower(), code_chain),', ' (lambda x: "math" in x["topic"].lower(), math_chain),', ' general_chain, # default', ')', 'print(branch.invoke({"topic": "code review"}))', ].join('\n'), }); ds.helpers.addCallout(slide, pres, theme, { x: 0.5, y: 4.85, w: 9.0, h: 0.3, kind: 'success', text: 'RunnableParallel выполняется параллельно -- отлично для multi-aspect анализа (sentiment + summary + keywords).', }); ds.helpers.addSourceLine(slide, pres, theme, { source: 'python.langchain.com/docs/how_to/branching/', }); ds.helpers.addPageNumber(slide, pres, theme, 17); } module.exports = { createSlide };