pub struct Content {
pub type_name: String,
pub text: Option<String>,
pub image_url: Option<String>,
}
Expand description
Represents different types of content that can be included in a message.
Content can be either text or images, supporting multi-modal interactions with OpenAI models. Images can be provided as URLs or loaded from local files and are automatically encoded as base64 data URLs.
§Supported Image Formats
- PNG
- JPEG/JPG
- GIF
§Fields
type_name
- The type of content (“input_text” or “input_image”)text
- Optional text contentimage_url
- Optional image URL or base64 data URL
§Examples
use openai_tools::common::message::Content;
// Create text content
let text_content = Content::from_text("Hello, world!");
// Create image content from URL
let image_content = Content::from_image_url("https://example.com/image.png");
// Create image content from local file
let file_content = Content::from_image_file("path/to/image.png");
Fields§
§type_name: String
The type of content (“input_text” or “input_image”)
text: Option<String>
Optional text content
image_url: Option<String>
Optional image URL or base64 data URL
Implementations§
Source§impl Content
impl Content
Sourcepub fn from_text<T: AsRef<str>>(text: T) -> Self
pub fn from_text<T: AsRef<str>>(text: T) -> Self
Creates a new Content instance with text content.
§Arguments
text
- The text content to include
§Returns
A new Content instance with type “input_text”
§Examples
use openai_tools::common::message::Content;
let content = Content::from_text("Hello, world!");
assert_eq!(content.type_name, "input_text");
Sourcepub fn from_image_url<T: AsRef<str>>(image_url: T) -> Self
pub fn from_image_url<T: AsRef<str>>(image_url: T) -> Self
Creates a new Content instance with an image URL.
§Arguments
image_url
- The URL of the image
§Returns
A new Content instance with type “input_image”
§Examples
use openai_tools::common::message::Content;
let content = Content::from_image_url("https://example.com/image.png");
assert_eq!(content.type_name, "input_image");
Sourcepub fn from_image_file<T: AsRef<str>>(file_path: T) -> Self
pub fn from_image_file<T: AsRef<str>>(file_path: T) -> Self
Creates a new Content instance from a local image file.
This method reads an image file from the filesystem, encodes it as base64, and creates a data URL suitable for use with OpenAI APIs.
§Arguments
file_path
- Path to the image file
§Returns
A new Content instance with type “input_image” and base64-encoded image data
§Panics
Panics if:
- The file cannot be opened
- The image cannot be decoded
- The image format is unsupported
- The image cannot be encoded to the buffer
§Supported Formats
- PNG
- JPEG/JPG
- GIF
§Examples
use openai_tools::common::message::Content;
let content = Content::from_image_file("path/to/image.png");
assert_eq!(content.type_name, "input_image");
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Content
impl<'de> Deserialize<'de> for Content
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 Content
impl RefUnwindSafe for Content
impl Send for Content
impl Sync for Content
impl Unpin for Content
impl UnwindSafe for Content
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()
.