Проблема: Claude плохо считает
response = client.messages.create(
model="claude-3-haiku-20240307",
messages=[{"role": "user", "content": "Multiply 1984135 by 9343116. Only respond with the result"}],
max_tokens=400
)
# Claude ответил: 18593367726060
# Правильный ответ: 18538003464660
# Ошибка на 55 миллионов!
Решение: даём Claude калькулятор
def calculator(operation, operand1, operand2):
if operation == "add":
return operand1 + operand2
elif operation == "subtract":
return operand1 - operand2
elif operation == "multiply":
return operand1 * operand2
elif operation == "divide":
if operand2 == 0:
raise ValueError("Cannot divide by zero.")
return operand1 / operand2
calculator_tool = {
"name": "calculator",
"description": "A simple calculator that performs basic arithmetic operations.",
"input_schema": {
"type": "object",
"properties": {
"operation": {
"type": "string",
"enum": ["add", "subtract", "multiply", "divide"],
"description": "The arithmetic operation to perform."
},
"operand1": {"type": "number", "description": "The first operand."},
"operand2": {"type": "number", "description": "The second operand."}
},
"required": ["operation", "operand1", "operand2"]
}
}
response = client.messages.create(
model="claude-3-haiku-20240307",
messages=[{"role": "user", "content": "Multiply 1984135 by 9343116. Only respond with the result"}],
max_tokens=300,
tools=[calculator_tool]
)
# response.stop_reason == "tool_use"
tool_name = response.content[0].name # "calculator"
tool_inputs = response.content[0].input # {"operation": "multiply", "operand1": 1984135, "operand2": 9343116}
result = calculator(tool_inputs["operation"], tool_inputs["operand1"], tool_inputs["operand2"])
# RESULT: 18538003464660 — верно!
Системный промпт против жадных инструментов
Если спросить "What color are emeralds?" с калькулятором — Claude может попытаться его вызвать. Фикс:
system="You have access to tools, but only use them when necessary."