pub struct MessageContent {
pub type_name: String,
pub text: Option<String>,
pub image_url: Option<String>,
}
Expand description
Represents the content of a message, which can be either text or an image.
This structure is used to encapsulate different types of message content that can be sent to or received from AI models. It supports both text-based content and image content (either as URLs or base64-encoded data).
§Fields
type_name
- The type of content (“input_text” for text, “input_image” for images)text
- Optional text content when the message contains textimage_url
- Optional image URL or base64-encoded image data when the message contains an image
§Example
use openai_tools::common::MessageContent;
// Create text content
let text_content = MessageContent::from_text("Hello, world!".to_string());
// Create image content from URL
// let image_content = MessageContent::from_image_url("https://example.com/image.jpg".to_string());
// Create image content from file
// let file_content = MessageContent::from_image_file("path/to/image.jpg".to_string());
Fields§
§type_name: String
§text: Option<String>
§image_url: Option<String>
Implementations§
Source§impl MessageContent
impl MessageContent
Sourcepub fn from_text(text: String) -> Self
pub fn from_text(text: String) -> Self
Creates a new MessageContent
containing text.
This constructor creates a message content instance specifically for text-based messages. The content type is automatically set to “input_text” and the image_url field is set to None.
§Arguments
text
- The text content to include in the message
§Returns
A new MessageContent
instance configured for text content.
§Example
use openai_tools::common::MessageContent;
let content = MessageContent::from_text("Hello, AI assistant!".to_string());
assert_eq!(content.type_name, "input_text");
assert_eq!(content.text, Some("Hello, AI assistant!".to_string()));
assert_eq!(content.image_url, None);
Sourcepub fn from_image_url(image_url: String) -> Self
pub fn from_image_url(image_url: String) -> Self
Creates a new MessageContent
containing an image from a URL.
This constructor creates a message content instance for image-based messages using an existing image URL. The content type is automatically set to “input_image” and the text field is set to None.
§Arguments
image_url
- The URL or base64-encoded data URI of the image
§Returns
A new MessageContent
instance configured for image content.
§Example
use openai_tools::common::MessageContent;
let content = MessageContent::from_image_url("https://example.com/image.jpg".to_string());
assert_eq!(content.type_name, "input_image");
assert_eq!(content.text, None);
assert_eq!(content.image_url, Some("https://example.com/image.jpg".to_string()));
Sourcepub fn from_image_file(file_path: String) -> Self
pub fn from_image_file(file_path: String) -> Self
Creates a new MessageContent
containing an image loaded from a file.
This constructor reads an image file from the local filesystem, encodes it as base64, and creates a data URI suitable for use with AI models. The content type is automatically set to “input_image” and the text field is set to None.
§Arguments
file_path
- The path to the image file to load
§Returns
A new MessageContent
instance configured for image content with base64-encoded data.
§Supported Formats
- PNG (.png)
- JPEG (.jpg, .jpeg)
- GIF (.gif)
Trait Implementations§
Source§impl Clone for MessageContent
impl Clone for MessageContent
Source§fn clone(&self) -> MessageContent
fn clone(&self) -> MessageContent
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for MessageContent
impl Debug for MessageContent
Source§impl Default for MessageContent
impl Default for MessageContent
Source§fn default() -> MessageContent
fn default() -> MessageContent
Source§impl<'de> Deserialize<'de> for MessageContent
impl<'de> Deserialize<'de> for MessageContent
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for MessageContent
impl RefUnwindSafe for MessageContent
impl Send for MessageContent
impl Sync for MessageContent
impl Unpin for MessageContent
impl UnwindSafe for MessageContent
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian()
.