outfox_openai/spec/run.rs
1use std::collections::HashMap;
2
3use derive_builder::Builder;
4use serde::{Deserialize, Serialize};
5
6use crate::{error::OpenAIError, spec::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 initial 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 #[serde(rename = "type")]
101 pub kind: TruncationObjectType,
102 /// The number of most recent messages from the thread when constructing the context for the run.
103 pub last_messages: Option<u32>,
104}
105
106#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
107pub struct RunObjectIncompleteDetails {
108 /// The reason why the run is incomplete. This will point to which specific token limit was reached over the course of the run.
109 pub reason: RunObjectIncompleteDetailsReason,
110}
111
112#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
113#[serde(rename_all = "snake_case")]
114pub enum RunObjectIncompleteDetailsReason {
115 MaxCompletionTokens,
116 MaxPromptTokens,
117}
118
119#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
120#[serde(rename_all = "snake_case")]
121pub enum RunStatus {
122 Queued,
123 InProgress,
124 RequiresAction,
125 Cancelling,
126 Cancelled,
127 Failed,
128 Completed,
129 Incomplete,
130 Expired,
131}
132
133#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
134pub struct RequiredAction {
135 /// For now, this is always `submit_tool_outputs`.
136 #[serde(rename = "type")]
137 pub kind: String,
138
139 pub submit_tool_outputs: SubmitToolOutputs,
140}
141
142#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
143pub struct SubmitToolOutputs {
144 pub tool_calls: Vec<RunToolCallObject>,
145}
146
147#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
148pub struct RunToolCallObject {
149 /// 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.
150 pub id: String,
151 /// The type of tool call the output is required for. For now, this is always `function`.
152 #[serde(rename = "type")]
153 pub kind: String,
154 /// The function definition.
155 pub function: FunctionCall,
156}
157
158#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
159pub struct LastError {
160 /// One of `server_error`, `rate_limit_exceeded`, or `invalid_prompt`.
161 pub code: LastErrorCode,
162 /// A human-readable description of the error.
163 pub message: String,
164}
165
166#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
167#[serde(rename_all = "snake_case")]
168pub enum LastErrorCode {
169 ServerError,
170 RateLimitExceeded,
171 InvalidPrompt,
172}
173
174#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
175pub struct RunCompletionUsage {
176 /// Number of completion tokens used over the course of the run.
177 pub completion_tokens: u32,
178 /// Number of prompt tokens used over the course of the run.
179 pub prompt_tokens: u32,
180 /// Total number of tokens used (prompt + completion).
181 pub total_tokens: u32,
182}
183
184#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
185#[builder(name = "CreateRunRequestBuilder")]
186#[builder(pattern = "mutable")]
187#[builder(setter(into, strip_option), default)]
188#[builder(derive(Debug))]
189#[builder(build_fn(error = "OpenAIError"))]
190pub struct CreateRunRequest {
191 /// The ID of the [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to execute this run.
192 pub assistant_id: String,
193
194 /// 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.
195 #[serde(skip_serializing_if = "Option::is_none")]
196 pub model: Option<String>,
197
198 /// 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.
199 #[serde(skip_serializing_if = "Option::is_none")]
200 pub instructions: Option<String>,
201
202 /// 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.
203 #[serde(skip_serializing_if = "Option::is_none")]
204 pub additional_instructions: Option<String>,
205
206 /// Adds additional messages to the thread before creating the run.
207 #[serde(skip_serializing_if = "Option::is_none")]
208 pub additional_messages: Option<Vec<CreateMessageRequest>>,
209
210 /// Override the tools the assistant can use for this run. This is useful for modifying the behavior on a per-run basis.
211 #[serde(skip_serializing_if = "Option::is_none")]
212 pub tools: Option<Vec<AssistantTools>>,
213
214 #[serde(skip_serializing_if = "Option::is_none")]
215 pub metadata: Option<HashMap<String, serde_json::Value>>,
216
217 /// The sampling temperature used for this run. If not set, defaults to 1.
218 #[serde(skip_serializing_if = "Option::is_none")]
219 pub temperature: Option<f32>,
220
221 /// 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.
222 ///
223 /// We generally recommend altering this or temperature but not both.
224 #[serde(skip_serializing_if = "Option::is_none")]
225 pub top_p: Option<f32>,
226
227 /// 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.
228 #[serde(skip_serializing_if = "Option::is_none")]
229 pub stream: Option<bool>,
230
231 /// 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.
232 #[serde(skip_serializing_if = "Option::is_none")]
233 pub max_prompt_tokens: Option<u32>,
234
235 /// 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.
236 #[serde(skip_serializing_if = "Option::is_none")]
237 pub max_completion_tokens: Option<u32>,
238
239 /// Controls for how a thread will be truncated prior to the run. Use this to control the initial context window of the run.
240 #[serde(skip_serializing_if = "Option::is_none")]
241 pub truncation_strategy: Option<TruncationObject>,
242
243 #[serde(skip_serializing_if = "Option::is_none")]
244 pub tool_choice: Option<AssistantsApiToolChoiceOption>,
245
246 /// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling) during tool use.
247 #[serde(skip_serializing_if = "Option::is_none")]
248 pub parallel_tool_calls: Option<bool>,
249
250 #[serde(skip_serializing_if = "Option::is_none")]
251 pub response_format: Option<AssistantsApiResponseFormatOption>,
252}
253
254#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
255pub struct ModifyRunRequest {
256 #[serde(skip_serializing_if = "Option::is_none")]
257 pub metadata: Option<HashMap<String, serde_json::Value>>,
258}
259
260#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
261pub struct ListRunsResponse {
262 pub object: String,
263 pub data: Vec<RunObject>,
264 pub first_id: Option<String>,
265 pub last_id: Option<String>,
266 pub has_more: bool,
267}
268
269#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
270pub struct SubmitToolOutputsRunRequest {
271 /// A list of tools for which the outputs are being submitted.
272 pub tool_outputs: Vec<ToolsOutputs>,
273 /// 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.
274 pub stream: Option<bool>,
275}
276
277#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
278#[builder(name = "ToolsOutputsBuilder")]
279#[builder(pattern = "mutable")]
280#[builder(setter(into, strip_option), default)]
281#[builder(derive(Debug))]
282#[builder(build_fn(error = "OpenAIError"))]
283pub struct ToolsOutputs {
284 /// The ID of the tool call in the `required_action` object within the run object the output is being submitted for.
285 pub tool_call_id: Option<String>,
286 /// The output of the tool call to be submitted to continue the run.
287 pub output: Option<String>,
288}