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

Create a run.

Created by hugo697 392 days ago Viewed 9708 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 run
7
 * Create a run.
8
 */
9
export async function main(
10
  auth: Openai,
11
  thread_id: string,
12
  body: {
13
    assistant_id: string;
14
    model?: string;
15
    instructions?: string;
16
    additional_instructions?: string;
17
    tools?: (
18
      | { type: "code_interpreter"; [k: string]: unknown }
19
      | { type: "retrieval"; [k: string]: unknown }
20
      | {
21
          type: "function";
22
          function: {
23
            description?: string;
24
            name: string;
25
            parameters?: { [k: string]: unknown };
26
            [k: string]: unknown;
27
          };
28
          [k: string]: unknown;
29
        }
30
    )[];
31
    metadata?: { [k: string]: unknown };
32
  }
33
) {
34
  const url = new URL(`https://api.openai.com/v1/threads/${thread_id}/runs`);
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