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: StringThe 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");