Elffuss BraidHub

kikocis/continue public

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