async_openai/
assistant_files.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use serde::Serialize;

use crate::{
    config::Config,
    error::OpenAIError,
    types::{
        AssistantFileObject, CreateAssistantFileRequest, DeleteAssistantFileResponse,
        ListAssistantFilesResponse,
    },
    Client,
};

/// Files attached to an assistant.
pub struct AssistantFiles<'c, C: Config> {
    client: &'c Client<C>,
    pub assistant_id: String,
}

impl<'c, C: Config> AssistantFiles<'c, C> {
    pub fn new(client: &'c Client<C>, assistant_id: &str) -> Self {
        Self {
            client,
            assistant_id: assistant_id.into(),
        }
    }

    /// 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).
    pub async fn create(
        &self,
        request: CreateAssistantFileRequest,
    ) -> Result<AssistantFileObject, OpenAIError> {
        self.client
            .post(&format!("/assistants/{}/files", self.assistant_id), request)
            .await
    }

    /// Retrieves an AssistantFile.
    pub async fn retrieve(&self, file_id: &str) -> Result<AssistantFileObject, OpenAIError> {
        self.client
            .get(&format!(
                "/assistants/{}/files/{file_id}",
                self.assistant_id
            ))
            .await
    }

    /// Delete an assistant file.
    pub async fn delete(&self, file_id: &str) -> Result<DeleteAssistantFileResponse, OpenAIError> {
        self.client
            .delete(&format!(
                "/assistants/{}/files/{file_id}",
                self.assistant_id
            ))
            .await
    }

    /// Returns a list of assistant files.
    pub async fn list<Q>(&self, query: &Q) -> Result<ListAssistantFilesResponse, OpenAIError>
    where
        Q: Serialize + ?Sized,
    {
        self.client
            .get_with_query(&format!("/assistants/{}/files", self.assistant_id), query)
            .await
    }
}