Changesets · Branches · Tags · Compare
| Changeset | 0c81f3ad06448bd7e9672a995f814630b8b2b1bda7dc487f41de9c4ab8e2b957 |
|---|---|
| Author | Suhas S <suhas.cse20@gmail.com> · 2025-07-25 19:23 +08:00 |
| Committer | Suhas S <suhas.cse20@gmail.com> · 2025-07-25 19:23 +08:00 |
| Parents | f326d567c433 |
2 files changed
modified packages/openai-adapters/src/index.ts +35 −0
| @@ -31,6 +31,33 @@ | ||
| 31 | 31 | }); |
| 32 | 32 | } |
| 33 | 33 | |
| 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 | + | |
| 34 | 61 | export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined { |
| 35 | 62 | switch (config.provider) { |
| 36 | 63 | case "openai": |
| @@ -115,6 +142,14 @@ | ||
| 115 | 142 | return openAICompatible("http://localhost:1234/", config); |
| 116 | 143 | case "mock": |
| 117 | 144 | 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; | |
| 118 | 153 | default: |
| 119 | 154 | return undefined; |
| 120 | 155 | } |
modified packages/openai-adapters/src/types.ts +1 −0
| @@ -57,6 +57,7 @@ | ||
| 57 | 57 | z.literal("scaleway"), |
| 58 | 58 | z.literal("ncompass"), |
| 59 | 59 | z.literal("relace"), |
| 60 | + z.literal("huggingface-inference-api"), | |
| 60 | 61 | ]), |
| 61 | 62 | }); |
| 62 | 63 | export type OpenAIConfig = z.infer<typeof OpenAIConfigSchema>; |