Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for organization API #265

Merged
merged 12 commits into from
Nov 11, 2024
48 changes: 48 additions & 0 deletions async-openai/src/invites.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use serde::Serialize;

use crate::{
config::Config,
error::OpenAIError,
types::{Invite, InviteDeleteResponse, InviteListResponse, InviteRequest},
Client,
};

/// Invite and manage invitations for an organization. Invited users are automatically added to the Default project.
pub struct Invites<'c, C: Config> {
client: &'c Client<C>,
}

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

/// Returns a list of runs belonging to a thread.
pub async fn list<Q>(&self, query: &Q) -> Result<InviteListResponse, OpenAIError>
where
Q: Serialize + ?Sized,
{
self.client
.get_with_query("/organization/invites", query)
.await
}

/// Retrieves an invite.
pub async fn retrieve(&self, invite_id: &str) -> Result<Invite, OpenAIError> {
self.client
.get(format!("/organization/invites/{invite_id}").as_str())
.await
}

/// Create an invite for a user to the organization. The invite must be accepted by the user before they have access to the organization.
pub async fn create(&self, request: InviteRequest) -> Result<Invite, OpenAIError> {
self.client.post("/organization/invites", request).await
}

/// Delete an invite. If the invite has already been accepted, it cannot be deleted.
pub async fn delete(&self, invite_id: &str) -> Result<InviteDeleteResponse, OpenAIError> {
self.client
.delete(format!("/organization/invites/{invite_id}").as_str())
.await
}
}
8 changes: 8 additions & 0 deletions async-openai/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,18 @@ pub mod error;
mod file;
mod fine_tuning;
mod image;
mod invites;
mod message_files;
mod messages;
mod model;
mod moderation;
mod project_users;
mod projects;
mod runs;
mod steps;
mod threads;
pub mod types;
mod users;
mod util;
mod vector_store_file_batches;
mod vector_store_files;
Expand All @@ -115,13 +119,17 @@ pub use embedding::Embeddings;
pub use file::Files;
pub use fine_tuning::FineTuning;
pub use image::Images;
pub use invites::Invites;
pub use message_files::MessageFiles;
pub use messages::Messages;
pub use model::Models;
pub use moderation::Moderations;
pub use project_users::ProjectUsers;
pub use projects::Projects;
pub use runs::Runs;
pub use steps::Steps;
pub use threads::Threads;
pub use users::Users;
pub use vector_store_file_batches::VectorStoreFileBatches;
pub use vector_store_files::VectorStoreFiles;
pub use vector_stores::VectorStores;
89 changes: 89 additions & 0 deletions async-openai/src/project_users.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use serde::Serialize;

use crate::{
config::Config,
error::OpenAIError,
types::{
ProjectUser, ProjectUserCreateRequest, ProjectUserListResponse, ProjectUserUpdateRequest,
},
Client,
};

/// Manage users within a project, including adding, updating roles, and removing users.
/// Users cannot be removed from the Default project, unless they are being removed from the organization.
pub struct ProjectUsers<'c, C: Config> {
client: &'c Client<C>,
}

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

/// Returns a list of users in the project.
pub async fn list<Q>(
&self,
project_id: String,
query: &Q,
) -> Result<ProjectUserListResponse, OpenAIError>
where
Q: Serialize + ?Sized,
{
self.client
.get_with_query(
format!("/organization/projects/{project_id}/users").as_str(),
query,
)
.await
}

/// Adds a user to the project. Users must already be members of the organization to be added to a project.
pub async fn create(
&self,
project_id: String,
request: ProjectUserCreateRequest,
) -> Result<ProjectUser, OpenAIError> {
self.client
.post(
format!("/organization/projects/{project_id}/users").as_str(),
request,
)
.await
}

/// Retrieves a user in the project.
pub async fn retrieve(
&self,
project_id: String,
user_id: String,
) -> Result<ProjectUser, OpenAIError> {
self.client
.get(format!("/organization/projects/{project_id}/users/{user_id}").as_str())
.await
}

/// Modifies a user's role in the project.
pub async fn modify(
&self,
project_id: String,
request: ProjectUserUpdateRequest,
) -> Result<ProjectUser, OpenAIError> {
self.client
.post(
format!("/organization/projects/{project_id}").as_str(),
request,
)
.await
}

/// Deletes a user from the project.
pub async fn delete(
&self,
project_id: String,
user_id: String,
) -> Result<ProjectUser, OpenAIError> {
self.client
.delete(format!("/organization/projects/{project_id}/users/{user_id}").as_str())
.await
}
}
63 changes: 63 additions & 0 deletions async-openai/src/projects.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use serde::Serialize;

use crate::{
config::Config,
error::OpenAIError,
types::{Project, ProjectCreateRequest, ProjectListResponse, ProjectUpdateRequest},
Client,
};

/// Manage the projects within an organization includes creation, updating, and archiving or projects.
/// The Default project cannot be modified or archived.
pub struct Projects<'c, C: Config> {
client: &'c Client<C>,
}

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

/// Returns a list of projects.
pub async fn list<Q>(&self, query: &Q) -> Result<ProjectListResponse, OpenAIError>
where
Q: Serialize + ?Sized,
{
self.client
.get_with_query("/organization/projects", query)
.await
}

/// Create a new project in the organization. Projects can be created and archived, but cannot be deleted.
pub async fn create(&self, request: ProjectCreateRequest) -> Result<Project, OpenAIError> {
self.client.post("/organization/projects", request).await
}

/// Retrieves a project.
pub async fn retrieve(&self, project_id: String) -> Result<Project, OpenAIError> {
self.client
.get(format!("/organization/projects/{project_id}").as_str())
.await
}

/// Modifies a project in the organization.
pub async fn modify(
&self,
project_id: String,
request: ProjectUpdateRequest,
) -> Result<Project, OpenAIError> {
self.client
.post(
format!("/organization/projects/{project_id}").as_str(),
request,
)
.await
}

/// Archives a project in the organization. Archived projects cannot be used or updated.
pub async fn archive(&self, project_id: String) -> Result<Project, OpenAIError> {
self.client
.post(format!("/organization/projects/{project_id}").as_str(), ())
.await
}
}
8 changes: 8 additions & 0 deletions async-openai/src/types/common.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
use std::path::PathBuf;

use bytes::Bytes;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq)]
pub enum InputSource {
Path { path: PathBuf },
Bytes { filename: String, bytes: Bytes },
VecU8 { filename: String, vec: Vec<u8> },
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum OrganizationRole {
Owner,
Reader,
}
66 changes: 66 additions & 0 deletions async-openai/src/types/invites.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::types::OpenAIError;
use derive_builder::Builder;
use serde::{Deserialize, Serialize};

use super::OrganizationRole;

#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum InviteStatus {
Accepted,
Expired,
Pending,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Builder)]
#[builder(name = "ProjectCreateRequestArgs")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option))]
#[builder(derive(Debug))]
#[builder(build_fn(error = "OpenAIError"))]
#[serde(rename_all = "snake_case")]
pub struct InviteRequest {
email: String,
role: OrganizationRole,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct InviteListResponse {
object: String,
data: Vec<Invite>,
first_id: Option<String>,
last_id: Option<String>,
has_more: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct InviteDeleteResponse {
/// The object type, which is always `organization.invite.deleted`
object: String,
id: String,
deleted: bool,
}

/// Represents an individual `invite` to the organization.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub struct Invite {
/// The object type, which is always `organization.invite`
object: String,
/// The identifier, which can be referenced in API endpoints
id: String,
/// The email address of the individual to whom the invite was sent
email: String,
/// `owner` or `reader`
role: OrganizationRole,
/// `accepted`, `expired`, or `pending`
status: InviteStatus,
/// The Unix timestamp (in seconds) of when the invite was sent.
invited_at: u32,
/// The Unix timestamp (in seconds) of when the invite expires.
expires_at: u32,
/// The Unix timestamp (in seconds) of when the invite was accepted.
accepted_at: u32,
}
8 changes: 8 additions & 0 deletions async-openai/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ mod embedding;
mod file;
mod fine_tuning;
mod image;
mod invites;
mod message;
mod message_file;
mod model;
mod moderation;
mod project_users;
mod projects;
#[cfg_attr(docsrs, doc(cfg(feature = "realtime")))]
#[cfg(feature = "realtime")]
pub mod realtime;
mod run;
mod step;
mod thread;
mod users;
mod vector_store;

pub use assistant::*;
Expand All @@ -37,13 +41,17 @@ pub use embedding::*;
pub use file::*;
pub use fine_tuning::*;
pub use image::*;
pub use invites::*;
pub use message::*;
pub use message_file::*;
pub use model::*;
pub use moderation::*;
pub use project_users::*;
pub use projects::*;
pub use run::*;
pub use step::*;
pub use thread::*;
pub use users::*;
pub use vector_store::*;

mod impls;
Expand Down
Loading