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
impl Message
Sourcepub const fn attachments(&self) -> &[Url]
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.
Sourcepub const fn annotations(&self) -> &[Annotation]
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
impl Message
Sourcepub const fn new(role: Role, content: String) -> Self
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 sendercontent
- The text content of the message
Sourcepub fn with_attachment<U: TryInto<Url, Error: Debug>>(self, url: U) -> Self
pub fn with_attachment<U: TryInto<Url, Error: Debug>>(self, url: U) -> Self
Sourcepub fn with_attachments<U: TryInto<Url, Error: Debug>>(
self,
urls: impl IntoIterator<Item = U>,
) -> Self
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);
Sourcepub fn with_annotation(self, annotation: Annotation) -> Self
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));
Sourcepub fn with_annotations(
self,
annotations: impl IntoIterator<Item = Annotation>,
) -> Self
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§
Auto Trait Implementations§
impl Freeze for Message
impl RefUnwindSafe for Message
impl Send for Message
impl Sync for Message
impl Unpin for Message
impl UnwindSafe for Message
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