pub struct Request { /* private fields */ }Implementations§
Source§impl Request
impl Request
Sourcepub async fn execute<F, Fut>(self, callback: F) -> Result<()>
pub async fn execute<F, Fut>(self, callback: F) -> Result<()>
Examples found in repository?
examples/basic_usage.rs (line 25)
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 43)
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 30-43)
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}Auto Trait Implementations§
impl !Freeze for Request
impl !RefUnwindSafe for Request
impl Send for Request
impl Sync for Request
impl Unpin for Request
impl !UnwindSafe for Request
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