|
| 1 | +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +import * as Core from './core'; |
| 4 | +import * as Errors from './error'; |
| 5 | +import { type Agent } from './_shims/index'; |
| 6 | +import * as Uploads from './uploads'; |
| 7 | +import * as Pagination from 'openai/pagination'; |
| 8 | +import * as API from 'openai/resources/index'; |
| 9 | + |
| 10 | +export interface ClientOptions { |
| 11 | + /** |
| 12 | + * Defaults to process.env['OPENAI_API_KEY']. |
| 13 | + */ |
| 14 | + apiKey?: string | undefined; |
| 15 | + |
| 16 | + /** |
| 17 | + * Defaults to process.env['OPENAI_ORG_ID']. |
| 18 | + */ |
| 19 | + organization?: string | null | undefined; |
| 20 | + |
| 21 | + /** |
| 22 | + * Defaults to process.env['OPENAI_PROJECT_ID']. |
| 23 | + */ |
| 24 | + project?: string | null | undefined; |
| 25 | + |
| 26 | + /** |
| 27 | + * Override the default base URL for the API, e.g., "https://api.example.com/v2/" |
| 28 | + * |
| 29 | + * Defaults to process.env['OPENAI_BASE_URL']. |
| 30 | + */ |
| 31 | + baseURL?: string | null | undefined; |
| 32 | + |
| 33 | + /** |
| 34 | + * The maximum amount of time (in milliseconds) that the client should wait for a response |
| 35 | + * from the server before timing out a single request. |
| 36 | + * |
| 37 | + * Note that request timeouts are retried by default, so in a worst-case scenario you may wait |
| 38 | + * much longer than this timeout before the promise succeeds or fails. |
| 39 | + */ |
| 40 | + timeout?: number; |
| 41 | + |
| 42 | + /** |
| 43 | + * An HTTP agent used to manage HTTP(S) connections. |
| 44 | + * |
| 45 | + * If not provided, an agent will be constructed by default in the Node.js environment, |
| 46 | + * otherwise no agent is used. |
| 47 | + */ |
| 48 | + httpAgent?: Agent; |
| 49 | + |
| 50 | + /** |
| 51 | + * Specify a custom `fetch` function implementation. |
| 52 | + * |
| 53 | + * If not provided, we use `node-fetch` on Node.js and otherwise expect that `fetch` is |
| 54 | + * defined globally. |
| 55 | + */ |
| 56 | + fetch?: Core.Fetch | undefined; |
| 57 | + |
| 58 | + /** |
| 59 | + * The maximum number of times that the client will retry a request in case of a |
| 60 | + * temporary failure, like a network error or a 5XX error from the server. |
| 61 | + * |
| 62 | + * @default 2 |
| 63 | + */ |
| 64 | + maxRetries?: number; |
| 65 | + |
| 66 | + /** |
| 67 | + * Default headers to include with every request to the API. |
| 68 | + * |
| 69 | + * These can be removed in individual requests by explicitly setting the |
| 70 | + * header to `undefined` or `null` in request options. |
| 71 | + */ |
| 72 | + defaultHeaders?: Core.Headers; |
| 73 | + |
| 74 | + /** |
| 75 | + * Default query parameters to include with every request to the API. |
| 76 | + * |
| 77 | + * These can be removed in individual requests by explicitly setting the |
| 78 | + * param to `undefined` in request options. |
| 79 | + */ |
| 80 | + defaultQuery?: Core.DefaultQuery; |
| 81 | + |
| 82 | + /** |
| 83 | + * By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers. |
| 84 | + * Only set this option to `true` if you understand the risks and have appropriate mitigations in place. |
| 85 | + */ |
| 86 | + dangerouslyAllowBrowser?: boolean; |
| 87 | +} |
| 88 | + |
| 89 | +/** API Client for interfacing with the OpenAI API. */ |
| 90 | +export class OpenAI extends Core.APIClient { |
| 91 | + apiKey: string; |
| 92 | + organization: string | null; |
| 93 | + project: string | null; |
| 94 | + |
| 95 | + private _options: ClientOptions; |
| 96 | + |
| 97 | + /** |
| 98 | + * API Client for interfacing with the OpenAI API. |
| 99 | + * |
| 100 | + * @param {string | undefined} [opts.apiKey=process.env['OPENAI_API_KEY'] ?? undefined] |
| 101 | + * @param {string | null | undefined} [opts.organization=process.env['OPENAI_ORG_ID'] ?? null] |
| 102 | + * @param {string | null | undefined} [opts.project=process.env['OPENAI_PROJECT_ID'] ?? null] |
| 103 | + * @param {string} [opts.baseURL=process.env['OPENAI_BASE_URL'] ?? https://api.openai.com/v1] - Override the default base URL for the API. |
| 104 | + * @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out. |
| 105 | + * @param {number} [opts.httpAgent] - An HTTP agent used to manage HTTP(s) connections. |
| 106 | + * @param {Core.Fetch} [opts.fetch] - Specify a custom `fetch` function implementation. |
| 107 | + * @param {number} [opts.maxRetries=2] - The maximum number of times the client will retry a request. |
| 108 | + * @param {Core.Headers} opts.defaultHeaders - Default headers to include with every request to the API. |
| 109 | + * @param {Core.DefaultQuery} opts.defaultQuery - Default query parameters to include with every request to the API. |
| 110 | + * @param {boolean} [opts.dangerouslyAllowBrowser=false] - By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers. |
| 111 | + */ |
| 112 | + constructor({ |
| 113 | + baseURL = Core.readEnv('OPENAI_BASE_URL'), |
| 114 | + apiKey = Core.readEnv('OPENAI_API_KEY'), |
| 115 | + organization = Core.readEnv('OPENAI_ORG_ID') ?? null, |
| 116 | + project = Core.readEnv('OPENAI_PROJECT_ID') ?? null, |
| 117 | + ...opts |
| 118 | + }: ClientOptions = {}) { |
| 119 | + if (apiKey === undefined) { |
| 120 | + throw new Errors.OpenAIError( |
| 121 | + "The OPENAI_API_KEY environment variable is missing or empty; either provide it, or instantiate the OpenAI client with an apiKey option, like new OpenAI({ apiKey: 'My API Key' }).", |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + const options: ClientOptions = { |
| 126 | + apiKey, |
| 127 | + organization, |
| 128 | + project, |
| 129 | + ...opts, |
| 130 | + baseURL: baseURL || `https://api.openai.com/v1`, |
| 131 | + }; |
| 132 | + |
| 133 | + if (!options.dangerouslyAllowBrowser && Core.isRunningInBrowser()) { |
| 134 | + throw new Errors.OpenAIError( |
| 135 | + "It looks like you're running in a browser-like environment.\n\nThis is disabled by default, as it risks exposing your secret API credentials to attackers.\nIf you understand the risks and have appropriate mitigations in place,\nyou can set the `dangerouslyAllowBrowser` option to `true`, e.g.,\n\nnew OpenAI({ apiKey, dangerouslyAllowBrowser: true });\n\nhttps://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety\n", |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + super({ |
| 140 | + baseURL: options.baseURL!, |
| 141 | + timeout: options.timeout ?? 600000 /* 10 minutes */, |
| 142 | + httpAgent: options.httpAgent, |
| 143 | + maxRetries: options.maxRetries, |
| 144 | + fetch: options.fetch, |
| 145 | + }); |
| 146 | + this._options = options; |
| 147 | + |
| 148 | + this.apiKey = apiKey; |
| 149 | + this.organization = organization; |
| 150 | + this.project = project; |
| 151 | + } |
| 152 | + |
| 153 | + completions: API.Completions = new API.Completions(this); |
| 154 | + chat: API.Chat = new API.Chat(this); |
| 155 | + embeddings: API.Embeddings = new API.Embeddings(this); |
| 156 | + files: API.Files = new API.Files(this); |
| 157 | + images: API.Images = new API.Images(this); |
| 158 | + audio: API.Audio = new API.Audio(this); |
| 159 | + moderations: API.Moderations = new API.Moderations(this); |
| 160 | + models: API.Models = new API.Models(this); |
| 161 | + fineTuning: API.FineTuning = new API.FineTuning(this); |
| 162 | + beta: API.Beta = new API.Beta(this); |
| 163 | + batches: API.Batches = new API.Batches(this); |
| 164 | + |
| 165 | + protected override defaultQuery(): Core.DefaultQuery | undefined { |
| 166 | + return this._options.defaultQuery; |
| 167 | + } |
| 168 | + |
| 169 | + protected override defaultHeaders(opts: Core.FinalRequestOptions): Core.Headers { |
| 170 | + return { |
| 171 | + ...super.defaultHeaders(opts), |
| 172 | + 'OpenAI-Organization': this.organization, |
| 173 | + 'OpenAI-Project': this.project, |
| 174 | + ...this._options.defaultHeaders, |
| 175 | + }; |
| 176 | + } |
| 177 | + |
| 178 | + protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers { |
| 179 | + return { Authorization: `Bearer ${this.apiKey}` }; |
| 180 | + } |
| 181 | + |
| 182 | + static OpenAI = this; |
| 183 | + |
| 184 | + static OpenAIError = Errors.OpenAIError; |
| 185 | + static APIError = Errors.APIError; |
| 186 | + static APIConnectionError = Errors.APIConnectionError; |
| 187 | + static APIConnectionTimeoutError = Errors.APIConnectionTimeoutError; |
| 188 | + static APIUserAbortError = Errors.APIUserAbortError; |
| 189 | + static NotFoundError = Errors.NotFoundError; |
| 190 | + static ConflictError = Errors.ConflictError; |
| 191 | + static RateLimitError = Errors.RateLimitError; |
| 192 | + static BadRequestError = Errors.BadRequestError; |
| 193 | + static AuthenticationError = Errors.AuthenticationError; |
| 194 | + static InternalServerError = Errors.InternalServerError; |
| 195 | + static PermissionDeniedError = Errors.PermissionDeniedError; |
| 196 | + static UnprocessableEntityError = Errors.UnprocessableEntityError; |
| 197 | + |
| 198 | + static toFile = Uploads.toFile; |
| 199 | + static fileFromPath = Uploads.fileFromPath; |
| 200 | +} |
| 201 | + |
| 202 | +export namespace OpenAI { |
| 203 | + export import RequestOptions = Core.RequestOptions; |
| 204 | + |
| 205 | + export import Page = Pagination.Page; |
| 206 | + export import PageResponse = Pagination.PageResponse; |
| 207 | + |
| 208 | + export import CursorPage = Pagination.CursorPage; |
| 209 | + export import CursorPageParams = Pagination.CursorPageParams; |
| 210 | + export import CursorPageResponse = Pagination.CursorPageResponse; |
| 211 | + |
| 212 | + export import Completions = API.Completions; |
| 213 | + export import Completion = API.Completion; |
| 214 | + export import CompletionChoice = API.CompletionChoice; |
| 215 | + export import CompletionUsage = API.CompletionUsage; |
| 216 | + export import CompletionCreateParams = API.CompletionCreateParams; |
| 217 | + export import CompletionCreateParamsNonStreaming = API.CompletionCreateParamsNonStreaming; |
| 218 | + export import CompletionCreateParamsStreaming = API.CompletionCreateParamsStreaming; |
| 219 | + |
| 220 | + export import Chat = API.Chat; |
| 221 | + export import ChatModel = API.ChatModel; |
| 222 | + export import ChatCompletion = API.ChatCompletion; |
| 223 | + export import ChatCompletionAssistantMessageParam = API.ChatCompletionAssistantMessageParam; |
| 224 | + export import ChatCompletionChunk = API.ChatCompletionChunk; |
| 225 | + export import ChatCompletionContentPart = API.ChatCompletionContentPart; |
| 226 | + export import ChatCompletionContentPartImage = API.ChatCompletionContentPartImage; |
| 227 | + export import ChatCompletionContentPartText = API.ChatCompletionContentPartText; |
| 228 | + export import ChatCompletionFunctionCallOption = API.ChatCompletionFunctionCallOption; |
| 229 | + export import ChatCompletionFunctionMessageParam = API.ChatCompletionFunctionMessageParam; |
| 230 | + export import ChatCompletionMessage = API.ChatCompletionMessage; |
| 231 | + export import ChatCompletionMessageParam = API.ChatCompletionMessageParam; |
| 232 | + export import ChatCompletionMessageToolCall = API.ChatCompletionMessageToolCall; |
| 233 | + export import ChatCompletionNamedToolChoice = API.ChatCompletionNamedToolChoice; |
| 234 | + export import ChatCompletionRole = API.ChatCompletionRole; |
| 235 | + export import ChatCompletionSystemMessageParam = API.ChatCompletionSystemMessageParam; |
| 236 | + export import ChatCompletionTokenLogprob = API.ChatCompletionTokenLogprob; |
| 237 | + export import ChatCompletionTool = API.ChatCompletionTool; |
| 238 | + export import ChatCompletionToolChoiceOption = API.ChatCompletionToolChoiceOption; |
| 239 | + export import ChatCompletionToolMessageParam = API.ChatCompletionToolMessageParam; |
| 240 | + export import ChatCompletionUserMessageParam = API.ChatCompletionUserMessageParam; |
| 241 | + export import ChatCompletionCreateParams = API.ChatCompletionCreateParams; |
| 242 | + export import ChatCompletionCreateParamsNonStreaming = API.ChatCompletionCreateParamsNonStreaming; |
| 243 | + export import ChatCompletionCreateParamsStreaming = API.ChatCompletionCreateParamsStreaming; |
| 244 | + |
| 245 | + export import Embeddings = API.Embeddings; |
| 246 | + export import CreateEmbeddingResponse = API.CreateEmbeddingResponse; |
| 247 | + export import Embedding = API.Embedding; |
| 248 | + export import EmbeddingCreateParams = API.EmbeddingCreateParams; |
| 249 | + |
| 250 | + export import Files = API.Files; |
| 251 | + export import FileContent = API.FileContent; |
| 252 | + export import FileDeleted = API.FileDeleted; |
| 253 | + export import FileObject = API.FileObject; |
| 254 | + export import FileObjectsPage = API.FileObjectsPage; |
| 255 | + export import FileCreateParams = API.FileCreateParams; |
| 256 | + export import FileListParams = API.FileListParams; |
| 257 | + |
| 258 | + export import Images = API.Images; |
| 259 | + export import Image = API.Image; |
| 260 | + export import ImagesResponse = API.ImagesResponse; |
| 261 | + export import ImageCreateVariationParams = API.ImageCreateVariationParams; |
| 262 | + export import ImageEditParams = API.ImageEditParams; |
| 263 | + export import ImageGenerateParams = API.ImageGenerateParams; |
| 264 | + |
| 265 | + export import Audio = API.Audio; |
| 266 | + |
| 267 | + export import Moderations = API.Moderations; |
| 268 | + export import Moderation = API.Moderation; |
| 269 | + export import ModerationCreateResponse = API.ModerationCreateResponse; |
| 270 | + export import ModerationCreateParams = API.ModerationCreateParams; |
| 271 | + |
| 272 | + export import Models = API.Models; |
| 273 | + export import Model = API.Model; |
| 274 | + export import ModelDeleted = API.ModelDeleted; |
| 275 | + export import ModelsPage = API.ModelsPage; |
| 276 | + |
| 277 | + export import FineTuning = API.FineTuning; |
| 278 | + |
| 279 | + export import Beta = API.Beta; |
| 280 | + |
| 281 | + export import Batches = API.Batches; |
| 282 | + export import Batch = API.Batch; |
| 283 | + export import BatchError = API.BatchError; |
| 284 | + export import BatchRequestCounts = API.BatchRequestCounts; |
| 285 | + export import BatchesPage = API.BatchesPage; |
| 286 | + export import BatchCreateParams = API.BatchCreateParams; |
| 287 | + export import BatchListParams = API.BatchListParams; |
| 288 | + |
| 289 | + export import ErrorObject = API.ErrorObject; |
| 290 | + export import FunctionDefinition = API.FunctionDefinition; |
| 291 | + export import FunctionParameters = API.FunctionParameters; |
| 292 | +} |
0 commit comments