| 1 | import Handlebars from "handlebars"; |
| 2 | import { |
| 3 | BaseCompletionOptions, |
| 4 | IdeSettings, |
| 5 | ILLM, |
| 6 | ILLMLogger, |
| 7 | JSONModelDescription, |
| 8 | LLMOptions, |
| 9 | } from "../.."; |
| 10 | import { renderTemplatedString } from "../../util/handlebars/renderTemplatedString"; |
| 11 | import { BaseLLM } from "../index"; |
| 12 | import Anthropic from "./Anthropic"; |
| 13 | import Asksage from "./Asksage"; |
| 14 | import Azure from "./Azure"; |
| 15 | import Bedrock from "./Bedrock"; |
| 16 | import BedrockImport from "./BedrockImport"; |
| 17 | import Cerebras from "./Cerebras"; |
| 18 | import Cloudflare from "./Cloudflare"; |
| 19 | import Cohere from "./Cohere"; |
| 20 | import DeepInfra from "./DeepInfra"; |
| 21 | import Deepseek from "./Deepseek"; |
| 22 | import Docker from "./Docker"; |
| 23 | import Fireworks from "./Fireworks"; |
| 24 | import Flowise from "./Flowise"; |
| 25 | import FunctionNetwork from "./FunctionNetwork"; |
| 26 | import Gemini from "./Gemini"; |
| 27 | import Groq from "./Groq"; |
| 28 | import HuggingFaceInferenceAPI from "./HuggingFaceInferenceAPI"; |
| 29 | import HuggingFaceTEIEmbeddingsProvider from "./HuggingFaceTEI"; |
| 30 | import HuggingFaceTGI from "./HuggingFaceTGI"; |
| 31 | import Inception from "./Inception"; |
| 32 | import Kindo from "./Kindo"; |
| 33 | import LlamaCpp from "./LlamaCpp"; |
| 34 | import Llamafile from "./Llamafile"; |
| 35 | import LlamaStack from "./LlamaStack"; |
| 36 | import Lemonade from "./Lemonade"; |
| 37 | import LMStudio from "./LMStudio"; |
| 38 | import Mistral from "./Mistral"; |
| 39 | import MockLLM from "./Mock"; |
| 40 | import Moonshot from "./Moonshot"; |
| 41 | import Msty from "./Msty"; |
| 42 | import NCompass from "./NCompass"; |
| 43 | import Nebius from "./Nebius"; |
| 44 | import Novita from "./Novita"; |
| 45 | import Nvidia from "./Nvidia"; |
| 46 | import Ollama from "./Ollama"; |
| 47 | import OpenAI from "./OpenAI"; |
| 48 | import OpenRouter from "./OpenRouter"; |
| 49 | import OVHcloud from "./OVHcloud"; |
| 50 | import { Relace } from "./Relace"; |
| 51 | import Replicate from "./Replicate"; |
| 52 | import SageMaker from "./SageMaker"; |
| 53 | import SambaNova from "./SambaNova"; |
| 54 | import Scaleway from "./Scaleway"; |
| 55 | import SiliconFlow from "./SiliconFlow"; |
| 56 | import ContinueProxy from "./stubs/ContinueProxy"; |
| 57 | import TARS from "./TARS"; |
| 58 | import TestLLM from "./Test"; |
| 59 | import TextGenWebUI from "./TextGenWebUI"; |
| 60 | import Together from "./Together"; |
| 61 | import Venice from "./Venice"; |
| 62 | import VertexAI from "./VertexAI"; |
| 63 | import Vllm from "./Vllm"; |
| 64 | import Voyage from "./Voyage"; |
| 65 | import WatsonX from "./WatsonX"; |
| 66 | import xAI from "./xAI"; |
| 67 | export const LLMClasses = [ |
| 68 | Anthropic, |
| 69 | Cohere, |
| 70 | FunctionNetwork, |
| 71 | Gemini, |
| 72 | Llamafile, |
| 73 | Moonshot, |
| 74 | Ollama, |
| 75 | Replicate, |
| 76 | TextGenWebUI, |
| 77 | Together, |
| 78 | Novita, |
| 79 | HuggingFaceTGI, |
| 80 | HuggingFaceTEIEmbeddingsProvider, |
| 81 | HuggingFaceInferenceAPI, |
| 82 | Kindo, |
| 83 | LlamaCpp, |
| 84 | OpenAI, |
| 85 | OVHcloud, |
| 86 | Lemonade, |
| 87 | LMStudio, |
| 88 | Mistral, |
| 89 | Bedrock, |
| 90 | BedrockImport, |
| 91 | SageMaker, |
| 92 | DeepInfra, |
| 93 | Flowise, |
| 94 | Groq, |
| 95 | Fireworks, |
| 96 | NCompass, |
| 97 | ContinueProxy, |
| 98 | Cloudflare, |
| 99 | Deepseek, |
| 100 | Docker, |
| 101 | Msty, |
| 102 | Azure, |
| 103 | WatsonX, |
| 104 | OpenRouter, |
| 105 | Nvidia, |
| 106 | Vllm, |
| 107 | SambaNova, |
| 108 | MockLLM, |
| 109 | TestLLM, |
| 110 | Cerebras, |
| 111 | Asksage, |
| 112 | Nebius, |
| 113 | Venice, |
| 114 | VertexAI, |
| 115 | xAI, |
| 116 | SiliconFlow, |
| 117 | Scaleway, |
| 118 | Relace, |
| 119 | Inception, |
| 120 | Voyage, |
| 121 | LlamaStack, |
| 122 | TARS, |
| 123 | ]; |
| 124 | |
| 125 | export async function llmFromDescription( |
| 126 | desc: JSONModelDescription, |
| 127 | readFile: (filepath: string) => Promise<string>, |
| 128 | getUriFromPath: (path: string) => Promise<string | undefined>, |
| 129 | uniqueId: string, |
| 130 | ideSettings: IdeSettings, |
| 131 | llmLogger: ILLMLogger, |
| 132 | completionOptions?: BaseCompletionOptions, |
| 133 | ): Promise<BaseLLM | undefined> { |
| 134 | const cls = LLMClasses.find((llm) => llm.providerName === desc.provider); |
| 135 | |
| 136 | if (!cls) { |
| 137 | return undefined; |
| 138 | } |
| 139 | |
| 140 | const finalCompletionOptions = { |
| 141 | ...completionOptions, |
| 142 | ...desc.completionOptions, |
| 143 | }; |
| 144 | |
| 145 | let baseChatSystemMessage: string | undefined = undefined; |
| 146 | if (desc.systemMessage !== undefined) { |
| 147 | // baseChatSystemMessage = DEFAULT_CHAT_SYSTEM_MESSAGE; |
| 148 | // baseChatSystemMessage += "\n\n"; |
| 149 | baseChatSystemMessage = await renderTemplatedString( |
| 150 | Handlebars, |
| 151 | desc.systemMessage, |
| 152 | {}, |
| 153 | [], |
| 154 | readFile, |
| 155 | getUriFromPath, |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | let options: LLMOptions = { |
| 160 | ...desc, |
| 161 | completionOptions: { |
| 162 | ...finalCompletionOptions, |
| 163 | model: (desc.model || cls.defaultOptions?.model) ?? "codellama-7b", |
| 164 | maxTokens: |
| 165 | finalCompletionOptions.maxTokens ?? |
| 166 | cls.defaultOptions?.completionOptions?.maxTokens, |
| 167 | }, |
| 168 | baseChatSystemMessage, |
| 169 | basePlanSystemMessage: baseChatSystemMessage, |
| 170 | baseAgentSystemMessage: baseChatSystemMessage, |
| 171 | logger: llmLogger, |
| 172 | uniqueId, |
| 173 | }; |
| 174 | |
| 175 | if (desc.provider === "continue-proxy") { |
| 176 | options.apiKey = ideSettings.userToken; |
| 177 | if (ideSettings.remoteConfigServerUrl) { |
| 178 | options.apiBase = new URL( |
| 179 | "/proxy/v1", |
| 180 | ideSettings.remoteConfigServerUrl, |
| 181 | ).toString(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return new cls(options); |
| 186 | } |
| 187 | |
| 188 | export function llmFromProviderAndOptions( |
| 189 | providerName: string, |
| 190 | llmOptions: LLMOptions, |
| 191 | ): ILLM { |
| 192 | const cls = LLMClasses.find((llm) => llm.providerName === providerName); |
| 193 | |
| 194 | if (!cls) { |
| 195 | throw new Error(`Unknown LLM provider type "${providerName}"`); |
| 196 | } |
| 197 | |
| 198 | return new cls(llmOptions); |
| 199 | } |