tool_use_usage/
tool_use_usage.rs

1// examples/tool_use_usage.rs
2
3use anthropic_sdk::{Client, ToolChoice};
4use dotenv::dotenv;
5use serde_json::json;
6
7#[tokio::main]
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    dotenv().ok();
10    let secret_key = std::env::var("ANTHROPIC_API_KEY").unwrap_or_default();
11
12    let request = Client::new()
13        .auth(secret_key.as_str())
14        .model("claude-3-opus-20240229")
15        .beta("tools-2024-04-04") // add the beta header
16        .tools(&json!([
17          {
18            "name": "get_weather",
19            "description": "Get the current weather in a given location",
20            "input_schema": {
21              "type": "object",
22              "properties": {
23                "location": {
24                  "type": "string",
25                  "description": "The city and state, e.g. San Francisco, CA"
26                }
27              },
28              "required": ["location"]
29            }
30          }
31        ]))
32        .tool_choice(ToolChoice::Auto)
33        .messages(&json!([
34          {
35            "role": "user",
36            "content": "What is the weather like in San Francisco?"
37          }
38        ]))
39        .max_tokens(1024)
40        .build()?;
41
42    if let Err(error) = request
43        .execute(|text| async move { println!("{text}") })
44        .await
45    {
46        eprintln!("Error: {error}");
47    }
48
49    Ok(())
50}