도구 정의하기
Coddy AI Engineering 여정의 도구와 에이전트 섹션에 포함된 레슨. 22개 중 1번째.
모델은 텍스트만 작성할 수 있습니다. 모델이 정보를 검색하거나 행동하도록 하려면 도구를 제공합니다. 도구는 모델이 실행을 요청할 수 있는 프로그램 내부의 함수입니다. 각 도구는 이름, 설명, 그리고 인자에 대한 JSON 스키마인 input_schema로 설명하고, 이 목록을 tools로 전달합니다.
time_tool = {
"name": "get_time",
"description": "Get the current local time in a city",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
}
response = client.messages.create(model="claude-opus-5-5", max_tokens=300,
tools=[time_tool], messages=[{"role": "user", "content": "What time is it in Lima?"}])
print(response.stop_reason)
print(response.content[0].name, response.content[0].input)이것은 tool_use, 그 다음 get_time {'city': 'Lima'}를 출력합니다.
모델은 아무것도 실행하지 않았습니다: 선택한 도구와 인수의 이름을 지정한 tool_use 블록과 중단 사유 "tool_use"로 응답했습니다. 질문에 도구가 필요하지 않은 경우, 텍스트로 응답하고 "end_turn"으로 중단되므로 도구 호출을 읽기 전에 확인하세요.
챌린지
쉬움모델에 날씨 도구를 제공하고 모델이 요청하는 내용을 출력하세요.
- 설명이
Get the current weather for a city이고 필수 문자열 속성인city를 갖는get_weather라는 이름의 도구를 정의하세요. tools=[...],model="claude-opus-5-5"및max_tokens=300과 함께 질문을 전송하세요.Stop:와 중단 이유를 출력하세요.- 타입이
"tool_use"인 모든 블록에 대해Tool:, 도구 이름, 공백 하나, 그리고 도구의 입력을 출력하세요.
예를 들어, What is the weather in Oslo? 입력에 대해 프로그램은 다음과 같이 출력합니다:
Stop: tool_use
Tool: get_weather {'city': 'Oslo'}날씨가 필요 없는 질문은 중단 줄만 출력합니다.
직접 해보기
import coddy_ai as anthropic # 프로덕션 환경: import anthropic
client = anthropic.Anthropic()
question = input()
# 1. "Get the current weather for a city"라는 설명을 가진 "get_weather"라는 도구를 정의합니다
# 그리고 하나의 필수 문자열 속성인 "city"를 설정합니다.
# 2. tools=[...], model="claude-opus-5-5", max_tokens=300과 함께 질문을 전송합니다.
# 3. 모든 tool_use 블록에 대해 "Stop: "과 중단 이유를 출력하고, 이어서 "Tool: <name> <input>"을 출력합니다.
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
도구와 에이전트의 모든 레슨
직접 연습해 보세요: 온라인 Python 컴파일러