1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { fetch } from "https://esm.town/v/std/fetch";
export async function openaiUploadFile({ key, data }: {
key: string;
data: any;
}) {
let file = data instanceof File || data instanceof Blob
? data
: typeof data === "string"
? new Blob([data])
: Array.isArray(data)
? new Blob([data.map((d) => JSON.stringify(d)).join("\n")])
: new Blob([JSON.stringify(data)]);
let formData = new FormData();
formData.append("purpose", "fine-tune");
formData.append("file", file, "data.json");
let result = await fetch("https://api.openai.com/v1/files", {
method: "POST",
headers: {
"authorization": `Bearer ${key}`,
},
body: formData,
}).then((r) => r.json());
if (result.error)
throw new Error("OpenAI Upload Error: " + result.error.message);
else
return result;
}
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.