Skip to content

Commit c920edc

Browse files
authored
feat: add conversations api (#460)
* conversations API * convversations example * update to types * update to example * explicit ConversationItem * move conversation inside responses * update examples/conversations * update example
1 parent 9a61c41 commit c920edc

File tree

12 files changed

+618
-6
lines changed

12 files changed

+618
-6
lines changed

async-openai/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- [x] Batch
2929
- [x] Chat
3030
- [x] Completions (Legacy)
31+
- [x] Conversations
3132
- [x] Embeddings
3233
- [x] Files
3334
- [x] Fine-Tuning

async-openai/src/client.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ use crate::{
1313
image::Images,
1414
moderation::Moderations,
1515
traits::AsyncTryFrom,
16-
Assistants, Audio, AuditLogs, Batches, Chat, Completions, Embeddings, FineTuning, Invites,
17-
Models, Projects, Responses, Threads, Uploads, Users, VectorStores, Videos,
16+
Assistants, Audio, AuditLogs, Batches, Chat, Completions, Conversations, Embeddings,
17+
FineTuning, Invites, Models, Projects, Responses, Threads, Uploads, Users, VectorStores,
18+
Videos,
1819
};
1920

2021
#[derive(Debug, Clone, Default)]
@@ -172,6 +173,11 @@ impl<C: Config> Client<C> {
172173
Responses::new(self)
173174
}
174175

176+
/// To call [Conversations] group related APIs using this client.
177+
pub fn conversations(&self) -> Conversations<'_, C> {
178+
Conversations::new(self)
179+
}
180+
175181
pub fn config(&self) -> &C {
176182
&self.config
177183
}

async-openai/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl Config for OpenAIConfig {
136136

137137
// hack for Assistants APIs
138138
// Calls to the Assistants API require that you pass a Beta header
139-
headers.insert(OPENAI_BETA_HEADER, "assistants=v2".parse().unwrap());
139+
// headers.insert(OPENAI_BETA_HEADER, "assistants=v2".parse().unwrap());
140140

141141
headers
142142
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use serde::Serialize;
2+
3+
use crate::{
4+
config::Config,
5+
error::OpenAIError,
6+
types::responses::{
7+
ConversationItem, ConversationItemList, ConversationResource,
8+
CreateConversationItemsRequest,
9+
},
10+
Client,
11+
};
12+
13+
/// Conversation items represent items within a conversation.
14+
pub struct ConversationItems<'c, C: Config> {
15+
client: &'c Client<C>,
16+
pub conversation_id: String,
17+
}
18+
19+
impl<'c, C: Config> ConversationItems<'c, C> {
20+
pub fn new(client: &'c Client<C>, conversation_id: &str) -> Self {
21+
Self {
22+
client,
23+
conversation_id: conversation_id.into(),
24+
}
25+
}
26+
27+
/// Create items in a conversation.
28+
#[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
29+
pub async fn create(
30+
&self,
31+
request: CreateConversationItemsRequest,
32+
) -> Result<ConversationItemList, OpenAIError> {
33+
self.client
34+
.post(
35+
&format!("/conversations/{}/items", &self.conversation_id),
36+
request,
37+
)
38+
.await
39+
}
40+
41+
/// List all items for a conversation.
42+
#[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
43+
pub async fn list<Q>(&self, query: &Q) -> Result<ConversationItemList, OpenAIError>
44+
where
45+
Q: Serialize + ?Sized,
46+
{
47+
self.client
48+
.get_with_query(
49+
&format!("/conversations/{}/items", &self.conversation_id),
50+
&query,
51+
)
52+
.await
53+
}
54+
55+
/// Retrieve an item from a conversation.
56+
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
57+
pub async fn retrieve(&self, item_id: &str) -> Result<ConversationItem, OpenAIError> {
58+
self.client
59+
.get(&format!(
60+
"/conversations/{}/items/{item_id}",
61+
&self.conversation_id
62+
))
63+
.await
64+
}
65+
66+
/// Delete an item from a conversation.
67+
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
68+
pub async fn delete(&self, item_id: &str) -> Result<ConversationResource, OpenAIError> {
69+
self.client
70+
.delete(&format!(
71+
"/conversations/{}/items/{item_id}",
72+
&self.conversation_id
73+
))
74+
.await
75+
}
76+
}

async-openai/src/conversations.rs

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

async-openai/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ mod chat;
148148
mod client;
149149
mod completion;
150150
pub mod config;
151+
mod conversation_items;
152+
mod conversations;
151153
mod download;
152154
mod embedding;
153155
pub mod error;
@@ -183,6 +185,8 @@ pub use batches::Batches;
183185
pub use chat::Chat;
184186
pub use client::Client;
185187
pub use completion::Completions;
188+
pub use conversation_items::ConversationItems;
189+
pub use conversations::Conversations;
186190
pub use embedding::Embeddings;
187191
pub use file::Files;
188192
pub use fine_tuning::FineTuning;

async-openai/src/types/common.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::path::PathBuf;
1+
use std::{collections::HashMap, path::PathBuf};
22

33
use bytes::Bytes;
44
use serde::{Deserialize, Serialize};
@@ -16,3 +16,19 @@ pub enum OrganizationRole {
1616
Owner,
1717
Reader,
1818
}
19+
20+
/// Set of 16 key-value pairs that can be attached to an object.
21+
/// This can be useful for storing additional information about the
22+
/// object in a structured format, and querying for objects via API
23+
/// or the dashboard. Keys are strings with a maximum length of 64
24+
/// characters. Values are strings with a maximum length of 512
25+
/// characters.
26+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
27+
#[serde(transparent)]
28+
pub struct Metadata(HashMap<String, String>);
29+
30+
impl From<HashMap<String, String>> for Metadata {
31+
fn from(value: HashMap<String, String>) -> Self {
32+
Self(value)
33+
}
34+
}

0 commit comments

Comments
 (0)