추출된 스키마는 메시지 content에 문자열화된 JSON 객체로 반환돼요. 스키마는 JSON Schema 문법과 Information Extract API의 입력 스키마 제약 조건을 따릅니다.
사용자는 시스템 메시지를 통해 스키마 생성에 대한 의도를 전달할 수 있어요.
생성된 스키마를 정보 추출 과정에 사용하기 전에, 특정 추출 요구에 더 잘 맞도록 편집해 보세요.
import base64import jsonfrom openai import OpenAIclient = OpenAI( api_key="UPSTAGE_API_KEY", base_url="https://api.upstage.ai/v1/information-extraction/schema-generation")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# Read the image file and encode it to base64img_path = "./bank_statement.png"base64_data = encode_img_to_base64(img_path)# Schema generation requestschema_response = client.chat.completions.create( model="information-extract", messages=[ { "role": "system", "content": "Generate schema about bank_name." }, { "role": "user", "content": [ { "type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_data}"} } ] } ],)schema = json.loads(schema_response.choices[0].message.content)
{ "type": "json_schema", "json_schema": { "name": "document_schema", "schema": { "type": "object", "properties": { "bank_name": { "type": "string", "description": "The name of bank in bank statement." } } } }}
Arrays: 동일한 키에 여러 값을 추출해야 한다면 array 타입을 사용할 수 있어요. {"type": "array"}에서는 값들을 정의하기 위해 "items" 키워드를 반드시 지정해야 합니다.
Objects: 하나의 키 아래 여러 속성을 정의하려면 object 타입을 사용할 수 있어요. {"type": "object"}에서는 구조를 정의하기 위해 "properties" 키워드가 뒤따라야 합니다. 또한, object 타입은 표 구조를 표현하기 위해 array의 items로도 사용할 수 있어요. 예시는 아래와 같습니다.
{ "type": "object", "properties": { "transactions": { "type": "array", "items": { "type": "object", "properties": { "transaction_date": { "type": "string", "description": "Date on which each transaction occurred." }, "transaction_description": { "type": "string", "description": "Description of each transaction." } } } } }}