Skip to content

Commit

Permalink
Merge pull request #6 from lino-levan/master
Browse files Browse the repository at this point in the history
fix: uploadFile not working
  • Loading branch information
load1n9 authored Apr 18, 2023
2 parents a779b21 + b761eda commit 2da40e1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
8 changes: 8 additions & 0 deletions examples/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { OpenAI } from "../mod.ts";

const openAI = new OpenAI(
"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",
);

// TODO: Do this more portably
console.log(await openAI.uploadFile("./testdata/example.jsonl", "fine-tune"));
4 changes: 4 additions & 0 deletions examples/testdata/example.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{"prompt": "red", "completion": "angry"}
{"prompt": "orange", "completion": "angry"}
{"prompt": "blue", "completion": "calm"}
{"prompt": "purple", "completion": "calm"}
36 changes: 30 additions & 6 deletions src/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import type {
EditOptions,
Embedding,
EmbeddingsOptions,
File,
FileInstance,
FileList,
FileSpecifier,
FineTune,
FineTuneEvent,
FineTuneEventList,
Expand Down Expand Up @@ -303,10 +304,33 @@ export class OpenAI {
*
* https://platform.openai.com/docs/api-reference/files/upload
*/
async uploadFile(file: string, purpose: string): Promise<File> {
return await this.#request(`/files`, {
file,
purpose,
async uploadFile(
file: FileSpecifier,
purpose: string,
): Promise<FileInstance> {
const formData = new FormData();

// Model specified
formData.append("file", file);

// File data
if (typeof file === "string") {
const fileData = await Deno.readFile(file);

formData.append(
"file",
new File([fileData], basename(file)),
);
} else {
// Deno types are wrong
formData.append("file", file as unknown as Blob);
}

formData.append("purpose", purpose);

return await this.#request(`/files`, formData, {
noContentType: true,
method: "POST",
});
}

Expand All @@ -326,7 +350,7 @@ export class OpenAI {
*
* https://platform.openai.com/docs/api-reference/files/retrieve
*/
async retrieveFile(fileId: string): Promise<File> {
async retrieveFile(fileId: string): Promise<FileInstance> {
return await this.#request(`/files/${fileId}`, undefined, {
method: "GET",
});
Expand Down
10 changes: 5 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ export interface Translation {
text: string;
}

export interface File {
export interface FileInstance {
id: string;
object: "file";
bytes: number;
Expand All @@ -679,7 +679,7 @@ export interface File {
}

export interface FileList {
data: File[];
data: FileInstance[];
object: "list";
}

Expand Down Expand Up @@ -714,10 +714,10 @@ export interface FineTune {
prompt_loss_weight: number;
};
organization_id: string;
result_files: File[];
result_files: FileInstance[];
status: "pending" | "succeeded" | "cancelled";
validation_files: File[];
training_files: File[];
validation_files: FileInstance[];
training_files: FileInstance[];
updated_at: number;
}

Expand Down

0 comments on commit 2da40e1

Please sign in to comment.