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

Create a thread.

Created by hugo697 156 days ago Viewed 5628 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 156 days ago
1
type Openai = {
2
  api_key: string;
3
  organization_id: string;
4
};
5
/**
6
 * Create thread
7
 * Create a thread.
8
 */
9
export async function main(
10
  auth: Openai,
11
  body: {
12
    messages?: {
13
      role: "user";
14
      content: string;
15
      file_ids?: string[];
16
      metadata?: { [k: string]: unknown };
17
    }[];
18
    metadata?: { [k: string]: unknown };
19
  }
20
) {
21
  const url = new URL(`https://api.openai.com/v1/threads`);
22

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