async_openai/responses/
conversations.rs

1use crate::{
2    config::Config,
3    error::OpenAIError,
4    types::responses::{
5        ConversationResource, CreateConversationRequest, DeleteConversationResponse,
6        UpdateConversationRequest,
7    },
8    Client, ConversationItems, RequestOptions,
9};
10
11pub struct Conversations<'c, C: Config> {
12    client: &'c Client<C>,
13    pub(crate) request_options: RequestOptions,
14}
15
16impl<'c, C: Config> Conversations<'c, C> {
17    pub fn new(client: &'c Client<C>) -> Self {
18        Self {
19            client,
20            request_options: RequestOptions::new(),
21        }
22    }
23
24    /// [ConversationItems] API group
25    pub fn items(&self, conversation_id: &str) -> ConversationItems<'_, C> {
26        ConversationItems::new(self.client, conversation_id)
27    }
28
29    /// Create a conversation.
30    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
31    pub async fn create(
32        &self,
33        request: CreateConversationRequest,
34    ) -> Result<ConversationResource, OpenAIError> {
35        self.client
36            .post("/conversations", request, &self.request_options)
37            .await
38    }
39
40    /// Retrieves a conversation.
41    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
42    pub async fn retrieve(
43        &self,
44        conversation_id: &str,
45    ) -> Result<ConversationResource, OpenAIError> {
46        self.client
47            .get(
48                &format!("/conversations/{conversation_id}"),
49                &self.request_options,
50            )
51            .await
52    }
53
54    /// Delete a conversation. Items in the conversation will not be deleted.
55    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
56    pub async fn delete(
57        &self,
58        conversation_id: &str,
59    ) -> Result<DeleteConversationResponse, OpenAIError> {
60        self.client
61            .delete(
62                &format!("/conversations/{conversation_id}"),
63                &self.request_options,
64            )
65            .await
66    }
67
68    /// Update a conversation.
69    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
70    pub async fn update(
71        &self,
72        conversation_id: &str,
73        request: UpdateConversationRequest,
74    ) -> Result<ConversationResource, OpenAIError> {
75        self.client
76            .post(
77                &format!("/conversations/{conversation_id}"),
78                request,
79                &self.request_options,
80            )
81            .await
82    }
83}