1use anthropic_api::{messages::*, Credentials};
24use serde_json::json;
25
26#[tokio::main]
27async fn main() {
28 let credentials = Credentials::from_env();
29
30 let calculator_tool = Tool {
32 name: "calculator".to_string(),
33 description: "A calculator that can perform basic arithmetic operations".to_string(),
34 input_schema: json!({
35 "type": "object",
36 "properties": {
37 "operation": {
38 "type": "string",
39 "enum": ["add", "subtract", "multiply", "divide"]
40 },
41 "operands": {
42 "type": "array",
43 "items": {"type": "number"},
44 "minItems": 2,
45 "maxItems": 2
46 }
47 },
48 "required": ["operation", "operands"]
49 }),
50 };
51
52 let content =
53 "You are a helpful AI assistant. Please calculate 15 + 27 using the calculator tool.";
54 let mut messages = vec![Message {
55 role: MessageRole::User,
56 content: MessageContent::Text(content.to_string()),
57 }];
58
59 println!("Claude: {}", content);
60
61 let response = MessagesResponse::builder("claude-3-7-sonnet-20250219", messages.clone(), 1024)
63 .credentials(credentials.clone())
64 .tools(vec![calculator_tool.clone()])
65 .tool_choice(ToolChoice::Any)
66 .create()
67 .await
68 .unwrap();
69
70 for content in response.content {
72 match content {
73 ResponseContentBlock::Text { text } => {
74 println!("Assistant: {}", text.trim());
75 messages.push(Message {
76 role: MessageRole::Assistant,
77 content: MessageContent::Text(text),
78 });
79 }
80 ResponseContentBlock::ToolUse { name, input, .. } => {
81 println!("Claude decided to use the tool: {}: {}", name, input);
82 }
83 ResponseContentBlock::Thinking {
84 signature,
85 thinking,
86 } => {
87 println!("Claude {} is thinking: {}", signature, thinking);
88 }
89 ResponseContentBlock::RedactedThinking { data } => {
90 println!("Claude is thinking: {}", data);
91 }
92 }
93 }
94}