Skip to content

Commit f388518

Browse files
authored
spec sync: Uploads + Administration APIs (#286)
* 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
1 parent ac204a3 commit f388518

30 files changed

+9895
-5097
lines changed

async-openai/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
- [x] Images
3535
- [x] Models
3636
- [x] Moderations
37-
- [ ] Organizations | Administration
37+
- [x] Organizations | Administration
3838
- [x] Realtime API types (Beta)
39-
- [ ] Uploads
39+
- [x] Uploads
4040
- SSE streaming on available APIs
4141
- Requests (except SSE streaming) including form submissions are retried with exponential backoff when [rate limited](https://platform.openai.com/docs/guides/rate-limits).
4242
- Ergonomic builder pattern for all request objects.
@@ -61,8 +61,8 @@ $Env:OPENAI_API_KEY='sk-...'
6161

6262
## Realtime API
6363

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

6767
## Image Generation Example
6868

async-openai/src/audit_logs.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use serde::Serialize;
2+
3+
use crate::{config::Config, error::OpenAIError, types::ListAuditLogsResponse, Client};
4+
5+
/// Logs of user actions and configuration changes within this organization.
6+
/// To log events, you must activate logging in the [Organization Settings](https://platform.openai.com/settings/organization/general).
7+
/// Once activated, for security reasons, logging cannot be deactivated.
8+
pub struct AuditLogs<'c, C: Config> {
9+
client: &'c Client<C>,
10+
}
11+
12+
impl<'c, C: Config> AuditLogs<'c, C> {
13+
pub fn new(client: &'c Client<C>) -> Self {
14+
Self { client }
15+
}
16+
17+
/// List user actions and configuration changes within this organization.
18+
pub async fn get<Q>(&self, query: &Q) -> Result<ListAuditLogsResponse, OpenAIError>
19+
where
20+
Q: Serialize + ?Sized,
21+
{
22+
self.client
23+
.get_with_query("/organization/audit_logs", query)
24+
.await
25+
}
26+
}

async-openai/src/client.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::{
1111
file::Files,
1212
image::Images,
1313
moderation::Moderations,
14-
Assistants, Audio, Batches, Chat, Completions, Embeddings, FineTuning, Models, Threads,
15-
VectorStores,
14+
Assistants, Audio, AuditLogs, Batches, Chat, Completions, Embeddings, FineTuning, Invites,
15+
Models, Projects, Threads, Users, VectorStores,
1616
};
1717

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

138+
/// To call [AuditLogs] group related APIs using this client.
139+
pub fn audit_logs(&self) -> AuditLogs<C> {
140+
AuditLogs::new(self)
141+
}
142+
143+
/// To call [Invites] group related APIs using this client.
144+
pub fn invites(&self) -> Invites<C> {
145+
Invites::new(self)
146+
}
147+
148+
/// To call [Users] group related APIs using this client.
149+
pub fn users(&self) -> Users<C> {
150+
Users::new(self)
151+
}
152+
153+
/// To call [Projects] group related APIs using this client.
154+
pub fn projects(&self) -> Projects<C> {
155+
Projects::new(self)
156+
}
157+
138158
pub fn config(&self) -> &C {
139159
&self.config
140160
}

async-openai/src/invites.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl<'c, C: Config> Invites<'c, C> {
1717
Self { client }
1818
}
1919

20-
/// Returns a list of runs belonging to a thread.
20+
/// Returns a list of invites in the organization.
2121
pub async fn list<Q>(&self, query: &Q) -> Result<InviteListResponse, OpenAIError>
2222
where
2323
Q: Serialize + ?Sized,

async-openai/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
mod assistant_files;
8181
mod assistants;
8282
mod audio;
83+
mod audit_logs;
8384
mod batches;
8485
mod chat;
8586
mod client;
@@ -96,12 +97,15 @@ mod message_files;
9697
mod messages;
9798
mod model;
9899
mod moderation;
100+
mod project_api_keys;
101+
mod project_service_accounts;
99102
mod project_users;
100103
mod projects;
101104
mod runs;
102105
mod steps;
103106
mod threads;
104107
pub mod types;
108+
mod uploads;
105109
mod users;
106110
mod util;
107111
mod vector_store_file_batches;
@@ -111,6 +115,7 @@ mod vector_stores;
111115
pub use assistant_files::AssistantFiles;
112116
pub use assistants::Assistants;
113117
pub use audio::Audio;
118+
pub use audit_logs::AuditLogs;
114119
pub use batches::Batches;
115120
pub use chat::Chat;
116121
pub use client::Client;
@@ -124,11 +129,14 @@ pub use message_files::MessageFiles;
124129
pub use messages::Messages;
125130
pub use model::Models;
126131
pub use moderation::Moderations;
132+
pub use project_api_keys::ProjectAPIKeys;
133+
pub use project_service_accounts::ProjectServiceAccounts;
127134
pub use project_users::ProjectUsers;
128135
pub use projects::Projects;
129136
pub use runs::Runs;
130137
pub use steps::Steps;
131138
pub use threads::Threads;
139+
pub use uploads::Uploads;
132140
pub use users::Users;
133141
pub use vector_store_file_batches::VectorStoreFileBatches;
134142
pub use vector_store_files::VectorStoreFiles;

async-openai/src/moderation.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
Client,
66
};
77

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

20-
/// Classifies if text is potentially harmful.
20+
/// Classifies if text and/or image inputs are potentially harmful. Learn
21+
/// more in the [moderation guide](https://platform.openai.com/docs/guides/moderation).
2122
pub async fn create(
2223
&self,
2324
request: CreateModerationRequest,

async-openai/src/project_api_keys.rs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use serde::Serialize;
2+
3+
use crate::{
4+
config::Config,
5+
error::OpenAIError,
6+
types::{ProjectApiKey, ProjectApiKeyDeleteResponse, ProjectApiKeyListResponse},
7+
Client,
8+
};
9+
10+
/// Manage API keys for a given project. Supports listing and deleting keys for users.
11+
/// This API does not allow issuing keys for users, as users need to authorize themselves to generate keys.
12+
pub struct ProjectAPIKeys<'c, C: Config> {
13+
client: &'c Client<C>,
14+
pub project_id: String,
15+
}
16+
17+
impl<'c, C: Config> ProjectAPIKeys<'c, C> {
18+
pub fn new(client: &'c Client<C>, project_id: &str) -> Self {
19+
Self {
20+
client,
21+
project_id: project_id.into(),
22+
}
23+
}
24+
25+
/// Returns a list of API keys in the project.
26+
pub async fn list<Q>(&self, query: &Q) -> Result<ProjectApiKeyListResponse, OpenAIError>
27+
where
28+
Q: Serialize + ?Sized,
29+
{
30+
self.client
31+
.get_with_query(
32+
format!("/organization/projects/{}/api_keys", self.project_id).as_str(),
33+
query,
34+
)
35+
.await
36+
}
37+
38+
/// Retrieves an API key in the project.
39+
pub async fn retrieve(&self, api_key: &str) -> Result<ProjectApiKey, OpenAIError> {
40+
self.client
41+
.get(
42+
format!(
43+
"/organization/projects/{}/api_keys/{api_key}",
44+
self.project_id
45+
)
46+
.as_str(),
47+
)
48+
.await
49+
}
50+
51+
/// Deletes an API key from the project.
52+
pub async fn delete(&self, api_key: &str) -> Result<ProjectApiKeyDeleteResponse, OpenAIError> {
53+
self.client
54+
.delete(
55+
format!(
56+
"/organization/projects/{}/api_keys/{api_key}",
57+
self.project_id
58+
)
59+
.as_str(),
60+
)
61+
.await
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
use serde::Serialize;
2+
3+
use crate::{
4+
config::Config,
5+
error::OpenAIError,
6+
types::{
7+
ProjectServiceAccount, ProjectServiceAccountCreateRequest,
8+
ProjectServiceAccountCreateResponse, ProjectServiceAccountDeleteResponse,
9+
ProjectServiceAccountListResponse,
10+
},
11+
Client,
12+
};
13+
14+
/// Manage service accounts within a project. A service account is a bot user that is not
15+
/// associated with a user. If a user leaves an organization, their keys and membership in projects
16+
/// will no longer work. Service accounts do not have this limitation.
17+
/// However, service accounts can also be deleted from a project.
18+
pub struct ProjectServiceAccounts<'c, C: Config> {
19+
client: &'c Client<C>,
20+
pub project_id: String,
21+
}
22+
23+
impl<'c, C: Config> ProjectServiceAccounts<'c, C> {
24+
pub fn new(client: &'c Client<C>, project_id: &str) -> Self {
25+
Self {
26+
client,
27+
project_id: project_id.into(),
28+
}
29+
}
30+
31+
/// Returns a list of service accounts in the project.
32+
pub async fn list<Q>(&self, query: &Q) -> Result<ProjectServiceAccountListResponse, OpenAIError>
33+
where
34+
Q: Serialize + ?Sized,
35+
{
36+
self.client
37+
.get_with_query(
38+
format!(
39+
"/organization/projects/{}/service_accounts",
40+
self.project_id
41+
)
42+
.as_str(),
43+
query,
44+
)
45+
.await
46+
}
47+
48+
/// Creates a new service account in the project. This also returns an unredacted API key for the service account.
49+
pub async fn create(
50+
&self,
51+
request: ProjectServiceAccountCreateRequest,
52+
) -> Result<ProjectServiceAccountCreateResponse, OpenAIError> {
53+
self.client
54+
.post(
55+
format!(
56+
"/organization/projects/{}/service_accounts",
57+
self.project_id
58+
)
59+
.as_str(),
60+
request,
61+
)
62+
.await
63+
}
64+
65+
/// Retrieves a service account in the project.
66+
pub async fn retrieve(
67+
&self,
68+
service_account_id: &str,
69+
) -> Result<ProjectServiceAccount, OpenAIError> {
70+
self.client
71+
.get(
72+
format!(
73+
"/organization/projects/{}/service_accounts/{service_account_id}",
74+
self.project_id
75+
)
76+
.as_str(),
77+
)
78+
.await
79+
}
80+
81+
/// Deletes a service account from the project.
82+
pub async fn delete(
83+
&self,
84+
service_account_id: &str,
85+
) -> Result<ProjectServiceAccountDeleteResponse, OpenAIError> {
86+
self.client
87+
.delete(
88+
format!(
89+
"/organization/projects/{}/service_accounts/{service_account_id}",
90+
self.project_id
91+
)
92+
.as_str(),
93+
)
94+
.await
95+
}
96+
}

0 commit comments

Comments
 (0)