portkey_sdk/model/
runs.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5use super::assistants::AssistantTool;
6use super::chat::{FunctionCall as ChatFunctionCall, ResponseFormat, ToolChoice as ChatToolChoice};
7
8/// Request to create a run.
9///
10/// # Example
11///
12/// ```rust,ignore
13/// use portkey::model::CreateRunRequest;
14///
15/// let request = CreateRunRequest::builder()
16///     .assistant_id("asst_abc123")
17///     .build()
18///     .unwrap();
19/// ```
20#[derive(Clone, Debug, Default, Serialize, Deserialize)]
21pub struct CreateRunRequest {
22    /// The ID of the assistant to use to execute this run.
23    pub assistant_id: String,
24
25    /// The ID of the Model to be used to execute this run.
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub model: Option<String>,
28
29    /// Override the default system message of the assistant.
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub instructions: Option<String>,
32
33    /// Appends additional instructions at the end of the instructions for the run.
34    #[serde(skip_serializing_if = "Option::is_none")]
35    pub additional_instructions: Option<String>,
36
37    /// Override the tools the assistant can use for this run.
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub tools: Option<Vec<AssistantTool>>,
40
41    /// Set of key-value pairs that can be attached to an object.
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub metadata: Option<HashMap<String, String>>,
44
45    /// What sampling temperature to use, between 0 and 2.
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub temperature: Option<f32>,
48
49    /// An alternative to sampling with temperature.
50    #[serde(skip_serializing_if = "Option::is_none")]
51    pub top_p: Option<f32>,
52
53    /// The maximum number of prompt tokens that may be used over the course of the run.
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub max_prompt_tokens: Option<i32>,
56
57    /// The maximum number of completion tokens that may be used over the course of the run.
58    #[serde(skip_serializing_if = "Option::is_none")]
59    pub max_completion_tokens: Option<i32>,
60
61    /// Controls for how a thread will be truncated.
62    #[serde(skip_serializing_if = "Option::is_none")]
63    pub truncation_strategy: Option<TruncationStrategy>,
64
65    /// Controls which (if any) tool is called by the model.
66    #[serde(skip_serializing_if = "Option::is_none")]
67    pub tool_choice: Option<ChatToolChoice>,
68
69    /// Specifies the format that the model must output.
70    #[serde(skip_serializing_if = "Option::is_none")]
71    pub response_format: Option<ResponseFormat>,
72}
73
74/// Modifies a run.
75#[derive(Clone, Debug, Default, Serialize, Deserialize)]
76pub struct ModifyRunRequest {
77    /// Set of key-value pairs that can be attached to an object.
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub metadata: Option<HashMap<String, String>>,
80}
81
82/// Request to submit tool outputs to run.
83#[derive(Clone, Debug, Serialize, Deserialize)]
84pub struct SubmitToolOutputsRequest {
85    /// A list of tools for which the outputs are being submitted.
86    pub tool_outputs: Vec<ToolOutput>,
87}
88
89/// Output from a tool.
90#[derive(Clone, Debug, Serialize, Deserialize)]
91pub struct ToolOutput {
92    /// The ID of the tool call.
93    pub tool_call_id: String,
94
95    /// The output of the tool call.
96    pub output: String,
97}
98
99/// A run object.
100#[derive(Clone, Debug, Serialize, Deserialize)]
101pub struct Run {
102    /// The identifier of the run.
103    pub id: String,
104
105    /// The object type, which is always "thread.run".
106    pub object: String,
107
108    /// The Unix timestamp (in seconds) for when the run was created.
109    pub created_at: i64,
110
111    /// The ID of the thread that was executed on as a part of this run.
112    pub thread_id: String,
113
114    /// The ID of the assistant used for execution of this run.
115    pub assistant_id: String,
116
117    /// The status of the run.
118    pub status: String,
119
120    /// Details on the action required to continue the run.
121    pub required_action: Option<RequiredAction>,
122
123    /// The last error associated with this run.
124    pub last_error: Option<RunError>,
125
126    /// The Unix timestamp (in seconds) for when the run will expire.
127    pub expires_at: Option<i64>,
128
129    /// The Unix timestamp (in seconds) for when the run was started.
130    pub started_at: Option<i64>,
131
132    /// The Unix timestamp (in seconds) for when the run was cancelled.
133    pub cancelled_at: Option<i64>,
134
135    /// The Unix timestamp (in seconds) for when the run failed.
136    pub failed_at: Option<i64>,
137
138    /// The Unix timestamp (in seconds) for when the run was completed.
139    pub completed_at: Option<i64>,
140
141    /// The model that the assistant used for this run.
142    pub model: String,
143
144    /// The instructions that the assistant used for this run.
145    pub instructions: String,
146
147    /// The list of tools that the assistant used for this run.
148    pub tools: Vec<AssistantTool>,
149
150    /// The list of File IDs the assistant used for this run.
151    pub file_ids: Vec<String>,
152
153    /// Set of key-value pairs that can be attached to an object.
154    pub metadata: HashMap<String, String>,
155
156    /// Usage statistics related to the run.
157    pub usage: Option<RunUsage>,
158
159    /// What sampling temperature was used for this run.
160    pub temperature: Option<f32>,
161
162    /// The nucleus sampling value used for this run.
163    pub top_p: Option<f32>,
164
165    /// The maximum number of prompt tokens specified for this run.
166    pub max_prompt_tokens: Option<i32>,
167
168    /// The maximum number of completion tokens specified for this run.
169    pub max_completion_tokens: Option<i32>,
170
171    /// Controls for how a thread will be truncated.
172    pub truncation_strategy: Option<TruncationStrategy>,
173
174    /// Controls which (if any) tool is called by the model.
175    pub tool_choice: Option<ChatToolChoice>,
176
177    /// Specifies the format that the model must output.
178    pub response_format: Option<ResponseFormat>,
179}
180
181/// Details on the action required to continue the run.
182#[derive(Clone, Debug, Serialize, Deserialize)]
183pub struct RequiredAction {
184    /// The type of action required.
185    #[serde(rename = "type")]
186    pub action_type: String,
187
188    /// Details on the tool outputs needed for this run to continue.
189    pub submit_tool_outputs: SubmitToolOutputs,
190}
191
192/// Details on the tool outputs needed for this run to continue.
193#[derive(Clone, Debug, Serialize, Deserialize)]
194pub struct SubmitToolOutputs {
195    /// A list of the relevant tool calls.
196    pub tool_calls: Vec<ToolCall>,
197}
198
199/// A tool call that the assistant made.
200#[derive(Clone, Debug, Serialize, Deserialize)]
201pub struct ToolCall {
202    /// The ID of the tool call.
203    pub id: String,
204
205    /// The type of tool call.
206    #[serde(rename = "type")]
207    pub tool_type: String,
208
209    /// The function definition.
210    pub function: ChatFunctionCall,
211}
212
213/// Error information for a failed run.
214#[derive(Clone, Debug, Serialize, Deserialize)]
215pub struct RunError {
216    /// One of server_error, rate_limit_exceeded, or invalid_prompt.
217    pub code: String,
218
219    /// A human-readable description of the error.
220    pub message: String,
221}
222
223/// Usage statistics for a run.
224#[derive(Clone, Debug, Serialize, Deserialize)]
225pub struct RunUsage {
226    /// Number of completion tokens used.
227    pub completion_tokens: i32,
228
229    /// Number of prompt tokens used.
230    pub prompt_tokens: i32,
231
232    /// Total number of tokens used.
233    pub total_tokens: i32,
234}
235
236/// Controls for how a thread will be truncated prior to the run.
237#[derive(Clone, Debug, Serialize, Deserialize)]
238#[serde(tag = "type")]
239pub enum TruncationStrategy {
240    #[serde(rename = "auto")]
241    Auto,
242    #[serde(rename = "last_messages")]
243    LastMessages { last_messages: i32 },
244}
245
246/// Response containing a list of runs.
247#[derive(Clone, Debug, Serialize, Deserialize)]
248pub struct ListRunsResponse {
249    pub object: String,
250    pub data: Vec<Run>,
251    pub first_id: Option<String>,
252    pub last_id: Option<String>,
253    pub has_more: bool,
254}
255
256/// A run step object.
257#[derive(Clone, Debug, Serialize, Deserialize)]
258pub struct RunStep {
259    /// The identifier of the run step.
260    pub id: String,
261
262    /// The object type, which is always "thread.run.step".
263    pub object: String,
264
265    /// The Unix timestamp (in seconds) for when the run step was created.
266    pub created_at: i64,
267
268    /// The ID of the assistant associated with the run step.
269    pub assistant_id: String,
270
271    /// The ID of the thread that was run.
272    pub thread_id: String,
273
274    /// The ID of the run that this run step is a part of.
275    pub run_id: String,
276
277    /// The type of run step.
278    #[serde(rename = "type")]
279    pub step_type: String,
280
281    /// The status of the run step.
282    pub status: String,
283
284    /// The details of the run step.
285    pub step_details: StepDetails,
286
287    /// The last error associated with this run step.
288    pub last_error: Option<RunError>,
289
290    /// The Unix timestamp (in seconds) for when the run step expired.
291    pub expired_at: Option<i64>,
292
293    /// The Unix timestamp (in seconds) for when the run step was cancelled.
294    pub cancelled_at: Option<i64>,
295
296    /// The Unix timestamp (in seconds) for when the run step failed.
297    pub failed_at: Option<i64>,
298
299    /// The Unix timestamp (in seconds) for when the run step completed.
300    pub completed_at: Option<i64>,
301
302    /// Set of key-value pairs that can be attached to an object.
303    pub metadata: HashMap<String, String>,
304
305    /// Usage statistics related to the run step.
306    pub usage: Option<RunUsage>,
307}
308
309/// Details of a run step.
310#[derive(Clone, Debug, Serialize, Deserialize)]
311#[serde(tag = "type")]
312pub enum StepDetails {
313    #[serde(rename = "message_creation")]
314    MessageCreation { message_creation: MessageCreation },
315    #[serde(rename = "tool_calls")]
316    ToolCalls { tool_calls: Vec<ToolCall> },
317}
318
319/// Details of a message creation step.
320#[derive(Clone, Debug, Serialize, Deserialize)]
321pub struct MessageCreation {
322    /// The ID of the message that was created by this run step.
323    pub message_id: String,
324}
325
326/// Response containing a list of run steps.
327#[derive(Clone, Debug, Serialize, Deserialize)]
328pub struct ListRunStepsResponse {
329    pub object: String,
330    pub data: Vec<RunStep>,
331    pub first_id: Option<String>,
332    pub last_id: Option<String>,
333    pub has_more: bool,
334}