Struct MessagesResource

Source
pub struct MessagesResource<'a> { /* private fields */ }
Expand description

Messages API resource for interacting with Claude

Implementations§

Source§

impl<'a> MessagesResource<'a>

Source

pub fn new(client: &'a Anthropic) -> Self

Create a new Messages resource

Source

pub async fn create(&self, params: MessageCreateParams) -> Result<Message>

Create a message with Claude

Send a structured list of input messages with text and/or image content, and Claude will generate the next message in the conversation.

§Example
use anthropic_sdk::{Anthropic, types::MessageCreateBuilder};
 
let client = Anthropic::from_env()?;
 
let message = client.messages().create(
    MessageCreateBuilder::new("claude-3-5-sonnet-latest", 1024)
        .user("Hello, Claude!")
        .build()
).await?;
 
println!("Claude responded: {:?}", message.content);
Source

pub async fn create_stream( &self, params: MessageCreateParams, ) -> Result<MessageStream>

Create a streaming message with Claude

Send a message request and receive a real-time stream of the response. This allows you to process Claude’s response as it’s being generated.

§Example
use anthropic_sdk::{Anthropic, MessageCreateBuilder};
use futures::StreamExt;
 
let client = Anthropic::from_env()?;
 
let stream = client.messages().create_stream(
    MessageCreateBuilder::new("claude-3-5-sonnet-latest", 1024)
        .user("Write a story about AI")
        .stream(true)
        .build()
).await?;
 
// Option 1: Use callbacks
let final_message = stream
    .on_text(|delta, _| print!("{}", delta))
    .on_error(|error| eprintln!("Error: {}", error))
    .final_message().await?;
 
// Option 2: Manual iteration
while let Some(event) = stream.next().await {
    // Process each event as needed
}
Source

pub async fn stream(&self, params: MessageCreateParams) -> Result<MessageStream>

Create a streaming message using the builder pattern

This is a convenience method that provides an ergonomic API for creating streaming messages.

§Example
use anthropic_sdk::Anthropic;
 
let client = Anthropic::from_env()?;
 
let final_message = client.messages()
    .create_with_builder("claude-3-5-sonnet-latest", 1024)
    .user("Write a poem about the ocean")
    .system("You are a creative poet.")
    .temperature(0.8)
    .stream()
    .await?
    .on_text(|delta, _| print!("{}", delta))
    .final_message()
    .await?;
Source

pub fn create_with_builder( &'a self, model: impl Into<String>, max_tokens: u32, ) -> MessageCreateBuilderWithClient<'a>

Create a message using the builder pattern

This is a convenience method that provides an ergonomic API for creating messages.

§Example
use anthropic_sdk::Anthropic;
 
let client = Anthropic::from_env()?;
 
let message = client.messages()
    .create_with_builder("claude-3-5-sonnet-latest", 1024)
    .user("What is the capital of France?")
    .system("You are a helpful geography assistant.")
    .temperature(0.3)
    .send()
    .await?;
 
println!("Response: {:?}", message.content);

Auto Trait Implementations§

§

impl<'a> Freeze for MessagesResource<'a>

§

impl<'a> !RefUnwindSafe for MessagesResource<'a>

§

impl<'a> Send for MessagesResource<'a>

§

impl<'a> Sync for MessagesResource<'a>

§

impl<'a> Unpin for MessagesResource<'a>

§

impl<'a> !UnwindSafe for MessagesResource<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,