GPT 구조화된 출력과 PDF
이 페이지에서는 GPT 시리즈의 구조화된 출력과 PDF 입력 방법을 설명합니다. 모델 ID와 API 설정은 GPT 시리즈 개요를 참고하십시오.
구조화된 출력
response_format으로 JSON Schema를 전달하며, strict: true이면 출력이 Schema를 엄격히 따릅니다.
gpt_json_schema.py
import json
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.ofox.ai/v1", api_key=os.environ["OFOX_API_KEY"])
resp = client.chat.completions.create(
model="openai/gpt-5.4-mini",
response_format={
"type": "json_schema",
"json_schema": {
"name": "price",
"strict": True,
"schema": {
"type": "object",
"properties": {"price": {"type": "number"}, "currency": {"type": "string"}},
"required": ["price", "currency"],
"additionalProperties": False,
},
},
},
messages=[{"role": "user", "content": "라켓과 공을 합쳐 1.10달러이고, 라켓이 공보다 1달러 비쌉니다. 공은 얼마입니까?"}],
)
print(json.loads(resp.choices[0].message.content))더 많은 작성법은 구조화된 출력을 참고하십시오.
PDF 입력
Chat Completions
gpt_pdf.py
import base64
import os
from openai import OpenAI
client = OpenAI(base_url="https://api.ofox.ai/v1", api_key=os.environ["OFOX_API_KEY"])
pdf_b64 = base64.b64encode(open("invoice.pdf", "rb").read()).decode()
resp = client.chat.completions.create(
model="openai/gpt-5.4-mini",
messages=[{"role": "user", "content": [
{"type": "file", "file": {"filename": "invoice.pdf", "file_data": f"data:application/pdf;base64,{pdf_b64}"}},
{"type": "text", "text": "이 청구서의 총액은 얼마입니까?"},
]}],
)
print(resp.choices[0].message.content)기타 오류는 자주 발생하는 오류를 참고하십시오.
마지막 업데이트