pip install anthropic pydantic
pip install anthropic requests python-dotenv
Create atomic, single-responsibility tool functions with clear Anthropic schemas.
1 import json 2 from datetime import datetime 3 4 def get_weather(location: str, unit: str = "celsius") -> dict: 5 """Get current weather. Simulated for the lab.""" 6 weather_data = { 7 "London": {"temp_c": 12, "condition": "Cloudy", "humidity": 78}, 8 "Tokyo": {"temp_c": 22, "condition": "Sunny", "humidity": 55}, 9 "New York": {"temp_c": 18, "condition": "Partly cloudy", "humidity": 65}, 10 } 11 data = weather_data.get(location, {"temp_c": 20, "condition": "Unknown", "humidity": 60}) 12 temp = data["temp_c"] if unit == "celsius" else round(data["temp_c"] * 9/5 + 32, 1) 13 return {"location": location, "temperature": temp, "unit": unit, "condition": data["condition"]} 14 15 def calculate(expression: str) -> dict: 16 """Safely evaluate mathematical expressions.""" 17 allowed_chars = set("0123456789+-*/(). ") 18 if any(c not in allowed_chars for c in expression): 19 return {"error": f"Invalid characters in expression: {expression}"} 20 result = eval(expression) 21 return {"expression": expression, "result": result} 22 23 TOOL_SCHEMAS = [ 24 { 25 "name": "get_weather", 26 "description": "Get current weather for a city. Returns temperature, humidity, and conditions.", 27 "input_schema": { 28 "type": "object", 29 "properties": { 30 "location": {"type": "string", "description": "City name (e.g., 'London', 'Tokyo')"}, 31 "unit": {"type": "string", "enum": ["celsius", "fahrenheit"], "description": "Temperature unit"} 32 }, 33 "required": ["location"] 34 } 35 }, 36 { 37 "name": "calculate", 38 "description": "Evaluate a mathematical expression with +, -, *, /, (, ). No variables.", 39 "input_schema": { 40 "type": "object", 41 "properties": {"expression": {"type": "string", "description": "Math expression like '25 * 4 + 10'"}}, 42 "required": ["expression"] 43 } 44 } 45 ] 46 47 TOOL_FNS = {"get_weather": get_weather, "calculate": calculate} 48
Two well-documented tools that run correctly when called directly
Sign in to share your feedback and join the discussion.