[AI] Azure OpenAI 組Prompt擷取指定資訊回覆 json

  • 193
  • 0
  • AI
  • 2026-06-30

我們常會需要LLM回覆 json 格式…

 分享一個組prompt新技巧, 我們常會需要LLM回覆 json 格式, 除了few slot(重覆user/assistant幾次)示範以外, Azure OpenAI還提供參數可設定更嚴謹的格式, 如下text屬性內容

{
  "model": "gpt-5.4-mini",
  "temperature": 0.1,
  "input": [
    {
      "role": "system",
      "content": "幫我列出以下內容的廠商名稱和請款總金額:\n "
    },
    {
      "role": "user",
      "content": "xx公司….項目…….金額…總金額…"
    },
    {
      "role": "assistant",
      "content": " {\"vendor_name\":\"xx公司 xx international furniture co., ltd.\",\"total_amount\":100800}"
    },
    ... 以上是few slot可以給多次    
    {
      "role": "user",
      "content": "....全文內文...."
    }
  ],
  -----以下為格式設定-----
  "text": {
    "format": {
      "type": "json_schema",
      "name": "billing_extraction", 
      "strict": true,
      "schema": {
        "type": "object",
        "properties": {
          "vendor_name": {
            "type": "string",
            "description": "從合約內文中擷取出的廠商或供應商名稱"
          },
          "total_amount": {
            "type": "number",
            "description": "請款總金額數字"
          }
        },
        "required": ["vendor_name", "total_amount"],
        "additionalProperties": false
      }
    }
  }
}

補充說明指定的type

type用途
json_schema✅ 強結構(推薦)
json_object✅ 弱 JSON
不設✅ 自由文字

Taiwan is a country. 臺灣是我的國家