34 lines
1.3 KiB
JavaScript
34 lines
1.3 KiB
JavaScript
|
|
// server/provider.mjs — the endpoint client. One OpenAI-compatible
|
||
|
|
// endpoint per deployment (server.json "endpoint"); the model name
|
||
|
|
// comes from the run (run.json override) or the config default.
|
||
|
|
export function makeProvider(endpoint) {
|
||
|
|
const base = String(endpoint.baseUrl ?? '').replace(/\/+$/, '');
|
||
|
|
if (!base) throw new Error('server.json: endpoint.baseUrl is required');
|
||
|
|
const key = endpoint.apiKey ?? process.env[endpoint.apiKeyEnv ?? 'RP_API_KEY'] ?? '';
|
||
|
|
const headers = { 'content-type': 'application/json', authorization: `Bearer ${key}` };
|
||
|
|
|
||
|
|
return {
|
||
|
|
async chat(model, messages, tools, params = {}) {
|
||
|
|
const res = await fetch(`${base}/chat/completions`, {
|
||
|
|
method: 'POST',
|
||
|
|
headers,
|
||
|
|
body: JSON.stringify({ model, messages, ...(tools ? { tools, tool_choice: 'auto' } : {}), ...params }),
|
||
|
|
});
|
||
|
|
if (!res.ok) {
|
||
|
|
throw new Error(`API ${res.status}: ${(await res.text()).slice(0, 300)}`);
|
||
|
|
}
|
||
|
|
const data = await res.json();
|
||
|
|
return data;
|
||
|
|
},
|
||
|
|
|
||
|
|
async models() {
|
||
|
|
const res = await fetch(`${base}/models`, { headers });
|
||
|
|
if (!res.ok) {
|
||
|
|
throw new Error(`API ${res.status}: ${(await res.text()).slice(0, 300)}`);
|
||
|
|
}
|
||
|
|
const data = await res.json();
|
||
|
|
return (data.data ?? []).map((m) => m.id).sort();
|
||
|
|
},
|
||
|
|
};
|
||
|
|
}
|