async function fetchOpenAI(endpoint, parameters) {
const { api_key, ...params } = {
...parameters,
api_key: API_KEY ?? openaiApiKey
};
if (!api_key) {
throw new Error(
"The @osteele/openai-api module must be imported with a `with` statement. See that notebook for usage instructions."
);
}
const response = await fetch(`https://api.openai.com/v1/${endpoint}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${api_key}`
},
body: JSON.stringify(params)
});
const data = await response.json();
if (response.status >= 400) {
throw new Error(data.error.message);
}
return data;
}