Skip to content

Commit 034dc97

Browse files
committed
Fix Hugging Face provider endpoint to router API
1 parent 56a6d96 commit 034dc97

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

engine/src/providers.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ function decodeDataUri(value) {
141141
return Buffer.from(match[1], 'base64');
142142
}
143143

144+
function normalizeBaseUrl(value, fallback) {
145+
const raw = String(value || fallback || '').trim();
146+
if (!raw) return '';
147+
return raw.replace(/\/+$/, '');
148+
}
149+
150+
function buildHuggingFaceModelUrl(providerConfig, model) {
151+
const configuredBase = resolveProviderValue(providerConfig, 'base_url', 'HUGGINGFACE_BASE_URL');
152+
const base = normalizeBaseUrl(configuredBase, 'https://router.huggingface.co/hf-inference');
153+
return `${base}/models/${encodeURIComponent(String(model || '').trim())}`;
154+
}
155+
144156
const NO_TEXT_IMAGE_SUFFIX = [
145157
'STRICT NO-TEXT RULE:',
146158
'Do not render any words, letters, numbers, symbols, labels, signs, logos, UI text, speech bubbles, subtitles, captions, or watermarks.',
@@ -266,7 +278,7 @@ async function generateTextWithProvider(providerConfig, prompt, runtimeConfig) {
266278
if (provider === 'huggingface') {
267279
const apiKey = resolveProviderValue(providerConfig, 'api_key', 'HUGGINGFACE_INFERENCE_API_TOKEN') || process.env.HUGGINGFACE_API_KEY || '';
268280
if (!apiKey) throw new Error('Missing HUGGINGFACE_INFERENCE_API_TOKEN for Hugging Face text provider');
269-
const { json } = await fetchJson(`https://api-inference.huggingface.co/models/${model}`, {
281+
const { json } = await fetchJson(buildHuggingFaceModelUrl(providerConfig, model), {
270282
method: 'POST',
271283
headers: {
272284
Authorization: `Bearer ${apiKey}`,
@@ -449,7 +461,7 @@ async function generateImageWithProvider(providerConfig, prompt, runtimeConfig,
449461
if (provider === 'huggingface') {
450462
const apiKey = resolveProviderValue(providerConfig, 'api_key', 'HUGGINGFACE_INFERENCE_API_TOKEN') || process.env.HUGGINGFACE_API_KEY || '';
451463
if (!apiKey) throw new Error('Missing HUGGINGFACE_INFERENCE_API_TOKEN for Hugging Face image provider');
452-
const response = await withTimeout(fetch(`https://api-inference.huggingface.co/models/${model}`, {
464+
const response = await withTimeout(fetch(buildHuggingFaceModelUrl(providerConfig, model), {
453465
method: 'POST',
454466
headers: {
455467
Authorization: `Bearer ${apiKey}`,

engine/tests/providers-temperature.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ describe('provider text temperature wiring', () => {
6767
{ timeout_ms: 1000, text_temperature: 1.1 }
6868
);
6969

70+
const url = String(fetchMock.mock.calls[0][0] || '');
7071
const init = fetchMock.mock.calls[0][1];
7172
const body = JSON.parse(String(init.body || '{}'));
73+
expect(url).toContain('router.huggingface.co/hf-inference/models/');
7274
expect(body.inputs).toBe('hello');
7375
expect(body.parameters.max_new_tokens).toBe(512);
7476
expect(body.parameters.temperature).toBe(1.1);

0 commit comments

Comments
 (0)