async_openai_alt/types/assistant.rs
1use std::collections::HashMap;
2
3use derive_builder::Builder;
4use serde::{Deserialize, Serialize};
5
6use crate::error::OpenAIError;
7
8use super::{FunctionName, FunctionObject, ResponseFormat};
9
10#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
11pub struct AssistantToolCodeInterpreterResources {
12 ///A list of [file](https://platform.openai.com/docs/api-reference/files) IDs made available to the `code_interpreter`` tool. There can be a maximum of 20 files associated with the tool.
13 pub file_ids: Vec<String>, // maxItems: 20
14}
15
16#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
17pub struct AssistantToolFileSearchResources {
18 /// The ID of the [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object) attached to this assistant. There can be a maximum of 1 vector store attached to the assistant.
19 pub vector_store_ids: Vec<String>,
20}
21
22#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
23pub struct AssistantToolResources {
24 #[serde(skip_serializing_if = "Option::is_none")]
25 pub code_interpreter: Option<AssistantToolCodeInterpreterResources>,
26 #[serde(skip_serializing_if = "Option::is_none")]
27 pub file_search: Option<AssistantToolFileSearchResources>,
28}
29
30#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
31pub struct CreateAssistantToolResources {
32 #[serde(skip_serializing_if = "Option::is_none")]
33 pub code_interpreter: Option<AssistantToolCodeInterpreterResources>,
34 #[serde(skip_serializing_if = "Option::is_none")]
35 pub file_search: Option<CreateAssistantToolFileSearchResources>,
36}
37
38#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
39pub struct CreateAssistantToolFileSearchResources {
40 /// The [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object) attached to this assistant. There can be a maximum of 1 vector store attached to the assistant.
41 pub vector_store_ids: Option<Vec<String>>,
42 /// A helper to create a [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object) with file_ids and attach it to this assistant. There can be a maximum of 1 vector store attached to the assistant.
43 pub vector_stores: Option<Vec<AssistantVectorStore>>,
44}
45
46#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
47pub struct AssistantVectorStore {
48 /// A list of [file](https://platform.openai.com/docs/api-reference/files) IDs to add to the vector store. There can be a maximum of 10000 files in a vector store.
49 pub file_ids: Vec<String>,
50
51 /// The chunking strategy used to chunk the file(s). If not set, will use the `auto` strategy.
52 pub chunking_strategy: Option<AssistantVectorStoreChunkingStrategy>,
53
54 /// Set of 16 key-value pairs that can be attached to a vector store. This can be useful for storing additional information about the vector store in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.
55 pub metadata: Option<HashMap<String, serde_json::Value>>,
56}
57
58#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
59#[serde(tag = "type")]
60pub enum AssistantVectorStoreChunkingStrategy {
61 /// The default strategy. This strategy currently uses a `max_chunk_size_tokens` of `800` and `chunk_overlap_tokens` of `400`.
62 #[default]
63 Auto,
64 Static(StaticChunkingStrategy),
65}
66
67/// Static Chunking Strategy
68#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
69pub struct StaticChunkingStrategy {
70 /// The maximum number of tokens in each chunk. The default value is `800`. The minimum value is `100` and the maximum value is `4096`.
71 max_chunk_size_tokens: u16,
72 /// The number of tokens that overlap between chunks. The default value is `400`.
73 ///
74 /// Note that the overlap must not exceed half of `max_chunk_size_tokens`.
75 chunk_overlap_tokens: u16,
76}
77
78/// Represents an `assistant` that can call the model and use tools.
79#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
80pub struct AssistantObject {
81 /// The identifier, which can be referenced in API endpoints.
82 pub id: String,
83 /// The object type, which is always `assistant`.
84 pub object: String,
85 /// The Unix timestamp (in seconds) for when the assistant was created.
86 pub created_at: i32,
87 /// The name of the assistant. The maximum length is 256 characters.
88 pub name: Option<String>,
89 /// The description of the assistant. The maximum length is 512 characters.
90 pub description: Option<String>,
91 pub model: String,
92 /// The system instructions that the assistant uses. The maximum length is 256,000 characters.
93 pub instructions: Option<String>,
94 /// A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types `code_interpreter`, `file_search`, or `function`.
95 pub tools: Vec<AssistantTools>,
96
97 /// A set of resources that are used by the assistant's tools. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs.
98 pub tool_resources: Option<AssistantToolResources>,
99 /// Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.
100 pub metadata: Option<HashMap<String, serde_json::Value>>,
101
102 /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
103 pub temperature: Option<f32>,
104
105 /// 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.
106 ///
107 /// We generally recommend altering this or temperature but not both.
108 pub top_p: Option<f32>,
109
110 pub response_format: Option<AssistantsApiResponseFormatOption>,
111}
112
113/// Specifies the format that the model must output. Compatible with [GPT-4o](https://platform.openai.com/docs/models/gpt-4o), [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4), and all GPT-3.5 Turbo models since `gpt-3.5-turbo-1106`.
114///
115/// Setting to `{ "type": "json_schema", "json_schema": {...} }` enables Structured Outputs which guarantees the model will match your supplied JSON schema. Learn more in the [Structured Outputs guide](https://platform.openai.com/docs/guides/structured-outputs).
116///
117/// Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the message the model generates is valid JSON.
118///
119/// **Important:** when using JSON mode, you **must** also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if `finish_reason="length"`, which indicates the generation exceeded `max_tokens` or the conversation exceeded the max context length.
120#[derive(Clone, Serialize, Debug, Deserialize, PartialEq, Default)]
121pub enum AssistantsApiResponseFormatOption {
122 #[default]
123 #[serde(rename = "auto")]
124 Auto,
125 #[serde(untagged)]
126 Format(ResponseFormat),
127}
128
129/// Retrieval tool
130#[derive(Clone, Serialize, Debug, Default, Deserialize, PartialEq)]
131pub struct AssistantToolsFileSearch {
132 /// Overrides for the file search tool.
133 #[serde(skip_serializing_if = "Option::is_none")]
134 pub file_search: Option<AssistantToolsFileSearchOverrides>,
135}
136
137#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
138pub struct AssistantToolsFileSearchOverrides {
139 /// The maximum number of results the file search tool should output. The default is 20 for gpt-4* models and 5 for gpt-3.5-turbo. This number should be between 1 and 50 inclusive.
140 ///
141 //// Note that the file search tool may output fewer than `max_num_results` results. See the [file search tool documentation](https://platform.openai.com/docs/assistants/tools/file-search/customizing-file-search-settings) for more information.
142 pub max_num_results: Option<u8>,
143 pub ranking_options: Option<FileSearchRankingOptions>,
144}
145
146#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
147pub enum FileSearchRanker {
148 #[serde(rename = "auto")]
149 Auto,
150 #[serde(rename = "default_2024_08_21")]
151 Default2024_08_21,
152}
153
154/// The ranking options for the file search.
155///
156/// See the [file search tool documentation](/docs/assistants/tools/file-search/customizing-file-search-settings) for more information.
157#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
158pub struct FileSearchRankingOptions {
159 /// The ranker to use for the file search. If not specified will use the `auto` ranker.
160 pub ranker: Option<FileSearchRanker>,
161 /// The score threshold for the file search. All values must be a floating point number between 0 and 1.
162 pub score_threshold: Option<f32>,
163}
164
165/// Function tool
166#[derive(Clone, Serialize, Debug, Default, Deserialize, PartialEq)]
167pub struct AssistantToolsFunction {
168 pub function: FunctionObject,
169}
170
171#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
172#[serde(tag = "type")]
173#[serde(rename_all = "snake_case")]
174pub enum AssistantTools {
175 CodeInterpreter,
176 FileSearch(AssistantToolsFileSearch),
177 Function(AssistantToolsFunction),
178}
179
180#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
181#[builder(name = "CreateAssistantRequestArgs")]
182#[builder(pattern = "mutable")]
183#[builder(setter(into, strip_option), default)]
184#[builder(derive(Debug))]
185#[builder(build_fn(error = "OpenAIError"))]
186pub struct CreateAssistantRequest {
187 /// ID of the model to use. You can use the [List models](https://platform.openai.com/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](https://platform.openai.com/docs/models/overview) for descriptions of them.
188 pub model: String,
189
190 /// The name of the assistant. The maximum length is 256 characters.
191 #[serde(skip_serializing_if = "Option::is_none")]
192 pub name: Option<String>,
193
194 /// The description of the assistant. The maximum length is 512 characters.
195 #[serde(skip_serializing_if = "Option::is_none")]
196 pub description: Option<String>,
197
198 /// The system instructions that the assistant uses. The maximum length is 256,000 characters.
199 #[serde(skip_serializing_if = "Option::is_none")]
200 pub instructions: Option<String>,
201
202 /// A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types `code_interpreter`, `file_search`, or `function`.
203 #[serde(skip_serializing_if = "Option::is_none")]
204 pub tools: Option<Vec<AssistantTools>>,
205
206 /// A set of resources that are used by the assistant's tools. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs.
207 #[serde(skip_serializing_if = "Option::is_none")]
208 pub tool_resources: Option<CreateAssistantToolResources>,
209
210 #[serde(skip_serializing_if = "Option::is_none")]
211 pub metadata: Option<HashMap<String, serde_json::Value>>,
212
213 /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
214 #[serde(skip_serializing_if = "Option::is_none")]
215 pub temperature: Option<f32>,
216
217 /// 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.
218 ///
219 /// We generally recommend altering this or temperature but not both.
220 #[serde(skip_serializing_if = "Option::is_none")]
221 pub top_p: Option<f32>,
222
223 #[serde(skip_serializing_if = "Option::is_none")]
224 pub response_format: Option<AssistantsApiResponseFormatOption>,
225}
226
227#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
228#[builder(name = "ModifyAssistantRequestArgs")]
229#[builder(pattern = "mutable")]
230#[builder(setter(into, strip_option), default)]
231#[builder(derive(Debug))]
232#[builder(build_fn(error = "OpenAIError"))]
233pub struct ModifyAssistantRequest {
234 /// ID of the model to use. You can use the [List models](https://platform.openai.com/docs/api-reference/models/list) API to see all of your available models, or see our [Model overview](https://platform.openai.com/docs/models/overview) for descriptions of them.
235 #[serde(skip_serializing_if = "Option::is_none")]
236 pub model: Option<String>,
237
238 /// The name of the assistant. The maximum length is 256 characters.
239 #[serde(skip_serializing_if = "Option::is_none")]
240 pub name: Option<String>,
241
242 /// The description of the assistant. The maximum length is 512 characters.
243 #[serde(skip_serializing_if = "Option::is_none")]
244 pub description: Option<String>,
245
246 /// The system instructions that the assistant uses. The maximum length is 256,000 characters.
247 #[serde(skip_serializing_if = "Option::is_none")]
248 pub instructions: Option<String>,
249
250 /// A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types `code_interpreter`, `file_search`, or `function`.
251 #[serde(skip_serializing_if = "Option::is_none")]
252 pub tools: Option<Vec<AssistantTools>>,
253
254 /// A set of resources that are used by the assistant's tools. The resources are specific to the type of tool. For example, the `code_interpreter` tool requires a list of file IDs, while the `file_search` tool requires a list of vector store IDs.
255 #[serde(skip_serializing_if = "Option::is_none")]
256 pub tool_resources: Option<AssistantToolResources>,
257 /// Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.
258 #[serde(skip_serializing_if = "Option::is_none")]
259 pub metadata: Option<HashMap<String, serde_json::Value>>,
260
261 /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
262 #[serde(skip_serializing_if = "Option::is_none")]
263 pub temperature: Option<f32>,
264
265 /// 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.
266 ///
267 /// We generally recommend altering this or temperature but not both.
268 #[serde(skip_serializing_if = "Option::is_none")]
269 pub top_p: Option<f32>,
270
271 #[serde(skip_serializing_if = "Option::is_none")]
272 pub response_format: Option<AssistantsApiResponseFormatOption>,
273}
274
275#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
276pub struct DeleteAssistantResponse {
277 pub id: String,
278 pub deleted: bool,
279 pub object: String,
280}
281
282#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
283pub struct ListAssistantsResponse {
284 pub object: String,
285 pub data: Vec<AssistantObject>,
286 pub first_id: Option<String>,
287 pub last_id: Option<String>,
288 pub has_more: bool,
289}
290
291/// Controls which (if any) tool is called by the model.
292/// `none` means the model will not call any tools and instead generates a message.
293/// `auto` is the default value and means the model can pick between generating a message or calling one or more tools.
294/// `required` means the model must call one or more tools before responding to the user.
295/// Specifying a particular tool like `{"type": "file_search"}` or `{"type": "function", "function": {"name": "my_function"}}` forces the model to call that tool.
296#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
297#[serde(rename_all = "lowercase")]
298pub enum AssistantsApiToolChoiceOption {
299 #[default]
300 None,
301 Auto,
302 Required,
303 #[serde(untagged)]
304 Named(AssistantsNamedToolChoice),
305}
306
307/// Specifies a tool the model should use. Use to force the model to call a specific tool.
308#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
309pub struct AssistantsNamedToolChoice {
310 /// The type of the tool. If type is `function`, the function name must be set
311 pub r#type: AssistantToolType,
312
313 pub function: Option<FunctionName>,
314}
315
316#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
317#[serde(rename_all = "snake_case")]
318pub enum AssistantToolType {
319 #[default]
320 Function,
321 CodeInterpreter,
322 FileSearch,
323}