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

Create an assistant with a model and instructions.

Created by hugo697 392 days ago Viewed 9760 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 392 days ago
1
type Openai = {
2
  api_key: string;
3
  organization_id: string;
4
};
5
/**
6
 * Create assistant
7
 * Create an assistant with a model and instructions.
8
 */
9
export async function main(
10
  auth: Openai,
11
  body: {
12
    model: string;
13
    name?: string;
14
    description?: string;
15
    instructions?: string;
16
    tools?: (
17
      | { type: "code_interpreter"; [k: string]: unknown }
18
      | { type: "retrieval"; [k: string]: unknown }
19
      | {
20
          type: "function";
21
          function: {
22
            description?: string;
23
            name: string;
24
            parameters?: { [k: string]: unknown };
25
            [k: string]: unknown;
26
          };
27
          [k: string]: unknown;
28
        }
29
    )[];
30
    file_ids?: string[];
31
    metadata?: { [k: string]: unknown };
32
  }
33
) {
34
  const url = new URL(`https://api.openai.com/v1/assistants`);
35

36
  const response = await fetch(url, {
37
    method: "POST",
38
    headers: {
39
      "OpenAI-Organization": auth.organization_id,
40
      "Content-Type": "application/json",
41
      Authorization: "Bearer " + auth.api_key,
42
    },
43
    body: JSON.stringify(body),
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51