openai-tools 1.1.0

Tools for OpenAI API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Message module for OpenAI tools.
//!
//! This module provides data structures and functionality for handling OpenAI API messages,
//! including text content, images, tool calls, and multi-modal interactions. It serves as
//! the foundation for communication between users and OpenAI models.
//!
//! ## Core Components
//!
//! - [`Message`] - The main message structure containing role, content, and metadata
//! - [`Content`] - Represents different types of content (text, images, etc.)
//! - [`ToolCall`] - Represents function calls made by OpenAI models
//!
//! ## Supported Content Types
//!
//! The module supports various content types for rich interactions:
//!
//! - **Text content**: Plain text messages
//! - **Image content**: Images from URLs or local files (PNG, JPEG, GIF)
//! - **Multi-modal content**: Combining text and images in a single message
//!
//! ## Usage in the Library
//!
//! This module is used throughout the OpenAI tools library:
//!
//! - In [`crate::chat::request`] - For Chat Completion API message handling
//! - In [`crate::responses::request`] - For Responses API message processing
//! - In [`crate::chat::response`] - For parsing OpenAI API responses
//!
//! ## Examples
//!
//! ### Basic Text Message
//!
//! ```rust,no_run
//! use openai_tools::common::message::Message;
//! use openai_tools::common::role::Role;
//!
//! # fn main() {
//! let message = Message::from_string(Role::User, "Hello, how are you?");
//! # }
//! ```
//!
//! ### Multi-modal Message with Text and Image
//!
//! ```rust,no_run
//! use openai_tools::common::message::{Message, Content};
//! use openai_tools::common::role::Role;
//!
//! # fn main() {
//! let contents = vec![
//!     Content::from_text("What's in this image?"),
//!     Content::from_image_file("path/to/image.png"),
//! ];
//! let message = Message::from_message_array(Role::User, contents);
//! # }
//! ```
//!

use crate::common::{function::Function, role::Role};
use base64::prelude::*;
use serde::{ser::SerializeStruct, Deserialize, Serialize};

/// Represents a tool call made by an OpenAI model.
///
/// Tool calls are generated when an OpenAI model decides to invoke a function
/// or tool as part of its response. This structure contains the metadata
/// necessary to identify and execute the requested function.
///
/// # Fields
///
/// * `id` - Unique identifier for this tool call
/// * `type_name` - The type of tool call (typically "function")
/// * `function` - The function details including name and arguments
///
/// # Examples
///
/// ```rust,no_run
/// use openai_tools::common::message::ToolCall;
/// use openai_tools::common::function::Function;
///
/// // Tool calls are typically received from OpenAI API responses
/// // and contain function invocation details
/// ```
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct ToolCall {
    /// Unique identifier for this tool call
    pub id: String,
    /// The type of tool call (e.g., "function")
    #[serde(rename = "type")]
    pub type_name: String,
    /// The function to be called with its arguments
    pub function: Function,
}

/// 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 content
/// * `image_url` - Optional image URL or base64 data URL
///
/// # Examples
///
/// ```rust,no_run
/// 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");
/// ```
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct Content {
    /// The type of content ("input_text" or "input_image")
    #[serde(rename = "type")]
    pub type_name: String,
    /// Optional text content
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
    /// Optional image URL or base64 data URL
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image_url: Option<String>,
}

impl Content {
    /// 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
    ///
    /// ```rust,no_run
    /// use openai_tools::common::message::Content;
    ///
    /// let content = Content::from_text("Hello, world!");
    /// assert_eq!(content.type_name, "input_text");
    /// ```
    pub fn from_text<T: AsRef<str>>(text: T) -> Self {
        Self { type_name: "input_text".to_string(), text: Some(text.as_ref().to_string()), image_url: None }
    }

    /// 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
    ///
    /// ```rust
    /// use openai_tools::common::message::Content;
    ///
    /// let content = Content::from_image_url("https://example.com/image.png");
    /// assert_eq!(content.type_name, "input_image");
    /// ```
    pub fn from_image_url<T: AsRef<str>>(image_url: T) -> Self {
        Self { type_name: "input_image".to_string(), text: None, image_url: Some(image_url.as_ref().to_string()) }
    }

    /// 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
    ///
    /// ```rust,no_run
    /// use openai_tools::common::message::Content;
    ///
    /// let content = Content::from_image_file("path/to/image.png");
    /// assert_eq!(content.type_name, "input_image");
    /// ```
    pub fn from_image_file<T: AsRef<str>>(file_path: T) -> Self {
        let ext = file_path.as_ref();
        let ext = std::path::Path::new(&ext).extension().and_then(|s| s.to_str()).unwrap();
        let img = image::ImageReader::open(file_path.as_ref()).expect("Failed to open image file").decode().expect("Failed to decode image");
        let img_fmt = match ext {
            "png" => image::ImageFormat::Png,
            "jpg" | "jpeg" => image::ImageFormat::Jpeg,
            "gif" => image::ImageFormat::Gif,
            _ => panic!("Unsupported image format"),
        };
        let mut buf = std::io::Cursor::new(Vec::new());
        img.write_to(&mut buf, img_fmt).expect("Failed to write image to buffer");
        let base64_string = BASE64_STANDARD.encode(buf.into_inner());
        let image_url = format!("data:image/{ext};base64,{base64_string}");
        Self { type_name: "input_image".to_string(), text: None, image_url: Some(image_url) }
    }
}

/// 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 item
/// * `content_list` - Optional list of content items for multi-modal messages
/// * `tool_calls` - Optional list of tool calls made by the assistant
/// * `tool_call_id` - Optional tool call ID for tracking specific tool calls
/// * `refusal` - Optional refusal message if the model declined to respond
/// * `annotations` - Optional list of annotations or metadata
///
/// # Examples
///
/// ```rust,no_run
/// 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);
/// ```
#[derive(Debug, Clone)]
pub struct Message {
    /// The role of the message sender
    pub role: Role,
    /// Optional single content item
    pub content: Option<Content>,
    /// Optional list of content items for multi-modal messages
    pub content_list: Option<Vec<Content>>,
    /// Optional list of tool calls made by the assistant
    pub tool_calls: Option<Vec<ToolCall>>,
    /// Optional refusal message if the model declined to respond
    pub refusal: Option<String>,
    /// Optional tool call ID for tracking specific tool calls
    pub tool_call_id: Option<String>,
    /// Optional list of annotations or metadata
    pub annotations: Option<Vec<String>>,
}

/// 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.
/// Additionally, it handles optional fields like `tool_call_id` for tool call responses.
impl Serialize for Message {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut state = serializer.serialize_struct("Message", 3)?;
        state.serialize_field("role", &self.role)?;

        // Ensure that either content or contents is present, but not both
        if self.role != Role::Assistant
            && ((self.content.is_none() && self.content_list.is_none()) || (self.content.is_some() && self.content_list.is_some()))
        {
            return Err(serde::ser::Error::custom("Message must have either content or contents"));
        }

        // Serialize optional fields
        if let Some(content) = &self.content {
            state.serialize_field("content", &content.text)?;
        }
        if let Some(contents) = &self.content_list {
            state.serialize_field("content", contents)?;
        }
        if let Some(tool_call_id) = &self.tool_call_id {
            state.serialize_field("tool_call_id", tool_call_id)?;
        }
        if let Some(tool_calls) = &self.tool_calls {
            state.serialize_field("tool_calls", tool_calls)?;
        }

        state.end()
    }
}

/// 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 including
/// `tool_call_id` for tool call tracking.
impl<'de> Deserialize<'de> for Message {
    fn deserialize<D>(deserializer: D) -> Result<Message, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct MessageData {
            role: Role,
            content: Option<String>,
            tool_calls: Option<Vec<ToolCall>>,
            refusal: Option<String>,
            annotations: Option<Vec<String>>,
        }

        let data = MessageData::deserialize(deserializer)?;
        let content = data.content.map(Content::from_text);

        Ok(Message {
            role: data.role,
            content,
            content_list: None,
            tool_calls: data.tool_calls,
            tool_call_id: None,
            refusal: data.refusal,
            annotations: data.annotations,
        })
    }
}

impl Message {
    /// 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 sender
    /// * `message` - The text content of the message
    ///
    /// # Returns
    ///
    /// A new Message instance with the specified role and text content
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use openai_tools::common::message::Message;
    /// use openai_tools::common::role::Role;
    ///
    /// let message = Message::from_string(Role::User, "Hello, how are you?");
    /// ```
    pub fn from_string<T: AsRef<str>>(role: Role, message: T) -> Self {
        Self {
            role,
            content: Some(Content::from_text(message.as_ref())),
            content_list: None,
            tool_calls: None,
            tool_call_id: None,
            refusal: None,
            annotations: None,
        }
    }

    /// 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 sender
    /// * `contents` - Vector of content items to include in the message
    ///
    /// # Returns
    ///
    /// A new Message instance with the specified role and content list
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// 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);
    /// ```
    pub fn from_message_array(role: Role, contents: Vec<Content>) -> Self {
        Self { role, content: None, content_list: Some(contents), tool_calls: None, tool_call_id: None, refusal: None, annotations: None }
    }

    /// Creates a new Message as a response to a specific tool call.
    ///
    /// This method is used to create messages that respond to tool calls made by
    /// OpenAI models. The message will have the Assistant role and includes the
    /// tool call ID for tracking purposes.
    ///
    /// # Arguments
    ///
    /// * `tool_call_response` - The response content for the tool call
    /// * `tool_call_id` - The ID of the tool call this message is responding to
    ///
    /// # Returns
    ///
    /// A new Message instance with Assistant role, response content, and tool call ID
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use openai_tools::common::message::Message;
    ///
    /// let response = Message::from_tool_call_response(
    ///     "The weather in Tokyo is 25°C and sunny",
    ///     "tool_call_123"
    /// );
    /// ```
    pub fn from_tool_call_response<T: AsRef<str>>(tool_call_response: T, tool_call_id: T) -> Self {
        Self {
            role: Role::Tool,
            content: Some(Content::from_text(tool_call_response.as_ref())),
            content_list: None,
            tool_calls: None,
            tool_call_id: Some(tool_call_id.as_ref().to_string()),
            refusal: None,
            annotations: None,
        }
    }
    /// 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
    ///
    /// ```rust,no_run
    /// 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();
    /// ```
    pub fn get_input_token_count(&self) -> usize {
        let bpe = tiktoken_rs::o200k_base().unwrap();
        if let Some(content) = &self.content {
            bpe.encode_with_special_tokens(&content.clone().text.unwrap()).len()
        } else if let Some(contents) = &self.content_list {
            let mut total_tokens = 0;
            for content in contents {
                if let Some(text) = &content.text {
                    total_tokens += bpe.encode_with_special_tokens(text).len();
                }
            }
            total_tokens
        } else {
            0 // No content to count tokens for
        }
    }
}