Sabertaz Blog

Search posts and tags

Search for posts by title or tags

LLM Basic Notes

Technical notes on Large Language Model (LLM): covering generative AI fundamentals, ChatGPT principles, scaling laws and emergent abilities, LangChain toolchain, AI Agent orchestration patterns, guardrails, and MCP servers

LLM Basic Notes

LLM Basic Notes

Generative AI

Generative Model

  • Autoregressive (AR) model: generate output one token at a time, conditioned on previous tokens.
  • Non-autoregressive (NAR) model: generate output all at once parallel, without conditioning on previous tokens.
AR ModelNAR Model
ParallelismLowHigh
SpeedSlowFast
QualityHighLow

结合上述两种方法 (Encoder + Decoder 架构):

  • 用 AR model 生成中间向量, 用 NAR model 生成最终输出.
  • 用 NAR model 多次生成, 逐步优化输出.
  • Speculative decoding: 用 NAR model 快速生成若干个预测输出, 作为 AR model 的后续输入, 使得 AR model 可以同时输出多个结果.

Scaling Law

现有的预训练语言模型对于数据的需求量远高于扩展法则 (e.g Chinchilla) 中所给出的估计规模. 很多更小的模型也能够通过使用超大规模的预训练数据获得较大的模型性能提升. 这种现象的一个重要原因是由于 Transformer 架构具有较好的数据扩展性. 目前为止, 还没有实验能够有效验证特定参数规模语言模型的饱和数据规模 (即随着数据规模的扩展, 模型性能不再提升).

Emergent Ability

大语言模型的涌现能力被非形式化定义为 在小型模型中不存在但在大模型中出现的能力:

  • In-context learning.
  • Instruction following.
  • Step-by-step reasoning.

ChatGPT

Fine-tuned GPT model on conversational data:

  • Pre-training: 学习文字接龙, 学习大规模资料 (self-supervised learning), 生成下一个单词.
  • Instruction-tuning (IT): 人工文字接龙, 人工标注部分问题的答案 (supervised learning), 引导模型生成的方向.
  • Reinforcement learning from human feedback (RLHF): 训练一个 reward model, 负责评价模型生成的答案, 提供人类反馈. 以 reward model 的评价分数为 reward, 通过强化学习优化模型. 一般聚焦于三个方面: 有用性 (Helpfulness), 诚实性 (Honesty), 无害性 (Harmlessness).

Diffusion Model

  • Forward process (diffusion) + reverse process (denoise).
  • Stable diffusion model.

Video Model

Generative videos as world models simulator.

LLM Toolchain

LangChain

LangChain aims to make programming with LLMs easier.

Model I/O module normalize LLM inputs (e.g. prompts), APIs, and outputs (e.g. completions):

Retrieval module help to process data alongside the user inputs, making it easier to retrieve relevant information:

Chains module link tasks together:

Agents module is chains with a list of functions (called tools) it can execute, while chains are hardcoded, agents choose their actions with the help of an LLM:

LLM Platform

  • OpenAI GPT API.
  • Google Gemini API.

AI Agents

AI agents powered by tricky LLMs prompting:

Agent Instruction

  • Use existing documents: 使用现有的操作程序、支持脚本或政策文档来创建 LLM 友好的 routines.
  • Prompt agents to break down tasks: 提供更小、更清晰的步骤有助于最大限度地减少歧义, 并帮助模型更好地遵循指令.
  • Define clear actions: 确保 routine 中的每一步都对应一个特定的行动或输出.
  • Capture edge cases: 实际交互通常会产生决策点, 一个健壮的 routine 会预测常见的变化, 并包含关于如何通过条件步骤或分支来处理它们的指令, e.g 在缺少所需信息时提供替代步骤.

Agent Orchestration

单智能体系统 (Single-agent systems):

多智能体系统中心模式 (Multi-agent systems in manager pattern): 其余智能体作为工具, 由中心智能体调用:

多智能体系统去中心模式 (Multi-agent systems in decentralized pattern), 多个代理作为对等体运行:

Agent Guardrails

构建防护措施:

  • 相关性分类器: 确保智能体响应保持在预期范围内, 通过标记偏离主题的查询.
  • 安全分类器: 检测试图利用系统漏洞的不安全输入 (越狱或提示注入).
  • PII 过滤器: 通过审查模型输出中任何潜在的个人身份信息 (PII), 防止不必要的个人身份信息泄露.
  • 内容审核: 标记有害或不当的输入 (仇恨言论、骚扰、暴力), 以保持安全、尊重的互动.
  • 工具安全措施: 通过评估您代理可用的每个工具的风险, 并根据只读与写入访问、可逆性、所需的账户权限和财务影响等因素分配低、中或高评级. 使用这些风险评级来触发自动化操作, 例如在高风险功能执行前暂停进行防护措施检查, 或在需要时升级到人工干预.
  • 基于规则的保护: 简单的确定性措施 (黑名单、输入长度限制、正则表达式过滤器) 以防止已知的威胁, 如禁止的术语或 SQL 注入.
  • 输出验证: 通过提示工程和内容检查确保响应与品牌价值一致, 防止可能损害品牌完整性的输出.

当超出失败阈值或高风险操作时, 触发人工干预计划, 是一项关键的安全保障措施:

MCP Server

LLM Tools

Documentation

LLM Reference