| 1 | import { ModelConfig } from "@continuedev/config-yaml"; |
| 2 | |
| 3 | import { |
| 4 | ContinueConfig, |
| 5 | IDE, |
| 6 | IdeSettings, |
| 7 | ILLMLogger, |
| 8 | LLMOptions, |
| 9 | } from "../.."; |
| 10 | import { BaseLLM } from "../../llm"; |
| 11 | import { LLMClasses } from "../../llm/llms"; |
| 12 | |
| 13 | const AUTODETECT = "AUTODETECT"; |
| 14 | |
| 15 | function getModelClass( |
| 16 | model: ModelConfig, |
| 17 | ): (typeof LLMClasses)[number] | undefined { |
| 18 | return LLMClasses.find((llm) => llm.providerName === model.provider); |
| 19 | } |
| 20 | |
| 21 | // function getContinueProxyModelName( |
| 22 | // ownerSlug: string, |
| 23 | // packageSlug: string, |
| 24 | // model: ModelConfig, |
| 25 | // ): string { |
| 26 | // return `${ownerSlug}/${packageSlug}/${model.provider}/${model.model}`; |
| 27 | // } |
| 28 | |
| 29 | async function modelConfigToBaseLLM({ |
| 30 | model, |
| 31 | uniqueId, |
| 32 | ideSettings, |
| 33 | llmLogger, |
| 34 | config, |
| 35 | }: { |
| 36 | model: ModelConfig; |
| 37 | uniqueId: string; |
| 38 | ideSettings: IdeSettings; |
| 39 | llmLogger: ILLMLogger; |
| 40 | config: ContinueConfig; |
| 41 | }): Promise<BaseLLM | undefined> { |
| 42 | const cls = getModelClass(model); |
| 43 | |
| 44 | if (!cls) { |
| 45 | return undefined; |
| 46 | } |
| 47 | |
| 48 | const { capabilities, ...rest } = model; |
| 49 | |
| 50 | let options: LLMOptions = { |
| 51 | ...rest, |
| 52 | contextLength: model.defaultCompletionOptions?.contextLength, |
| 53 | completionOptions: { |
| 54 | ...(model.defaultCompletionOptions ?? {}), |
| 55 | model: model.model, |
| 56 | maxTokens: |
| 57 | model.defaultCompletionOptions?.maxTokens ?? |
| 58 | cls.defaultOptions?.completionOptions?.maxTokens, |
| 59 | }, |
| 60 | logger: llmLogger, |
| 61 | uniqueId, |
| 62 | title: model.name, |
| 63 | template: model.promptTemplates?.chat, |
| 64 | promptTemplates: model.promptTemplates, |
| 65 | baseAgentSystemMessage: model.chatOptions?.baseAgentSystemMessage, |
| 66 | basePlanSystemMessage: model.chatOptions?.basePlanSystemMessage, |
| 67 | baseChatSystemMessage: model.chatOptions?.baseSystemMessage, |
| 68 | capabilities: { |
| 69 | tools: model.capabilities?.includes("tool_use"), |
| 70 | uploadImage: model.capabilities?.includes("image_input"), |
| 71 | nextEdit: model.capabilities?.includes("next_edit"), |
| 72 | }, |
| 73 | autocompleteOptions: model.autocompleteOptions, |
| 74 | }; |
| 75 | |
| 76 | // Model capabilities - need to be undefined if not found |
| 77 | // To fallback to our autodetection |
| 78 | if (capabilities?.find((c) => c === "tool_use")) { |
| 79 | options.capabilities = { |
| 80 | ...options.capabilities, |
| 81 | tools: true, |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | if (capabilities?.find((c) => c === "image_input")) { |
| 86 | options.capabilities = { |
| 87 | ...options.capabilities, |
| 88 | uploadImage: true, |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | if (model.embedOptions?.maxBatchSize) { |
| 93 | options.maxEmbeddingBatchSize = model.embedOptions.maxBatchSize; |
| 94 | } |
| 95 | if (model.embedOptions?.maxChunkSize) { |
| 96 | options.maxEmbeddingChunkSize = model.embedOptions.maxChunkSize; |
| 97 | } |
| 98 | |
| 99 | // These are params that are at model config level in JSON |
| 100 | // But we decided to move to nested `env` in YAML |
| 101 | // Since types vary and we don't want to blindly spread env for now, |
| 102 | // Each one is handled individually here |
| 103 | const env = model.env ?? {}; |
| 104 | if ( |
| 105 | "useLegacyCompletionsEndpoint" in env && |
| 106 | typeof env.useLegacyCompletionsEndpoint === "boolean" |
| 107 | ) { |
| 108 | options.useLegacyCompletionsEndpoint = env.useLegacyCompletionsEndpoint; |
| 109 | } |
| 110 | if ("apiType" in env && typeof env.apiType === "string") { |
| 111 | options.apiType = env.apiType; |
| 112 | } |
| 113 | if ("apiVersion" in env && typeof env.apiVersion === "string") { |
| 114 | options.apiVersion = env.apiVersion; |
| 115 | } |
| 116 | if ("deployment" in env && typeof env.deployment === "string") { |
| 117 | options.deployment = env.deployment; |
| 118 | } |
| 119 | if ("deploymentId" in env && typeof env.deploymentId === "string") { |
| 120 | options.deploymentId = env.deploymentId; |
| 121 | } |
| 122 | if ("projectId" in env && typeof env.projectId === "string") { |
| 123 | options.projectId = env.projectId; |
| 124 | } |
| 125 | if ("region" in env && typeof env.region === "string") { |
| 126 | options.region = env.region; |
| 127 | } |
| 128 | if ("profile" in env && typeof env.profile === "string") { |
| 129 | options.profile = env.profile; |
| 130 | } |
| 131 | if ("accessKeyId" in env && typeof env.accessKeyId === "string") { |
| 132 | options.accessKeyId = env.accessKeyId; |
| 133 | } |
| 134 | if ("secretAccessKey" in env && typeof env.secretAccessKey === "string") { |
| 135 | options.secretAccessKey = env.secretAccessKey; |
| 136 | } |
| 137 | if ("modelArn" in env && typeof env.modelArn === "string") { |
| 138 | options.modelArn = env.modelArn; |
| 139 | } |
| 140 | if ("aiGatewaySlug" in env && typeof env.aiGatewaySlug === "string") { |
| 141 | options.aiGatewaySlug = env.aiGatewaySlug; |
| 142 | } |
| 143 | if ("accountId" in env && typeof env.accountId === "string") { |
| 144 | options.accountId = env.accountId; |
| 145 | } |
| 146 | |
| 147 | const llm = new cls(options); |
| 148 | return llm; |
| 149 | } |
| 150 | |
| 151 | async function autodetectModels({ |
| 152 | llm, |
| 153 | model, |
| 154 | ide, |
| 155 | uniqueId, |
| 156 | ideSettings, |
| 157 | llmLogger, |
| 158 | config, |
| 159 | }: { |
| 160 | llm: BaseLLM; |
| 161 | model: ModelConfig; |
| 162 | ide: IDE; |
| 163 | uniqueId: string; |
| 164 | ideSettings: IdeSettings; |
| 165 | llmLogger: ILLMLogger; |
| 166 | config: ContinueConfig; |
| 167 | }): Promise<BaseLLM[]> { |
| 168 | try { |
| 169 | const modelNames = await llm.listModels(); |
| 170 | const detectedModels = await Promise.all( |
| 171 | modelNames.map(async (modelName) => { |
| 172 | // To ensure there are no infinite loops |
| 173 | if (modelName === AUTODETECT) { |
| 174 | return undefined; |
| 175 | } |
| 176 | |
| 177 | return await modelConfigToBaseLLM({ |
| 178 | model: { |
| 179 | ...model, |
| 180 | model: modelName, |
| 181 | name: modelName, |
| 182 | }, |
| 183 | uniqueId, |
| 184 | ideSettings, |
| 185 | llmLogger, |
| 186 | config, |
| 187 | }); |
| 188 | }), |
| 189 | ); |
| 190 | return detectedModels.filter((x) => typeof x !== "undefined") as BaseLLM[]; |
| 191 | } catch (e) { |
| 192 | console.warn("Error listing models: ", e); |
| 193 | return []; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | export async function llmsFromModelConfig({ |
| 198 | model, |
| 199 | ide, |
| 200 | uniqueId, |
| 201 | ideSettings, |
| 202 | llmLogger, |
| 203 | config, |
| 204 | }: { |
| 205 | model: ModelConfig; |
| 206 | ide: IDE; |
| 207 | uniqueId: string; |
| 208 | ideSettings: IdeSettings; |
| 209 | llmLogger: ILLMLogger; |
| 210 | config: ContinueConfig; |
| 211 | }): Promise<BaseLLM[]> { |
| 212 | const baseLlm = await modelConfigToBaseLLM({ |
| 213 | model, |
| 214 | uniqueId, |
| 215 | ideSettings, |
| 216 | llmLogger, |
| 217 | config, |
| 218 | }); |
| 219 | if (!baseLlm) { |
| 220 | return []; |
| 221 | } |
| 222 | |
| 223 | if (model.model === AUTODETECT) { |
| 224 | const models = await autodetectModels({ |
| 225 | llm: baseLlm, |
| 226 | model, |
| 227 | ide, |
| 228 | uniqueId, |
| 229 | ideSettings, |
| 230 | llmLogger, |
| 231 | config, |
| 232 | }); |
| 233 | return models; |
| 234 | } else { |
| 235 | return [baseLlm]; |
| 236 | } |
| 237 | } |