逐日AI

面试题库

共 328 题,当前筛选 1 题。

14 天用 Agent 搭一条 AI 短剧生产线

D2 剧本 Agent:把一句话变成人物卡、场景与分镜的结构化数据

  • 怎么让模型稳定输出合法的结构化数据?schema 校验失败时你会怎么处理?How do you get a model to emit valid structured data reliably, and what do you do when schema validation fails?
    国内高频海外高频基础#structured-output#schema-validation

    分析过程 · 先想清楚再作答

    1. 这题的题眼在后半句。前半句答「用 JSON 模式」就结束的人,等于说自己没在生产里跑过——真正的活儿全在校验失败之后。
    2. 先把三条路摆开:提示词约束加本地校验;厂商提供的 JSON 模式或结构化输出参数;把数据结构定义成工具的参数 schema 让模型去调。各家对后两条的支持程度和字段名都不一样,选它就等于把这段代码绑在某一家上。
    3. 给出选择依据:要跨厂商、要能离线跑,就选第一条,代价是自己写抠 JSON 与校验;只服务一家且追求成功率,就用那一家的结构化输出。抠 JSON 这一步必须处理围栏与前后寒暄,直接解析整段回复在真实模型上很容易炸。
    4. 校验失败的处理是一条阶梯,别只答重试:把带路径的问题原样喂回去让它只修这些(比换更大的模型有效);仍不过就降级到只要必填字段的最小结构;再不过就整轮失败并留档,让人来看,而不是吞掉异常返回一个空数组。
    5. 还有一条区分度很高:校验要分两层。判类型与范围只能挡住格式错,挡不住写错对象——引用了不存在的场景 id、镜号重复,这类稿子能通过类型检查,然后在下游某一步才爆。引用完整性必须单独查一遍。
    6. 可预期的追问:重试几次合适?两次。第一次是模型没听话,第二次带着具体问题还改不对,说明是提示词或 schema 本身有问题,再重试只是花钱买同一个错误。

    How to reason about it · think before answering

    1. The real question is the second half. Answering only use JSON mode signals you have never run this in production, because all the work happens after validation fails.
    2. Lay out three paths: prompt constraints plus local validation; a vendor's JSON mode or structured-output parameter; or defining the data structure as a tool's parameter schema. Vendor support and field names differ, so the latter two bind that code to one vendor.
    3. State the selection rule: cross-vendor or offline-capable means path one, paying with your own JSON extraction and validator; single-vendor and success-rate-driven means use their structured output. Extraction must handle code fences and surrounding chatter — parsing the whole reply directly breaks often.
    4. Handle failure as a ladder, not just a retry: feed the path-annotated issues back and ask it to fix only those (more effective than upgrading the model); then degrade to a minimal required-fields-only structure; then fail the round and persist the artifact for a human — never swallow the error and return an empty array.
    5. High-signal point: validate in two layers. Type and range checks catch malformed data but not wrong references — a nonexistent scene id or a duplicate shot number passes typing and explodes downstream. Referential integrity needs its own pass.
    6. Likely follow-up: how many retries? Two. The first covers a disobedient model; if it still fails with concrete issues in hand, the prompt or the schema itself is wrong and more retries just buy the same error.

    答题要点

    • 三条路:提示词加本地校验、厂商结构化输出参数、工具参数 schema,后两条会绑定厂商
    • 抠 JSON 要处理围栏与前后寒暄,不能直接解析整段回复
    • 失败处理是阶梯:带路径的问题喂回去只修这些、降级到最小结构、整轮失败留档给人
    • 校验分两层,类型与范围之外必须单独查引用完整性与 id 唯一性
    • 重试上限两次,再不过说明是提示词或 schema 的问题,不是运气问题

    Key points

    • Three paths: prompt plus local validation, vendor structured output, or tool parameter schema — the latter two bind you to a vendor
    • JSON extraction must handle code fences and surrounding prose; never parse the whole reply directly
    • Failure handling is a ladder: feed back path-annotated issues, degrade to a minimal structure, then fail the round and persist for a human
    • Validate in two layers — types and ranges, then referential integrity and id uniqueness
    • Cap retries at two; beyond that the prompt or schema is wrong, not luck