구조화된 트리아지
Coddy AI Engineering 여정의 코드에서 모델 호출하기 섹션에 포함된 레슨. 22개 중 17번째.
챌린지
중급요약은 사람이 읽기 위한 텍스트입니다. 티켓을 분류하기 위해 코드에는 읽을 수 있는 필드가 필요합니다. 이번 단계에서는 요약 요청을 구조화된 출력으로 교체하세요.
- 첫 번째 줄 아래에
import json을 추가하고, 클라이언트 뒤에SCHEMA딕셔너리를 추가하세요:category(문자열, enum"billing","shipping","account","technical","other"),urgent(불리언),summary(문자열)를 속성으로 가지며, 세 가지 모두 필수이고,"additionalProperties": False인 객체입니다. - 반복문 전에 빈 리스트
results를 생성하세요. - 반복문 안에서 티켓 자체를 메시지로 전송하고(
Summarize:제거),output_config={"format": {"type": "json_schema", "schema": SCHEMA}}를 추가하세요. json.loads를 사용하여 텍스트 블록을 파싱하고, 딕셔너리를results에 추가한 후, 요약 줄 대신#<number> <category> | urgent: <yes or no> | <summary>를 출력하세요.
이전과 마찬가지로 입력을 계속 읽고 Tickets: <count>를 출력하세요. 예를 들어 입력 1과 My parcel arrived damaged and I need a new one immediately.에 대해 프로그램은 다음과 같이 출력합니다:
Tickets: 1
#1 shipping | urgent: yes | My parcel arrived damaged and I need a new one immediately직접 해보기
import coddy_ai as anthropic # 프로덕션 환경: import anthropic
client = anthropic.Anthropic()
count = int(input())
tickets = [input() for _ in range(count)]
print(f"Tickets: {count}")
# 각 티켓에 대해 모델에 요약을 요청하고 "#<number> "와 응답을 출력합니다
for number, ticket in enumerate(tickets, start=1):
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=200,
messages=[{"role": "user", "content": "Summarize: " + ticket}],
)
for block in response.content:
if block.type == "text":
print(f"#{number} {block.text}")
코드에서 모델 호출하기의 모든 레슨
직접 연습해 보세요: 온라인 Python 컴파일러