async_openai_wasm/
assistants.rs

1use serde::Serialize;
2
3use crate::{
4    Client,
5    config::Config,
6    error::OpenAIError,
7    types::{
8        AssistantObject, CreateAssistantRequest, DeleteAssistantResponse, ListAssistantsResponse,
9        ModifyAssistantRequest,
10    },
11};
12
13/// Build assistants that can call models and use tools to perform tasks.
14///
15/// [Get started with the Assistants API](https://platform.openai.com/docs/assistants)
16pub struct Assistants<'c, C: Config> {
17    client: &'c Client<C>,
18}
19
20impl<'c, C: Config> Assistants<'c, C> {
21    pub fn new(client: &'c Client<C>) -> Self {
22        Self { client }
23    }
24
25    /// Create an assistant with a model and instructions.
26    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
27    pub async fn create(
28        &self,
29        request: CreateAssistantRequest,
30    ) -> Result<AssistantObject, OpenAIError> {
31        self.client.post("/assistants", request).await
32    }
33
34    /// Retrieves an assistant.
35    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
36    pub async fn retrieve(&self, assistant_id: &str) -> Result<AssistantObject, OpenAIError> {
37        self.client
38            .get(&format!("/assistants/{assistant_id}"))
39            .await
40    }
41
42    /// Modifies an assistant.
43    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
44    pub async fn update(
45        &self,
46        assistant_id: &str,
47        request: ModifyAssistantRequest,
48    ) -> Result<AssistantObject, OpenAIError> {
49        self.client
50            .post(&format!("/assistants/{assistant_id}"), request)
51            .await
52    }
53
54    /// Delete an assistant.
55    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
56    pub async fn delete(&self, assistant_id: &str) -> Result<DeleteAssistantResponse, OpenAIError> {
57        self.client
58            .delete(&format!("/assistants/{assistant_id}"))
59            .await
60    }
61
62    /// Returns a list of assistants.
63    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
64    pub async fn list<Q>(&self, query: &Q) -> Result<ListAssistantsResponse, OpenAIError>
65    where
66        Q: Serialize + ?Sized,
67    {
68        self.client.get_with_query("/assistants", &query).await
69    }
70}