Elffuss BraidHub

kikocis/continue public

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