Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: uploadFile not working #6

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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