type Openai = {
api_key: string;
organization_id: string;
};
/**
* Create thread and run
* Create a thread and run it in one request.
*/
export async function main(
auth: Openai,
body: {
assistant_id: string;
thread?: {
messages?: {
role: "user";
content: string;
file_ids?: string[];
metadata?: { [k: string]: unknown };
}[];
metadata?: { [k: string]: unknown };
};
model?: string;
instructions?: string;
tools?: (
| { type: "code_interpreter"; [k: string]: unknown }
| { type: "retrieval"; [k: string]: unknown }
| {
type: "function";
function: {
description?: string;
name: string;
parameters?: { [k: string]: unknown };
[k: string]: unknown;
};
[k: string]: unknown;
}
)[];
metadata?: { [k: string]: unknown };
}
) {
const url = new URL(`https://api.openai.com/v1/threads/runs`);
const response = await fetch(url, {
method: "POST",
headers: {
"OpenAI-Organization": auth.organization_id,
"Content-Type": "application/json",
Authorization: "Bearer " + auth.api_key,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 328 days ago
type Openai = {
api_key: string;
organization_id: string;
};
/**
* Create thread and run
* Create a thread and run it in one request.
*/
export async function main(
auth: Openai,
body: {
assistant_id: string;
thread?: {
messages?: {
role: "user";
content: string;
file_ids?: string[];
metadata?: { [k: string]: unknown };
}[];
metadata?: { [k: string]: unknown };
};
model?: string;
instructions?: string;
tools?: (
| { type: "code_interpreter"; [k: string]: unknown }
| { type: "retrieval"; [k: string]: unknown }
| {
type: "function";
function: {
description?: string;
name: string;
parameters: { [k: string]: unknown };
[k: string]: unknown;
};
[k: string]: unknown;
}
)[];
metadata?: { [k: string]: unknown };
}
) {
const url = new URL(`https://api.openai.com/v1/threads/runs`);
const response = await fetch(url, {
method: "POST",
headers: {
"OpenAI-Organization": auth.organization_id,
"Content-Type": "application/json",
Authorization: "Bearer " + auth.api_key,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 387 days ago