Elffuss BraidHub

kikocis/continue public

main · 6.0 KiBRawPermalink
1import {
2 mergeConfigYamlRequestOptions,
3 ModelConfig,
4} from "@continuedev/config-yaml";
5
6import { ContinueConfig, ILLMLogger, LLMOptions } from "../..";
7import { BaseLLM } from "../../llm";
8import { LLMClasses } from "../../llm/llms";
9
10const AUTODETECT = "AUTODETECT";
11
12function getModelClass(
13 model: ModelConfig,
14): (typeof LLMClasses)[number] | undefined {
15 return LLMClasses.find((llm) => llm.providerName === model.provider);
16}
17
18// function getContinueProxyModelName(
19// ownerSlug: string,
20// packageSlug: string,
21// model: ModelConfig,
22// ): string {
23// return `${ownerSlug}/${packageSlug}/${model.provider}/${model.model}`;
24// }
25
26async function modelConfigToBaseLLM({
27 model,
28 uniqueId,
29 llmLogger,
30 config,
31 isFromAutoDetect,
32}: {
33 model: ModelConfig;
34 uniqueId: string;
35 llmLogger: ILLMLogger;
36 config: ContinueConfig;
37 isFromAutoDetect?: boolean;
38}): Promise<BaseLLM | undefined> {
39 const cls = getModelClass(model);
40
41 if (!cls) {
42 return undefined;
43 }
44
45 const { capabilities, ...rest } = model;
46
47 const mergedRequestOptions = mergeConfigYamlRequestOptions(
48 rest.requestOptions,
49 config.requestOptions,
50 );
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 requestOptions: mergedRequestOptions,
78 };
79
80 // Model capabilities - need to be undefined if not found
81 // To fallback to our autodetection
82 if (capabilities?.find((c) => c === "tool_use")) {
83 options.capabilities = {
84 ...options.capabilities,
85 tools: true,
86 };
87 }
88
89 if (capabilities?.find((c) => c === "image_input")) {
90 options.capabilities = {
91 ...options.capabilities,
92 uploadImage: true,
93 };
94 }
95
96 if (model.embedOptions?.maxBatchSize) {
97 options.maxEmbeddingBatchSize = model.embedOptions.maxBatchSize;
98 }
99 if (model.embedOptions?.maxChunkSize) {
100 options.maxEmbeddingChunkSize = model.embedOptions.maxChunkSize;
101 }
102
103 // These are params that are at model config level in JSON
104 // But we decided to move to nested `env` in YAML
105 // Since types vary and we don't want to blindly spread env for now,
106 // Each one is handled individually here
107 const env = model.env ?? {};
108 if (
109 "useLegacyCompletionsEndpoint" in env &&
110 typeof env.useLegacyCompletionsEndpoint === "boolean"
111 ) {
112 options.useLegacyCompletionsEndpoint = env.useLegacyCompletionsEndpoint;
113 }
114 if ("apiType" in env && typeof env.apiType === "string") {
115 options.apiType = env.apiType;
116 }
117 if ("apiVersion" in env && typeof env.apiVersion === "string") {
118 options.apiVersion = env.apiVersion;
119 }
120 if ("deployment" in env && typeof env.deployment === "string") {
121 options.deployment = env.deployment;
122 }
123 if ("deploymentId" in env && typeof env.deploymentId === "string") {
124 options.deploymentId = env.deploymentId;
125 }
126 if ("projectId" in env && typeof env.projectId === "string") {
127 options.projectId = env.projectId;
128 }
129 if ("region" in env && typeof env.region === "string") {
130 options.region = env.region;
131 }
132 if ("profile" in env && typeof env.profile === "string") {
133 options.profile = env.profile;
134 }
135 if ("accessKeyId" in env && typeof env.accessKeyId === "string") {
136 options.accessKeyId = env.accessKeyId;
137 }
138 if ("secretAccessKey" in env && typeof env.secretAccessKey === "string") {
139 options.secretAccessKey = env.secretAccessKey;
140 }
141 if ("modelArn" in env && typeof env.modelArn === "string") {
142 options.modelArn = env.modelArn;
143 }
144 if ("aiGatewaySlug" in env && typeof env.aiGatewaySlug === "string") {
145 options.aiGatewaySlug = env.aiGatewaySlug;
146 }
147 if ("accountId" in env && typeof env.accountId === "string") {
148 options.accountId = env.accountId;
149 }
150
151 const llm = new cls(options);
152 return llm;
153}
154
155async function autodetectModels({
156 llm,
157 model,
158 uniqueId,
159 llmLogger,
160 config,
161}: {
162 llm: BaseLLM;
163 model: ModelConfig;
164 uniqueId: string;
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 return await modelConfigToBaseLLM({
177 model: {
178 ...model,
179 model: modelName,
180 name: modelName,
181 },
182 uniqueId,
183 llmLogger,
184 config,
185 isFromAutoDetect: true,
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 uniqueId,
199 llmLogger,
200 config,
201}: {
202 model: ModelConfig;
203 uniqueId: string;
204 llmLogger: ILLMLogger;
205 config: ContinueConfig;
206}): Promise<BaseLLM[]> {
207 const baseLlm = await modelConfigToBaseLLM({
208 model,
209 uniqueId,
210 llmLogger,
211 config,
212 });
213 if (!baseLlm) {
214 return [];
215 }
216
217 if (model.model === AUTODETECT) {
218 const models = await autodetectModels({
219 llm: baseLlm,
220 model,
221 uniqueId,
222 llmLogger,
223 config,
224 });
225 return models;
226 } else {
227 return [baseLlm];
228 }
229}