dynamo_async_openai/types/
thread.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Based on https://github.com/64bit/async-openai/ by Himanshu Neema
5// Original Copyright (c) 2022 Himanshu Neema
6// Licensed under MIT License (see ATTRIBUTIONS-Rust.md)
7//
8// Modifications Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
9// Licensed under Apache 2.0
10
11use std::collections::HashMap;
12
13use derive_builder::Builder;
14use serde::{Deserialize, Serialize};
15
16use crate::error::OpenAIError;
17
18use super::{
19    AssistantToolResources, AssistantTools, AssistantsApiResponseFormatOption,
20    AssistantsApiToolChoiceOption, CreateAssistantToolResources, CreateMessageRequest,
21    TruncationObject,
22};
23
24/// Represents a thread that contains [messages](https://platform.openai.com/docs/api-reference/messages).
25#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
26pub struct ThreadObject {
27    /// The identifier, which can be referenced in API endpoints.
28    pub id: String,
29    /// The object type, which is always `thread`.
30    pub object: String,
31    /// The Unix timestamp (in seconds) for when the thread was created.
32    pub created_at: i32,
33
34    /// A set of resources that are made available to the assistant's tools in this thread. 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.
35    pub tool_resources: Option<AssistantToolResources>,
36
37    pub metadata: Option<HashMap<String, serde_json::Value>>,
38}
39
40#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
41#[builder(name = "CreateThreadRequestArgs")]
42#[builder(pattern = "mutable")]
43#[builder(setter(into, strip_option), default)]
44#[builder(derive(Debug))]
45#[builder(build_fn(error = "OpenAIError"))]
46pub struct CreateThreadRequest {
47    /// A list of [messages](https://platform.openai.com/docs/api-reference/messages) to start the thread with.
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub messages: Option<Vec<CreateMessageRequest>>,
50
51    /// A set of resources that are made available to the assistant's tools in this thread. 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.
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub tool_resources: Option<CreateAssistantToolResources>,
54
55    #[serde(skip_serializing_if = "Option::is_none")]
56    pub metadata: Option<HashMap<String, serde_json::Value>>,
57}
58
59#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
60pub struct ModifyThreadRequest {
61    #[serde(skip_serializing_if = "Option::is_none")]
62    pub metadata: Option<HashMap<String, serde_json::Value>>,
63
64    /// A set of resources that are made available to the assistant's tools in this thread. 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.
65    #[serde(skip_serializing_if = "Option::is_none")]
66    pub tool_resources: Option<AssistantToolResources>,
67}
68
69#[derive(Clone, Serialize, Default, Debug, Deserialize, PartialEq)]
70pub struct DeleteThreadResponse {
71    pub id: String,
72    pub deleted: bool,
73    pub object: String,
74}
75
76#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
77#[builder(name = "CreateThreadAndRunRequestArgs")]
78#[builder(pattern = "mutable")]
79#[builder(setter(into, strip_option), default)]
80#[builder(derive(Debug))]
81#[builder(build_fn(error = "OpenAIError"))]
82pub struct CreateThreadAndRunRequest {
83    /// The ID of the [assistant](https://platform.openai.com/docs/api-reference/assistants) to use to execute this run.
84    pub assistant_id: String,
85
86    /// If no thread is provided, an empty thread will be created.
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub thread: Option<CreateThreadRequest>,
89
90    /// 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.
91    #[serde(skip_serializing_if = "Option::is_none")]
92    pub model: Option<String>,
93
94    /// Override the default system message of the assistant. This is useful for modifying the behavior on a per-run basis.
95    #[serde(skip_serializing_if = "Option::is_none")]
96    pub instructions: Option<String>,
97
98    /// Override the tools the assistant can use for this run. This is useful for modifying the 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 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.
103    #[serde(skip_serializing_if = "Option::is_none")]
104    pub tool_resources: Option<AssistantToolResources>,
105
106    #[serde(skip_serializing_if = "Option::is_none")]
107    pub metadata: Option<HashMap<String, serde_json::Value>>,
108
109    /// 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.
110    #[serde(skip_serializing_if = "Option::is_none")]
111    pub temperature: Option<f32>,
112
113    ///  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.
114    ///
115    /// We generally recommend altering this or temperature but not both.
116    #[serde(skip_serializing_if = "Option::is_none")]
117    pub top_p: Option<f32>,
118
119    /// 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.
120    #[serde(skip_serializing_if = "Option::is_none")]
121    pub stream: Option<bool>,
122
123    /// 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.
124    #[serde(skip_serializing_if = "Option::is_none")]
125    pub max_prompt_tokens: Option<u32>,
126
127    /// 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.
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub max_completion_tokens: Option<u32>,
130
131    /// Controls for how a thread will be truncated prior to the run. Use this to control the intial context window of the run.
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub truncation_strategy: Option<TruncationObject>,
134
135    #[serde(skip_serializing_if = "Option::is_none")]
136    pub tool_choice: Option<AssistantsApiToolChoiceOption>,
137
138    /// Whether to enable [parallel function calling](https://platform.openai.com/docs/guides/function-calling/parallel-function-calling) during tool use.
139    #[serde(skip_serializing_if = "Option::is_none")]
140    pub parallel_tool_calls: Option<bool>,
141
142    #[serde(skip_serializing_if = "Option::is_none")]
143    pub response_format: Option<AssistantsApiResponseFormatOption>,
144}