import axios, {AxiosPromise, AxiosRequestConfig, Method} from 'axios'; import FormData from 'form-data'; import stream from 'stream'; import {DEFAULT_API_VERSION, ORIGIN} from './config'; import { CreateAnswerRequestViaDocuments, CreateAnswerRequestViaFile, CreateAnswerResponse, CreateClassificationRequestViaExamples, CreateClassificationRequestViaFile, CreateClassificationResponse, CreateCompletionRequest, CreateCompletionResponse, CreateCompletionViaGetRequest, CreateSearchRequestViaDocuments, CreateSearchRequestViaFile, CreateSearchResponse, EngineResponse, EnginesListResponse, ListFilesResponse, Purpose, RetrieveFileResponse, UploadFileResponse } from './types'; export class OpenAI { constructor(private readonly apiKey: string, private readonly apiVersion: string = DEFAULT_API_VERSION) { } protected wrapAuthenticatedRequest(url: string, method: Method, data: {} | FormData | undefined = undefined, axiosConfig: AxiosRequestConfig = {}): AxiosPromise { function camelToUnderscore(key) { let result = key.replace(/([A-Z])/g, ' $1'); return result.split(' ').join('_').toLowerCase(); } return axios({ ...axiosConfig, url, headers: { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json', ...(axiosConfig.headers || {}) }, data, method, }); } protected assembleRequestUrl(relativeAddress: string): string { return `${ORIGIN}/${this.apiVersion}/${relativeAddress}`; } public engines = { _client: this, list(): AxiosPromise { return this._client.wrapAuthenticatedRequest( this._client.assembleRequestUrl('engines'), 'GET', {} ); }, retrieve(engineId: string): AxiosPromise { return this._client.wrapAuthenticatedRequest( this._client.assembleRequestUrl(`engines/${engineId}`), 'GET', {} ); } }; public completions = { _client: this, create(engineId: string, body: CreateCompletionRequest): AxiosPromise { return this._client.wrapAuthenticatedRequest( this._client.assembleRequestUrl(`engines/${engineId}/completions`), 'POST', body ); }, createViaGet(engineId: string, body: CreateCompletionViaGetRequest): AxiosPromise { return this._client.wrapAuthenticatedRequest( this._client.assembleRequestUrl(`engines/${engineId}/completions/browser_stream`), 'GET', body ); }, }; public searches = { _client: this, create(engineId: string, body: CreateSearchRequestViaDocuments | CreateSearchRequestViaFile): AxiosPromise { return this._client.wrapAuthenticatedRequest( this._client.assembleRequestUrl(`engines/${engineId}/search`), 'POST', body ); } }; public classifications = { _client: this, create(body: CreateClassificationRequestViaExamples | CreateClassificationRequestViaFile): AxiosPromise { return this._client.wrapAuthenticatedRequest( this._client.assembleRequestUrl('classifications'), 'POST', body ); } }; public answers = { _client: this, create(body: CreateAnswerRequestViaDocuments | CreateAnswerRequestViaFile): AxiosPromise { return this._client.wrapAuthenticatedRequest( this._client.assembleRequestUrl('answers'), 'POST', body ); } }; public files = { _client: this, list(): AxiosPromise { return this._client.wrapAuthenticatedRequest( this._client.assembleRequestUrl('files'), 'GET' ); }, upload(body: { file: string, purpose: Purpose }): AxiosPromise { const formData = new FormData(); const file = new stream.Readable(); file.push(body.file); file.push(null); (file as any).path = 'file.jsonl'; formData.append('purpose', body.purpose); formData.append('file', file); return this._client.wrapAuthenticatedRequest( this._client.assembleRequestUrl('files'), 'POST', formData, { headers: formData.getHeaders() } ); }, retrieve(fileId: string): AxiosPromise { return this._client.wrapAuthenticatedRequest( this._client.assembleRequestUrl(`files/${fileId}`), 'GET' ); }, delete(fileId: string): AxiosPromise { return this._client.wrapAuthenticatedRequest( this._client.assembleRequestUrl(`files/${fileId}`), 'DELETE' ); } }; }