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