Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions async-openai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- [x] Batch
- [x] Chat
- [x] Completions (Legacy)
- [x] Conversations
- [x] Embeddings
- [x] Files
- [x] Fine-Tuning
Expand Down
10 changes: 8 additions & 2 deletions async-openai/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ use crate::{
image::Images,
moderation::Moderations,
traits::AsyncTryFrom,
Assistants, Audio, AuditLogs, Batches, Chat, Completions, Embeddings, FineTuning, Invites,
Models, Projects, Responses, Threads, Uploads, Users, VectorStores, Videos,
Assistants, Audio, AuditLogs, Batches, Chat, Completions, Conversations, Embeddings,
FineTuning, Invites, Models, Projects, Responses, Threads, Uploads, Users, VectorStores,
Videos,
};

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

/// To call [Conversations] group related APIs using this client.
pub fn conversations(&self) -> Conversations<'_, C> {
Conversations::new(self)
}

pub fn config(&self) -> &C {
&self.config
}
Expand Down
2 changes: 1 addition & 1 deletion async-openai/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Config for OpenAIConfig {

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

headers
}
Expand Down
76 changes: 76 additions & 0 deletions async-openai/src/conversation_items.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use serde::Serialize;

use crate::{
config::Config,
error::OpenAIError,
types::responses::{
ConversationItem, ConversationItemList, ConversationResource,
CreateConversationItemsRequest,
},
Client,
};

/// Conversation items represent items within a conversation.
pub struct ConversationItems<'c, C: Config> {
client: &'c Client<C>,
pub conversation_id: String,
}

impl<'c, C: Config> ConversationItems<'c, C> {
pub fn new(client: &'c Client<C>, conversation_id: &str) -> Self {
Self {
client,
conversation_id: conversation_id.into(),
}
}

/// Create items in a conversation.
#[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
pub async fn create(
&self,
request: CreateConversationItemsRequest,
) -> Result<ConversationItemList, OpenAIError> {
self.client
.post(
&format!("/conversations/{}/items", &self.conversation_id),
request,
)
.await
}

/// List all items for a conversation.
#[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
pub async fn list<Q>(&self, query: &Q) -> Result<ConversationItemList, OpenAIError>
where
Q: Serialize + ?Sized,
{
self.client
.get_with_query(
&format!("/conversations/{}/items", &self.conversation_id),
&query,
)
.await
}

/// Retrieve an item from a conversation.
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
pub async fn retrieve(&self, item_id: &str) -> Result<ConversationItem, OpenAIError> {
self.client
.get(&format!(
"/conversations/{}/items/{item_id}",
&self.conversation_id
))
.await
}

/// Delete an item from a conversation.
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
pub async fn delete(&self, item_id: &str) -> Result<ConversationResource, OpenAIError> {
self.client
.delete(&format!(
"/conversations/{}/items/{item_id}",
&self.conversation_id
))
.await
}
}
68 changes: 68 additions & 0 deletions async-openai/src/conversations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crate::{
config::Config,
conversation_items::ConversationItems,
error::OpenAIError,
types::responses::{
ConversationResource, CreateConversationRequest, DeleteConversationResponse,
UpdateConversationRequest,
},
Client,
};

pub struct Conversations<'c, C: Config> {
client: &'c Client<C>,
}

impl<'c, C: Config> Conversations<'c, C> {
pub fn new(client: &'c Client<C>) -> Self {
Self { client }
}

/// [ConversationItems] API group
pub fn items(&self, conversation_id: &str) -> ConversationItems<'_, C> {
ConversationItems::new(self.client, conversation_id)
}

/// Create a conversation.
#[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
pub async fn create(
&self,
request: CreateConversationRequest,
) -> Result<ConversationResource, OpenAIError> {
self.client.post("/conversations", request).await
}

/// Retrieves a conversation.
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
pub async fn retrieve(
&self,
conversation_id: &str,
) -> Result<ConversationResource, OpenAIError> {
self.client
.get(&format!("/conversations/{conversation_id}"))
.await
}

/// Delete a conversation. Items in the conversation will not be deleted.
#[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
pub async fn delete(
&self,
conversation_id: &str,
) -> Result<DeleteConversationResponse, OpenAIError> {
self.client
.delete(&format!("/conversations/{conversation_id}"))
.await
}

/// Update a conversation.
#[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
pub async fn update(
&self,
conversation_id: &str,
request: UpdateConversationRequest,
) -> Result<ConversationResource, OpenAIError> {
self.client
.post(&format!("/conversations/{conversation_id}"), request)
.await
}
}
4 changes: 4 additions & 0 deletions async-openai/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ mod chat;
mod client;
mod completion;
pub mod config;
mod conversation_items;
mod conversations;
mod download;
mod embedding;
pub mod error;
Expand Down Expand Up @@ -183,6 +185,8 @@ pub use batches::Batches;
pub use chat::Chat;
pub use client::Client;
pub use completion::Completions;
pub use conversation_items::ConversationItems;
pub use conversations::Conversations;
pub use embedding::Embeddings;
pub use file::Files;
pub use fine_tuning::FineTuning;
Expand Down
18 changes: 17 additions & 1 deletion async-openai/src/types/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::{collections::HashMap, path::PathBuf};

use bytes::Bytes;
use serde::{Deserialize, Serialize};
Expand All @@ -16,3 +16,19 @@ pub enum OrganizationRole {
Owner,
Reader,
}

/// Set of 16 key-value pairs that can be attached to an object.
/// This can be useful for storing additional information about the
/// object in a structured format, and querying for objects via API
/// or the dashboard. Keys are strings with a maximum length of 64
/// characters. Values are strings with a maximum length of 512
/// characters.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
#[serde(transparent)]
pub struct Metadata(HashMap<String, String>);

impl From<HashMap<String, String>> for Metadata {
fn from(value: HashMap<String, String>) -> Self {
Self(value)
}
}
Loading
Loading