openai
Version:
The official TypeScript library for the OpenAI API
395 lines (369 loc) • 11.4 kB
text/typescript
// File generated from our OpenAPI spec by Castiron. See CONTRIBUTING.md for details.
import { APIResource } from '../../../../core/resource';
import { APIPromise } from '../../../../core/api-promise';
import {
ConversationCursorPage,
type ConversationCursorPageParams,
PagePromise,
} from '../../../../core/pagination';
import { RequestOptions } from '../../../../internal/request-options';
import { path } from '../../../../internal/utils/path';
function resolveResourceRequestOptions(
options: RequestOptions | undefined,
buildOptions: (options: RequestOptions | undefined) => RequestOptions | Promise<RequestOptions>,
): Promise<RequestOptions> {
return Promise.resolve(options).then(buildOptions);
}
// Recognizable options across SDK runtime versions. Keep this independent of
// private RequestOptions fields so older handwritten runtimes still compile.
const normalizeRequestOptionsForQueryKeys = new Set([
'method',
'path',
'query',
'body',
'headers',
'maxRetries',
'stream',
'timeout',
'httpAgent',
'fetchOptions',
'signal',
'idempotencyKey',
'defaultBaseURL',
'__metadata',
'__binaryRequest',
'__binaryResponse',
'__streamClass',
'__streamMode',
'__security',
'__synthesizeEventData',
]);
function normalizeRequestOptionsForQuery(
value: unknown,
queryKeys: ReadonlyArray<string>,
options: RequestOptions | undefined,
):
| ({
[K in 'headers' | 'maxRetries' | 'timeout' | 'signal' | 'idempotencyKey' | 'query']?: RequestOptions[K];
} & {
[
K in
| 'method'
| 'path'
| 'body'
| 'stream'
| 'httpAgent'
| 'fetchOptions'
| 'defaultBaseURL'
| '__metadata'
| '__binaryRequest'
| '__binaryResponse'
| '__streamClass'
| '__streamMode'
| '__security'
| '__synthesizeEventData'
]?: never;
})
| undefined {
if (typeof value !== 'object' || value === null) return undefined;
// Optional never fields can still be explicitly undefined unless consumers
// enable exactOptionalPropertyTypes. Snapshot data without invoking getters.
const entries = Object.entries(Object.getOwnPropertyDescriptors(value)).filter(
([, descriptor]) => descriptor.enumerable && (!('value' in descriptor) || descriptor.value !== undefined),
);
const keys = entries.map(([key]) => key);
const requestOnly = keys.some(
(key) => normalizeRequestOptionsForQueryKeys.has(key) && !queryKeys.includes(key),
);
if (!requestOnly) return undefined;
// Declared query fields, including stream, must use the query argument.
// Mixing them with request-only options is ambiguous and could change the return type.
if (
options !== undefined ||
keys.some((key) => !normalizeRequestOptionsForQueryKeys.has(key) || queryKeys.includes(key))
) {
throw new TypeError('Query parameters and request options must be passed as separate arguments.');
}
// The query position must not gain authority to change the request destination
// or transport. Those overrides require the explicit request options argument.
if (
keys.some(
(key) => !['headers', 'maxRetries', 'timeout', 'signal', 'idempotencyKey', 'query'].includes(key),
)
) {
throw new TypeError('Pass transport overrides in the explicit request options argument.');
}
// Copy only the validated fields. Spreading value would reintroduce undefined
// transport overrides, and deleting them would mutate the caller's object.
return Object.fromEntries(
entries.map(([key, descriptor]) => {
if ('value' in descriptor) return [key, descriptor.value];
return [key, descriptor.get ? Reflect.apply(descriptor.get, value, []) : undefined];
}),
) as {
[K in 'headers' | 'maxRetries' | 'timeout' | 'signal' | 'idempotencyKey' | 'query']?: RequestOptions[K];
} & {
[
K in
| 'method'
| 'path'
| 'body'
| 'stream'
| 'httpAgent'
| 'fetchOptions'
| 'defaultBaseURL'
| '__metadata'
| '__binaryRequest'
| '__binaryResponse'
| '__streamClass'
| '__streamMode'
| '__security'
| '__synthesizeEventData'
]?: never;
};
}
export class RateLimits extends APIResource {
/**
* Returns the rate limits per model for a project.
*
* @example
* ```ts
* // Automatically fetches more pages as needed.
* for await (const projectRateLimit of client.admin.organization.projects.rateLimits.listRateLimits(
* 'project_id',
* )) {
* // ...
* }
* ```
*/
listRateLimits(
projectID: string,
query?:
| (RateLimitListRateLimitsParams &
(
| {
[
K in
| 'method'
| 'path'
| 'query'
| 'body'
| 'headers'
| 'maxRetries'
| 'stream'
| 'timeout'
| 'httpAgent'
| 'fetchOptions'
| 'signal'
| 'idempotencyKey'
| 'defaultBaseURL'
| '__metadata'
| '__binaryRequest'
| '__binaryResponse'
| '__streamClass'
| '__streamMode'
| '__security'
| '__synthesizeEventData'
]?: never;
}
| null
| undefined
))
| null
| undefined,
options?: RequestOptions,
): PagePromise<ProjectRateLimitsPage, ProjectRateLimit>;
listRateLimits(
projectID: string,
options?: {
[K in 'headers' | 'maxRetries' | 'timeout' | 'signal' | 'idempotencyKey' | 'query']?: RequestOptions[K];
} & {
[
K in
| 'method'
| 'path'
| 'body'
| 'stream'
| 'httpAgent'
| 'fetchOptions'
| 'defaultBaseURL'
| '__metadata'
| '__binaryRequest'
| '__binaryResponse'
| '__streamClass'
| '__streamMode'
| '__security'
| '__synthesizeEventData'
]?: never;
},
): PagePromise<ProjectRateLimitsPage, ProjectRateLimit>;
listRateLimits(
projectID: string,
query:
| RateLimitListRateLimitsParams
| ({
[
K in 'headers' | 'maxRetries' | 'timeout' | 'signal' | 'idempotencyKey' | 'query'
]?: RequestOptions[K];
} & {
[
K in
| 'method'
| 'path'
| 'body'
| 'stream'
| 'httpAgent'
| 'fetchOptions'
| 'defaultBaseURL'
| '__metadata'
| '__binaryRequest'
| '__binaryResponse'
| '__streamClass'
| '__streamMode'
| '__security'
| '__synthesizeEventData'
]?: never;
})
| null
| undefined = {},
options?: RequestOptions,
): PagePromise<ProjectRateLimitsPage, ProjectRateLimit> {
const normalizeRequestOptionsForQueryOptions = normalizeRequestOptionsForQuery(
query,
['after', 'before', 'limit'],
options,
);
if (normalizeRequestOptionsForQueryOptions !== undefined) {
options = normalizeRequestOptionsForQueryOptions;
query = {};
}
query = query as RateLimitListRateLimitsParams | null | undefined;
return this._client.getAPIList(
path`/organization/projects/${projectID}/rate_limits`,
ConversationCursorPage<ProjectRateLimit>,
resolveResourceRequestOptions(options, (options) => ({
query,
...options,
__security: { adminAPIKeyAuth: true },
})),
);
}
/**
* Updates a project rate limit.
*
* @example
* ```ts
* const projectRateLimit =
* await client.admin.organization.projects.rateLimits.updateRateLimit(
* 'rate_limit_id',
* { project_id: 'project_id' },
* );
* ```
*/
updateRateLimit(
rateLimitID: string,
params: RateLimitUpdateRateLimitParams,
options?: RequestOptions,
): APIPromise<ProjectRateLimit> {
const { project_id, ...body } = params;
return this._client.post(
path`/organization/projects/${project_id}/rate_limits/${rateLimitID}`,
resolveResourceRequestOptions(options, (options) => ({
body,
...options,
__security: { adminAPIKeyAuth: true },
})),
);
}
}
export type ProjectRateLimitsPage = ConversationCursorPage<ProjectRateLimit>;
/**
* Represents a project rate limit config.
*/
export interface ProjectRateLimit {
/**
* The identifier, which can be referenced in API endpoints.
*/
id: string;
/**
* The maximum requests per minute.
*/
max_requests_per_1_minute: number;
/**
* The maximum tokens per minute.
*/
max_tokens_per_1_minute: number;
/**
* The model this rate limit applies to.
*/
model: string;
/**
* The object type, which is always `project.rate_limit`
*/
object: 'project.rate_limit';
/**
* The maximum batch input tokens per day. Only present for relevant models.
*/
batch_1_day_max_input_tokens?: number;
/**
* The maximum audio megabytes per minute. Only present for relevant models.
*/
max_audio_megabytes_per_1_minute?: number;
/**
* The maximum images per minute. Only present for relevant models.
*/
max_images_per_1_minute?: number;
/**
* The maximum requests per day. Only present for relevant models.
*/
max_requests_per_1_day?: number;
}
export interface RateLimitListRateLimitsParams extends ConversationCursorPageParams {
/**
* A cursor for use in pagination. `before` is an object ID that defines your place
* in the list. For instance, if you make a list request and receive 100 objects,
* beginning with obj_foo, your subsequent call can include before=obj_foo in order
* to fetch the previous page of the list.
*/
before?: string;
}
export interface RateLimitUpdateRateLimitParams {
/**
* Path param: The ID of the project.
*/
project_id: string;
/**
* Body param: The maximum batch input tokens per day. Only relevant for certain
* models.
*/
batch_1_day_max_input_tokens?: number;
/**
* Body param: The maximum audio megabytes per minute. Only relevant for certain
* models.
*/
max_audio_megabytes_per_1_minute?: number;
/**
* Body param: The maximum images per minute. Only relevant for certain models.
*/
max_images_per_1_minute?: number;
/**
* Body param: The maximum requests per day. Only relevant for certain models.
*/
max_requests_per_1_day?: number;
/**
* Body param: The maximum requests per minute.
*/
max_requests_per_1_minute?: number;
/**
* Body param: The maximum tokens per minute.
*/
max_tokens_per_1_minute?: number;
}
export declare namespace RateLimits {
export {
type ProjectRateLimit as ProjectRateLimit,
type ProjectRateLimitsPage as ProjectRateLimitsPage,
type RateLimitListRateLimitsParams as RateLimitListRateLimitsParams,
type RateLimitUpdateRateLimitParams as RateLimitUpdateRateLimitParams,
};
}