Json Mode 实战

JSON Mode

JSON Mode:让 LLM 稳定输出 JSON 的技术

核心代码

completion = client.chat.completions.create(
    model = "gpt-40-mini",
    response_format={"type": "json_obkect"},
    message = [
        {
            "role": "system",
            "content": '你现在是一个 JSON 对象提取专家,请参考我的 JSON 定义输出 JSON 对象。示例: {"service_name":"","action":""},其中,action 可以是 get_log (获取日志)、restart(重启服务)、delete(删除工作负载)',
        },
        {
            "role": "user",
            "content": "帮我重启 payment 服务。",
        },
    ],
)

示例

# pip install --upgrade openai
from openai import OpenAI

client = OpenAI(
    api_key="sk-your-api-key",
    base_url="https://api.apiyi.com/v1",
)

completion = client.chat.completions.create(
    model="gpt-4o-mini",
    response_format={"type": "json_object"},
    messages=[
        {
            "role": "system",
            "content": '你现在是一个 JSON 对象提取专家,请参考我的 JSON 定义输出 JSON 对象。示例:{"service_name":"","action":""},其中,action 可以是 get_log(获取日志)、restart(重启服务)、delete(删除工作负载)',
        },
        {
            "role": "user",
            "content": "帮我重启 payment 服务。",
        },
    ],
)

print(completion.choices[0].message.content)
{"service_name":"payment","action":"restart"}

要求: 参数配置 response_format={"type": "json_object"}

system prompt 包含 JSON 关键字.

使用场景:

从语义化内容中提取参数,然后交由程序处理。

few-shot 表现更稳定.

缺点:

JSON Schema 相对固定,难以实现复杂业务逻辑.

查找最近 2 天 payment 服务的日志

{"service_name":"","time_range":""}

# Output
{"service_name":"payment","time_range":"-2day"}

重启 payment 服务

{"service_name":"","action":""}

# Output
{"service_name":"payment","action":"restart"}

部署 payment 服务,镜像 nginx,端口 80

{"service_name":"","image":"","port":""}

# Output
{"service_name":"payment","image":"nginx","port":"80"}

System Prompt 可能需要这么写:

当查找日志时,JSON 格式:{"service_name":"","time_range":""}

当重启服务时,JSON 格式:{"service_name":"","action":""}

当部署服务时,JSON 格式:{"service_name":"","image":"","port":""}

……【无法枚举的情况】