Skip to main content

outfox_openai/spec/assistants/
thread.rs

1use std::collections::HashMap;
2
3use derive_builder::Builder;
4use serde::{Deserialize, Serialize};
5
6use crate::error::OpenAIError;
7use crate::spec::assistants::{
8    AssistantToolResources, AssistantTools, AssistantsApiResponseFormatOption,
9    AssistantsApiToolChoiceOption, CreateAssistantToolResources, CreateMessageRequest,
10    TruncationObject,
11};
12
13/// Represents a thread that contains [messages](https://platform.openai.com/docs/api-reference/messages).
14#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
15pub struct ThreadObject {
16    /// The identifier, which can be referenced in API endpoints.
17    pub id: String,
18    /// The object type, which is always `thread`.
19    pub object: String,
20    /// The Unix timestamp (in seconds) for when the thread was created.
21    pub created_at: u64,
22
23    /// A set of resources that are made available to the assistant's tools in this thread. The
24    /// resources are specific to the type of tool. For example, the `code_interpreter` tool
25    /// requires a list of file IDs, while the `file_search` tool requires a list of vector store
26    /// IDs.
27    pub tool_resources: Option<AssistantToolResources>,
28
29    pub metadata: Option<HashMap<String, serde_json::Value>>,
30}
31
32#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
33#[builder(name = "CreateThreadRequestArgs")]
34#[builder(pattern = "mutable")]
35#[builder(setter(into, strip_option), default)]
36#[builder(derive(Debug))]
37#[builder(build_fn(error = "OpenAIError"))]
38pub struct CreateThreadRequest {
39    /// A list of [messages](https://platform.openai.com/docs/api-reference/messages) to start the thread with.
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub messages: Option<Vec<CreateMessageRequest>>,
42
43    /// A set of resources that are made available to the assistant's tools in this thread. The
44    /// resources are specific to the type of tool. For example, the `code_interpreter` tool
45    /// requires a list of file IDs, while the `file_search` tool requires a list of vector store
46    /// IDs.
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub tool_resources: Option<CreateAssistantToolResources>,
49
50    #[serde(skip_serializing_if = "Option::is_none")]
51    pub metadata: Option<HashMap<String, serde_json::Value>>,
52}
53
54#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
55pub struct ModifyThreadRequest {
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub metadata: Option<HashMap<String, serde_json::Value>>,
58
59    /// A set of resources that are made available to the assistant's tools in this thread. The
60    /// resources are specific to the type of tool. For example, the `code_interpreter` tool
61    /// requires a list of file IDs, while the `file_search` tool requires a list of vector store
62    /// IDs.
63    #[serde(skip_serializing_if = "Option::is_none")]
64    pub tool_resources: Option<AssistantToolResources>,
65}
66
67#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
68pub struct DeleteThreadResponse {
69    pub id: String,
70    pub deleted: bool,
71    pub object: String,
72}
73
74#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
75#[builder(name = "CreateThreadAndRunRequestArgs")]
76#[builder(pattern = "mutable")]
77#[builder(setter(into, strip_option), default)]
78#[builder(derive(Debug))]
79#[builder(build_fn(error = "OpenAIError"))]
80pub struct CreateThreadAndRunRequest {
81    /// The ID of the [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to execute this run.
82    pub assistant_id: String,
83
84    /// If no thread is provided, an empty thread will be created.
85    #[serde(skip_serializing_if = "Option::is_none")]
86    pub thread: Option<CreateThreadRequest>,
87
88    /// 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.
89    #[serde(skip_serializing_if = "Option::is_none")]
90    pub model: Option<String>,
91
92    /// Override the default system message of the assistant. This is useful for modifying the
93    /// behavior on a per-run basis.
94    #[serde(skip_serializing_if = "Option::is_none")]
95    pub instructions: Option<String>,
96
97    /// Override the tools the assistant can use for this run. This is useful for modifying the
98    /// behavior on a per-run basis.
99    #[serde(skip_serializing_if = "Option::is_none")]
100    pub tools: Option<Vec<AssistantTools>>,
101
102    /// A set of resources that are used by the assistant's tools. The resources are specific to
103    /// the type of tool. For example, the `code_interpreter` tool requires a list of file IDs,
104    /// while the `file_search` tool requires a list of vector store IDs.
105    #[serde(skip_serializing_if = "Option::is_none")]
106    pub tool_resources: Option<AssistantToolResources>,
107
108    #[serde(skip_serializing_if = "Option::is_none")]
109    pub metadata: Option<HashMap<String, serde_json::Value>>,
110
111    /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the
112    /// output more random, while lower values like 0.2 will make it more focused and
113    /// deterministic.
114    #[serde(skip_serializing_if = "Option::is_none")]
115    pub temperature: Option<f32>,
116
117    ///  An alternative to sampling with temperature, called nucleus sampling, where the model
118    /// considers the results of the tokens with top_p probability mass. So 0.1 means only the
119    /// tokens comprising the top 10% probability mass are considered.
120    ///
121    /// We generally recommend altering this or temperature but not both.
122    #[serde(skip_serializing_if = "Option::is_none")]
123    pub top_p: Option<f32>,
124
125    /// If `true`, returns a stream of events that happen during the Run as server-sent events,
126    /// terminating when the Run enters a terminal state with a `data: [DONE]` message.
127    #[serde(skip_serializing_if = "Option::is_none")]
128    pub stream: Option<bool>,
129
130    /// The maximum number of prompt tokens that may be used over the course of the run. The run
131    /// will make a best effort to use only the number of prompt tokens specified, across multiple
132    /// turns of the run. If the run exceeds the number of prompt tokens specified, the run will
133    /// end with status `incomplete`. See `incomplete_details` for more info.
134    #[serde(skip_serializing_if = "Option::is_none")]
135    pub max_prompt_tokens: Option<u32>,
136
137    /// The maximum number of completion tokens that may be used over the course of the run. The
138    /// run will make a best effort to use only the number of completion tokens specified, across
139    /// multiple turns of the run. If the run exceeds the number of completion tokens specified,
140    /// the run will end with status `incomplete`. See `incomplete_details` for more info.
141    #[serde(skip_serializing_if = "Option::is_none")]
142    pub max_completion_tokens: Option<u32>,
143
144    /// Controls for how a thread will be truncated prior to the run. Use this to control the
145    /// intial context window of the run.
146    #[serde(skip_serializing_if = "Option::is_none")]
147    pub truncation_strategy: Option<TruncationObject>,
148
149    #[serde(skip_serializing_if = "Option::is_none")]
150    pub tool_choice: Option<AssistantsApiToolChoiceOption>,
151
152    /// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling) during tool use.
153    #[serde(skip_serializing_if = "Option::is_none")]
154    pub parallel_tool_calls: Option<bool>,
155
156    #[serde(skip_serializing_if = "Option::is_none")]
157    pub response_format: Option<AssistantsApiResponseFormatOption>,
158}