async_openai/types/
message.rs

1use std::collections::HashMap;
2
3use derive_builder::Builder;
4use serde::{Deserialize, Serialize};
5
6use crate::error::OpenAIError;
7
8use super::{ImageDetail, ImageUrl};
9
10#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
11#[serde(rename_all = "lowercase")]
12pub enum MessageRole {
13    #[default]
14    User,
15    Assistant,
16}
17
18#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
19#[serde(rename_all = "snake_case")]
20pub enum MessageStatus {
21    InProgress,
22    Incomplete,
23    Completed,
24}
25
26#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
27#[serde(rename_all = "snake_case")]
28pub enum MessageIncompleteDetailsType {
29    ContentFilter,
30    MaxTokens,
31    RunCancelled,
32    RunExpired,
33    RunFailed,
34}
35
36#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
37pub struct MessageIncompleteDetails {
38    /// The reason the message is incomplete.
39    pub reason: MessageIncompleteDetailsType,
40}
41
42///  Represents a message within a [thread](https://platform.openai.com/docs/api-reference/threads).
43#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
44pub struct MessageObject {
45    /// The identifier, which can be referenced in API endpoints.
46    pub id: String,
47    /// The object type, which is always `thread.message`.
48    pub object: String,
49    /// The Unix timestamp (in seconds) for when the message was created.
50    pub created_at: i32,
51    /// The [thread](https://platform.openai.com/docs/api-reference/threads) ID that this message belongs to.
52    pub thread_id: String,
53
54    /// The status of the message, which can be either `in_progress`, `incomplete`, or `completed`.
55    pub status: Option<MessageStatus>,
56
57    /// On an incomplete message, details about why the message is incomplete.
58    pub incomplete_details: Option<MessageIncompleteDetails>,
59
60    /// The Unix timestamp (in seconds) for when the message was completed.
61    pub completed_at: Option<u32>,
62
63    /// The Unix timestamp (in seconds) for when the message was marked as incomplete.
64    pub incomplete_at: Option<u32>,
65
66    /// The entity that produced the message. One of `user` or `assistant`.
67    pub role: MessageRole,
68
69    /// The content of the message in array of text and/or images.
70    pub content: Vec<MessageContent>,
71
72    /// If applicable, the ID of the [assistant](https://platform.openai.com/docs/api-reference/assistants) that authored this message.
73    pub assistant_id: Option<String>,
74
75    /// The ID of the [run](https://platform.openai.com/docs/api-reference/runs) associated with the creation of this message. Value is `null` when messages are created manually using the create message or create thread endpoints.
76    pub run_id: Option<String>,
77
78    /// A list of files attached to the message, and the tools they were added to.
79    pub attachments: Option<Vec<MessageAttachment>>,
80
81    pub metadata: Option<HashMap<String, serde_json::Value>>,
82}
83
84#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
85pub struct MessageAttachment {
86    /// The ID of the file to attach to the message.
87    pub file_id: String,
88    /// The tools to add this file to.
89    pub tools: Vec<MessageAttachmentTool>,
90}
91
92#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
93#[serde(tag = "type")]
94#[serde(rename_all = "snake_case")]
95pub enum MessageAttachmentTool {
96    CodeInterpreter,
97    FileSearch,
98}
99
100#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
101#[serde(tag = "type")]
102#[serde(rename_all = "snake_case")]
103pub enum MessageContent {
104    Text(MessageContentTextObject),
105    ImageFile(MessageContentImageFileObject),
106    ImageUrl(MessageContentImageUrlObject),
107    Refusal(MessageContentRefusalObject),
108}
109
110#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
111pub struct MessageContentRefusalObject {
112    pub refusal: String,
113}
114
115/// The text content that is part of a message.
116#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
117pub struct MessageContentTextObject {
118    pub text: TextData,
119}
120
121#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
122pub struct TextData {
123    /// The data that makes up the text.
124    pub value: String,
125    pub annotations: Vec<MessageContentTextAnnotations>,
126}
127
128#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
129#[serde(tag = "type")]
130#[serde(rename_all = "snake_case")]
131pub enum MessageContentTextAnnotations {
132    /// A citation within the message that points to a specific quote from a specific File associated with the assistant or the message. Generated when the assistant uses the "retrieval" tool to search files.
133    FileCitation(MessageContentTextAnnotationsFileCitationObject),
134    /// A URL for the file that's generated when the assistant used the `code_interpreter` tool to generate a file.
135    FilePath(MessageContentTextAnnotationsFilePathObject),
136}
137
138/// A citation within the message that points to a specific quote from a specific File associated with the assistant or the message. Generated when the assistant uses the "file_search" tool to search files.
139#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
140pub struct MessageContentTextAnnotationsFileCitationObject {
141    /// The text in the message content that needs to be replaced.
142    pub text: String,
143    pub file_citation: FileCitation,
144    pub start_index: u32,
145    pub end_index: u32,
146}
147
148#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
149pub struct FileCitation {
150    /// The ID of the specific File the citation is from.
151    pub file_id: String,
152    /// The specific quote in the file.
153    pub quote: Option<String>,
154}
155
156#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
157pub struct MessageContentTextAnnotationsFilePathObject {
158    /// The text in the message content that needs to be replaced.
159    pub text: String,
160    pub file_path: FilePath,
161    pub start_index: u32,
162    pub end_index: u32,
163}
164
165#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
166pub struct FilePath {
167    /// The ID of the file that was generated.
168    pub file_id: String,
169}
170
171/// References an image [File](https://platform.openai.com/docs/api-reference/files) in the content of a message.
172#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
173pub struct MessageContentImageFileObject {
174    pub image_file: ImageFile,
175}
176
177#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
178pub struct ImageFile {
179    /// The [File](https://platform.openai.com/docs/api-reference/files) ID of the image in the message content. Set `purpose="vision"` when uploading the File if you need to later display the file content.
180    pub file_id: String,
181    /// Specifies the detail level of the image if specified by the user. `low` uses fewer tokens, you can opt in to high resolution using `high`.
182    pub detail: Option<ImageDetail>,
183}
184
185/// References an image URL in the content of a message.
186#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
187pub struct MessageContentImageUrlObject {
188    pub image_url: ImageUrl,
189}
190
191#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
192pub struct MessageRequestContentTextObject {
193    /// Text content to be sent to the model
194    pub text: String,
195}
196
197#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
198#[serde(untagged)]
199pub enum CreateMessageRequestContent {
200    /// The text contents of the message.
201    Content(String),
202    /// An array of content parts with a defined type, each can be of type `text` or images can be passed with `image_url` or `image_file`. Image types are only supported on [Vision-compatible models](https://platform.openai.com/docs/models/overview).
203    ContentArray(Vec<MessageContentInput>),
204}
205
206#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
207#[serde(tag = "type")]
208#[serde(rename_all = "snake_case")]
209pub enum MessageContentInput {
210    Text(MessageRequestContentTextObject),
211    ImageFile(MessageContentImageFileObject),
212    ImageUrl(MessageContentImageUrlObject),
213}
214#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
215#[builder(name = "CreateMessageRequestArgs")]
216#[builder(pattern = "mutable")]
217#[builder(setter(into, strip_option), default)]
218#[builder(derive(Debug))]
219#[builder(build_fn(error = "OpenAIError"))]
220pub struct CreateMessageRequest {
221    /// The role of the entity that is creating the message. Allowed values include:
222    /// - `user`: Indicates the message is sent by an actual user and should be used in most cases to represent user-generated messages.
223    /// - `assistant`: Indicates the message is generated by the assistant. Use this value to insert messages from the assistant into the conversation.
224    pub role: MessageRole,
225    /// The content of the message.
226    pub content: CreateMessageRequestContent,
227
228    /// A list of files attached to the message, and the tools they should be added to.
229    pub attachments: Option<Vec<MessageAttachment>>,
230
231    #[serde(skip_serializing_if = "Option::is_none")]
232    pub metadata: Option<HashMap<String, serde_json::Value>>,
233}
234
235#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
236pub struct ModifyMessageRequest {
237    #[serde(skip_serializing_if = "Option::is_none")]
238    pub metadata: Option<HashMap<String, serde_json::Value>>,
239}
240
241#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
242pub struct DeleteMessageResponse {
243    pub id: String,
244    pub deleted: bool,
245    pub object: String,
246}
247
248#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
249pub struct ListMessagesResponse {
250    pub object: String,
251    pub data: Vec<MessageObject>,
252    pub first_id: Option<String>,
253    pub last_id: Option<String>,
254    pub has_more: bool,
255}
256
257/// Represents a message delta i.e. any changed fields on a message during streaming.
258#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
259pub struct MessageDeltaObject {
260    /// The identifier of the message, which can be referenced in API endpoints.
261    pub id: String,
262    /// The object type, which is always `thread.message.delta`.
263    pub object: String,
264    /// The delta containing the fields that have changed on the Message.
265    pub delta: MessageDelta,
266}
267
268#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
269pub struct MessageDelta {
270    /// The entity that produced the message. One of `user` or `assistant`.
271    pub role: Option<MessageRole>,
272    ///  The content of the message in array of text and/or images.
273    pub content: Option<Vec<MessageDeltaContent>>,
274}
275
276#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
277#[serde(tag = "type")]
278#[serde(rename_all = "snake_case")]
279pub enum MessageDeltaContent {
280    ImageFile(MessageDeltaContentImageFileObject),
281    ImageUrl(MessageDeltaContentImageUrlObject),
282    Text(MessageDeltaContentTextObject),
283    Refusal(MessageDeltaContentRefusalObject),
284}
285
286#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
287pub struct MessageDeltaContentRefusalObject {
288    /// The index of the refusal part in the message.
289    pub index: i32,
290    pub refusal: Option<String>,
291}
292
293/// The text content that is part of a message.
294#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
295pub struct MessageDeltaContentTextObject {
296    /// The index of the content part in the message.
297    pub index: u32,
298    pub text: Option<MessageDeltaContentText>,
299}
300
301#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
302pub struct MessageDeltaContentText {
303    /// The data that makes up the text.
304    pub value: Option<String>,
305    pub annotations: Option<Vec<MessageDeltaContentTextAnnotations>>,
306}
307
308#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
309#[serde(tag = "type")]
310#[serde(rename_all = "snake_case")]
311pub enum MessageDeltaContentTextAnnotations {
312    FileCitation(MessageDeltaContentTextAnnotationsFileCitationObject),
313    FilePath(MessageDeltaContentTextAnnotationsFilePathObject),
314}
315
316/// A citation within the message that points to a specific quote from a specific File associated with the assistant or the message. Generated when the assistant uses the "file_search" tool to search files.
317#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
318pub struct MessageDeltaContentTextAnnotationsFileCitationObject {
319    /// The index of the annotation in the text content part.
320    pub index: u32,
321    /// The text in the message content that needs to be replaced.
322    pub text: Option<String>,
323    pub file_citation: Option<FileCitation>,
324    pub start_index: Option<u32>,
325    pub end_index: Option<u32>,
326}
327
328/// A URL for the file that's generated when the assistant used the `code_interpreter` tool to generate a file.
329#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
330pub struct MessageDeltaContentTextAnnotationsFilePathObject {
331    /// The index of the annotation in the text content part.
332    pub index: u32,
333    /// The text in the message content that needs to be replaced.
334    pub text: Option<String>,
335    pub file_path: Option<FilePath>,
336    pub start_index: Option<u32>,
337    pub end_index: Option<u32>,
338}
339
340/// References an image [File](https://platform.openai.com/docs/api-reference/files) in the content of a message.
341#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
342pub struct MessageDeltaContentImageFileObject {
343    /// The index of the content part in the message.
344    pub index: u32,
345
346    pub image_file: Option<ImageFile>,
347}
348
349#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
350pub struct MessageDeltaContentImageUrlObject {
351    /// The index of the content part in the message.
352    pub index: u32,
353
354    pub image_url: Option<ImageUrl>,
355}