pub struct Message {
pub role: Role,
pub content: Option<Content>,
pub content_list: Option<Vec<Content>>,
pub tool_calls: Option<Vec<ToolCall>>,
pub refusal: Option<String>,
pub annotations: Option<Vec<String>>,
}
Expand description
Represents a message in an OpenAI conversation.
Messages are the core communication unit between users and OpenAI models. They can contain various types of content including text, images, tool calls, and metadata like refusals and annotations.
§Content Types
A message can contain either:
- Single content (
content
field) - for simple text messages - Multiple content items (
content_list
field) - for multi-modal messages
§Fields
role
- The role of the message sender (User, Assistant, System, etc.)content
- Optional single content itemcontent_list
- Optional list of content items for multi-modal messagestool_calls
- Optional list of tool calls made by the assistantrefusal
- Optional refusal message if the model declined to respondannotations
- Optional list of annotations or metadata
§Examples
use openai_tools::common::message::{Message, Content};
use openai_tools::common::role::Role;
// Simple text message
let message = Message::from_string(Role::User, "Hello!");
// Multi-modal message with text and image
let contents = vec![
Content::from_text("What's in this image?"),
Content::from_image_url("https://example.com/image.png"),
];
let message = Message::from_message_array(Role::User, contents);
Fields§
§role: Role
The role of the message sender
content: Option<Content>
Optional single content item
content_list: Option<Vec<Content>>
Optional list of content items for multi-modal messages
tool_calls: Option<Vec<ToolCall>>
Optional list of tool calls made by the assistant
refusal: Option<String>
Optional refusal message if the model declined to respond
annotations: Option<Vec<String>>
Optional list of annotations or metadata
Implementations§
Source§impl Message
impl Message
Sourcepub fn from_string<T: AsRef<str>>(role: Role, message: T) -> Self
pub fn from_string<T: AsRef<str>>(role: Role, message: T) -> Self
Creates a new Message with a single text content.
This is a convenience method for creating simple text messages.
§Arguments
role
- The role of the message sendermessage
- The text content of the message
§Returns
A new Message instance with the specified role and text content
§Examples
use openai_tools::common::message::Message;
use openai_tools::common::role::Role;
let message = Message::from_string(Role::User, "Hello, how are you?");
Sourcepub fn from_message_array(role: Role, contents: Vec<Content>) -> Self
pub fn from_message_array(role: Role, contents: Vec<Content>) -> Self
Creates a new Message with multiple content items.
This method is used for multi-modal messages that contain multiple types of content such as text and images.
§Arguments
role
- The role of the message sendercontents
- Vector of content items to include in the message
§Returns
A new Message instance with the specified role and content list
§Examples
use openai_tools::common::message::{Message, Content};
use openai_tools::common::role::Role;
let contents = vec![
Content::from_text("What's in this image?"),
Content::from_image_url("https://example.com/image.png"),
];
let message = Message::from_message_array(Role::User, contents);
Sourcepub fn get_input_token_count(&self) -> usize
pub fn get_input_token_count(&self) -> usize
Calculates the approximate token count for the message content.
This method uses the tiktoken library to estimate the number of tokens that would be consumed by this message when sent to OpenAI’s API. Only text content is counted; images are not included in the calculation.
§Returns
The estimated number of tokens for the text content in this message
§Examples
use openai_tools::common::message::Message;
use openai_tools::common::role::Role;
let message = Message::from_string(Role::User, "Hello, world!");
let token_count = message.get_input_token_count();
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Message
Custom deserialization implementation for Message.
impl<'de> Deserialize<'de> for Message
Custom deserialization implementation for Message.
This implementation handles the deserialization of messages from OpenAI API responses, converting string content to Content objects and handling optional fields.
Source§fn deserialize<D>(deserializer: D) -> Result<Message, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Message, D::Error>where
D: Deserializer<'de>,
Source§impl Serialize for Message
Custom serialization implementation for Message.
impl Serialize for Message
Custom serialization implementation for Message.
This implementation ensures that messages are serialized correctly for the OpenAI API,
handling the mutually exclusive nature of content
and content_list
fields.
Either content
or content_list
must be present, but not both.
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
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 more