Azure Databricks의 구조화된 출력은 정의된 JSON 형식으로 응답을 생성할 수 있게 해주며, AI 애플리케이션 워크플로우의 일부입니다. 필드를 통해 response_format 지원되는 모든 채팅 모델과 함께 작업합니다. 기본 모델 제공자와 상관없이 동일한 요청 형식을 사용합니다. Azure Databricks는 모든 공급자별 변환을 처리하므로 공급자의 기본 구조화 출력 형식을 사용할 필요가 없습니다.
팁 (조언)
지니 코드 (에이전트 모드)는 이 작업을 수행할 수 있습니다. 다음 예제 프롬프트를 사용해 보세요.
Query the databricks-gpt-oss-20b model with a JSON schema response format to extract title, authors, abstract, and keywords from a research paper description. Note: this model returns content as a list of blocks; extract the block with type "text" and parse it.
구조화된 출력이란?
구조적 출력은 입력 데이터에서 JSON 개체의 형태로 구조화된 데이터를 생성하는 방법을 제공합니다. 텍스트, 구조화되지 않은 JSON 개체 및 특정 JSON 스키마를 준수하는 JSON 개체를 생성하도록 선택할 수 있습니다. 구조적 출력은 토큰당 종량제 및 프로비전된 처리량 엔드포인트를 사용하여 제공되는 채팅 모델에 대해 지원됩니다.
Databricks는 다음 시나리오에 대해 구조화된 출력을 사용하는 것이 좋습니다.
- 대량의 문서에서 데이터를 추출합니다. 예를 들어 제품 검토 피드백을 부정, 긍정 또는 중립으로 식별하고 분류합니다.
- 출력이 지정된 형식이어야 하는 일괄 처리 유추 작업입니다.
- 구조화되지 않은 데이터를 구조화된 데이터로 전환하는 것과 같은 데이터 처리입니다.
구조화된 출력 사용
채팅 요청에서 response_format를 사용하여 구조화된 출력을 지정하세요. Foundation 모델 REST API 참조를 참조하세요.
다음은 특정 JSON 스키마에 대한 연구 논문의 데이터 추출 예제입니다.
import os
import json
from openai import OpenAI
DATABRICKS_TOKEN = os.environ.get('YOUR_DATABRICKS_TOKEN')
DATABRICKS_BASE_URL = os.environ.get('YOUR_DATABRICKS_BASE_URL')
client = OpenAI(
api_key=DATABRICKS_TOKEN,
base_url=DATABRICKS_BASE_URL
)
response_format = {
"type": "json_schema",
"json_schema": {
"name": "research_paper_extraction",
"schema": {
"type": "object",
"properties": {
"title": { "type": "string" },
"authors": {
"type": "array",
"items": { "type": "string" }
},
"abstract": { "type": "string" },
"keywords": {
"type": "array",
"items": { "type": "string" }
}
},
},
"strict": True
}
}
messages = [{
"role": "system",
"content": "You are an expert at structured data extraction. You will be given unstructured text from a research paper and should convert it into the given structure."
},
{
"role": "user",
"content": "..."
}]
response = client.chat.completions.create(
model="databricks-gpt-oss-20b",
messages=messages,
response_format=response_format
)
print(json.dumps(response.choices[0].message.model_dump()['content'], indent=2))
다음은 JSON 추출의 예이지만 JSON 스키마는 미리 알 수 없습니다.
import os
import json
from openai import OpenAI
DATABRICKS_TOKEN = os.environ.get('YOUR_DATABRICKS_TOKEN')
DATABRICKS_BASE_URL = os.environ.get('YOUR_DATABRICKS_BASE_URL')
client = OpenAI(
api_key=DATABRICKS_TOKEN,
base_url=DATABRICKS_BASE_URL
)
response_format = {
"type": "json_object",
}
messages = [
{
"role": "user",
"content": "Extract the name, size, price, and color from this product description as a JSON object:\n<description>\nThe SmartHome Mini is a compact smart home assistant available in black or white for only $49.99. It's 5 inches wide.\n</description>"
}]
response = client.chat.completions.create(
model="databricks-gpt-oss-20b",
messages=messages,
response_format=response_format
)
print(json.dumps(response.choices[0].message.model_dump()['content'], indent=2))
JSON 스키마
파운데이션 모델 API는 OpenAI에서 허용하는 구조적 출력을 광범위하게 지원합니다. 그러나 JSON 스키마 정의에 더 간단한 JSON 스키마를 사용하면 고품질 JSON 생성이 발생합니다. 더 높은 품질의 생성을 촉진하기 위해 Foundation Model API는
다음 함수 호출 정의 키는 지원되지 않습니다.
-
pattern을 사용한 정규식. - 복잡한 중첩 또는 스키마 구성 및 유효성 검사에 다음을 사용합니다:
anyOf,oneOf,allOf,prefixItems, 또는$ref. - 특수한 경우인
[type, “null”]를 제외하면, 목록에는 하나는 유효한 JSON 형식이고 다른 하나는"null"인 형식 목록이 포함됩니다.
Anthropic Claude 모델을 사용한 구조화된 출력
다음 예시는 Claude 모델을 사용하여 특정 JSON 스키마로 데이터를 추출합니다. 이전 예시와 다른 유일한 점은 값입니다 model .
import os
import json
from openai import OpenAI
DATABRICKS_TOKEN = os.environ.get('YOUR_DATABRICKS_TOKEN')
DATABRICKS_BASE_URL = os.environ.get('YOUR_DATABRICKS_BASE_URL')
client = OpenAI(
api_key=DATABRICKS_TOKEN,
base_url=DATABRICKS_BASE_URL
)
response_format = {
"type": "json_schema",
"json_schema": {
"name": "research_paper_extraction",
"schema": {
"type": "object",
"properties": {
"title": { "type": "string" },
"authors": {
"type": "array",
"items": { "type": "string" }
},
"abstract": { "type": "string" },
"keywords": {
"type": "array",
"items": { "type": "string" }
}
},
},
"strict": True
}
}
messages = [{
"role": "system",
"content": "You are an expert at structured data extraction. You will be given unstructured text from a research paper and should convert it into the given structure."
},
{
"role": "user",
"content": "..."
}]
response = client.chat.completions.create(
model="databricks-claude-sonnet-4-5",
messages=messages,
response_format=response_format
)
print(json.dumps(response.choices[0].message.model_dump()['content'], indent=2))
클로드 모델은 구조화된 출력에 대한 추가 제약 조건을 가집니다. 제한 섹션을 참조하세요.
토큰 사용량
프롬프트 주입 및 기타 기술은 구조화된 출력의 품질을 향상시키는 데 사용됩니다. 이렇게 하면 모델에서 사용하는 입력 및 출력 토큰의 수에 영향을 미치므로 청구에 영향을 줍니다.
제한 사항
- JSON 스키마에 지정된 최대 키 수는
64. - 파운데이션 모델 API는 개체 및 배열에 대한 길이 또는 크기 제약 조건을 적용하지 않습니다.
- 여기에는
maxProperties,minProperties및maxLength와 같은 키워드가 포함됩니다.
- 여기에는
- 중첩된 JSON 스키마는 품질이 저하됩니다. 가능한 경우 더 나은 결과를 위해 JSON 스키마를 평면화해 봅니다.
Anthropic Claude 모델은 구조화된 출력에 대해 다음과 같은 추가 제약 조건을 가집니다:
- 구조화된 출력 타입만
json_schema지원됩니다.json_object은 지원되지 않습니다. 무제한 출력의 경우 . 를 생략response_format하세요. - 구조화된 출력은 스트리밍에서 지원되지 않습니다. . 를 지정할
stream때 로false설정하세요response_format. - Claude 구조화 출력의 매개변수는
response_format또는tools와 결합tool_choice할 수 없습니다.