Skip to content

Commit

Permalink
spec sync: Uploads + Administration APIs (64bit#286)
Browse files Browse the repository at this point in the history
* uploads API

* moderations API update

* CreateFineTuningJobRequest comment docs updated

* added Audit Logs API

* organization invite API updated

* organization users API updated

* updated project APIs

* fix project archive url

* update Client to create audit_logs, invites, users, and projects API groups

* updated ProjectUsers APIs

* addeds project service account APIs

* ProjectServiceAccounts api group

* added project api keys APIs

* updated README

* updated spec from upstream https://github.com/openai/openai-openapi/blob/d033c364c6574068ee89f3d5f845a4830bddd503/openapi.yaml

* fix PromptTokensDetails and CompletionTokensDetails

* update function-call and function-call-stream examples to use non-deprecated model: gpt-4o-mini

* fix moderation types

* update moderation example

* add missing doc comments

* fix: add missing pub modifier
  • Loading branch information
64bit authored Nov 12, 2024
1 parent ac204a3 commit f388518
Show file tree
Hide file tree
Showing 30 changed files with 9,895 additions and 5,097 deletions.
8 changes: 4 additions & 4 deletions async-openai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
- [x] Images
- [x] Models
- [x] Moderations
- [ ] Organizations | Administration
- [x] Organizations | Administration
- [x] Realtime API types (Beta)
- [ ] Uploads
- [x] Uploads
- SSE streaming on available APIs
- Requests (except SSE streaming) including form submissions are retried with exponential backoff when [rate limited](https://platform.openai.com/docs/guides/rate-limits).
- Ergonomic builder pattern for all request objects.
Expand All @@ -61,8 +61,8 @@ $Env:OPENAI_API_KEY='sk-...'

## Realtime API

Only types for Realtime API are implemented, and can be enabled with feature flag `realtime`
These types may change when OpenAI releases official specs for them.
Only types for Realtime API are implemented, and can be enabled with feature flag `realtime`.
These types may change if/when OpenAI releases official specs for them.

## Image Generation Example

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

use crate::{config::Config, error::OpenAIError, types::ListAuditLogsResponse, Client};

/// Logs of user actions and configuration changes within this organization.
/// To log events, you must activate logging in the [Organization Settings](https://platform.openai.com/settings/organization/general).
/// Once activated, for security reasons, logging cannot be deactivated.
pub struct AuditLogs<'c, C: Config> {
client: &'c Client<C>,
}

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

/// List user actions and configuration changes within this organization.
pub async fn get<Q>(&self, query: &Q) -> Result<ListAuditLogsResponse, OpenAIError>
where
Q: Serialize + ?Sized,
{
self.client
.get_with_query("/organization/audit_logs", query)
.await
}
}
24 changes: 22 additions & 2 deletions async-openai/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::{
file::Files,
image::Images,
moderation::Moderations,
Assistants, Audio, Batches, Chat, Completions, Embeddings, FineTuning, Models, Threads,
VectorStores,
Assistants, Audio, AuditLogs, Batches, Chat, Completions, Embeddings, FineTuning, Invites,
Models, Projects, Threads, Users, VectorStores,
};

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

/// To call [AuditLogs] group related APIs using this client.
pub fn audit_logs(&self) -> AuditLogs<C> {
AuditLogs::new(self)
}

/// To call [Invites] group related APIs using this client.
pub fn invites(&self) -> Invites<C> {
Invites::new(self)
}

/// To call [Users] group related APIs using this client.
pub fn users(&self) -> Users<C> {
Users::new(self)
}

/// To call [Projects] group related APIs using this client.
pub fn projects(&self) -> Projects<C> {
Projects::new(self)
}

pub fn config(&self) -> &C {
&self.config
}
Expand Down
2 changes: 1 addition & 1 deletion async-openai/src/invites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl<'c, C: Config> Invites<'c, C> {
Self { client }
}

/// Returns a list of runs belonging to a thread.
/// Returns a list of invites in the organization.
pub async fn list<Q>(&self, query: &Q) -> Result<InviteListResponse, OpenAIError>
where
Q: Serialize + ?Sized,
Expand Down
8 changes: 8 additions & 0 deletions async-openai/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
mod assistant_files;
mod assistants;
mod audio;
mod audit_logs;
mod batches;
mod chat;
mod client;
Expand All @@ -96,12 +97,15 @@ mod message_files;
mod messages;
mod model;
mod moderation;
mod project_api_keys;
mod project_service_accounts;
mod project_users;
mod projects;
mod runs;
mod steps;
mod threads;
pub mod types;
mod uploads;
mod users;
mod util;
mod vector_store_file_batches;
Expand All @@ -111,6 +115,7 @@ mod vector_stores;
pub use assistant_files::AssistantFiles;
pub use assistants::Assistants;
pub use audio::Audio;
pub use audit_logs::AuditLogs;
pub use batches::Batches;
pub use chat::Chat;
pub use client::Client;
Expand All @@ -124,11 +129,14 @@ pub use message_files::MessageFiles;
pub use messages::Messages;
pub use model::Models;
pub use moderation::Moderations;
pub use project_api_keys::ProjectAPIKeys;
pub use project_service_accounts::ProjectServiceAccounts;
pub use project_users::ProjectUsers;
pub use projects::Projects;
pub use runs::Runs;
pub use steps::Steps;
pub use threads::Threads;
pub use uploads::Uploads;
pub use users::Users;
pub use vector_store_file_batches::VectorStoreFileBatches;
pub use vector_store_files::VectorStoreFiles;
Expand Down
5 changes: 3 additions & 2 deletions async-openai/src/moderation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
Client,
};

/// Given some input text, outputs if the model classifies it as potentially harmful across several categories.
/// Given text and/or image inputs, classifies if those inputs are potentially harmful across several categories.
///
/// Related guide: [Moderations](https://platform.openai.com/docs/guides/moderation)
pub struct Moderations<'c, C: Config> {
Expand All @@ -17,7 +17,8 @@ impl<'c, C: Config> Moderations<'c, C> {
Self { client }
}

/// Classifies if text is potentially harmful.
/// Classifies if text and/or image inputs are potentially harmful. Learn
/// more in the [moderation guide](https://platform.openai.com/docs/guides/moderation).
pub async fn create(
&self,
request: CreateModerationRequest,
Expand Down
63 changes: 63 additions & 0 deletions async-openai/src/project_api_keys.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::{ProjectApiKey, ProjectApiKeyDeleteResponse, ProjectApiKeyListResponse},
Client,
};

/// Manage API keys for a given project. Supports listing and deleting keys for users.
/// This API does not allow issuing keys for users, as users need to authorize themselves to generate keys.
pub struct ProjectAPIKeys<'c, C: Config> {
client: &'c Client<C>,
pub project_id: String,
}

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

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

/// Retrieves an API key in the project.
pub async fn retrieve(&self, api_key: &str) -> Result<ProjectApiKey, OpenAIError> {
self.client
.get(
format!(
"/organization/projects/{}/api_keys/{api_key}",
self.project_id
)
.as_str(),
)
.await
}

/// Deletes an API key from the project.
pub async fn delete(&self, api_key: &str) -> Result<ProjectApiKeyDeleteResponse, OpenAIError> {
self.client
.delete(
format!(
"/organization/projects/{}/api_keys/{api_key}",
self.project_id
)
.as_str(),
)
.await
}
}
96 changes: 96 additions & 0 deletions async-openai/src/project_service_accounts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use serde::Serialize;

use crate::{
config::Config,
error::OpenAIError,
types::{
ProjectServiceAccount, ProjectServiceAccountCreateRequest,
ProjectServiceAccountCreateResponse, ProjectServiceAccountDeleteResponse,
ProjectServiceAccountListResponse,
},
Client,
};

/// Manage service accounts within a project. A service account is a bot user that is not
/// associated with a user. If a user leaves an organization, their keys and membership in projects
/// will no longer work. Service accounts do not have this limitation.
/// However, service accounts can also be deleted from a project.
pub struct ProjectServiceAccounts<'c, C: Config> {
client: &'c Client<C>,
pub project_id: String,
}

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

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

/// Creates a new service account in the project. This also returns an unredacted API key for the service account.
pub async fn create(
&self,
request: ProjectServiceAccountCreateRequest,
) -> Result<ProjectServiceAccountCreateResponse, OpenAIError> {
self.client
.post(
format!(
"/organization/projects/{}/service_accounts",
self.project_id
)
.as_str(),
request,
)
.await
}

/// Retrieves a service account in the project.
pub async fn retrieve(
&self,
service_account_id: &str,
) -> Result<ProjectServiceAccount, OpenAIError> {
self.client
.get(
format!(
"/organization/projects/{}/service_accounts/{service_account_id}",
self.project_id
)
.as_str(),
)
.await
}

/// Deletes a service account from the project.
pub async fn delete(
&self,
service_account_id: &str,
) -> Result<ProjectServiceAccountDeleteResponse, OpenAIError> {
self.client
.delete(
format!(
"/organization/projects/{}/service_accounts/{service_account_id}",
self.project_id
)
.as_str(),
)
.await
}
}
Loading

0 comments on commit f388518

Please sign in to comment.