| 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 { CohereApi } from "./apis/Cohere.js"; |
| 6 | import { DeepSeekApi } from "./apis/DeepSeek.js"; |
| 7 | import { GeminiApi } from "./apis/Gemini.js"; |
| 8 | import { InceptionApi } from "./apis/Inception.js"; |
| 9 | import { JinaApi } from "./apis/Jina.js"; |
| 10 | import { MockApi } from "./apis/Mock.js"; |
| 11 | import { MoonshotApi } from "./apis/Moonshot.js"; |
| 12 | import { OpenAIApi } from "./apis/OpenAI.js"; |
| 13 | import { RelaceApi } from "./apis/Relace.js"; |
| 14 | import { WatsonXApi } from "./apis/WatsonX.js"; |
| 15 | import { BaseLlmApi } from "./apis/base.js"; |
| 16 | import { LLMConfig, OpenAIConfigSchema } from "./types.js"; |
| 17 | |
| 18 | dotenv.config(); |
| 19 | |
| 20 | function openAICompatible( |
| 21 | apiBase: string, |
| 22 | config: z.infer<typeof OpenAIConfigSchema>, |
| 23 | ): OpenAIApi { |
| 24 | return new OpenAIApi({ |
| 25 | ...config, |
| 26 | apiBase: config.apiBase ?? apiBase, |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | export function constructLlmApi(config: LLMConfig): BaseLlmApi | undefined { |
| 31 | switch (config.provider) { |
| 32 | case "openai": |
| 33 | return new OpenAIApi(config); |
| 34 | case "azure": |
| 35 | return new AzureApi(config); |
| 36 | case "cohere": |
| 37 | return new CohereApi(config); |
| 38 | case "anthropic": |
| 39 | return new AnthropicApi(config); |
| 40 | case "gemini": |
| 41 | return new GeminiApi(config); |
| 42 | case "jina": |
| 43 | return new JinaApi(config); |
| 44 | case "deepseek": |
| 45 | return new DeepSeekApi(config); |
| 46 | case "moonshot": |
| 47 | return new MoonshotApi(config); |
| 48 | case "relace": |
| 49 | return new RelaceApi(config); |
| 50 | case "inception": |
| 51 | return new InceptionApi(config); |
| 52 | case "watsonx": |
| 53 | return new WatsonXApi(config); |
| 54 | case "x-ai": |
| 55 | return openAICompatible("https://api.x.ai/v1/", config); |
| 56 | case "voyage": |
| 57 | return openAICompatible("https://api.voyageai.com/v1/", config); |
| 58 | case "mistral": |
| 59 | return openAICompatible("https://api.mistral.ai/v1/", config); |
| 60 | case "deepinfra": |
| 61 | return openAICompatible("https://api.deepinfra.com/v1/openai/", config); |
| 62 | case "vllm": |
| 63 | return openAICompatible("http://localhost:8000/v1/", config); |
| 64 | case "groq": |
| 65 | return openAICompatible("https://api.groq.com/openai/v1/", config); |
| 66 | case "sambanova": |
| 67 | return openAICompatible("https://api.sambanova.ai/v1/", config); |
| 68 | case "text-gen-webui": |
| 69 | return openAICompatible("http://127.0.0.1:5000/v1/", config); |
| 70 | case "openrouter": |
| 71 | return openAICompatible("https://openrouter.ai/api/v1/", config); |
| 72 | case "cerebras": |
| 73 | return openAICompatible("https://api.cerebras.ai/v1/", config); |
| 74 | case "kindo": |
| 75 | return openAICompatible("https://llm.kindo.ai/v1/", config); |
| 76 | case "msty": |
| 77 | return openAICompatible("http://localhost:10000", config); |
| 78 | case "nvidia": |
| 79 | return openAICompatible("https://integrate.api.nvidia.com/v1/", config); |
| 80 | case "ovhcloud": |
| 81 | return openAICompatible( |
| 82 | "https://oai.endpoints.kepler.ai.cloud.ovh.net/v1/", |
| 83 | config, |
| 84 | ); |
| 85 | case "scaleway": |
| 86 | return openAICompatible("https://api.scaleway.ai/v1/", config); |
| 87 | case "fireworks": |
| 88 | return openAICompatible("https://api.fireworks.ai/inference/v1/", config); |
| 89 | case "together": |
| 90 | return openAICompatible("https://api.together.xyz/v1/", config); |
| 91 | case "ncompass": |
| 92 | return openAICompatible("https://api.ncompass.tech/v1", config); |
| 93 | case "novita": |
| 94 | return openAICompatible("https://api.novita.ai/v3/openai", config); |
| 95 | case "nebius": |
| 96 | return openAICompatible("https://api.studio.nebius.ai/v1/", config); |
| 97 | case "function-network": |
| 98 | return openAICompatible("https://api.function.network/v1/", config); |
| 99 | case "llama.cpp": |
| 100 | case "llamafile": |
| 101 | return openAICompatible("http://localhost:8000/", config); |
| 102 | case "lmstudio": |
| 103 | return openAICompatible("http://localhost:1234/", config); |
| 104 | case "mock": |
| 105 | return new MockApi(); |
| 106 | default: |
| 107 | return undefined; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | export { |
| 112 | type ChatCompletion, |
| 113 | type ChatCompletionChunk, |
| 114 | type ChatCompletionCreateParams, |
| 115 | type ChatCompletionCreateParamsNonStreaming, |
| 116 | type ChatCompletionCreateParamsStreaming, |
| 117 | type Completion, |
| 118 | type CompletionCreateParams, |
| 119 | type CompletionCreateParamsNonStreaming, |
| 120 | type CompletionCreateParamsStreaming, |
| 121 | } from "openai/resources/index"; |
| 122 | |
| 123 | // export |
| 124 | export type { BaseLlmApi } from "./apis/base.js"; |
| 125 | export type { LLMConfig } from "./types.js"; |