from anthropic import Anthropic
from anthropic.types import (
MessageParam,
TextBlockParam,
SearchResultBlockParam,
ToolResultBlockParam
)
client = Anthropic()
# 지식 베이스 검색 도구 정의
knowledge_base_tool = {
"name": "search_knowledge_base",
"description": "회사 지식 베이스에서 정보를 검색합니다",
"input_schema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "검색 쿼리"
}
},
"required": ["query"]
}
}
# 도구 호출을 처리하는 함수
def search_knowledge_base(query):
# 여기에 검색 로직
# 올바른 형식으로 검색 결과를 반환
return [
SearchResultBlockParam(
type="search_result",
source="https://docs.company.com/product-guide",
title="제품 구성 가이드",
content=[
TextBlockParam(
type="text",
text="제품을 구성하려면 설정 > 구성으로 이동하세요. 기본 타임아웃은 30초이지만, 필요에 따라 10-120초 사이에서 조정할 수 있습니다."
)
],
citations={"enabled": True}
),
SearchResultBlockParam(
type="search_result",
source="https://docs.company.com/troubleshooting",
title="문제 해결 가이드",
content=[
TextBlockParam(
type="text",
text="타임아웃 오류가 발생하면 먼저 구성 설정을 확인하세요. 일반적인 원인으로는 네트워크 지연과 잘못된 타임아웃 값이 있습니다."
)
],
citations={"enabled": True}
)
]
# 도구와 함께 메시지 생성
response = client.messages.create(
model="claude-sonnet-4-20250514", # 지원되는 모든 모델에서 작동
max_tokens=1024,
tools=[knowledge_base_tool],
messages=[
MessageParam(
role="user",
content="타임아웃 설정을 어떻게 구성하나요?"
)
]
)
# Claude가 도구를 호출할 때 검색 결과 제공
if response.content[0].type == "tool_use":
tool_result = search_knowledge_base(response.content[0].input["query"])
# 도구 결과를 다시 보내기
final_response = client.messages.create(
model="claude-sonnet-4-20250514", # 지원되는 모든 모델에서 작동
max_tokens=1024,
messages=[
MessageParam(role="user", content="타임아웃 설정을 어떻게 구성하나요?"),
MessageParam(role="assistant", content=response.content),
MessageParam(
role="user",
content=[
ToolResultBlockParam(
type="tool_result",
tool_use_id=response.content[0].id,
content=tool_result # 검색 결과가 여기에 들어감
)
]
)
]
)