Elffuss BraidHub

kikocis/continue public

22e692d80113 · 5.9 KiBRawPermalink
1import { ModelConfig } from "@continuedev/config-yaml";
2
3import {
4 ContinueConfig,
5 IDE,
6 IdeSettings,
7 ILLMLogger,
8 LLMOptions,
9} from "../..";
10import { BaseLLM } from "../../llm";
11import { LLMClasses } from "../../llm/llms";
12
13const AUTODETECT = "AUTODETECT";
14
15function 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
29async 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 },
72 autocompleteOptions: model.autocompleteOptions,
73 };
74
75 // Model capabilities - need to be undefined if not found
76 // To fallback to our autodetection
77 if (capabilities?.find((c) => c === "tool_use")) {
78 options.capabilities = {
79 ...options.capabilities,
80 tools: true,
81 };
82 }
83
84 if (capabilities?.find((c) => c === "image_input")) {
85 options.capabilities = {
86 ...options.capabilities,
87 uploadImage: true,
88 };
89 }
90
91 if (model.embedOptions?.maxBatchSize) {
92 options.maxEmbeddingBatchSize = model.embedOptions.maxBatchSize;
93 }
94 if (model.embedOptions?.maxChunkSize) {
95 options.maxEmbeddingChunkSize = model.embedOptions.maxChunkSize;
96 }
97
98 // These are params that are at model config level in JSON
99 // But we decided to move to nested `env` in YAML
100 // Since types vary and we don't want to blindly spread env for now,
101 // Each one is handled individually here
102 const env = model.env ?? {};
103 if (
104 "useLegacyCompletionsEndpoint" in env &&
105 typeof env.useLegacyCompletionsEndpoint === "boolean"
106 ) {
107 options.useLegacyCompletionsEndpoint = env.useLegacyCompletionsEndpoint;
108 }
109 if ("apiType" in env && typeof env.apiType === "string") {
110 options.apiType = env.apiType;
111 }
112 if ("apiVersion" in env && typeof env.apiVersion === "string") {
113 options.apiVersion = env.apiVersion;
114 }
115 if ("deployment" in env && typeof env.deployment === "string") {
116 options.deployment = env.deployment;
117 }
118 if ("deploymentId" in env && typeof env.deploymentId === "string") {
119 options.deploymentId = env.deploymentId;
120 }
121 if ("projectId" in env && typeof env.projectId === "string") {
122 options.projectId = env.projectId;
123 }
124 if ("region" in env && typeof env.region === "string") {
125 options.region = env.region;
126 }
127 if ("profile" in env && typeof env.profile === "string") {
128 options.profile = env.profile;
129 }
130 if ("accessKeyId" in env && typeof env.accessKeyId === "string") {
131 options.accessKeyId = env.accessKeyId;
132 }
133 if ("secretAccessKey" in env && typeof env.secretAccessKey === "string") {
134 options.secretAccessKey = env.secretAccessKey;
135 }
136 if ("modelArn" in env && typeof env.modelArn === "string") {
137 options.modelArn = env.modelArn;
138 }
139 if ("aiGatewaySlug" in env && typeof env.aiGatewaySlug === "string") {
140 options.aiGatewaySlug = env.aiGatewaySlug;
141 }
142 if ("accountId" in env && typeof env.accountId === "string") {
143 options.accountId = env.accountId;
144 }
145
146 const llm = new cls(options);
147 return llm;
148}
149
150async function autodetectModels({
151 llm,
152 model,
153 ide,
154 uniqueId,
155 ideSettings,
156 llmLogger,
157 config,
158}: {
159 llm: BaseLLM;
160 model: ModelConfig;
161 ide: IDE;
162 uniqueId: string;
163 ideSettings: IdeSettings;
164 llmLogger: ILLMLogger;
165 config: ContinueConfig;
166}): Promise<BaseLLM[]> {
167 try {
168 const modelNames = await llm.listModels();
169 const detectedModels = await Promise.all(
170 modelNames.map(async (modelName) => {
171 // To ensure there are no infinite loops
172 if (modelName === AUTODETECT) {
173 return undefined;
174 }
175
176 return await modelConfigToBaseLLM({
177 model: {
178 ...model,
179 model: modelName,
180 name: modelName,
181 },
182 uniqueId,
183 ideSettings,
184 llmLogger,
185 config,
186 });
187 }),
188 );
189 return detectedModels.filter((x) => typeof x !== "undefined") as BaseLLM[];
190 } catch (e) {
191 console.warn("Error listing models: ", e);
192 return [];
193 }
194}
195
196export async function llmsFromModelConfig({
197 model,
198 ide,
199 uniqueId,
200 ideSettings,
201 llmLogger,
202 config,
203}: {
204 model: ModelConfig;
205 ide: IDE;
206 uniqueId: string;
207 ideSettings: IdeSettings;
208 llmLogger: ILLMLogger;
209 config: ContinueConfig;
210}): Promise<BaseLLM[]> {
211 const baseLlm = await modelConfigToBaseLLM({
212 model,
213 uniqueId,
214 ideSettings,
215 llmLogger,
216 config,
217 });
218 if (!baseLlm) {
219 return [];
220 }
221
222 if (model.model === AUTODETECT) {
223 const models = await autodetectModels({
224 llm: baseLlm,
225 model,
226 ide,
227 uniqueId,
228 ideSettings,
229 llmLogger,
230 config,
231 });
232 return models;
233 } else {
234 return [baseLlm];
235 }
236}