pub struct Client { /* private fields */ }Implementations§
Source§impl Client
impl Client
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
examples/basic_usage.rs (line 12)
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 .version("2023-06-01")
14 // Set verbose to true if you need return the response as it is from Anthropic
15 // .verbose(true)
16 .auth(secret_key.as_str())
17 .model("claude-3-opus-20240229")
18 .messages(&json!([
19 {"role": "user", "content": "Write me a poem about bravery"}
20 ]))
21 .max_tokens(1024)
22 .build()?;
23
24 if let Err(error) = request
25 .execute(|text| async move { println!("{text}") })
26 .await
27 {
28 eprintln!("Error: {error}");
29 }
30
31 Ok(())
32}More examples
examples/tool_use_usage.rs (line 12)
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}examples/streaming_usage.rs (line 13)
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10 dotenv().ok();
11 let secret_key = std::env::var("ANTHROPIC_API_KEY").unwrap_or_default();
12
13 let request = Client::new()
14 .auth(secret_key.as_str())
15 .model("claude-3-opus-20240229")
16 .messages(&json!([
17 {"role": "user", "content": "Write me a poem about bravery"}
18 ]))
19 .system("Make it sound like Edgar Allan Poe")
20 .temperature(0.1)
21 .max_tokens(1024)
22 .stream(true)
23 .build()?;
24
25 let message = Arc::new(Mutex::new(String::new()));
26 let message_clone = message.clone();
27
28 // NOTE: you should spawn a new thread if you need to
29 if let Err(error) = request
30 .execute(move |text| {
31 let message_clone = message_clone.clone();
32 async move {
33 println!("{text}");
34
35 {
36 let mut message = message_clone.lock().unwrap();
37 *message = format!("{}{}", *message, text);
38 drop(message);
39 }
40 // Mimic async process
41 // tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
42 }
43 })
44 .await
45 {
46 eprintln!("Error: {error}");
47 }
48
49 let final_message = message.lock().unwrap();
50 println!("Message: {}", *final_message); // or get the whole message at the end
51
52 Ok(())
53}Sourcepub fn auth(self, secret_key: &str) -> Self
pub fn auth(self, secret_key: &str) -> Self
Examples found in repository?
examples/basic_usage.rs (line 16)
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 .version("2023-06-01")
14 // Set verbose to true if you need return the response as it is from Anthropic
15 // .verbose(true)
16 .auth(secret_key.as_str())
17 .model("claude-3-opus-20240229")
18 .messages(&json!([
19 {"role": "user", "content": "Write me a poem about bravery"}
20 ]))
21 .max_tokens(1024)
22 .build()?;
23
24 if let Err(error) = request
25 .execute(|text| async move { println!("{text}") })
26 .await
27 {
28 eprintln!("Error: {error}");
29 }
30
31 Ok(())
32}More examples
examples/tool_use_usage.rs (line 13)
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}examples/streaming_usage.rs (line 14)
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10 dotenv().ok();
11 let secret_key = std::env::var("ANTHROPIC_API_KEY").unwrap_or_default();
12
13 let request = Client::new()
14 .auth(secret_key.as_str())
15 .model("claude-3-opus-20240229")
16 .messages(&json!([
17 {"role": "user", "content": "Write me a poem about bravery"}
18 ]))
19 .system("Make it sound like Edgar Allan Poe")
20 .temperature(0.1)
21 .max_tokens(1024)
22 .stream(true)
23 .build()?;
24
25 let message = Arc::new(Mutex::new(String::new()));
26 let message_clone = message.clone();
27
28 // NOTE: you should spawn a new thread if you need to
29 if let Err(error) = request
30 .execute(move |text| {
31 let message_clone = message_clone.clone();
32 async move {
33 println!("{text}");
34
35 {
36 let mut message = message_clone.lock().unwrap();
37 *message = format!("{}{}", *message, text);
38 drop(message);
39 }
40 // Mimic async process
41 // tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
42 }
43 })
44 .await
45 {
46 eprintln!("Error: {error}");
47 }
48
49 let final_message = message.lock().unwrap();
50 println!("Message: {}", *final_message); // or get the whole message at the end
51
52 Ok(())
53}Sourcepub fn model(self, model: &str) -> Self
pub fn model(self, model: &str) -> Self
Examples found in repository?
examples/basic_usage.rs (line 17)
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 .version("2023-06-01")
14 // Set verbose to true if you need return the response as it is from Anthropic
15 // .verbose(true)
16 .auth(secret_key.as_str())
17 .model("claude-3-opus-20240229")
18 .messages(&json!([
19 {"role": "user", "content": "Write me a poem about bravery"}
20 ]))
21 .max_tokens(1024)
22 .build()?;
23
24 if let Err(error) = request
25 .execute(|text| async move { println!("{text}") })
26 .await
27 {
28 eprintln!("Error: {error}");
29 }
30
31 Ok(())
32}More examples
examples/tool_use_usage.rs (line 14)
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}examples/streaming_usage.rs (line 15)
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10 dotenv().ok();
11 let secret_key = std::env::var("ANTHROPIC_API_KEY").unwrap_or_default();
12
13 let request = Client::new()
14 .auth(secret_key.as_str())
15 .model("claude-3-opus-20240229")
16 .messages(&json!([
17 {"role": "user", "content": "Write me a poem about bravery"}
18 ]))
19 .system("Make it sound like Edgar Allan Poe")
20 .temperature(0.1)
21 .max_tokens(1024)
22 .stream(true)
23 .build()?;
24
25 let message = Arc::new(Mutex::new(String::new()));
26 let message_clone = message.clone();
27
28 // NOTE: you should spawn a new thread if you need to
29 if let Err(error) = request
30 .execute(move |text| {
31 let message_clone = message_clone.clone();
32 async move {
33 println!("{text}");
34
35 {
36 let mut message = message_clone.lock().unwrap();
37 *message = format!("{}{}", *message, text);
38 drop(message);
39 }
40 // Mimic async process
41 // tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
42 }
43 })
44 .await
45 {
46 eprintln!("Error: {error}");
47 }
48
49 let final_message = message.lock().unwrap();
50 println!("Message: {}", *final_message); // or get the whole message at the end
51
52 Ok(())
53}Sourcepub fn messages(self, messages: &Value) -> Self
pub fn messages(self, messages: &Value) -> Self
Examples found in repository?
examples/basic_usage.rs (lines 18-20)
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 .version("2023-06-01")
14 // Set verbose to true if you need return the response as it is from Anthropic
15 // .verbose(true)
16 .auth(secret_key.as_str())
17 .model("claude-3-opus-20240229")
18 .messages(&json!([
19 {"role": "user", "content": "Write me a poem about bravery"}
20 ]))
21 .max_tokens(1024)
22 .build()?;
23
24 if let Err(error) = request
25 .execute(|text| async move { println!("{text}") })
26 .await
27 {
28 eprintln!("Error: {error}");
29 }
30
31 Ok(())
32}More examples
examples/tool_use_usage.rs (lines 33-38)
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}examples/streaming_usage.rs (lines 16-18)
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10 dotenv().ok();
11 let secret_key = std::env::var("ANTHROPIC_API_KEY").unwrap_or_default();
12
13 let request = Client::new()
14 .auth(secret_key.as_str())
15 .model("claude-3-opus-20240229")
16 .messages(&json!([
17 {"role": "user", "content": "Write me a poem about bravery"}
18 ]))
19 .system("Make it sound like Edgar Allan Poe")
20 .temperature(0.1)
21 .max_tokens(1024)
22 .stream(true)
23 .build()?;
24
25 let message = Arc::new(Mutex::new(String::new()));
26 let message_clone = message.clone();
27
28 // NOTE: you should spawn a new thread if you need to
29 if let Err(error) = request
30 .execute(move |text| {
31 let message_clone = message_clone.clone();
32 async move {
33 println!("{text}");
34
35 {
36 let mut message = message_clone.lock().unwrap();
37 *message = format!("{}{}", *message, text);
38 drop(message);
39 }
40 // Mimic async process
41 // tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
42 }
43 })
44 .await
45 {
46 eprintln!("Error: {error}");
47 }
48
49 let final_message = message.lock().unwrap();
50 println!("Message: {}", *final_message); // or get the whole message at the end
51
52 Ok(())
53}Sourcepub fn tools(self, tools: &Value) -> Self
pub fn tools(self, tools: &Value) -> Self
Examples found in repository?
examples/tool_use_usage.rs (lines 16-31)
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}Sourcepub fn tool_choice(self, tool_choice: ToolChoice) -> Self
pub fn tool_choice(self, tool_choice: ToolChoice) -> Self
Examples found in repository?
examples/tool_use_usage.rs (line 32)
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}pub fn metadata(self, metadata: &Value) -> Self
Sourcepub fn max_tokens(self, max_tokens: i32) -> Self
pub fn max_tokens(self, max_tokens: i32) -> Self
Examples found in repository?
examples/basic_usage.rs (line 21)
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 .version("2023-06-01")
14 // Set verbose to true if you need return the response as it is from Anthropic
15 // .verbose(true)
16 .auth(secret_key.as_str())
17 .model("claude-3-opus-20240229")
18 .messages(&json!([
19 {"role": "user", "content": "Write me a poem about bravery"}
20 ]))
21 .max_tokens(1024)
22 .build()?;
23
24 if let Err(error) = request
25 .execute(|text| async move { println!("{text}") })
26 .await
27 {
28 eprintln!("Error: {error}");
29 }
30
31 Ok(())
32}More examples
examples/tool_use_usage.rs (line 39)
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}examples/streaming_usage.rs (line 21)
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10 dotenv().ok();
11 let secret_key = std::env::var("ANTHROPIC_API_KEY").unwrap_or_default();
12
13 let request = Client::new()
14 .auth(secret_key.as_str())
15 .model("claude-3-opus-20240229")
16 .messages(&json!([
17 {"role": "user", "content": "Write me a poem about bravery"}
18 ]))
19 .system("Make it sound like Edgar Allan Poe")
20 .temperature(0.1)
21 .max_tokens(1024)
22 .stream(true)
23 .build()?;
24
25 let message = Arc::new(Mutex::new(String::new()));
26 let message_clone = message.clone();
27
28 // NOTE: you should spawn a new thread if you need to
29 if let Err(error) = request
30 .execute(move |text| {
31 let message_clone = message_clone.clone();
32 async move {
33 println!("{text}");
34
35 {
36 let mut message = message_clone.lock().unwrap();
37 *message = format!("{}{}", *message, text);
38 drop(message);
39 }
40 // Mimic async process
41 // tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
42 }
43 })
44 .await
45 {
46 eprintln!("Error: {error}");
47 }
48
49 let final_message = message.lock().unwrap();
50 println!("Message: {}", *final_message); // or get the whole message at the end
51
52 Ok(())
53}Sourcepub fn temperature(self, temperature: f32) -> Self
pub fn temperature(self, temperature: f32) -> Self
Examples found in repository?
examples/streaming_usage.rs (line 20)
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10 dotenv().ok();
11 let secret_key = std::env::var("ANTHROPIC_API_KEY").unwrap_or_default();
12
13 let request = Client::new()
14 .auth(secret_key.as_str())
15 .model("claude-3-opus-20240229")
16 .messages(&json!([
17 {"role": "user", "content": "Write me a poem about bravery"}
18 ]))
19 .system("Make it sound like Edgar Allan Poe")
20 .temperature(0.1)
21 .max_tokens(1024)
22 .stream(true)
23 .build()?;
24
25 let message = Arc::new(Mutex::new(String::new()));
26 let message_clone = message.clone();
27
28 // NOTE: you should spawn a new thread if you need to
29 if let Err(error) = request
30 .execute(move |text| {
31 let message_clone = message_clone.clone();
32 async move {
33 println!("{text}");
34
35 {
36 let mut message = message_clone.lock().unwrap();
37 *message = format!("{}{}", *message, text);
38 drop(message);
39 }
40 // Mimic async process
41 // tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
42 }
43 })
44 .await
45 {
46 eprintln!("Error: {error}");
47 }
48
49 let final_message = message.lock().unwrap();
50 println!("Message: {}", *final_message); // or get the whole message at the end
51
52 Ok(())
53}Sourcepub fn system(self, system: &str) -> Self
pub fn system(self, system: &str) -> Self
Examples found in repository?
examples/streaming_usage.rs (line 19)
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10 dotenv().ok();
11 let secret_key = std::env::var("ANTHROPIC_API_KEY").unwrap_or_default();
12
13 let request = Client::new()
14 .auth(secret_key.as_str())
15 .model("claude-3-opus-20240229")
16 .messages(&json!([
17 {"role": "user", "content": "Write me a poem about bravery"}
18 ]))
19 .system("Make it sound like Edgar Allan Poe")
20 .temperature(0.1)
21 .max_tokens(1024)
22 .stream(true)
23 .build()?;
24
25 let message = Arc::new(Mutex::new(String::new()));
26 let message_clone = message.clone();
27
28 // NOTE: you should spawn a new thread if you need to
29 if let Err(error) = request
30 .execute(move |text| {
31 let message_clone = message_clone.clone();
32 async move {
33 println!("{text}");
34
35 {
36 let mut message = message_clone.lock().unwrap();
37 *message = format!("{}{}", *message, text);
38 drop(message);
39 }
40 // Mimic async process
41 // tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
42 }
43 })
44 .await
45 {
46 eprintln!("Error: {error}");
47 }
48
49 let final_message = message.lock().unwrap();
50 println!("Message: {}", *final_message); // or get the whole message at the end
51
52 Ok(())
53}Sourcepub fn version(self, version: &str) -> Self
pub fn version(self, version: &str) -> Self
Examples found in repository?
examples/basic_usage.rs (line 13)
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 .version("2023-06-01")
14 // Set verbose to true if you need return the response as it is from Anthropic
15 // .verbose(true)
16 .auth(secret_key.as_str())
17 .model("claude-3-opus-20240229")
18 .messages(&json!([
19 {"role": "user", "content": "Write me a poem about bravery"}
20 ]))
21 .max_tokens(1024)
22 .build()?;
23
24 if let Err(error) = request
25 .execute(|text| async move { println!("{text}") })
26 .await
27 {
28 eprintln!("Error: {error}");
29 }
30
31 Ok(())
32}Sourcepub fn stream(self, stream: bool) -> Self
pub fn stream(self, stream: bool) -> Self
Examples found in repository?
examples/streaming_usage.rs (line 22)
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10 dotenv().ok();
11 let secret_key = std::env::var("ANTHROPIC_API_KEY").unwrap_or_default();
12
13 let request = Client::new()
14 .auth(secret_key.as_str())
15 .model("claude-3-opus-20240229")
16 .messages(&json!([
17 {"role": "user", "content": "Write me a poem about bravery"}
18 ]))
19 .system("Make it sound like Edgar Allan Poe")
20 .temperature(0.1)
21 .max_tokens(1024)
22 .stream(true)
23 .build()?;
24
25 let message = Arc::new(Mutex::new(String::new()));
26 let message_clone = message.clone();
27
28 // NOTE: you should spawn a new thread if you need to
29 if let Err(error) = request
30 .execute(move |text| {
31 let message_clone = message_clone.clone();
32 async move {
33 println!("{text}");
34
35 {
36 let mut message = message_clone.lock().unwrap();
37 *message = format!("{}{}", *message, text);
38 drop(message);
39 }
40 // Mimic async process
41 // tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
42 }
43 })
44 .await
45 {
46 eprintln!("Error: {error}");
47 }
48
49 let final_message = message.lock().unwrap();
50 println!("Message: {}", *final_message); // or get the whole message at the end
51
52 Ok(())
53}pub fn verbose(self, verbose: bool) -> Self
Sourcepub fn beta(self, beta: &str) -> Self
pub fn beta(self, beta: &str) -> Self
Examples found in repository?
examples/tool_use_usage.rs (line 15)
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}pub fn stop_sequences(self, stop_sequences: Vec<String>) -> Self
pub fn top_k(self, top_k: i32) -> Self
pub fn top_p(self, top_p: f64) -> Self
Sourcepub fn build(self) -> Result<Request, ReqwestError>
pub fn build(self) -> Result<Request, ReqwestError>
Examples found in repository?
examples/basic_usage.rs (line 22)
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 .version("2023-06-01")
14 // Set verbose to true if you need return the response as it is from Anthropic
15 // .verbose(true)
16 .auth(secret_key.as_str())
17 .model("claude-3-opus-20240229")
18 .messages(&json!([
19 {"role": "user", "content": "Write me a poem about bravery"}
20 ]))
21 .max_tokens(1024)
22 .build()?;
23
24 if let Err(error) = request
25 .execute(|text| async move { println!("{text}") })
26 .await
27 {
28 eprintln!("Error: {error}");
29 }
30
31 Ok(())
32}More examples
examples/tool_use_usage.rs (line 40)
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}examples/streaming_usage.rs (line 23)
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10 dotenv().ok();
11 let secret_key = std::env::var("ANTHROPIC_API_KEY").unwrap_or_default();
12
13 let request = Client::new()
14 .auth(secret_key.as_str())
15 .model("claude-3-opus-20240229")
16 .messages(&json!([
17 {"role": "user", "content": "Write me a poem about bravery"}
18 ]))
19 .system("Make it sound like Edgar Allan Poe")
20 .temperature(0.1)
21 .max_tokens(1024)
22 .stream(true)
23 .build()?;
24
25 let message = Arc::new(Mutex::new(String::new()));
26 let message_clone = message.clone();
27
28 // NOTE: you should spawn a new thread if you need to
29 if let Err(error) = request
30 .execute(move |text| {
31 let message_clone = message_clone.clone();
32 async move {
33 println!("{text}");
34
35 {
36 let mut message = message_clone.lock().unwrap();
37 *message = format!("{}{}", *message, text);
38 drop(message);
39 }
40 // Mimic async process
41 // tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
42 }
43 })
44 .await
45 {
46 eprintln!("Error: {error}");
47 }
48
49 let final_message = message.lock().unwrap();
50 println!("Message: {}", *final_message); // or get the whole message at the end
51
52 Ok(())
53}pub fn builder(self) -> Result<RequestBuilder, ReqwestError>
Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more