Elffuss BraidHub

kikocis/continue public

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