Menu
CoddyTech

도구 호출 읽기

Coddy AI Engineering 여정의 도구와 에이전트 섹션에 포함된 레슨. 22개 중 2번째.

tool_use 블록은 세 가지 항목을 포함합니다: name(어떤 도구인지), input(모델이 선택한 인자의 딕셔너리이며, 사용자의 스키마 키를 가짐) 및 id(toolu_...와 같은 고유 ID로, 다음 레슨에서 결과와 함께 다시 보냅니다). 코드는 이 값들을 읽고 해당하는 Python 함수를 호출합니다.

def get_hours(day):
    return HOURS.get(day, "closed")

for block in response.content:
    if block.type == "tool_use" and block.name == "get_hours":
        print(block.id.startswith("toolu_"))
        print(get_hours(block.input["day"]))

What are your opening hours on Saturday?의 경우 True를 출력한 다음 10 am to 2 pm을 출력합니다.

인자는 모델에서 전달되므로 이를 사용자 입력처럼 다루어야 합니다. 모델이 지칭하는 항목이 데이터에 없을 수도 있습니다. 좋은 함수는 오류로 중단되는 대신 이러한 경우에 대해 명확한 메시지를 반환하는데, 모델이 다음에 읽는 내용이 바로 그 메시지이기 때문입니다.

challenge icon

챌린지

쉬움

모델이 요청하는 도구를 실행하세요. 시작 코드에는 get_price, 도구, 그리고 호출이 정의되어 있습니다.

  1. 타입이 "tool_use"인 블록을 찾으세요.
  2. Tool: 과 해당 이름을 출력하세요.
  3. Item: 과 해당 input의 item 인수를 출력하세요.
  4. 해당 item으로 get_price를 호출하고 Result: 과 반환하는 결과를 출력하세요.

예를 들어, 입력 What is the price of the lamp?에 대해 프로그램은 다음과 같이 출력합니다:

Tool: get_price
Item: lamp
Result: The lamp costs 40 dollars.

직접 해보기

import coddy_ai as anthropic  # 프로덕션 환경에서는: import anthropic

client = anthropic.Anthropic()

PRICES = {"lamp": 40, "desk": 120, "chair": 65}


def get_price(item):
    if item in PRICES:
        return f"The {item} costs {PRICES[item]} dollars."
    return f"No price for {item}."


price_tool = {
    "name": "get_price",
    "description": "Look up the price of an item in dollars",
    "input_schema": {
        "type": "object",
        "properties": {"item": {"type": "string", "description": "The item to price"}},
        "required": ["item"],
    },
}

question = input()
response = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=300,
    tools=[price_tool],
    messages=[{"role": "user", "content": question}],
)

# tool_use 블록의 경우: "Tool: <name>", "Item: <item>"을 출력하고,
# 해당 아이템으로 get_price를 실행한 후 "Result: <what it returns>"를 출력하세요
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

도구와 에이전트의 모든 레슨

직접 연습해 보세요: 온라인 Python 컴파일러