Merge pull request #6797 from su0as/5763-huggingface-router-openai-compat

feat: add HuggingFace OpenAI-compatible router detection
This commit is contained in:
Dallin Romney 2025-09-12 15:18:26 -06:00 • committed by GitHub
commit f8d0b410bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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":
@ -117,6 +144,14 @@ export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined {
return openAICompatible("http://localhost:11434/v1/", 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

@ -58,6 +58,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>;