| 1 | import dotenv from "dotenv"; |
| 2 | import { z } from "zod"; |
| 3 | import { AnthropicApi } from "./apis/Anthropic.js"; |
| 4 | import { AzureApi } from "./apis/Azure.js"; |
| 5 | import { BedrockApi } from "./apis/Bedrock.js"; |
| 6 | import { CohereApi } from "./apis/Cohere.js"; |
| 7 | import { ContinueProxyApi } from "./apis/ContinueProxy.js"; |
| 8 | import { DeepSeekApi } from "./apis/DeepSeek.js"; |
| 9 | import { GeminiApi } from "./apis/Gemini.js"; |
| 10 | import { InceptionApi } from "./apis/Inception.js"; |
| 11 | import { JinaApi } from "./apis/Jina.js"; |
| 12 | import { LlamastackApi } from "./apis/LlamaStack.js"; |
| 13 | import { MockApi } from "./apis/Mock.js"; |
| 14 | import { MoonshotApi } from "./apis/Moonshot.js"; |
| 15 | import { OpenAIApi } from "./apis/OpenAI.js"; |
| 16 | import { RelaceApi } from "./apis/Relace.js"; |
| 17 | import { VertexAIApi } from "./apis/VertexAI.js"; |
| 18 | import { WatsonXApi } from "./apis/WatsonX.js"; |
| 19 | import { BaseLlmApi } from "./apis/base.js"; |
| 20 | import { LLMConfig, OpenAIConfigSchema } from "./types.js"; |
| 21 | import { CometAPIApi } from "./apis/CometAPI.js"; |
| 22 | import { appendPathToUrlIfNotPresent } from "./util/appendPathToUrl.js"; |
| 23 | |
| 24 | dotenv.config(); |
| 25 | |
| 26 | function openAICompatible( |
| 27 | apiBase: string, |
| 28 | config: z.infer<typeof OpenAIConfigSchema>, |
| 29 | ): OpenAIApi { |
| 30 | return new OpenAIApi({ |
| 31 | ...config, |
| 32 | apiBase: config.apiBase ?? apiBase, |
| 33 | }); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Detects if a HuggingFace API URL is using an OpenAI-compatible router |
| 38 | * @param url The URL to check |
| 39 | * @returns true if the URL appears to be using an OpenAI-compatible router |
| 40 | */ |
| 41 | function isHuggingFaceOpenAICompatible(url: string): boolean { |
| 42 | if (!url) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | // Normalize the URL to lowercase for case-insensitive matching |
| 47 | const normalizedUrl = url.toLowerCase(); |
| 48 | |
| 49 | // Check for common OpenAI-compatible patterns |
| 50 | const openAIPatterns = [ |
| 51 | "/v1/", // Standard OpenAI v1 API pattern |
| 52 | "/openai/", // Explicit OpenAI compatibility path |
| 53 | "/v1/chat/completions", // Specific OpenAI chat completions endpoint |
| 54 | "/v1/completions", // OpenAI completions endpoint |
| 55 | "/v1/embeddings", // OpenAI embeddings endpoint |
| 56 | "/v1/models", // OpenAI models endpoint |
| 57 | ]; |
| 58 | |
| 59 | // Check if the URL contains any of the OpenAI-compatible patterns |
| 60 | return openAIPatterns.some((pattern) => normalizedUrl.includes(pattern)); |
| 61 | } |
| 62 | |
| 63 | export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined { |
| 64 | switch (config.provider) { |
| 65 | case "openai": |
| 66 | return new OpenAIApi(config); |
| 67 | case "azure": |
| 68 | return new AzureApi(config); |
| 69 | case "bedrock": |
| 70 | return new BedrockApi(config); |
| 71 | case "cohere": |
| 72 | return new CohereApi(config); |
| 73 | case "cometapi": |
| 74 | return new CometAPIApi(config); |
| 75 | case "anthropic": |
| 76 | return new AnthropicApi(config); |
| 77 | case "gemini": |
| 78 | return new GeminiApi(config); |
| 79 | case "jina": |
| 80 | return new JinaApi(config); |
| 81 | case "deepseek": |
| 82 | return new DeepSeekApi(config); |
| 83 | case "moonshot": |
| 84 | return new MoonshotApi(config); |
| 85 | case "relace": |
| 86 | return new RelaceApi(config); |
| 87 | case "inception": |
| 88 | return new InceptionApi(config); |
| 89 | case "watsonx": |
| 90 | return new WatsonXApi(config); |
| 91 | case "vertexai": |
| 92 | return new VertexAIApi(config); |
| 93 | case "llamastack": |
| 94 | return new LlamastackApi(config); |
| 95 | case "continue-proxy": |
| 96 | return new ContinueProxyApi(config); |
| 97 | case "xAI": |
| 98 | return openAICompatible("https://api.x.ai/v1/", config); |
| 99 | case "voyage": |
| 100 | return openAICompatible("https://api.voyageai.com/v1/", config); |
| 101 | case "mistral": |
| 102 | return openAICompatible("https://api.mistral.ai/v1/", config); |
| 103 | case "deepinfra": |
| 104 | return openAICompatible("https://api.deepinfra.com/v1/openai/", config); |
| 105 | case "vllm": |
| 106 | return openAICompatible("http://localhost:8000/v1/", config); |
| 107 | case "groq": |
| 108 | return openAICompatible("https://api.groq.com/openai/v1/", config); |
| 109 | case "sambanova": |
| 110 | return openAICompatible("https://api.sambanova.ai/v1/", config); |
| 111 | case "text-gen-webui": |
| 112 | return openAICompatible("http://127.0.0.1:5000/v1/", config); |
| 113 | case "openrouter": |
| 114 | return openAICompatible("https://openrouter.ai/api/v1/", config); |
| 115 | case "cerebras": |
| 116 | return openAICompatible("https://api.cerebras.ai/v1/", config); |
| 117 | case "kindo": |
| 118 | return openAICompatible("https://llm.kindo.ai/v1/", config); |
| 119 | case "msty": |
| 120 | return openAICompatible("http://localhost:10000", config); |
| 121 | case "nvidia": |
| 122 | return openAICompatible("https://integrate.api.nvidia.com/v1/", config); |
| 123 | case "ovhcloud": |
| 124 | return openAICompatible( |
| 125 | "https://oai.endpoints.kepler.ai.cloud.ovh.net/v1/", |
| 126 | config, |
| 127 | ); |
| 128 | case "scaleway": |
| 129 | return openAICompatible("https://api.scaleway.ai/v1/", config); |
| 130 | case "fireworks": |
| 131 | return openAICompatible("https://api.fireworks.ai/inference/v1/", config); |
| 132 | case "together": |
| 133 | return openAICompatible("https://api.together.xyz/v1/", config); |
| 134 | case "ncompass": |
| 135 | return openAICompatible("https://api.ncompass.tech/v1", config); |
| 136 | case "novita": |
| 137 | return openAICompatible("https://api.novita.ai/v3/openai", config); |
| 138 | case "nebius": |
| 139 | return openAICompatible("https://api.studio.nebius.ai/v1/", config); |
| 140 | case "function-network": |
| 141 | return openAICompatible("https://api.function.network/v1/", config); |
| 142 | case "llama.cpp": |
| 143 | case "llamafile": |
| 144 | return openAICompatible("http://localhost:8000/", config); |
| 145 | case "lmstudio": |
| 146 | return openAICompatible("http://localhost:1234/", config); |
| 147 | case "ollama": |
| 148 | // for openai compaitability, we need to add /v1 to the end of the url |
| 149 | // this is required for cli (for core, endpoints are overriden by core/llm/llms/Ollama.ts) |
| 150 | if (config.apiBase) |
| 151 | config.apiBase = appendPathToUrlIfNotPresent(config.apiBase, "v1"); |
| 152 | return openAICompatible("http://localhost:11434/v1/", config); |
| 153 | case "mock": |
| 154 | return new MockApi(); |
| 155 | case "huggingface-inference-api": |
| 156 | // Check if it's an OpenAI-compatible router |
| 157 | if (config.apiBase && isHuggingFaceOpenAICompatible(config.apiBase)) { |
| 158 | return openAICompatible(config.apiBase, config); |
| 159 | } |
| 160 | // Return undefined for native HuggingFace endpoints |
| 161 | // (handled by HuggingFaceInferenceAPI class in core) |
| 162 | return undefined; |
| 163 | default: |
| 164 | return undefined; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | export { |
| 169 | type ChatCompletion, |
| 170 | type ChatCompletionChunk, |
| 171 | type ChatCompletionCreateParams, |
| 172 | type ChatCompletionCreateParamsNonStreaming, |
| 173 | type ChatCompletionCreateParamsStreaming, |
| 174 | type Completion, |
| 175 | type CompletionCreateParams, |
| 176 | type CompletionCreateParamsNonStreaming, |
| 177 | type CompletionCreateParamsStreaming, |
| 178 | } from "openai/resources/index"; |
| 179 | |
| 180 | // export |
| 181 | export type { BaseLlmApi } from "./apis/base.js"; |
| 182 | export type { LLMConfig } from "./types.js"; |
| 183 | |
| 184 | export { |
| 185 | addCacheControlToLastTwoUserMessages, |
| 186 | getAnthropicErrorMessage, |
| 187 | getAnthropicHeaders, |
| 188 | getAnthropicMediaTypeFromDataUrl, |
| 189 | } from "./apis/AnthropicUtils.js"; |