[{"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\u002F01-hello-llm":74,"i-ri:arrow-left-s-line":1414,"i-ri:menu-2-line":1416,"i-ri:arrow-right-s-line":1418,"i-ep:copy-document":1420},{"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":18,"path":18,"title":19,"updatedAt":21,"meta":78,"body":80,"toc":1400},"bm90ZTpub3Rlcy9haS8wMS1sbG0tYmFzaWNzLzAxLWhlbGxvLWxsbS5tZA","note","notes\u002Fai\u002F01-llm-basics\u002F01-hello-llm.md",{"id":75,"type":76,"sourcePath":77,"slug":79,"title":19,"updatedAt":21,"lastUpdated":21},"ai\u002F01-llm-basics\u002F01-hello-llm",{"type":81,"children":82},"root",[83,91,107,114,126,137,142,147,167,172,178,183,190,195,228,236,260,266,271,290,295,378,383,454,459,601,613,619,630,653,657,663,668,674,733,739,752,948,981,987,992,1290,1365,1371,1384,1394],{"type":84,"tag":85,"props":86,"children":88},"element","h1",{"id":87},"llm-交互基础透过现象看本质",[89],{"type":90,"value":19},"text",{"type":84,"tag":92,"props":93,"children":94},"blockquote",{},[95],{"type":84,"tag":96,"props":97,"children":98},"p",{},[99,105],{"type":84,"tag":100,"props":101,"children":102},"strong",{},[103],{"type":90,"value":104},"摘要",{"type":90,"value":106},"：在构建 AI Agent 之前，我们需要先理解它的“大脑”——大语言模型 (LLM) 是如何工作的。本文将脱离具体的编程语言，从 HTTP 协议和核心概念的角度，剖析 LLM 的交互原理。",{"type":84,"tag":108,"props":109,"children":111},"h2",{"id":110},"_1-什么是-llm",[112],{"type":90,"value":113},"1. 什么是 LLM？",{"type":84,"tag":96,"props":115,"children":116},{},[117,119,124],{"type":90,"value":118},"当我们使用 ChatGPT 时，它看起来像是一个无所不知的助手。但从计算机科学的角度来看，LLM 本质上是一个",{"type":84,"tag":100,"props":120,"children":121},{},[122],{"type":90,"value":123},"概率预测函数",{"type":90,"value":125},"。",{"type":84,"tag":96,"props":127,"children":128},{},[129,131,136],{"type":90,"value":130},"当你输入 \"The sky is\" 时，模型并不是在像人类一样“思考”天空的颜色。它是在基于海量的训练数据，计算字典中每一个词（Token）出现在这句话后面的",{"type":84,"tag":100,"props":132,"children":133},{},[134],{"type":90,"value":135},"概率",{"type":90,"value":125},{"type":84,"tag":96,"props":138,"children":139},{},[140],{"type":90,"value":141},"$$ P(w_i | w_1, w_2, ..., w_) $$",{"type":84,"tag":96,"props":143,"children":144},{},[145],{"type":90,"value":146},"它可能会计算出：",{"type":84,"tag":148,"props":149,"children":150},"ul",{},[151,157,162],{"type":84,"tag":152,"props":153,"children":154},"li",{},[155],{"type":90,"value":156},"\"blue\": 85%",{"type":84,"tag":152,"props":158,"children":159},{},[160],{"type":90,"value":161},"\"gray\": 10%",{"type":84,"tag":152,"props":163,"children":164},{},[165],{"type":90,"value":166},"\"limit\": 1%",{"type":84,"tag":96,"props":168,"children":169},{},[170],{"type":90,"value":171},"然后根据某种策略（如下文提到的 Temperature）选择其中一个词输出。这就是为什么 LLM 有时候会产生“幻觉”——它只是在预测最顺口的下一个词，而不是在陈述事实。",{"type":84,"tag":108,"props":173,"children":175},{"id":174},"_2-核心概念",[176],{"type":90,"value":177},"2. 核心概念",{"type":84,"tag":96,"props":179,"children":180},{},[181],{"type":90,"value":182},"在调用 API 之前，必须理解以下几个关键概念，它们直接决定了你的应用成本和效果。",{"type":84,"tag":184,"props":185,"children":187},"h3",{"id":186},"_21-token-词元",[188],{"type":90,"value":189},"2.1 Token (词元)",{"type":84,"tag":96,"props":191,"children":192},{},[193],{"type":90,"value":194},"Token 是 LLM 处理文本的最小单位，不等同于字符 (Character)。",{"type":84,"tag":148,"props":196,"children":197},{},[198,208,218],{"type":84,"tag":152,"props":199,"children":200},{},[201,206],{"type":84,"tag":100,"props":202,"children":203},{},[204],{"type":90,"value":205},"英文",{"type":90,"value":207},"：通常 1 个单词 = 1 个 Token（如 \"apple\"）。复杂单词可能被拆分。",{"type":84,"tag":152,"props":209,"children":210},{},[211,216],{"type":84,"tag":100,"props":212,"children":213},{},[214],{"type":90,"value":215},"中文",{"type":90,"value":217},"：通常 1 个汉字 = 1~2 个 Token（取决于具体模型的分词器）。",{"type":84,"tag":152,"props":219,"children":220},{},[221,226],{"type":84,"tag":100,"props":222,"children":223},{},[224],{"type":90,"value":225},"代码",{"type":90,"value":227},"：代码中的空格、缩进、符号都会消耗 Token。",{"type":84,"tag":96,"props":229,"children":230},{},[231],{"type":84,"tag":100,"props":232,"children":233},{},[234],{"type":90,"value":235},"为什么重要？",{"type":84,"tag":237,"props":238,"children":239},"ol",{},[240,250],{"type":84,"tag":152,"props":241,"children":242},{},[243,248],{"type":84,"tag":100,"props":244,"children":245},{},[246],{"type":90,"value":247},"计费",{"type":90,"value":249},"：LLM API 通常按 Token 数量收费（Prompt Tokens + Completion Tokens）。",{"type":84,"tag":152,"props":251,"children":252},{},[253,258],{"type":84,"tag":100,"props":254,"children":255},{},[256],{"type":90,"value":257},"限制",{"type":90,"value":259},"：每个模型都有最大上下文窗口（Context Window），单位也是 Token。",{"type":84,"tag":184,"props":261,"children":263},{"id":262},"_22-context-window-上下文窗口",[264],{"type":90,"value":265},"2.2 Context Window (上下文窗口)",{"type":84,"tag":96,"props":267,"children":268},{},[269],{"type":90,"value":270},"这是 LLM 的“短期记忆”容量。",{"type":84,"tag":96,"props":272,"children":273},{},[274,276,281,283,288],{"type":90,"value":275},"OpenAI 的 API 是 ",{"type":84,"tag":100,"props":277,"children":278},{},[279],{"type":90,"value":280},"Stateless (无状态)",{"type":90,"value":282}," 的。这意味着模型不会记住你上一句话说了什么。如果你想实现多轮对话，你必须",{"type":84,"tag":100,"props":284,"children":285},{},[286],{"type":90,"value":287},"每一次",{"type":90,"value":289},"都把之前的对话历史打包发给模型。",{"type":84,"tag":96,"props":291,"children":292},{},[293],{"type":90,"value":294},"请求 1:",{"type":84,"tag":296,"props":297,"children":302},"pre",{"className":298,"code":299,"language":300,"meta":301,"style":301},"language-json shiki shiki-themes github-light github-dark","[\n  {\"role\": \"user\", \"content\": \"我叫 Alice。\"}\n]\n","json","",[303],{"type":84,"tag":304,"props":305,"children":306},"code",{"__ignoreMap":301},[307,319,369],{"type":84,"tag":308,"props":309,"children":312},"span",{"class":310,"line":311},"line",1,[313],{"type":84,"tag":308,"props":314,"children":316},{"style":315},"--shiki-default:#24292E;--shiki-dark:#E1E4E8",[317],{"type":90,"value":318},"[\n",{"type":84,"tag":308,"props":320,"children":322},{"class":310,"line":321},2,[323,328,334,339,345,350,355,359,364],{"type":84,"tag":308,"props":324,"children":325},{"style":315},[326],{"type":90,"value":327},"  {",{"type":84,"tag":308,"props":329,"children":331},{"style":330},"--shiki-default:#005CC5;--shiki-dark:#79B8FF",[332],{"type":90,"value":333},"\"role\"",{"type":84,"tag":308,"props":335,"children":336},{"style":315},[337],{"type":90,"value":338},": ",{"type":84,"tag":308,"props":340,"children":342},{"style":341},"--shiki-default:#032F62;--shiki-dark:#9ECBFF",[343],{"type":90,"value":344},"\"user\"",{"type":84,"tag":308,"props":346,"children":347},{"style":315},[348],{"type":90,"value":349},", ",{"type":84,"tag":308,"props":351,"children":352},{"style":330},[353],{"type":90,"value":354},"\"content\"",{"type":84,"tag":308,"props":356,"children":357},{"style":315},[358],{"type":90,"value":338},{"type":84,"tag":308,"props":360,"children":361},{"style":341},[362],{"type":90,"value":363},"\"我叫 Alice。\"",{"type":84,"tag":308,"props":365,"children":366},{"style":315},[367],{"type":90,"value":368},"}\n",{"type":84,"tag":308,"props":370,"children":372},{"class":310,"line":371},3,[373],{"type":84,"tag":308,"props":374,"children":375},{"style":315},[376],{"type":90,"value":377},"]\n",{"type":84,"tag":96,"props":379,"children":380},{},[381],{"type":90,"value":382},"请求 2 (如果不带历史):",{"type":84,"tag":296,"props":384,"children":386},{"className":298,"code":385,"language":300,"meta":301,"style":301},"[\n  {\"role\": \"user\", \"content\": \"我刚才说我叫什么？\"}\n]\n\u002F\u002F 模型回答：我不知道。\n",[387],{"type":84,"tag":304,"props":388,"children":389},{"__ignoreMap":301},[390,397,437,444],{"type":84,"tag":308,"props":391,"children":392},{"class":310,"line":311},[393],{"type":84,"tag":308,"props":394,"children":395},{"style":315},[396],{"type":90,"value":318},{"type":84,"tag":308,"props":398,"children":399},{"class":310,"line":321},[400,404,408,412,416,420,424,428,433],{"type":84,"tag":308,"props":401,"children":402},{"style":315},[403],{"type":90,"value":327},{"type":84,"tag":308,"props":405,"children":406},{"style":330},[407],{"type":90,"value":333},{"type":84,"tag":308,"props":409,"children":410},{"style":315},[411],{"type":90,"value":338},{"type":84,"tag":308,"props":413,"children":414},{"style":341},[415],{"type":90,"value":344},{"type":84,"tag":308,"props":417,"children":418},{"style":315},[419],{"type":90,"value":349},{"type":84,"tag":308,"props":421,"children":422},{"style":330},[423],{"type":90,"value":354},{"type":84,"tag":308,"props":425,"children":426},{"style":315},[427],{"type":90,"value":338},{"type":84,"tag":308,"props":429,"children":430},{"style":341},[431],{"type":90,"value":432},"\"我刚才说我叫什么？\"",{"type":84,"tag":308,"props":434,"children":435},{"style":315},[436],{"type":90,"value":368},{"type":84,"tag":308,"props":438,"children":439},{"class":310,"line":371},[440],{"type":84,"tag":308,"props":441,"children":442},{"style":315},[443],{"type":90,"value":377},{"type":84,"tag":308,"props":445,"children":447},{"class":310,"line":446},4,[448],{"type":84,"tag":308,"props":449,"children":451},{"style":450},"--shiki-default:#6A737D;--shiki-dark:#6A737D",[452],{"type":90,"value":453},"\u002F\u002F 模型回答：我不知道。\n",{"type":84,"tag":96,"props":455,"children":456},{},[457],{"type":90,"value":458},"请求 2 (正确的做法):",{"type":84,"tag":296,"props":460,"children":462},{"className":298,"code":461,"language":300,"meta":301,"style":301},"[\n  {\"role\": \"user\", \"content\": \"我叫 Alice。\"},\n  {\"role\": \"assistant\", \"content\": \"你好 Alice！\"},\n  {\"role\": \"user\", \"content\": \"我刚才说我叫什么？\"}\n]\n",[463],{"type":84,"tag":304,"props":464,"children":465},{"__ignoreMap":301},[466,473,513,554,593],{"type":84,"tag":308,"props":467,"children":468},{"class":310,"line":311},[469],{"type":84,"tag":308,"props":470,"children":471},{"style":315},[472],{"type":90,"value":318},{"type":84,"tag":308,"props":474,"children":475},{"class":310,"line":321},[476,480,484,488,492,496,500,504,508],{"type":84,"tag":308,"props":477,"children":478},{"style":315},[479],{"type":90,"value":327},{"type":84,"tag":308,"props":481,"children":482},{"style":330},[483],{"type":90,"value":333},{"type":84,"tag":308,"props":485,"children":486},{"style":315},[487],{"type":90,"value":338},{"type":84,"tag":308,"props":489,"children":490},{"style":341},[491],{"type":90,"value":344},{"type":84,"tag":308,"props":493,"children":494},{"style":315},[495],{"type":90,"value":349},{"type":84,"tag":308,"props":497,"children":498},{"style":330},[499],{"type":90,"value":354},{"type":84,"tag":308,"props":501,"children":502},{"style":315},[503],{"type":90,"value":338},{"type":84,"tag":308,"props":505,"children":506},{"style":341},[507],{"type":90,"value":363},{"type":84,"tag":308,"props":509,"children":510},{"style":315},[511],{"type":90,"value":512},"},\n",{"type":84,"tag":308,"props":514,"children":515},{"class":310,"line":371},[516,520,524,528,533,537,541,545,550],{"type":84,"tag":308,"props":517,"children":518},{"style":315},[519],{"type":90,"value":327},{"type":84,"tag":308,"props":521,"children":522},{"style":330},[523],{"type":90,"value":333},{"type":84,"tag":308,"props":525,"children":526},{"style":315},[527],{"type":90,"value":338},{"type":84,"tag":308,"props":529,"children":530},{"style":341},[531],{"type":90,"value":532},"\"assistant\"",{"type":84,"tag":308,"props":534,"children":535},{"style":315},[536],{"type":90,"value":349},{"type":84,"tag":308,"props":538,"children":539},{"style":330},[540],{"type":90,"value":354},{"type":84,"tag":308,"props":542,"children":543},{"style":315},[544],{"type":90,"value":338},{"type":84,"tag":308,"props":546,"children":547},{"style":341},[548],{"type":90,"value":549},"\"你好 Alice！\"",{"type":84,"tag":308,"props":551,"children":552},{"style":315},[553],{"type":90,"value":512},{"type":84,"tag":308,"props":555,"children":556},{"class":310,"line":446},[557,561,565,569,573,577,581,585,589],{"type":84,"tag":308,"props":558,"children":559},{"style":315},[560],{"type":90,"value":327},{"type":84,"tag":308,"props":562,"children":563},{"style":330},[564],{"type":90,"value":333},{"type":84,"tag":308,"props":566,"children":567},{"style":315},[568],{"type":90,"value":338},{"type":84,"tag":308,"props":570,"children":571},{"style":341},[572],{"type":90,"value":344},{"type":84,"tag":308,"props":574,"children":575},{"style":315},[576],{"type":90,"value":349},{"type":84,"tag":308,"props":578,"children":579},{"style":330},[580],{"type":90,"value":354},{"type":84,"tag":308,"props":582,"children":583},{"style":315},[584],{"type":90,"value":338},{"type":84,"tag":308,"props":586,"children":587},{"style":341},[588],{"type":90,"value":432},{"type":84,"tag":308,"props":590,"children":591},{"style":315},[592],{"type":90,"value":368},{"type":84,"tag":308,"props":594,"children":596},{"class":310,"line":595},5,[597],{"type":84,"tag":308,"props":598,"children":599},{"style":315},[600],{"type":90,"value":377},{"type":84,"tag":96,"props":602,"children":603},{},[604,606,611],{"type":90,"value":605},"随着对话变长，Token 消耗会呈线性甚至指数级增长，直到撞墙（Context Window Limit）。这就是为什么",{"type":84,"tag":100,"props":607,"children":608},{},[609],{"type":90,"value":610},"记忆管理",{"type":90,"value":612},"（Memory）在 Agent 开发中如此重要。",{"type":84,"tag":184,"props":614,"children":616},{"id":615},"_23-temperature-温度",[617],{"type":90,"value":618},"2.3 Temperature (温度)",{"type":84,"tag":96,"props":620,"children":621},{},[622,624,629],{"type":90,"value":623},"这个参数控制了模型输出的",{"type":84,"tag":100,"props":625,"children":626},{},[627],{"type":90,"value":628},"随机性",{"type":90,"value":125},{"type":84,"tag":148,"props":631,"children":632},{},[633,643],{"type":84,"tag":152,"props":634,"children":635},{},[636,641],{"type":84,"tag":100,"props":637,"children":638},{},[639],{"type":90,"value":640},"Temperature = 0.0",{"type":90,"value":642},": 理性模式。模型总是选择概率最高的那个词。适合代码生成、数学解题、数据提取。",{"type":84,"tag":152,"props":644,"children":645},{},[646,651],{"type":84,"tag":100,"props":647,"children":648},{},[649],{"type":90,"value":650},"Temperature > 0.8",{"type":90,"value":652},": 创意模式。模型会尝试选择概率较低的词，带来意想不到的惊喜（或惊吓）。适合写诗、创意写作。",{"type":84,"tag":654,"props":655,"children":656},"hr",{},[],{"type":84,"tag":108,"props":658,"children":660},{"id":659},"_3-openai-api-协议标准",[661],{"type":90,"value":662},"3. OpenAI API 协议标准",{"type":84,"tag":96,"props":664,"children":665},{},[666],{"type":90,"value":667},"目前，OpenAI 的 Chat Completions API 已经成为事实上的行业标准。无论是开源模型（如 Llama 3, Qwen）还是闭源模型（Claude, DeepSeek），通常都提供兼容此格式的接口。",{"type":84,"tag":184,"props":669,"children":671},{"id":670},"_31-核心端点",[672],{"type":90,"value":673},"3.1 核心端点",{"type":84,"tag":296,"props":675,"children":679},{"className":676,"code":677,"language":678,"meta":301,"style":301},"language-http shiki shiki-themes github-light github-dark","POST \u002Fv1\u002Fchat\u002Fcompletions\nContent-Type: application\u002Fjson\nAuthorization: Bearer $OPENAI_API_KEY\n","http",[680],{"type":84,"tag":304,"props":681,"children":682},{"__ignoreMap":301},[683,697,716],{"type":84,"tag":308,"props":684,"children":685},{"class":310,"line":311},[686,692],{"type":84,"tag":308,"props":687,"children":689},{"style":688},"--shiki-default:#D73A49;--shiki-dark:#F97583",[690],{"type":90,"value":691},"POST",{"type":84,"tag":308,"props":693,"children":694},{"style":315},[695],{"type":90,"value":696}," \u002Fv1\u002Fchat\u002Fcompletions\n",{"type":84,"tag":308,"props":698,"children":699},{"class":310,"line":321},[700,706,711],{"type":84,"tag":308,"props":701,"children":703},{"style":702},"--shiki-default:#22863A;--shiki-dark:#85E89D",[704],{"type":90,"value":705},"Content-Type",{"type":84,"tag":308,"props":707,"children":708},{"style":688},[709],{"type":90,"value":710},":",{"type":84,"tag":308,"props":712,"children":713},{"style":341},[714],{"type":90,"value":715}," application\u002Fjson\n",{"type":84,"tag":308,"props":717,"children":718},{"class":310,"line":371},[719,724,728],{"type":84,"tag":308,"props":720,"children":721},{"style":702},[722],{"type":90,"value":723},"Authorization",{"type":84,"tag":308,"props":725,"children":726},{"style":688},[727],{"type":90,"value":710},{"type":84,"tag":308,"props":729,"children":730},{"style":341},[731],{"type":90,"value":732}," Bearer $OPENAI_API_KEY\n",{"type":84,"tag":184,"props":734,"children":736},{"id":735},"_32-请求结构",[737],{"type":90,"value":738},"3.2 请求结构",{"type":84,"tag":96,"props":740,"children":741},{},[742,744,750],{"type":90,"value":743},"最关键的参数是 ",{"type":84,"tag":304,"props":745,"children":747},{"className":746},[],[748],{"type":90,"value":749},"messages",{"type":90,"value":751}," 数组。它模拟了对话的剧本。",{"type":84,"tag":296,"props":753,"children":755},{"className":298,"code":754,"language":300,"meta":301,"style":301},"{\n  \"model\": \"gpt-4o\",\n  \"messages\": [\n    {\n      \"role\": \"system\", \n      \"content\": \"你是一个精通 Python 的编程助手，回答要简洁。\"\n    },\n    {\n      \"role\": \"user\", \n      \"content\": \"如何用一行代码反转列表？\"\n    }\n  ],\n  \"temperature\": 0.0\n}\n",[756],{"type":84,"tag":304,"props":757,"children":758},{"__ignoreMap":301},[759,767,789,802,810,832,850,859,867,887,904,913,922,940],{"type":84,"tag":308,"props":760,"children":761},{"class":310,"line":311},[762],{"type":84,"tag":308,"props":763,"children":764},{"style":315},[765],{"type":90,"value":766},"{\n",{"type":84,"tag":308,"props":768,"children":769},{"class":310,"line":321},[770,775,779,784],{"type":84,"tag":308,"props":771,"children":772},{"style":330},[773],{"type":90,"value":774},"  \"model\"",{"type":84,"tag":308,"props":776,"children":777},{"style":315},[778],{"type":90,"value":338},{"type":84,"tag":308,"props":780,"children":781},{"style":341},[782],{"type":90,"value":783},"\"gpt-4o\"",{"type":84,"tag":308,"props":785,"children":786},{"style":315},[787],{"type":90,"value":788},",\n",{"type":84,"tag":308,"props":790,"children":791},{"class":310,"line":371},[792,797],{"type":84,"tag":308,"props":793,"children":794},{"style":330},[795],{"type":90,"value":796},"  \"messages\"",{"type":84,"tag":308,"props":798,"children":799},{"style":315},[800],{"type":90,"value":801},": [\n",{"type":84,"tag":308,"props":803,"children":804},{"class":310,"line":446},[805],{"type":84,"tag":308,"props":806,"children":807},{"style":315},[808],{"type":90,"value":809},"    {\n",{"type":84,"tag":308,"props":811,"children":812},{"class":310,"line":595},[813,818,822,827],{"type":84,"tag":308,"props":814,"children":815},{"style":330},[816],{"type":90,"value":817},"      \"role\"",{"type":84,"tag":308,"props":819,"children":820},{"style":315},[821],{"type":90,"value":338},{"type":84,"tag":308,"props":823,"children":824},{"style":341},[825],{"type":90,"value":826},"\"system\"",{"type":84,"tag":308,"props":828,"children":829},{"style":315},[830],{"type":90,"value":831},", \n",{"type":84,"tag":308,"props":833,"children":835},{"class":310,"line":834},6,[836,841,845],{"type":84,"tag":308,"props":837,"children":838},{"style":330},[839],{"type":90,"value":840},"      \"content\"",{"type":84,"tag":308,"props":842,"children":843},{"style":315},[844],{"type":90,"value":338},{"type":84,"tag":308,"props":846,"children":847},{"style":341},[848],{"type":90,"value":849},"\"你是一个精通 Python 的编程助手，回答要简洁。\"\n",{"type":84,"tag":308,"props":851,"children":853},{"class":310,"line":852},7,[854],{"type":84,"tag":308,"props":855,"children":856},{"style":315},[857],{"type":90,"value":858},"    },\n",{"type":84,"tag":308,"props":860,"children":862},{"class":310,"line":861},8,[863],{"type":84,"tag":308,"props":864,"children":865},{"style":315},[866],{"type":90,"value":809},{"type":84,"tag":308,"props":868,"children":870},{"class":310,"line":869},9,[871,875,879,883],{"type":84,"tag":308,"props":872,"children":873},{"style":330},[874],{"type":90,"value":817},{"type":84,"tag":308,"props":876,"children":877},{"style":315},[878],{"type":90,"value":338},{"type":84,"tag":308,"props":880,"children":881},{"style":341},[882],{"type":90,"value":344},{"type":84,"tag":308,"props":884,"children":885},{"style":315},[886],{"type":90,"value":831},{"type":84,"tag":308,"props":888,"children":890},{"class":310,"line":889},10,[891,895,899],{"type":84,"tag":308,"props":892,"children":893},{"style":330},[894],{"type":90,"value":840},{"type":84,"tag":308,"props":896,"children":897},{"style":315},[898],{"type":90,"value":338},{"type":84,"tag":308,"props":900,"children":901},{"style":341},[902],{"type":90,"value":903},"\"如何用一行代码反转列表？\"\n",{"type":84,"tag":308,"props":905,"children":907},{"class":310,"line":906},11,[908],{"type":84,"tag":308,"props":909,"children":910},{"style":315},[911],{"type":90,"value":912},"    }\n",{"type":84,"tag":308,"props":914,"children":916},{"class":310,"line":915},12,[917],{"type":84,"tag":308,"props":918,"children":919},{"style":315},[920],{"type":90,"value":921},"  ],\n",{"type":84,"tag":308,"props":923,"children":925},{"class":310,"line":924},13,[926,931,935],{"type":84,"tag":308,"props":927,"children":928},{"style":330},[929],{"type":90,"value":930},"  \"temperature\"",{"type":84,"tag":308,"props":932,"children":933},{"style":315},[934],{"type":90,"value":338},{"type":84,"tag":308,"props":936,"children":937},{"style":330},[938],{"type":90,"value":939},"0.0\n",{"type":84,"tag":308,"props":941,"children":943},{"class":310,"line":942},14,[944],{"type":84,"tag":308,"props":945,"children":946},{"style":315},[947],{"type":90,"value":368},{"type":84,"tag":148,"props":949,"children":950},{},[951,961,971],{"type":84,"tag":152,"props":952,"children":953},{},[954,959],{"type":84,"tag":100,"props":955,"children":956},{},[957],{"type":90,"value":958},"system",{"type":90,"value":960},": “上帝视角”指令，设定模型的角色、语气和行为规范。",{"type":84,"tag":152,"props":962,"children":963},{},[964,969],{"type":84,"tag":100,"props":965,"children":966},{},[967],{"type":90,"value":968},"user",{"type":90,"value":970},": 用户的输入。",{"type":84,"tag":152,"props":972,"children":973},{},[974,979],{"type":84,"tag":100,"props":975,"children":976},{},[977],{"type":90,"value":978},"assistant",{"type":90,"value":980},": 模型之前的回复（用于构建历史上下文）。",{"type":84,"tag":184,"props":982,"children":984},{"id":983},"_33-响应结构",[985],{"type":90,"value":986},"3.3 响应结构",{"type":84,"tag":96,"props":988,"children":989},{},[990],{"type":90,"value":991},"API 会返回一个包含预测结果的 JSON 对象：",{"type":84,"tag":296,"props":993,"children":995},{"className":298,"code":994,"language":300,"meta":301,"style":301},"{\n  \"id\": \"chatcmpl-123\",\n  \"object\": \"chat.completion\",\n  \"created\": 1677652288,\n  \"choices\": [\n    {\n      \"index\": 0,\n      \"message\": {\n        \"role\": \"assistant\",\n        \"content\": \"my_list[::-1]\"\n      },\n      \"finish_reason\": \"stop\"\n    }\n  ],\n  \"usage\": {\n    \"prompt_tokens\": 25,\n    \"completion_tokens\": 5,\n    \"total_tokens\": 30\n  }\n}\n",[996],{"type":84,"tag":304,"props":997,"children":998},{"__ignoreMap":301},[999,1006,1027,1048,1069,1081,1088,1109,1122,1142,1159,1167,1184,1191,1198,1211,1233,1255,1273,1282],{"type":84,"tag":308,"props":1000,"children":1001},{"class":310,"line":311},[1002],{"type":84,"tag":308,"props":1003,"children":1004},{"style":315},[1005],{"type":90,"value":766},{"type":84,"tag":308,"props":1007,"children":1008},{"class":310,"line":321},[1009,1014,1018,1023],{"type":84,"tag":308,"props":1010,"children":1011},{"style":330},[1012],{"type":90,"value":1013},"  \"id\"",{"type":84,"tag":308,"props":1015,"children":1016},{"style":315},[1017],{"type":90,"value":338},{"type":84,"tag":308,"props":1019,"children":1020},{"style":341},[1021],{"type":90,"value":1022},"\"chatcmpl-123\"",{"type":84,"tag":308,"props":1024,"children":1025},{"style":315},[1026],{"type":90,"value":788},{"type":84,"tag":308,"props":1028,"children":1029},{"class":310,"line":371},[1030,1035,1039,1044],{"type":84,"tag":308,"props":1031,"children":1032},{"style":330},[1033],{"type":90,"value":1034},"  \"object\"",{"type":84,"tag":308,"props":1036,"children":1037},{"style":315},[1038],{"type":90,"value":338},{"type":84,"tag":308,"props":1040,"children":1041},{"style":341},[1042],{"type":90,"value":1043},"\"chat.completion\"",{"type":84,"tag":308,"props":1045,"children":1046},{"style":315},[1047],{"type":90,"value":788},{"type":84,"tag":308,"props":1049,"children":1050},{"class":310,"line":446},[1051,1056,1060,1065],{"type":84,"tag":308,"props":1052,"children":1053},{"style":330},[1054],{"type":90,"value":1055},"  \"created\"",{"type":84,"tag":308,"props":1057,"children":1058},{"style":315},[1059],{"type":90,"value":338},{"type":84,"tag":308,"props":1061,"children":1062},{"style":330},[1063],{"type":90,"value":1064},"1677652288",{"type":84,"tag":308,"props":1066,"children":1067},{"style":315},[1068],{"type":90,"value":788},{"type":84,"tag":308,"props":1070,"children":1071},{"class":310,"line":595},[1072,1077],{"type":84,"tag":308,"props":1073,"children":1074},{"style":330},[1075],{"type":90,"value":1076},"  \"choices\"",{"type":84,"tag":308,"props":1078,"children":1079},{"style":315},[1080],{"type":90,"value":801},{"type":84,"tag":308,"props":1082,"children":1083},{"class":310,"line":834},[1084],{"type":84,"tag":308,"props":1085,"children":1086},{"style":315},[1087],{"type":90,"value":809},{"type":84,"tag":308,"props":1089,"children":1090},{"class":310,"line":852},[1091,1096,1100,1105],{"type":84,"tag":308,"props":1092,"children":1093},{"style":330},[1094],{"type":90,"value":1095},"      \"index\"",{"type":84,"tag":308,"props":1097,"children":1098},{"style":315},[1099],{"type":90,"value":338},{"type":84,"tag":308,"props":1101,"children":1102},{"style":330},[1103],{"type":90,"value":1104},"0",{"type":84,"tag":308,"props":1106,"children":1107},{"style":315},[1108],{"type":90,"value":788},{"type":84,"tag":308,"props":1110,"children":1111},{"class":310,"line":861},[1112,1117],{"type":84,"tag":308,"props":1113,"children":1114},{"style":330},[1115],{"type":90,"value":1116},"      \"message\"",{"type":84,"tag":308,"props":1118,"children":1119},{"style":315},[1120],{"type":90,"value":1121},": {\n",{"type":84,"tag":308,"props":1123,"children":1124},{"class":310,"line":869},[1125,1130,1134,1138],{"type":84,"tag":308,"props":1126,"children":1127},{"style":330},[1128],{"type":90,"value":1129},"        \"role\"",{"type":84,"tag":308,"props":1131,"children":1132},{"style":315},[1133],{"type":90,"value":338},{"type":84,"tag":308,"props":1135,"children":1136},{"style":341},[1137],{"type":90,"value":532},{"type":84,"tag":308,"props":1139,"children":1140},{"style":315},[1141],{"type":90,"value":788},{"type":84,"tag":308,"props":1143,"children":1144},{"class":310,"line":889},[1145,1150,1154],{"type":84,"tag":308,"props":1146,"children":1147},{"style":330},[1148],{"type":90,"value":1149},"        \"content\"",{"type":84,"tag":308,"props":1151,"children":1152},{"style":315},[1153],{"type":90,"value":338},{"type":84,"tag":308,"props":1155,"children":1156},{"style":341},[1157],{"type":90,"value":1158},"\"my_list[::-1]\"\n",{"type":84,"tag":308,"props":1160,"children":1161},{"class":310,"line":906},[1162],{"type":84,"tag":308,"props":1163,"children":1164},{"style":315},[1165],{"type":90,"value":1166},"      },\n",{"type":84,"tag":308,"props":1168,"children":1169},{"class":310,"line":915},[1170,1175,1179],{"type":84,"tag":308,"props":1171,"children":1172},{"style":330},[1173],{"type":90,"value":1174},"      \"finish_reason\"",{"type":84,"tag":308,"props":1176,"children":1177},{"style":315},[1178],{"type":90,"value":338},{"type":84,"tag":308,"props":1180,"children":1181},{"style":341},[1182],{"type":90,"value":1183},"\"stop\"\n",{"type":84,"tag":308,"props":1185,"children":1186},{"class":310,"line":924},[1187],{"type":84,"tag":308,"props":1188,"children":1189},{"style":315},[1190],{"type":90,"value":912},{"type":84,"tag":308,"props":1192,"children":1193},{"class":310,"line":942},[1194],{"type":84,"tag":308,"props":1195,"children":1196},{"style":315},[1197],{"type":90,"value":921},{"type":84,"tag":308,"props":1199,"children":1201},{"class":310,"line":1200},15,[1202,1207],{"type":84,"tag":308,"props":1203,"children":1204},{"style":330},[1205],{"type":90,"value":1206},"  \"usage\"",{"type":84,"tag":308,"props":1208,"children":1209},{"style":315},[1210],{"type":90,"value":1121},{"type":84,"tag":308,"props":1212,"children":1214},{"class":310,"line":1213},16,[1215,1220,1224,1229],{"type":84,"tag":308,"props":1216,"children":1217},{"style":330},[1218],{"type":90,"value":1219},"    \"prompt_tokens\"",{"type":84,"tag":308,"props":1221,"children":1222},{"style":315},[1223],{"type":90,"value":338},{"type":84,"tag":308,"props":1225,"children":1226},{"style":330},[1227],{"type":90,"value":1228},"25",{"type":84,"tag":308,"props":1230,"children":1231},{"style":315},[1232],{"type":90,"value":788},{"type":84,"tag":308,"props":1234,"children":1236},{"class":310,"line":1235},17,[1237,1242,1246,1251],{"type":84,"tag":308,"props":1238,"children":1239},{"style":330},[1240],{"type":90,"value":1241},"    \"completion_tokens\"",{"type":84,"tag":308,"props":1243,"children":1244},{"style":315},[1245],{"type":90,"value":338},{"type":84,"tag":308,"props":1247,"children":1248},{"style":330},[1249],{"type":90,"value":1250},"5",{"type":84,"tag":308,"props":1252,"children":1253},{"style":315},[1254],{"type":90,"value":788},{"type":84,"tag":308,"props":1256,"children":1258},{"class":310,"line":1257},18,[1259,1264,1268],{"type":84,"tag":308,"props":1260,"children":1261},{"style":330},[1262],{"type":90,"value":1263},"    \"total_tokens\"",{"type":84,"tag":308,"props":1265,"children":1266},{"style":315},[1267],{"type":90,"value":338},{"type":84,"tag":308,"props":1269,"children":1270},{"style":330},[1271],{"type":90,"value":1272},"30\n",{"type":84,"tag":308,"props":1274,"children":1276},{"class":310,"line":1275},19,[1277],{"type":84,"tag":308,"props":1278,"children":1279},{"style":315},[1280],{"type":90,"value":1281},"  }\n",{"type":84,"tag":308,"props":1283,"children":1285},{"class":310,"line":1284},20,[1286],{"type":84,"tag":308,"props":1287,"children":1288},{"style":315},[1289],{"type":90,"value":368},{"type":84,"tag":148,"props":1291,"children":1292},{},[1293,1309,1319],{"type":84,"tag":152,"props":1294,"children":1295},{},[1296,1307],{"type":84,"tag":100,"props":1297,"children":1298},{},[1299,1301,1305],{"type":90,"value":1300},"choices",{"type":84,"tag":308,"props":1302,"children":1303},{},[1304],{"type":90,"value":1104},{"type":90,"value":1306},".message.content",{"type":90,"value":1308},": 这就是我们需要的回答。",{"type":84,"tag":152,"props":1310,"children":1311},{},[1312,1317],{"type":84,"tag":100,"props":1313,"children":1314},{},[1315],{"type":90,"value":1316},"usage",{"type":90,"value":1318},": 用于统计消耗和计费。",{"type":84,"tag":152,"props":1320,"children":1321},{},[1322,1327,1329],{"type":84,"tag":100,"props":1323,"children":1324},{},[1325],{"type":90,"value":1326},"finish_reason",{"type":90,"value":1328},":\n",{"type":84,"tag":148,"props":1330,"children":1331},{},[1332,1343,1354],{"type":84,"tag":152,"props":1333,"children":1334},{},[1335,1341],{"type":84,"tag":304,"props":1336,"children":1338},{"className":1337},[],[1339],{"type":90,"value":1340},"stop",{"type":90,"value":1342},": 正常结束。",{"type":84,"tag":152,"props":1344,"children":1345},{},[1346,1352],{"type":84,"tag":304,"props":1347,"children":1349},{"className":1348},[],[1350],{"type":90,"value":1351},"length",{"type":90,"value":1353},": 达到了 max_tokens 限制，回答被截断。",{"type":84,"tag":152,"props":1355,"children":1356},{},[1357,1363],{"type":84,"tag":304,"props":1358,"children":1360},{"className":1359},[],[1361],{"type":90,"value":1362},"tool_calls",{"type":90,"value":1364},": 模型想要调用工具（这是 Agent 的核心，后续会讲）。",{"type":84,"tag":108,"props":1366,"children":1368},{"id":1367},"_4-总结",[1369],{"type":90,"value":1370},"4. 总结",{"type":84,"tag":96,"props":1372,"children":1373},{},[1374,1376,1382],{"type":90,"value":1375},"掌握了这个 HTTP 协议，你就脱离了对特定 SDK（如 Python ",{"type":84,"tag":304,"props":1377,"children":1379},{"className":1378},[],[1380],{"type":90,"value":1381},"openai",{"type":90,"value":1383}," 库）的依赖。无论是用 Node.js, Go, Rust 还是 curl，你都可以轻松地与世界上最聪明的大脑进行交互。",{"type":84,"tag":96,"props":1385,"children":1386},{},[1387,1389],{"type":90,"value":1388},"这也是构建 AI Agent 的第一步：",{"type":84,"tag":100,"props":1390,"children":1391},{},[1392],{"type":90,"value":1393},"理解如何通过结构化的 JSON 数据与模型沟通。",{"type":84,"tag":1395,"props":1396,"children":1397},"style",{},[1398],{"type":90,"value":1399},"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":301,"searchDepth":321,"depth":321,"links":1401},[1402,1403,1408,1413],{"id":110,"depth":321,"text":113},{"id":174,"depth":321,"text":177,"children":1404},[1405,1406,1407],{"id":186,"depth":371,"text":189},{"id":262,"depth":371,"text":265},{"id":615,"depth":371,"text":618},{"id":659,"depth":321,"text":662,"children":1409},[1410,1411,1412],{"id":670,"depth":371,"text":673},{"id":735,"depth":371,"text":738},{"id":983,"depth":371,"text":986},{"id":1367,"depth":321,"text":1370},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1415},"\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":1417},"\u003Cpath fill=\"currentColor\" d=\"M3 4h18v2H3zm0 7h12v2H3zm0 7h18v2H3z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1419},"\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":1421,"height":1421,"rotate":4,"vFlip":6,"hFlip":6,"body":1422},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>"]