> For the complete documentation index, see [llms.txt](https://doc.afarensis.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.afarensis.com/neng-li/json-mo-shi.md).

# JSON 模式

概述 Afarensis API 的聊天完成功能允许用户通过指定系统消息来让模型返回有意义的 JSON 对象作为输出。这一方法在多数情况下非常有效，但在某些情况下，模型可能会生成无法解析为有效 JSON 对象的输出。

JSON 模式功能 为了防止错误输出并提高模型的性能，当调用 gpt-4-turbo-preview 或 gpt-3.5-turbo-0125 时，可以通过设置 response\_format 为 {"type": "json\_object"} 来启用 JSON 模式。此模式确保模型只生成可以解析为有效 JSON 对象的字符串。

关键注意事项 生成 JSON 指示: 在使用 JSON 模式时，请确保模型通过对话中的消息（如系统消息）得到生成 JSON 的明确指示。如果未明确指示生成 JSON，模型可能会生成无限的空格流，导致请求持续进行直至达到令牌限制。为了帮助确保您不会忘记，API 会在未在上下文中明确提及 "JSON" 字符串时抛出错误。

处理部分 JSON 输出: 如果 finish\_reason 为 length，表明由于 max\_tokens 限制或超出令牌限制，返回的 JSON 可能是不完整的（即被截断）。在解析响应之前，请进行检查以防止这种情况发生。

架构匹配: 启用 JSON 模式并不保证输出与任何特定架构匹配，但确保输出在解析时不会出错。

示例使用 from afarensis import Afarensis client = Afarensis()

response = client.chat.completions.create( model="gpt-3.5-turbo-0125", response\_format={"type": "json\_object"}, messages=\[ {"role": "system", "content": "You are a helpful assistant designed to output JSON."}, {"role": "user", "content": "Who won the world series in 2020?"} ] ) print(response.choices\[0].message.content) 在此示例中，响应包含以下 JSON 对象：

"content": "{"winner": "Los Angeles Dodgers"}" JSON 模式的常驻启用 请注意，当模型在函数调用过程中生成参数时，始终启用 JSON 模式。
