Java 后端面试题库 / 全部题解免费阅读理解原理,有条理地回答

输出太长或者被截断,预算参数怎么设?

先说出你的答案

定义边界、关键机制、一个反例;P8题再补容量、失败与验证。

查看完整题解

参考回答

直接回答

先给输入、历史和工具结果留空间,再设生成上限。Chat Completions 用适用的 max_completion_tokens,Responses 查 max_output_tokens。推理模型的生成预算可能包含推理用量。

讲清原理

输出太短时,字段或表格可能只出现一半。检查 Chat Completions 的 finish_reason 或 Responses 的状态,再决定压缩输入、分段或扩大预算。

例子与易错点

下面用模拟响应区分自然结束和长度截断。未完整的 JSON 应作为失败或待恢复状态。

import json

def parse_object(choice):
    if choice["finish_reason"] != "stop":
        return None
    try:
        value = json.loads(choice["message"]["content"])
    except (TypeError, json.JSONDecodeError):
        return None
    return value if isinstance(value, dict) else None

complete = {"finish_reason": "stop", "message": {"content": '{"total":2}'}}
truncated = {"finish_reason": "length", "message": {"content": '{"total":'}}
assert parse_object(complete) == {"total": 2}
assert parse_object(truncated) is None
print("a truncated response was rejected even though some text arrived")

相关题:第 6 题、第 7 题、第 21 题、第 23 题、第 27 题。

这道题,你掌握得怎么样?

掌握程度由你主动标记,阅读和收藏不会自动改变状态。