01 LLM简介

LLM

一、LLM 是什么?

LLM(Large Language Model,大语言模型)是一种用海量文本训练出来的神经网络,核心能力是:根据已有文字,预测下一个最可能出现的文字,并反复这样做,从而「生成」一整段回复。Agent、聊天机器人、代码助手,底层几乎都是 LLM。后面调 API、写 Prompt、做 Function Calling 时容易踩坑。

二、Token 是什么?

Token 是模型处理文本的最小单位,不是「一个字 = 一个 token」。

英文:"Hello world" 大约 2 个 token

中文:"你好世界" 大约 4~8 个 token(因模型和分词器而异)

分词过程叫 Tokenization,由模型自带的 tokenizer 完成。

为什么重要

  • 计费:API 通常按 token 数收费(输入 + 输出)
  • 长度限制:上下文窗口用 token 衡量,不是字符数
  • 截断:超出窗口的内容会被截掉或报错,输入没超,但输出太长也会被截断

三、上下文窗口(Context Window)

上下文窗口 = 模型单次能「看到」的最大 token 数,输入 + 输出合计不能超过这个上限。多轮对话时,每一轮都要把历史消息再发一遍。历史越长,占用的 token 越多,迟早会顶到窗口上限。

四、模型(Model)是什么

模型 = 一套训练好的权重 + 对应的 tokenizer + 能力配置。调用 API 时要指定 model 参数,例如:

复制代码
model = "deepseek-chat"   # 或 gpt-4o、qwen-plus 等

为什么有不同模型

维度 说明
能力 推理、代码、多语言强弱不同
速度 小模型快,大模型慢
价格 通常能力越强越贵
窗口 上下文上限不同

五、API 调用流程(核心)

#mermaid-svg-MPcR6lidxhP1yfBK{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-MPcR6lidxhP1yfBK .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-MPcR6lidxhP1yfBK .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-MPcR6lidxhP1yfBK .error-icon{fill:#552222;}#mermaid-svg-MPcR6lidxhP1yfBK .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-MPcR6lidxhP1yfBK .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-MPcR6lidxhP1yfBK .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-MPcR6lidxhP1yfBK .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-MPcR6lidxhP1yfBK .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-MPcR6lidxhP1yfBK .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-MPcR6lidxhP1yfBK .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-MPcR6lidxhP1yfBK .marker{fill:#333333;stroke:#333333;}#mermaid-svg-MPcR6lidxhP1yfBK .marker.cross{stroke:#333333;}#mermaid-svg-MPcR6lidxhP1yfBK svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-MPcR6lidxhP1yfBK p{margin:0;}#mermaid-svg-MPcR6lidxhP1yfBK .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-MPcR6lidxhP1yfBK .cluster-label text{fill:#333;}#mermaid-svg-MPcR6lidxhP1yfBK .cluster-label span{color:#333;}#mermaid-svg-MPcR6lidxhP1yfBK .cluster-label span p{background-color:transparent;}#mermaid-svg-MPcR6lidxhP1yfBK .label text,#mermaid-svg-MPcR6lidxhP1yfBK span{fill:#333;color:#333;}#mermaid-svg-MPcR6lidxhP1yfBK .node rect,#mermaid-svg-MPcR6lidxhP1yfBK .node circle,#mermaid-svg-MPcR6lidxhP1yfBK .node ellipse,#mermaid-svg-MPcR6lidxhP1yfBK .node polygon,#mermaid-svg-MPcR6lidxhP1yfBK .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-MPcR6lidxhP1yfBK .rough-node .label text,#mermaid-svg-MPcR6lidxhP1yfBK .node .label text,#mermaid-svg-MPcR6lidxhP1yfBK .image-shape .label,#mermaid-svg-MPcR6lidxhP1yfBK .icon-shape .label{text-anchor:middle;}#mermaid-svg-MPcR6lidxhP1yfBK .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-MPcR6lidxhP1yfBK .rough-node .label,#mermaid-svg-MPcR6lidxhP1yfBK .node .label,#mermaid-svg-MPcR6lidxhP1yfBK .image-shape .label,#mermaid-svg-MPcR6lidxhP1yfBK .icon-shape .label{text-align:center;}#mermaid-svg-MPcR6lidxhP1yfBK .node.clickable{cursor:pointer;}#mermaid-svg-MPcR6lidxhP1yfBK .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-MPcR6lidxhP1yfBK .arrowheadPath{fill:#333333;}#mermaid-svg-MPcR6lidxhP1yfBK .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-MPcR6lidxhP1yfBK .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-MPcR6lidxhP1yfBK .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MPcR6lidxhP1yfBK .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-MPcR6lidxhP1yfBK .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MPcR6lidxhP1yfBK .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-MPcR6lidxhP1yfBK .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-MPcR6lidxhP1yfBK .cluster text{fill:#333;}#mermaid-svg-MPcR6lidxhP1yfBK .cluster span{color:#333;}#mermaid-svg-MPcR6lidxhP1yfBK div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-MPcR6lidxhP1yfBK .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-MPcR6lidxhP1yfBK rect.text{fill:none;stroke-width:0;}#mermaid-svg-MPcR6lidxhP1yfBK .icon-shape,#mermaid-svg-MPcR6lidxhP1yfBK .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-MPcR6lidxhP1yfBK .icon-shape p,#mermaid-svg-MPcR6lidxhP1yfBK .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-MPcR6lidxhP1yfBK .icon-shape .label rect,#mermaid-svg-MPcR6lidxhP1yfBK .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-MPcR6lidxhP1yfBK .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-MPcR6lidxhP1yfBK .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-MPcR6lidxhP1yfBK :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 1. HTTP POST JSON
2. 鉴权 + 调度 + 选模型
3. tokenizer encode

prompt 文字 → token ID
4. 自回归推理

逐个生成 token ID
5. tokenizer decode

token ID → 回复文字
6. 统计 token 数

封装 JSON 响应
用户 / 你的程序
API 服务商
推理服务
GPU 集群 + 大模型

一句话对照

步骤 发生什么 谁在做
请求 发 JSON(含 prompt 文字) 你的程序
调度 鉴权、选模型、分配算力 API 服务商
Encode 文字 → token ID 推理服务(tokenizer)
推理 逐个生成新 token ID GPU + 大模型
Decode token ID → 文字 推理服务(tokenizer)
响应 JSON(content + usage API 服务商 → 你的程序

术语对照

英文 是什么 在流程里对应
推理 Inference 一个动作:用已训练好的模型,根据输入算出输出 对照表第 4 步
推理服务 Inference Service 一套程序/系统:负责把请求送进模型、再把结果拿出来 Mermaid 里的 INF推理服务

GPU 集群

是跑大模型的物理算力,通常是一批装了 NVIDIA GPU(如 A100、H100)的服务器组成的集群。

大模型推理要做海量矩阵运算,GPU 很适合这种并行计算

API 服务商(OpenAI、DeepSeek 等)把模型部署在这些 GPU 上

你发请求 → 服务商调度 → 分配到某台/某组 GPU 上执行推理

所以流程里「GPU 集群,大模型」可以理解为:

概念 是什么
大模型 软件:训练好的 AI 模型本身
GPU 集群 硬件:运行这个模型的算力基础设施

两者配合:GPU 集群负责算,大模型负责生成 token。

典型响应结构

典型结构类似:

json 复制代码
{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "你好!有什么可以帮你的?"   // 已是普通文字,不是 token
      }
    }
  ],
  "usage": {
    "prompt_tokens": 10,
    "completion_tokens": 8,
    "total_tokens": 18
  }
}

六、和 Agent 的关系

今天只建立地图,后面会细化:

复制代码
Chatbot:  用户 → LLM → 回复(一轮或多轮)
Agent:    用户 → LLM →「我要调工具」→ 执行工具 → 结果再给 LLM → ... → 最终回复

LLM 在 Agent 里是决策者,不是唯一组件。工具、记忆、循环逻辑都是你自己写的代码

相关推荐
CoderJia程序员甲4 小时前
GitHub 热榜项目 - 周榜(2026-08-22)
ai·大模型·llm·github·ai教程
武子康4 小时前
多 Agent 不是多开几个终端:Pi 的 Sub-agent 取舍
人工智能·llm·agent
武子康5 小时前
Email Thread 不是 Agent Session:生产级异步通信网关的状态、幂等与审批合同
人工智能·llm·agent
liulilittle5 小时前
llmx 学习手册 06 —— CPU 指令集优化(AVX-512 三层演进)
c++·学习·算法·ai·llm
zhangphil5 小时前
Python Web后端框架FastAPI vs Flask
python·ai·llm
uncle_ll6 小时前
大模型落地选型GGUF 量化与 Ollama 部署指南
人工智能·大模型·llm·ollama·gguf
Esaka_Forever15 小时前
Skills安全要点总结
安全·llm
TechEdu2026061 天前
[人工智能]Claude、GPT、Gemini、Copilot与Llama:概念、架构、应用与评估
人工智能·ai·llm
长谷深风1111 天前
为什么你的 Tool 总被模型选错?
java·大数据·ai·llm·ai agent·工具设计·agent设计
boooooooom1 天前
尝尝咸淡:从朴素 RAG 到 Graph RAG——一个烹饪问答系统的三层检索升级之路
前端·后端·llm