0
Create assistant file
One script reply has been approved by the moderators Verified

Create an assistant file by attaching a File to an assistant.

Created by hugo697 136 days ago Viewed 3726 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 136 days ago
1
type Openai = {
2
  api_key: string;
3
  organization_id: string;
4
};
5
/**
6
 * Create assistant file
7
 * Create an assistant file by attaching a File to an assistant.
8
 */
9
export async function main(
10
  auth: Openai,
11
  assistant_id: string,
12
  body: { file_id: string }
13
) {
14
  const url = new URL(
15
    `https://api.openai.com/v1/assistants/${assistant_id}/files`
16
  );
17

18
  const response = await fetch(url, {
19
    method: "POST",
20
    headers: {
21
      "OpenAI-Organization": auth.organization_id,
22
      "Content-Type": "application/json",
23
      Authorization: "Bearer " + auth.api_key,
24
    },
25
    body: JSON.stringify(body),
26
  });
27
  if (!response.ok) {
28
    const text = await response.text();
29
    throw new Error(`${response.status} ${text}`);
30
  }
31
  return await response.json();
32
}
33