Struct Message

Source
pub struct Message { /* private fields */ }
Expand description

A message in a conversation.

Contains a Role, text content, and optional attachments and annotations. Messages form the building blocks of conversations with AI language models.

§Example

use ai_types::llm::{Message, Role};
use url::Url;

// Create messages using convenience constructors
let user_msg = Message::user("Hello, how are you?");
let system_msg = Message::system("You are a helpful assistant.");
let custom_msg = Message::new(Role::Assistant, "I'm doing well!".to_string());

// Add attachments to a message
let msg_with_attachment = Message::user("Check out this image")
    .with_attachment("https://example.com/image.jpg");

Implementations§

Source§

impl Message

Source

pub const fn role(&self) -> Role

Returns the message sender role.

Source

pub const fn content(&self) -> &str

Returns the text content of the message.

Source

pub const fn attachments(&self) -> &[Url]

Returns the attachment URLs associated with the message. URLs to external resources like images, documents, or other media that are referenced by this message.

Source

pub const fn annotations(&self) -> &[Annotation]

Returns Message annotations. See Annotation for details.

Metadata annotations for URLs mentioned in the message content, providing additional context like titles and descriptions.

Source§

impl Message

Source

pub const fn new(role: Role, content: String) -> Self

Creates a new message with the specified role and content.

§Arguments
  • role - The role of the message sender
  • content - The text content of the message
Source

pub fn user(content: impl Into<String>) -> Self

Creates a new user message.

§Arguments
  • content - The text content of the message
Source

pub fn assistant(content: impl Into<String>) -> Self

Creates a new assistant message.

§Arguments
  • content - The text content of the message
Source

pub fn system(content: impl Into<String>) -> Self

Creates a new system message.

§Arguments
  • content - The text content of the message
Source

pub fn tool(content: impl Into<String>) -> Self

Creates a new tool message.

§Arguments
  • content - The text content of the message
Source

pub fn with_attachment<U: TryInto<Url, Error: Debug>>(self, url: U) -> Self

Adds an attachment URL to the message.

§Arguments
  • url - The URL to attach
§Panics

Panics if the URL conversion fails.

Source

pub fn with_attachments<U: TryInto<Url, Error: Debug>>( self, urls: impl IntoIterator<Item = U>, ) -> Self

Adds multiple attachment URLs to the message.

§Arguments
  • urls - An iterable of URLs to attach
§Panics

Panics if any URL conversion fails.

§Example
use ai_types::llm::Message;
use url::Url;

let urls = [
    "https://example.com",
    "https://example.org",
];
let message = Message::user("Check these links").with_attachments(urls);
Source

pub fn with_annotation(self, annotation: Annotation) -> Self

Adds an annotation to the message.

§Arguments
  • annotation - The annotation to add
§Example
use ai_types::llm::{Message, Annotation, UrlAnnotation};
use url::Url;

let url_annotation = UrlAnnotation::new(
    "https://example.com",
    "Example Site",
    "An example website",
    0,
    10,
);


let message = Message::user("Visit https://example.com")
    .with_annotation(Annotation::Url(url_annotation));
Source

pub fn with_annotations( self, annotations: impl IntoIterator<Item = Annotation>, ) -> Self

Adds multiple annotations to the message.

§Arguments
  • annotations - An iterable of annotations to add

Trait Implementations§

Source§

impl Clone for Message

Source§

fn clone(&self) -> Message

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Message

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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> ErasedDestructor for T
where T: 'static,