forked from continuedev/continue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchProviderModels.ts
More file actions
144 lines (133 loc) · 4.04 KB
/
Copy pathfetchProviderModels.ts
File metadata and controls
144 lines (133 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { ModelProviderTags } from "../../../components/modelSelection/utils";
import { IIdeMessenger } from "../../../context/IdeMessenger";
import { ModelPackage } from "./models";
import { models } from "./models";
import { ollamaStaticModels, providers } from "./providers";
interface FetchedModel {
name: string;
modelId?: string;
description?: string;
icon?: string;
contextLength?: number;
maxTokens?: number;
supportsTools?: boolean;
}
function modelParams(model: FetchedModel): Record<string, any> {
const params: Record<string, any> = {};
if (model.contextLength) params.contextLength = model.contextLength;
if (model.maxTokens) {
params.completionOptions = { maxTokens: model.maxTokens };
}
if (model.supportsTools) {
params.capabilities = { tools: true };
}
return params;
}
function toOllamaPackage(model: FetchedModel): ModelPackage {
return {
title: model.name,
description: model.description || model.name,
refUrl: `https://ollama.com/library/${model.name}`,
params: {
title: model.name,
model: model.name,
...(model.supportsTools ? { capabilities: { tools: true } } : {}),
},
isOpenSource: true,
tags: [ModelProviderTags.Local, ModelProviderTags.OpenSource],
providerOptions: ["ollama"],
icon: model.icon ?? "ollama.png",
};
}
function toOpenRouterPackage(model: FetchedModel): ModelPackage {
const id = model.modelId ?? model.name;
return {
title: model.name,
description: model.name,
refUrl: `https://openrouter.ai/models/${id}`,
params: {
title: model.name,
model: id,
...modelParams(model),
},
isOpenSource: false,
tags: [ModelProviderTags.RequiresApiKey],
providerOptions: ["openrouter"],
icon: model.icon ?? "openrouter.png",
};
}
function toGenericPackage(model: FetchedModel, provider: string): ModelPackage {
const id = model.modelId ?? model.name;
return {
title: model.name,
description: model.name,
params: {
title: model.name,
model: id,
...modelParams(model),
},
isOpenSource: false,
providerOptions: [provider],
};
}
async function fetchModels(
ideMessenger: IIdeMessenger,
provider: string,
apiKey?: string,
apiBase?: string,
): Promise<FetchedModel[]> {
const response = await ideMessenger.request("models/fetch", {
provider,
apiKey,
apiBase,
});
if (response.status === "error" || !response.content) {
return [];
}
return response.content.sort((a, b) => a.name.localeCompare(b.name));
}
export async function fetchProviderModels(
ideMessenger: IIdeMessenger,
provider: string,
apiKey: string,
apiBase?: string,
): Promise<ModelPackage[]> {
const models = await fetchModels(ideMessenger, provider, apiKey, apiBase);
return models.map((m) => toGenericPackage(m, provider));
}
export async function initializeDynamicModels(ideMessenger: IIdeMessenger) {
const ollamaAutodetect = {
...models.AUTODETECT,
params: { ...models.AUTODETECT.params, title: "Ollama" },
};
if (providers.ollama) {
providers.ollama.popularPackages = [
ollamaAutodetect,
...ollamaStaticModels,
];
try {
const fetched = await fetchModels(ideMessenger, "ollama");
const fetchedPkgs = fetched.map(toOllamaPackage);
const popularTitles = new Set(
providers.ollama.popularPackages.map((p) => p.title),
);
const additional = fetchedPkgs.filter((p) => !popularTitles.has(p.title));
providers.ollama.packages = [
...providers.ollama.popularPackages,
...additional,
];
} catch (error) {
console.error("Failed to initialize Ollama models:", error);
providers.ollama.packages = [...providers.ollama.popularPackages];
}
}
try {
const fetched = await fetchModels(ideMessenger, "openrouter");
const packages = fetched.map(toOpenRouterPackage);
if (packages.length > 0 && providers.openrouter) {
providers.openrouter.packages = packages;
}
} catch (error) {
console.error("Failed to initialize OpenRouter models:", error);
}
}