模型与向量化Models & embeddings · 单元Unit 03
模型边界:ChatModel、Formatter 与凭证Model boundary: ChatModel, Formatter and credentials
模型层把各家协议收敛为统一输入输出。学完本单元,你能定位一次模型调用失败到底是凭证、请求格式、参数还是重试策略的问题。The model layer normalizes vendor protocols into unified input and output. After this unit you can tell whether a failed call is caused by credentials, request formatting, parameters or retry policy.
学习目标Learning goals
- 区分模型、格式化器与凭证的职责Separate model, formatter and credential responsibilities
- 理解流式输出与结构化输出的接入方式Understand streaming and structured-output integration
- 为模型调用配置超时、重试与降级Configure timeouts, retries and fallbacks for model calls
概念闪卡Concept cards
点击卡片翻面查看解释。Click a card to flip it and reveal the explanation.
关键 APIKey APIs
点击进入对应的 Python 或 Java 参考页。Open the exact Python or Java reference for each type.
问题巩固Check yourself
完成 10 道题;答错会自动进入错题本。Answer all ten questions. Wrong answers are added to your wrong-answer book automatically.
01Python 聊天模型的公共基类是?What is the common base class for Python chat models?
解析Why ChatModelBase 定义统一的生成、token 计数与结构化输出能力。ChatModelBase defines unified generation, token counting and structured-output capabilities.
02Formatter 的核心职责是什么?What is the formatter responsible for?
解析Why Formatter 对齐消息角色、内容块和工具调用 id,是跨厂商兼容的关键层。The formatter aligns roles, content blocks and tool-call ids, making cross-vendor compatibility work.
03接入 OpenAI 兼容协议时应使用哪个格式化器?Which formatter should you use for OpenAI-compatible protocols?
解析Why OpenAIChatFormatter 处理 OpenAI Chat Completions 的请求与响应格式。OpenAIChatFormatter handles OpenAI Chat Completions request and response formats.
04Java 侧模型能力的统一接口是?What is the unified Java model interface?
解析Why Java 的 Model 接口声明 stream、模型名、上下文窗口和原生结构化输出能力。Java's Model interface declares stream, model name, context window and native structured-output support.
05Java 中配置超时、重试次数与退避策略使用?Which Java type configures timeouts, attempts and backoff?
解析Why ExecutionConfig 提供 timeout、maxAttempts、initialBackoff、maxBackoff 等字段。ExecutionConfig provides timeout, maxAttempts, initialBackoff and maxBackoff.
06按配置创建厂商凭证的工厂类是?Which factory creates vendor credentials from configuration?
解析Why CredentialFactory 支持注册、按字典创建凭证并列出可用 schema。CredentialFactory registers credential types, creates them from mappings and lists available schemas.
07Python 中请求结构化输出的方法是?Which Python method requests structured output?
解析Why generate_structured_output 按 schema 返回结构化结果;不支持原生能力时通常需要校验与重试。generate_structured_output returns schema-shaped output; without native support it usually needs validation and retries.
08Java 中判断模型是否原生支持结构化输出的方法是?How does Java check native structured-output support?
解析Why supportsNativeStructuredOutput 返回模型是否原生支持,避免盲目依赖提示词约束。supportsNativeStructuredOutput tells you whether the model supports it natively, avoiding prompt-only constraints.
09记录输入、输出与缓存 token 的类型是?Which type records input, output and cached tokens?
解析Why Python 与 Java 都提供 ChatUsage,用于成本核算和配额控制。Both Python and Java provide ChatUsage for cost accounting and quota control.
10模型超时与重试逻辑最适合放在哪里?Where do model timeouts and retries belong?
解析Why 把重试放在统一模型层可以避免重复业务副作用,也便于观测和限流。Centralizing retries in the model layer avoids duplicate business side effects and improves observability and rate limiting.