Skip to content

Commit

Permalink
feat: added files route support
Browse files Browse the repository at this point in the history
  • Loading branch information
csgulati09 committed Feb 23, 2024
1 parent 72481d9 commit 2005aca
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 1 deletion.
149 changes: 149 additions & 0 deletions src/apis/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { ApiClientInterface } from "../_types/generalTypes";
import { ApiResource } from "../apiResource";
import { RequestOptions } from "../baseClient";
import { OPEN_AI_API_KEY, PORTKEY_DEV_BASE_URL } from "../constants";
import { overrideConfig } from "../utils";
import { createHeaders } from "./createHeaders";
import OpenAI from "openai";

export class MainFiles extends ApiResource {

async create(
_body: FileCreateParams,
params?: ApiClientInterface,
opts?: RequestOptions
): Promise<FileObject> {
const body: FileCreateParams = _body;
if (params) {
const config = overrideConfig(this.client.config, params.config);
this.client.customHeaders = {
...this.client.customHeaders,
...createHeaders({ ...params, config }),
};
}

const OAIclient = new OpenAI({
apiKey: OPEN_AI_API_KEY,
baseURL: PORTKEY_DEV_BASE_URL,
defaultHeaders: this.client.customHeaders,
});

const result = await OAIclient.files.create(body, opts);
return result;
}

async list(
_query: FileListParams,
params?: ApiClientInterface,
opts?: RequestOptions
): Promise<any> {
const query: FileListParams = _query;
if (params) {
const config = overrideConfig(this.client.config, params.config);
this.client.customHeaders = {
...this.client.customHeaders,
...createHeaders({ ...params, config }),
};
}

const OAIclient = new OpenAI({
apiKey: OPEN_AI_API_KEY,
baseURL: PORTKEY_DEV_BASE_URL,
defaultHeaders: this.client.customHeaders,
});

const result = await OAIclient.files.list(query, opts);
return result;
}

async retrieve(
fileId: string,
params?: ApiClientInterface,
opts?: RequestOptions
): Promise<any> {
if (params) {
const config = overrideConfig(this.client.config, params.config);
this.client.customHeaders = {
...this.client.customHeaders,
...createHeaders({ ...params, config }),
};
}

const OAIclient = new OpenAI({
apiKey: OPEN_AI_API_KEY,
baseURL: PORTKEY_DEV_BASE_URL,
defaultHeaders: this.client.customHeaders,
});

const result = await OAIclient.files.retrieve(fileId, opts);
return result;
}

async del(
fileId: string,
params?: ApiClientInterface,
opts?: RequestOptions
): Promise<any> {
if (params) {
const config = overrideConfig(this.client.config, params.config);
this.client.customHeaders = {
...this.client.customHeaders,
...createHeaders({ ...params, config }),
};
}

const OAIclient = new OpenAI({
apiKey: OPEN_AI_API_KEY,
baseURL: PORTKEY_DEV_BASE_URL,
defaultHeaders: this.client.customHeaders,
});

const result = await OAIclient.files.del(fileId, opts);
return result;
}

async retrieveContent(
fileId: string,
params?: ApiClientInterface,
opts?: RequestOptions
): Promise<any> {
if (params) {
const config = overrideConfig(this.client.config, params.config);
this.client.customHeaders = {
...this.client.customHeaders,
...createHeaders({ ...params, config }),
};
}

const OAIclient = new OpenAI({
apiKey: OPEN_AI_API_KEY,
baseURL: PORTKEY_DEV_BASE_URL,
defaultHeaders: this.client.customHeaders,
});

const result = await OAIclient.files.content(fileId, opts);
return result;
}

}


export interface FileCreateParams {
file: any;
purpose: 'fine-tune' | 'assistants';
}

export interface FileObject {
id: string;
bytes: number;
created_at: number;
filename: string;
object: 'file';
purpose: 'fine-tune' | 'fine-tune-results' | 'assistants' | 'assistants_output';
status: 'uploaded' | 'processed' | 'error';
status_details?: string;
}

export interface FileListParams {
purpose?: string;
}
2 changes: 1 addition & 1 deletion src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export { Embeddings } from "./embeddings";
export { Images } from "./images";
export { Assistants } from "./assistants";
export { Threads } from "./threads";
// export { Files } from "./files";
export { MainFiles } from "./files";

1 change: 1 addition & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class Portkey extends ApiClient {
feedback = new API.Feedback(this);
embeddings = new API.Embeddings(this);
images = new API.Images(this);
files = new API.MainFiles(this);
beta = {
assistants: new API.Assistants(this),
threads: new API.Threads(this)
Expand Down

0 comments on commit 2005aca

Please sign in to comment.