async_openai/types/
run.rs

1use std::collections::HashMap;
2
3use derive_builder::Builder;
4use serde::{Deserialize, Serialize};
5
6use crate::{error::OpenAIError, types::FunctionCall};
7
8use super::{
9    AssistantTools, AssistantsApiResponseFormatOption, AssistantsApiToolChoiceOption,
10    CreateMessageRequest,
11};
12
13/// Represents an execution run on a [thread](https://platform.openai.com/docs/api-reference/threads).
14#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
15pub struct RunObject {
16    /// The identifier, which can be referenced in API endpoints.
17    pub id: String,
18    /// The object type, which is always `thread.run`.
19    pub object: String,
20    /// The Unix timestamp (in seconds) for when the run was created.
21    pub created_at: i32,
22    ///The ID of the [thread](https://platform.openai.com/docs/api-reference/threads) that was executed on as a part of this run.
23    pub thread_id: String,
24
25    /// The ID of the [assistant](https://platform.openai.com/docs/api-reference/assistants) used for execution of this run.
26    pub assistant_id: Option<String>,
27
28    /// The status of the run, which can be either `queued`, `in_progress`, `requires_action`, `cancelling`, `cancelled`, `failed`, `completed`, `incomplete`, or `expired`.
29    pub status: RunStatus,
30
31    /// Details on the action required to continue the run. Will be `null` if no action is required.
32    pub required_action: Option<RequiredAction>,
33
34    /// The last error associated with this run. Will be `null` if there are no errors.
35    pub last_error: Option<LastError>,
36
37    /// The Unix timestamp (in seconds) for when the run will expire.
38    pub expires_at: Option<i32>,
39    ///  The Unix timestamp (in seconds) for when the run was started.
40    pub started_at: Option<i32>,
41    /// The Unix timestamp (in seconds) for when the run was cancelled.
42    pub cancelled_at: Option<i32>,
43    /// The Unix timestamp (in seconds) for when the run failed.
44    pub failed_at: Option<i32>,
45    ///The Unix timestamp (in seconds) for when the run was completed.
46    pub completed_at: Option<i32>,
47
48    /// Details on why the run is incomplete. Will be `null` if the run is not incomplete.
49    pub incomplete_details: Option<RunObjectIncompleteDetails>,
50
51    /// The model that the [assistant](https://platform.openai.com/docs/api-reference/assistants) used for this run.
52    pub model: String,
53
54    /// The instructions that the [assistant](https://platform.openai.com/docs/api-reference/assistants) used for this run.
55    pub instructions: String,
56
57    /// The list of tools that the [assistant](https://platform.openai.com/docs/api-reference/assistants) used for this run.
58    pub tools: Vec<AssistantTools>,
59
60    pub metadata: Option<HashMap<String, serde_json::Value>>,
61
62    /// Usage statistics related to the run. This value will be `null` if the run is not in a terminal state (i.e. `in_progress`, `queued`, etc.).
63    pub usage: Option<RunCompletionUsage>,
64
65    /// The sampling temperature used for this run. If not set, defaults to 1.
66    pub temperature: Option<f32>,
67
68    /// The nucleus sampling value used for this run. If not set, defaults to 1.
69    pub top_p: Option<f32>,
70
71    /// The maximum number of prompt tokens specified to have been used over the course of the run.
72    pub max_prompt_tokens: Option<u32>,
73
74    /// The maximum number of completion tokens specified to have been used over the course of the run.
75    pub max_completion_tokens: Option<u32>,
76
77    /// Controls for how a thread will be truncated prior to the run. Use this to control the intial context window of the run.
78    pub truncation_strategy: Option<TruncationObject>,
79
80    pub tool_choice: Option<AssistantsApiToolChoiceOption>,
81
82    /// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling) during tool use.
83    pub parallel_tool_calls: bool,
84
85    pub response_format: Option<AssistantsApiResponseFormatOption>,
86}
87
88#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
89#[serde(rename_all = "snake_case")]
90pub enum TruncationObjectType {
91    #[default]
92    Auto,
93    LastMessages,
94}
95
96/// Thread Truncation Controls
97#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
98pub struct TruncationObject {
99    /// The truncation strategy to use for the thread. The default is `auto`. If set to `last_messages`, the thread will be truncated to the n most recent messages in the thread. When set to `auto`, messages in the middle of the thread will be dropped to fit the context length of the model, `max_prompt_tokens`.
100    pub r#type: TruncationObjectType,
101    /// The number of most recent messages from the thread when constructing the context for the run.
102    pub last_messages: Option<u32>,
103}
104
105#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
106pub struct RunObjectIncompleteDetails {
107    /// The reason why the run is incomplete. This will point to which specific token limit was reached over the course of the run.
108    pub reason: RunObjectIncompleteDetailsReason,
109}
110
111#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
112#[serde(rename_all = "snake_case")]
113pub enum RunObjectIncompleteDetailsReason {
114    MaxCompletionTokens,
115    MaxPromptTokens,
116}
117
118#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
119#[serde(rename_all = "snake_case")]
120pub enum RunStatus {
121    Queued,
122    InProgress,
123    RequiresAction,
124    Cancelling,
125    Cancelled,
126    Failed,
127    Completed,
128    Incomplete,
129    Expired,
130}
131
132#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
133pub struct RequiredAction {
134    /// For now, this is always `submit_tool_outputs`.
135    pub r#type: String,
136
137    pub submit_tool_outputs: SubmitToolOutputs,
138}
139
140#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
141pub struct SubmitToolOutputs {
142    pub tool_calls: Vec<RunToolCallObject>,
143}
144
145#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
146pub struct RunToolCallObject {
147    /// The ID of the tool call. This ID must be referenced when you submit the tool outputs in using the [Submit tool outputs to run](https://platform.openai.com/docs/api-reference/runs/submitToolOutputs) endpoint.
148    pub id: String,
149    /// The type of tool call the output is required for. For now, this is always `function`.
150    pub r#type: String,
151    /// The function definition.
152    pub function: FunctionCall,
153}
154
155#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
156pub struct LastError {
157    /// One of `server_error`, `rate_limit_exceeded`, or `invalid_prompt`.
158    pub code: LastErrorCode,
159    /// A human-readable description of the error.
160    pub message: String,
161}
162
163#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
164#[serde(rename_all = "snake_case")]
165pub enum LastErrorCode {
166    ServerError,
167    RateLimitExceeded,
168    InvalidPrompt,
169}
170
171#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
172pub struct RunCompletionUsage {
173    /// Number of completion tokens used over the course of the run.
174    pub completion_tokens: u32,
175    /// Number of prompt tokens used over the course of the run.
176    pub prompt_tokens: u32,
177    /// Total number of tokens used (prompt + completion).
178    pub total_tokens: u32,
179}
180
181#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
182#[builder(name = "CreateRunRequestArgs")]
183#[builder(pattern = "mutable")]
184#[builder(setter(into, strip_option), default)]
185#[builder(derive(Debug))]
186#[builder(build_fn(error = "OpenAIError"))]
187pub struct CreateRunRequest {
188    /// The ID of the [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to execute this run.
189    pub assistant_id: String,
190
191    /// The ID of the [Model](https://platform.openai.com/docs/api-reference/models) to be used to execute this run. If a value is provided here, it will override the model associated with the assistant. If not, the model associated with the assistant will be used.
192    #[serde(skip_serializing_if = "Option::is_none")]
193    pub model: Option<String>,
194
195    /// Overrides the [instructions](https://platform.openai.com/docs/api-reference/assistants/createAssistant) of the assistant. This is useful for modifying the behavior on a per-run basis.
196    #[serde(skip_serializing_if = "Option::is_none")]
197    pub instructions: Option<String>,
198
199    /// Appends additional instructions at the end of the instructions for the run. This is useful for modifying the behavior on a per-run basis without overriding other instructions.
200    #[serde(skip_serializing_if = "Option::is_none")]
201    pub additional_instructions: Option<String>,
202
203    /// Adds additional messages to the thread before creating the run.
204    #[serde(skip_serializing_if = "Option::is_none")]
205    pub additional_messages: Option<Vec<CreateMessageRequest>>,
206
207    /// Override the tools the assistant can use for this run. This is useful for modifying the behavior on a per-run basis.
208    #[serde(skip_serializing_if = "Option::is_none")]
209    pub tools: Option<Vec<AssistantTools>>,
210
211    #[serde(skip_serializing_if = "Option::is_none")]
212    pub metadata: Option<HashMap<String, serde_json::Value>>,
213
214    /// The sampling temperature used for this run. If not set, defaults to 1.
215    #[serde(skip_serializing_if = "Option::is_none")]
216    pub temperature: Option<f32>,
217
218    ///  An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
219    ///
220    /// We generally recommend altering this or temperature but not both.
221    #[serde(skip_serializing_if = "Option::is_none")]
222    pub top_p: Option<f32>,
223
224    /// If `true`, returns a stream of events that happen during the Run as server-sent events, terminating when the Run enters a terminal state with a `data: [DONE]` message.
225    #[serde(skip_serializing_if = "Option::is_none")]
226    pub stream: Option<bool>,
227
228    /// The maximum number of prompt tokens that may be used over the course of the run. The run will make a best effort to use only the number of prompt tokens specified, across multiple turns of the run. If the run exceeds the number of prompt tokens specified, the run will end with status `incomplete`. See `incomplete_details` for more info.
229    #[serde(skip_serializing_if = "Option::is_none")]
230    pub max_prompt_tokens: Option<u32>,
231
232    /// The maximum number of completion tokens that may be used over the course of the run. The run will make a best effort to use only the number of completion tokens specified, across multiple turns of the run. If the run exceeds the number of completion tokens specified, the run will end with status `incomplete`. See `incomplete_details` for more info.
233    #[serde(skip_serializing_if = "Option::is_none")]
234    pub max_completion_tokens: Option<u32>,
235
236    /// Controls for how a thread will be truncated prior to the run. Use this to control the intial context window of the run.
237    #[serde(skip_serializing_if = "Option::is_none")]
238    pub truncation_strategy: Option<TruncationObject>,
239
240    #[serde(skip_serializing_if = "Option::is_none")]
241    pub tool_choice: Option<AssistantsApiToolChoiceOption>,
242
243    /// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling) during tool use.
244    #[serde(skip_serializing_if = "Option::is_none")]
245    pub parallel_tool_calls: Option<bool>,
246
247    #[serde(skip_serializing_if = "Option::is_none")]
248    pub response_format: Option<AssistantsApiResponseFormatOption>,
249}
250
251#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
252pub struct ModifyRunRequest {
253    #[serde(skip_serializing_if = "Option::is_none")]
254    pub metadata: Option<HashMap<String, serde_json::Value>>,
255}
256
257#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
258pub struct ListRunsResponse {
259    pub object: String,
260    pub data: Vec<RunObject>,
261    pub first_id: Option<String>,
262    pub last_id: Option<String>,
263    pub has_more: bool,
264}
265
266#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
267pub struct SubmitToolOutputsRunRequest {
268    /// A list of tools for which the outputs are being submitted.
269    pub tool_outputs: Vec<ToolsOutputs>,
270    /// If `true`, returns a stream of events that happen during the Run as server-sent events, terminating when the Run enters a terminal state with a `data: [DONE]` message.
271    pub stream: Option<bool>,
272}
273
274#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
275#[builder(name = "ToolsOutputsArgs")]
276#[builder(pattern = "mutable")]
277#[builder(setter(into, strip_option), default)]
278#[builder(derive(Debug))]
279#[builder(build_fn(error = "OpenAIError"))]
280pub struct ToolsOutputs {
281    /// The ID of the tool call in the `required_action` object within the run object the output is being submitted for.
282    pub tool_call_id: Option<String>,
283    /// The output of the tool call to be submitted to continue the run.
284    pub output: Option<String>,
285}