mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-11 02:02:22 +00:00
21 lines
675 B
JavaScript
21 lines
675 B
JavaScript
function getChatCompletionsUrl(baseURL) {
|
|
const root = (baseURL || "https://api.openai.com/v1").replace(/\/+$/, "");
|
|
return `${root}/chat/completions`;
|
|
}
|
|
|
|
export async function chat({ apiKey, baseURL, model, messages, tools }) {
|
|
const res = await fetch(getChatCompletionsUrl(baseURL), {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Authorization": `Bearer ${apiKey}`,
|
|
},
|
|
body: JSON.stringify({ model, messages, tools }),
|
|
});
|
|
if (!res.ok) {
|
|
const text = await res.text();
|
|
throw new Error(`OpenAI error ${res.status}: ${text}`);
|
|
}
|
|
return await res.json();
|
|
}
|