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

Create a message.

Created by hugo697 156 days ago Viewed 5739 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 message
7
 * Create a message.
8
 */
9
export async function main(
10
  auth: Openai,
11
  thread_id: string,
12
  body: {
13
    role: "user";
14
    content: string;
15
    file_ids?: string[];
16
    metadata?: { [k: string]: unknown };
17
  }
18
) {
19
  const url = new URL(
20
    `https://api.openai.com/v1/threads/${thread_id}/messages`
21
  );
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