이 문서는 영어 원문을 자동 번역한 것으로, 내용이 원문과 다를 수 있습니다.영어 원문 보기
예시
요청
# pip install openai
import base64
import json
from openai import OpenAI
client = OpenAI(
api_key="UPSTAGE_API_KEY_INJECT",
base_url="https://api.upstage.ai/v1/document-classification"
)
def encode_img_to_base64(img_path):
with open(img_path, 'rb') as img_file:
img_bytes = img_file.read()
base64_data = base64.b64encode(img_bytes).decode('utf-8')
return base64_data
filepath = "./your_document.png"
base64_data = encode_img_to_base64(filepath)
response = client.chat.completions.create(
model="document-classify",
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {"url": f"data:application/octet-stream;base64,{base64_data}"}
}
]
}
],
response_format={
"type": "json_schema",
"json_schema": {
"name": "document-classify",
"schema": {
"type": "string",
"oneOf": [
{"const": "invoice", "description": "Commercial invoice with itemized charges and billing information"},
{"const": "receipt", "description": "Receipt showing purchase transaction details"},
{"const": "contract", "description": "Legal agreement or contract document"},
{"const": "cv", "description": "Curriculum vitae or resume"},
{"const": "others", "description": "Other"}
]
}
}
}
)
print(response) 응답
{
"id": "8f111e4201de097efa38d4a4d174c001",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": None,
"message": {
"content": "invoice",
"refusal": None,
"role": "assistant",
"annotations": None,
"audio": None,
"function_call": None,
"tool_calls": [{
"id": None,
"function": {
"arguments": {"document_type": {"_value": "invoice", "confidence_score": 1.0}},
"name": "additional_values"
},
"type": "function"
}]
}
}
],
"created": 1756270462,
"model": "document-classify-260304",
"object": None,
"service_tier": None,
"system_fingerprint": None,
"usage": {
"completion_tokens": 6,
"prompt_tokens": 1765,
"total_tokens": 1771,
"completion_tokens_details": None,
"prompt_tokens_details": None
}
}