async_openai_alt/
assistant_files.rs

1use serde::Serialize;
2
3use crate::{
4    config::Config,
5    error::OpenAIError,
6    types::{
7        AssistantFileObject, CreateAssistantFileRequest, DeleteAssistantFileResponse,
8        ListAssistantFilesResponse,
9    },
10    Client,
11};
12
13/// Files attached to an assistant.
14pub struct AssistantFiles<'c, C: Config> {
15    client: &'c Client<C>,
16    pub assistant_id: String,
17}
18
19impl<'c, C: Config> AssistantFiles<'c, C> {
20    pub fn new(client: &'c Client<C>, assistant_id: &str) -> Self {
21        Self {
22            client,
23            assistant_id: assistant_id.into(),
24        }
25    }
26
27    /// Create an assistant file by attaching a [File](https://platform.openai.com/docs/api-reference/files) to an [assistant](https://platform.openai.com/docs/api-reference/assistants).
28    pub async fn create(
29        &self,
30        request: CreateAssistantFileRequest,
31    ) -> Result<AssistantFileObject, OpenAIError> {
32        self.client
33            .post(&format!("/assistants/{}/files", self.assistant_id), request)
34            .await
35    }
36
37    /// Retrieves an AssistantFile.
38    pub async fn retrieve(&self, file_id: &str) -> Result<AssistantFileObject, OpenAIError> {
39        self.client
40            .get(&format!(
41                "/assistants/{}/files/{file_id}",
42                self.assistant_id
43            ))
44            .await
45    }
46
47    /// Delete an assistant file.
48    pub async fn delete(&self, file_id: &str) -> Result<DeleteAssistantFileResponse, OpenAIError> {
49        self.client
50            .delete(&format!(
51                "/assistants/{}/files/{file_id}",
52                self.assistant_id
53            ))
54            .await
55    }
56
57    /// Returns a list of assistant files.
58    pub async fn list<Q>(&self, query: &Q) -> Result<ListAssistantFilesResponse, OpenAIError>
59    where
60        Q: Serialize + ?Sized,
61    {
62        self.client
63            .get_with_query(&format!("/assistants/{}/files", self.assistant_id), query)
64            .await
65    }
66}