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
This commit is contained in:
Suhas S 2025-07-25 19:23:25 +08:00
parent 1af5d0369b
commit a8a03d7d98
2 changed files with 36 additions and 0 deletions

View file

@ -31,6 +31,33 @@ function openAICompatible(
});
}
/**
* Detects if a HuggingFace API URL is using an OpenAI-compatible router
* @param url The URL to check
* @returns true if the URL appears to be using an OpenAI-compatible router
*/
function isHuggingFaceOpenAICompatible(url: string): boolean {
if (!url) {
return false;
}
// Normalize the URL to lowercase for case-insensitive matching
const normalizedUrl = url.toLowerCase();
// Check for common OpenAI-compatible patterns
const openAIPatterns = [
'/v1/', // Standard OpenAI v1 API pattern
'/openai/', // Explicit OpenAI compatibility path
'/v1/chat/completions', // Specific OpenAI chat completions endpoint
'/v1/completions', // OpenAI completions endpoint
'/v1/embeddings', // OpenAI embeddings endpoint
'/v1/models', // OpenAI models endpoint
];
// Check if the URL contains any of the OpenAI-compatible patterns
return openAIPatterns.some(pattern => normalizedUrl.includes(pattern));
}
export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined {
switch (config.provider) {
case "openai":
@ -115,6 +142,14 @@ export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined {
return openAICompatible("http://localhost:1234/", config);
case "mock":
return new MockApi();
case "huggingface-inference-api":
// Check if it's an OpenAI-compatible router
if (config.apiBase && isHuggingFaceOpenAICompatible(config.apiBase)) {
return openAICompatible(config.apiBase, config);
}
// Return undefined for native HuggingFace endpoints
// (handled by HuggingFaceInferenceAPI class in core)
return undefined;
default:
return undefined;
}

View file

@ -57,6 +57,7 @@ export const OpenAIConfigSchema = BasePlusConfig.extend({
z.literal("scaleway"),
z.literal("ncompass"),
z.literal("relace"),
z.literal("huggingface-inference-api"),
]),
});
export type OpenAIConfig = z.infer<typeof OpenAIConfigSchema>;