async_openai/
conversations.rs

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