[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"i-ri:search-line":3,"i-ri:moon-line":8,"i-ri:sun-line":10,"i-ri:menu-3-line":12,"i-ri:close-line":14,"notes-module-items-ai":16,"notes-page-ai-01-llm-basics\u002F02-streaming-and-prompts":74,"i-ri:arrow-left-s-line":586,"i-ri:menu-2-line":588,"i-ri:arrow-right-s-line":590,"i-ep:copy-document":592},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":7},0,24,false,"\u003Cpath fill=\"currentColor\" d=\"m18.031 16.617l4.283 4.282l-1.415 1.415l-4.282-4.283A8.96 8.96 0 0 1 11 20c-4.968 0-9-4.032-9-9s4.032-9 9-9s9 4.032 9 9a8.96 8.96 0 0 1-1.969 5.617m-2.006-.742A6.98 6.98 0 0 0 18 11c0-3.867-3.133-7-7-7s-7 3.133-7 7s3.133 7 7 7a6.98 6.98 0 0 0 4.875-1.975z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":9},"\u003Cpath fill=\"currentColor\" d=\"M10 7a7 7 0 0 0 12 4.9v.1c0 5.523-4.477 10-10 10S2 17.523 2 12S6.477 2 12 2h.1A6.98 6.98 0 0 0 10 7m-6 5a8 8 0 0 0 15.062 3.762A9 9 0 0 1 8.238 4.938A8 8 0 0 0 4 12\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":11},"\u003Cpath fill=\"currentColor\" d=\"M12 18a6 6 0 1 1 0-12a6 6 0 0 1 0 12m0-2a4 4 0 1 0 0-8a4 4 0 0 0 0 8M11 1h2v3h-2zm0 19h2v3h-2zM3.515 4.929l1.414-1.414L7.05 5.636L5.636 7.05zM16.95 18.364l1.414-1.414l2.121 2.121l-1.414 1.414zm2.121-14.85l1.414 1.415l-2.121 2.121l-1.414-1.414zM5.636 16.95l1.414 1.414l-2.121 2.121l-1.414-1.414zM23 11v2h-3v-2zM4 11v2H1v-2z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":13},"\u003Cpath fill=\"currentColor\" d=\"M3 4h18v2H3zm6 7h12v2H9zm-6 7h18v2H3z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":15},"\u003Cpath fill=\"currentColor\" d=\"m12 10.587l4.95-4.95l1.414 1.414l-4.95 4.95l4.95 4.95l-1.415 1.414l-4.95-4.95l-4.949 4.95l-1.414-1.415l4.95-4.95l-4.95-4.95L7.05 5.638z\"\u002F>",[17,22,26,30,34,38,42,46,50,54,58,62,66,70],{"path":18,"title":19,"meta":20},"\u002Fnotes\u002Fai\u002F01-llm-basics\u002F01-hello-llm","LLM 交互基础：透过现象看本质",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},"2026-11-05T09:47:15.000Z",{"path":23,"title":24,"meta":25},"\u002Fnotes\u002Fai\u002F01-llm-basics\u002F02-streaming-and-prompts","进阶交互：流式传输与提示词工程",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":27,"title":28,"meta":29},"\u002Fnotes\u002Fai\u002F01-llm-basics\u002F03-function-calling","结构化输出与 Function Calling",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":31,"title":32,"meta":33},"\u002Fnotes\u002Fai\u002F02-agent-architecture\u002F01-tool-use","Agent 核心能力 ①：Tool Use（工具调用）",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":35,"title":36,"meta":37},"\u002Fnotes\u002Fai\u002F02-agent-architecture\u002F02-mini-cursor-cli","实战项目：Mini Cursor（CLI 版）",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":39,"title":40,"meta":41},"\u002Fnotes\u002Fai\u002F02-agent-architecture\u002F03-memory","Agent 核心能力 ②：Memory（记忆管理）",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":43,"title":44,"meta":45},"\u002Fnotes\u002Fai\u002F02-agent-architecture\u002F04-planning","Agent 核心能力 ③：Planning（规划能力）",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":47,"title":48,"meta":49},"\u002Fnotes\u002Fai\u002F02-agent-architecture\u002F05-review-diff-chunking","代码改了很多，AI 为什么只审了前面？",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":51,"title":52,"meta":53},"\u002Fnotes\u002Fai\u002F02-agent-architecture","Agent 架构笔记",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":55,"title":56,"meta":57},"\u002Fnotes\u002Fai\u002F03-rag\u002F01-vector-db-and-embeddings-node","RAG ①：Embeddings 与向量数据库入门（Node.js 优先）",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":59,"title":60,"meta":61},"\u002Fnotes\u002Fai\u002F04-document-extraction\u002F01-ocr-field-evidence","OCR 读对了字，为什么地址仍然错？",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":63,"title":64,"meta":65},"\u002Fnotes\u002Fai\u002F05-development-workflow\u002F01-task-recovery","AI 任务做一半，中断后怎么继续？",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":67,"title":68,"meta":69},"\u002Fnotes\u002Fai","AI 应用开发",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"path":71,"title":72,"meta":73},"\u002Fnotes\u002Fai\u002Froadmap","AI Agent 全栈开发学习路线图 (Roadmap)",{"date":-1,"tags":-1,"summary":-1,"lastUpdated":21},{"id":75,"type":76,"sourcePath":77,"routePath":23,"path":23,"title":24,"updatedAt":21,"meta":78,"body":80,"toc":574},"bm90ZTpub3Rlcy9haS8wMS1sbG0tYmFzaWNzLzAyLXN0cmVhbWluZy1hbmQtcHJvbXB0cy5tZA","note","notes\u002Fai\u002F01-llm-basics\u002F02-streaming-and-prompts.md",{"id":75,"type":76,"sourcePath":77,"slug":79,"title":24,"updatedAt":21,"lastUpdated":21},"ai\u002F01-llm-basics\u002F02-streaming-and-prompts",{"type":81,"children":82},"root",[83,91,121,128,135,147,152,164,170,200,312,322,358,364,376,382,421,427,432,441,449,458,473,482,490,496,501,545,551,556,568],{"type":84,"tag":85,"props":86,"children":88},"element","h1",{"id":87},"进阶交互流式传输与提示词工程",[89],{"type":90,"value":24},"text",{"type":84,"tag":92,"props":93,"children":94},"blockquote",{},[95],{"type":84,"tag":96,"props":97,"children":98},"p",{},[99,105,107,112,114,119],{"type":84,"tag":100,"props":101,"children":102},"strong",{},[103],{"type":90,"value":104},"摘要",{"type":90,"value":106},"：在掌握了基础的请求-响应模式后，本章将探讨两个提升 Agent 体验的关键技术：",{"type":84,"tag":100,"props":108,"children":109},{},[110],{"type":90,"value":111},"流式传输 (Streaming)",{"type":90,"value":113}," 让用户不再枯等加载；",{"type":84,"tag":100,"props":115,"children":116},{},[117],{"type":90,"value":118},"提示词工程 (Prompt Engineering)",{"type":90,"value":120}," 让模型变得更听话、更聪明。",{"type":84,"tag":122,"props":123,"children":125},"h2",{"id":124},"_1-流式传输-streaming",[126],{"type":90,"value":127},"1. 流式传输 (Streaming)",{"type":84,"tag":129,"props":130,"children":132},"h3",{"id":131},"_11-为什么需要-streaming",[133],{"type":90,"value":134},"1.1 为什么需要 Streaming？",{"type":84,"tag":96,"props":136,"children":137},{},[138,140,145],{"type":90,"value":139},"大语言模型的生成是",{"type":84,"tag":100,"props":141,"children":142},{},[143],{"type":90,"value":144},"逐词 (Token-by-Token)",{"type":90,"value":146}," 进行的。如果一个回答有 500 个词，使用普通模式（非流式），用户必须等待整个回答全部生成完毕（可能需要 10 秒以上）才能看到第一个字。",{"type":84,"tag":96,"props":148,"children":149},{},[150],{"type":90,"value":151},"这种体验是灾难性的。",{"type":84,"tag":96,"props":153,"children":154},{},[155,157,162],{"type":90,"value":156},"流式传输利用 ",{"type":84,"tag":100,"props":158,"children":159},{},[160],{"type":90,"value":161},"Server-Sent Events (SSE)",{"type":90,"value":163}," 技术，一旦模型生成了第一个词，就立即推送到前端。这就实现了 ChatGPT 那种“打字机”般的流畅体验。",{"type":84,"tag":129,"props":165,"children":167},{"id":166},"_12-sse-协议原理",[168],{"type":90,"value":169},"1.2 SSE 协议原理",{"type":84,"tag":96,"props":171,"children":172},{},[173,175,182,184,190,192,198],{"type":90,"value":174},"当你设置 ",{"type":84,"tag":176,"props":177,"children":179},"code",{"className":178},[],[180],{"type":90,"value":181},"stream: true",{"type":90,"value":183}," 时，API 返回的 Content-Type 变为 ",{"type":84,"tag":176,"props":185,"children":187},{"className":186},[],[188],{"type":90,"value":189},"text\u002Fevent-stream",{"type":90,"value":191},"。\n响应体不再是一个巨大的 JSON，而是一系列以 ",{"type":84,"tag":176,"props":193,"children":195},{"className":194},[],[196],{"type":90,"value":197},"data:",{"type":90,"value":199}," 开头的数据块：",{"type":84,"tag":201,"props":202,"children":207},"pre",{"className":203,"code":204,"language":205,"meta":206,"style":206},"language-http shiki shiki-themes github-light github-dark","data: {\"choices\": [{\"delta\": {\"content\": \"Hello\"}}]}\n\ndata: {\"choices\": [{\"delta\": {\"content\": \" world\"}}]}\n\ndata: {\"choices\": [{\"delta\": {\"content\": \"!\"}}]}\n\ndata: [DONE]\n","http","",[208],{"type":84,"tag":176,"props":209,"children":210},{"__ignoreMap":206},[211,235,245,262,270,287,295],{"type":84,"tag":212,"props":213,"children":216},"span",{"class":214,"line":215},"line",1,[217,223,229],{"type":84,"tag":212,"props":218,"children":220},{"style":219},"--shiki-default:#22863A;--shiki-dark:#85E89D",[221],{"type":90,"value":222},"data",{"type":84,"tag":212,"props":224,"children":226},{"style":225},"--shiki-default:#D73A49;--shiki-dark:#F97583",[227],{"type":90,"value":228},":",{"type":84,"tag":212,"props":230,"children":232},{"style":231},"--shiki-default:#032F62;--shiki-dark:#9ECBFF",[233],{"type":90,"value":234}," {\"choices\": [{\"delta\": {\"content\": \"Hello\"}}]}\n",{"type":84,"tag":212,"props":236,"children":238},{"class":214,"line":237},2,[239],{"type":84,"tag":212,"props":240,"children":242},{"emptyLinePlaceholder":241},true,[243],{"type":90,"value":244},"\n",{"type":84,"tag":212,"props":246,"children":248},{"class":214,"line":247},3,[249,253,257],{"type":84,"tag":212,"props":250,"children":251},{"style":219},[252],{"type":90,"value":222},{"type":84,"tag":212,"props":254,"children":255},{"style":225},[256],{"type":90,"value":228},{"type":84,"tag":212,"props":258,"children":259},{"style":231},[260],{"type":90,"value":261}," {\"choices\": [{\"delta\": {\"content\": \" world\"}}]}\n",{"type":84,"tag":212,"props":263,"children":265},{"class":214,"line":264},4,[266],{"type":84,"tag":212,"props":267,"children":268},{"emptyLinePlaceholder":241},[269],{"type":90,"value":244},{"type":84,"tag":212,"props":271,"children":273},{"class":214,"line":272},5,[274,278,282],{"type":84,"tag":212,"props":275,"children":276},{"style":219},[277],{"type":90,"value":222},{"type":84,"tag":212,"props":279,"children":280},{"style":225},[281],{"type":90,"value":228},{"type":84,"tag":212,"props":283,"children":284},{"style":231},[285],{"type":90,"value":286}," {\"choices\": [{\"delta\": {\"content\": \"!\"}}]}\n",{"type":84,"tag":212,"props":288,"children":290},{"class":214,"line":289},6,[291],{"type":84,"tag":212,"props":292,"children":293},{"emptyLinePlaceholder":241},[294],{"type":90,"value":244},{"type":84,"tag":212,"props":296,"children":298},{"class":214,"line":297},7,[299,303,307],{"type":84,"tag":212,"props":300,"children":301},{"style":219},[302],{"type":90,"value":222},{"type":84,"tag":212,"props":304,"children":305},{"style":225},[306],{"type":90,"value":228},{"type":84,"tag":212,"props":308,"children":309},{"style":231},[310],{"type":90,"value":311}," [DONE]\n",{"type":84,"tag":96,"props":313,"children":314},{},[315,320],{"type":84,"tag":100,"props":316,"children":317},{},[318],{"type":90,"value":319},"关键点",{"type":90,"value":321},"：",{"type":84,"tag":323,"props":324,"children":325},"ul",{},[326,345],{"type":84,"tag":327,"props":328,"children":329},"li",{},[330,335,337,343],{"type":84,"tag":100,"props":331,"children":332},{},[333],{"type":90,"value":334},"Delta",{"type":90,"value":336},": 注意这里返回的是 ",{"type":84,"tag":176,"props":338,"children":340},{"className":339},[],[341],{"type":90,"value":342},"delta",{"type":90,"value":344},"（增量），而不是完整的 message。你需要自己在客户端拼接这些碎片。",{"type":84,"tag":327,"props":346,"children":347},{},[348,356],{"type":84,"tag":100,"props":349,"children":350},{},[351],{"type":84,"tag":212,"props":352,"children":353},{},[354],{"type":90,"value":355},"DONE",{"type":90,"value":357},": 这是一个特殊的标记，表示传输结束。",{"type":84,"tag":122,"props":359,"children":361},{"id":360},"_2-提示词工程-prompt-engineering",[362],{"type":90,"value":363},"2. 提示词工程 (Prompt Engineering)",{"type":84,"tag":96,"props":365,"children":366},{},[367,369,374],{"type":90,"value":368},"提示词不仅是“怎么说话”，更是一种",{"type":84,"tag":100,"props":370,"children":371},{},[372],{"type":90,"value":373},"对模型思维方式的编程",{"type":90,"value":375},"。",{"type":84,"tag":129,"props":377,"children":379},{"id":378},"_21-基础技巧",[380],{"type":90,"value":381},"2.1 基础技巧",{"type":84,"tag":323,"props":383,"children":384},{},[385,403],{"type":84,"tag":327,"props":386,"children":387},{},[388,393,395],{"type":84,"tag":100,"props":389,"children":390},{},[391],{"type":90,"value":392},"Zero-shot (零样本)",{"type":90,"value":394},": 直接提问。\n",{"type":84,"tag":92,"props":396,"children":397},{},[398],{"type":84,"tag":96,"props":399,"children":400},{},[401],{"type":90,"value":402},"\"将这句话翻译成法语：Hello World\"",{"type":84,"tag":327,"props":404,"children":405},{},[406,411,413],{"type":84,"tag":100,"props":407,"children":408},{},[409],{"type":90,"value":410},"Few-shot (少样本)",{"type":90,"value":412},": 给几个例子，让模型照猫画虎。这是最有效的提升准确率的方法。\n",{"type":84,"tag":92,"props":414,"children":415},{},[416],{"type":84,"tag":96,"props":417,"children":418},{},[419],{"type":90,"value":420},"\"将中文翻译成英文，参考以下格式：\n苹果 -> Apple\n香蕉 -> Banana\n橘子 -> \"\n(模型回答: Orange)",{"type":84,"tag":129,"props":422,"children":424},{"id":423},"_22-思维链-chain-of-thought-cot",[425],{"type":90,"value":426},"2.2 思维链 (Chain of Thought, CoT)",{"type":84,"tag":96,"props":428,"children":429},{},[430],{"type":90,"value":431},"让模型在给出答案之前，先展示思考过程。这能显著提升逻辑推理能力（数学、编程）。",{"type":84,"tag":96,"props":433,"children":434},{},[435,440],{"type":84,"tag":100,"props":436,"children":437},{},[438],{"type":90,"value":439},"普通 Prompt",{"type":90,"value":228},{"type":84,"tag":92,"props":442,"children":443},{},[444],{"type":84,"tag":96,"props":445,"children":446},{},[447],{"type":90,"value":448},"\"Roger 有 5 个网球，他又买了 2 罐，每罐 3 个。现在他有多少个？\"",{"type":84,"tag":96,"props":450,"children":451},{},[452,457],{"type":84,"tag":100,"props":453,"children":454},{},[455],{"type":90,"value":456},"CoT Prompt",{"type":90,"value":228},{"type":84,"tag":92,"props":459,"children":460},{},[461],{"type":84,"tag":96,"props":462,"children":463},{},[464,466,471],{"type":90,"value":465},"\"Roger 有 5 个网球，他又买了 2 罐，每罐 3 个。现在他有多少个？",{"type":84,"tag":100,"props":467,"children":468},{},[469],{"type":90,"value":470},"请一步步思考 (Let's think step by step)。",{"type":90,"value":472},"\"",{"type":84,"tag":96,"props":474,"children":475},{},[476,481],{"type":84,"tag":100,"props":477,"children":478},{},[479],{"type":90,"value":480},"模型回答",{"type":90,"value":228},{"type":84,"tag":92,"props":483,"children":484},{},[485],{"type":84,"tag":96,"props":486,"children":487},{},[488],{"type":90,"value":489},"\"Roger 起初有 5 个球。\n2 罐网球，每罐 3 个，意味着 2 * 3 = 6 个球。\n5 + 6 = 11。\n所以答案是 11。\"",{"type":84,"tag":129,"props":491,"children":493},{"id":492},"_23-system-prompt-的艺术",[494],{"type":90,"value":495},"2.3 System Prompt 的艺术",{"type":84,"tag":96,"props":497,"children":498},{},[499],{"type":90,"value":500},"System Prompt 是 Agent 的“人设”。好的 System Prompt 包含：",{"type":84,"tag":502,"props":503,"children":504},"ol",{},[505,515,525,535],{"type":84,"tag":327,"props":506,"children":507},{},[508,513],{"type":84,"tag":100,"props":509,"children":510},{},[511],{"type":90,"value":512},"角色定义",{"type":90,"value":514},": \"你是一个资深的 Python 架构师。\"",{"type":84,"tag":327,"props":516,"children":517},{},[518,523],{"type":84,"tag":100,"props":519,"children":520},{},[521],{"type":90,"value":522},"边界限制",{"type":90,"value":524},": \"只回答编程相关问题，如果用户问政治，礼貌拒绝。\"",{"type":84,"tag":327,"props":526,"children":527},{},[528,533],{"type":84,"tag":100,"props":529,"children":530},{},[531],{"type":90,"value":532},"输出格式",{"type":90,"value":534},": \"总是以 Markdown 格式输出代码。\"",{"type":84,"tag":327,"props":536,"children":537},{},[538,543],{"type":84,"tag":100,"props":539,"children":540},{},[541],{"type":90,"value":542},"风格指导",{"type":90,"value":544},": \"语气要幽默，多用 Emoji。\"",{"type":84,"tag":122,"props":546,"children":548},{"id":547},"_3-下一步",[549],{"type":90,"value":550},"3. 下一步",{"type":84,"tag":96,"props":552,"children":553},{},[554],{"type":90,"value":555},"掌握了流式传输，我们可以构建实时性极强的应用；掌握了 Prompt Engineering，我们可以让模型处理更复杂的逻辑。",{"type":84,"tag":96,"props":557,"children":558},{},[559,561,566],{"type":90,"value":560},"接下来，我们将进入真正的 Agent 核心领域：",{"type":84,"tag":100,"props":562,"children":563},{},[564],{"type":90,"value":565},"结构化输出与工具调用 (Function Calling)",{"type":90,"value":567},"。那是模型从“聊天机器人”进化为“智能助理”的分水岭。",{"type":84,"tag":569,"props":570,"children":571},"style",{},[572],{"type":90,"value":573},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":206,"searchDepth":237,"depth":237,"links":575},[576,580,585],{"id":124,"depth":237,"text":127,"children":577},[578,579],{"id":131,"depth":247,"text":134},{"id":166,"depth":247,"text":169},{"id":360,"depth":237,"text":363,"children":581},[582,583,584],{"id":378,"depth":247,"text":381},{"id":423,"depth":247,"text":426},{"id":492,"depth":247,"text":495},{"id":547,"depth":237,"text":550},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":587},"\u003Cpath fill=\"currentColor\" d=\"m10.828 12l4.95 4.95l-1.414 1.415L8 12l6.364-6.364l1.414 1.414z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":589},"\u003Cpath fill=\"currentColor\" d=\"M3 4h18v2H3zm0 7h12v2H3zm0 7h18v2H3z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":591},"\u003Cpath fill=\"currentColor\" d=\"m13.172 12l-4.95-4.95l1.414-1.413L16 12l-6.364 6.364l-1.414-1.415z\"\u002F>",{"left":4,"top":4,"width":593,"height":593,"rotate":4,"vFlip":6,"hFlip":6,"body":594},1024,"\u003Cpath fill=\"currentColor\" d=\"M768 832a128 128 0 0 1-128 128H192A128 128 0 0 1 64 832V384a128 128 0 0 1 128-128v64a64 64 0 0 0-64 64v448a64 64 0 0 0 64 64h448a64 64 0 0 0 64-64z\"\u002F>\u003Cpath fill=\"currentColor\" d=\"M384 128a64 64 0 0 0-64 64v448a64 64 0 0 0 64 64h448a64 64 0 0 0 64-64V192a64 64 0 0 0-64-64zm0-64h448a128 128 0 0 1 128 128v448a128 128 0 0 1-128 128H384a128 128 0 0 1-128-128V192A128 128 0 0 1 384 64\"\u002F>"]