Skip to content

Commit a8a03d7

Browse files
committed
feat: add HuggingFace OpenAI-compatible router detection
- Add isHuggingFaceOpenAICompatible function to detect OpenAI-compatible HuggingFace endpoints - Add huggingface-inference-api provider to constructLlmApi switch case - Route OpenAI-compatible HuggingFace endpoints through OpenAI adapter - Return undefined for native HuggingFace endpoints to use existing HuggingFaceInferenceAPI class - Add huggingface-inference-api to provider types in OpenAIConfigSchema Fixes #5763
1 parent 1af5d03 commit a8a03d7

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

packages/openai-adapters/src/index.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,33 @@ function openAICompatible(
3131
});
3232
}
3333

34+
/**
35+
* Detects if a HuggingFace API URL is using an OpenAI-compatible router
36+
* @param url The URL to check
37+
* @returns true if the URL appears to be using an OpenAI-compatible router
38+
*/
39+
function isHuggingFaceOpenAICompatible(url: string): boolean {
40+
if (!url) {
41+
return false;
42+
}
43+
44+
// Normalize the URL to lowercase for case-insensitive matching
45+
const normalizedUrl = url.toLowerCase();
46+
47+
// Check for common OpenAI-compatible patterns
48+
const openAIPatterns = [
49+
'/v1/', // Standard OpenAI v1 API pattern
50+
'/openai/', // Explicit OpenAI compatibility path
51+
'/v1/chat/completions', // Specific OpenAI chat completions endpoint
52+
'/v1/completions', // OpenAI completions endpoint
53+
'/v1/embeddings', // OpenAI embeddings endpoint
54+
'/v1/models', // OpenAI models endpoint
55+
];
56+
57+
// Check if the URL contains any of the OpenAI-compatible patterns
58+
return openAIPatterns.some(pattern => normalizedUrl.includes(pattern));
59+
}
60+
3461
export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined {
3562
switch (config.provider) {
3663
case "openai":
@@ -115,6 +142,14 @@ export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined {
115142
return openAICompatible("http://localhost:1234/", config);
116143
case "mock":
117144
return new MockApi();
145+
case "huggingface-inference-api":
146+
// Check if it's an OpenAI-compatible router
147+
if (config.apiBase && isHuggingFaceOpenAICompatible(config.apiBase)) {
148+
return openAICompatible(config.apiBase, config);
149+
}
150+
// Return undefined for native HuggingFace endpoints
151+
// (handled by HuggingFaceInferenceAPI class in core)
152+
return undefined;
118153
default:
119154
return undefined;
120155
}

packages/openai-adapters/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export const OpenAIConfigSchema = BasePlusConfig.extend({
5757
z.literal("scaleway"),
5858
z.literal("ncompass"),
5959
z.literal("relace"),
60+
z.literal("huggingface-inference-api"),
6061
]),
6162
});
6263
export type OpenAIConfig = z.infer<typeof OpenAIConfigSchema>;

0 commit comments

Comments
 (0)