forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseModels.ts
161 lines (138 loc) · 4.52 KB
/
parseModels.ts
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { produce } from 'immer';
import { LOBE_DEFAULT_MODEL_LIST } from '@/config/modelProviders';
import { ChatModelCard } from '@/types/llm';
/**
* Parse model string to add or remove models.
*/
export const parseModelString = (modelString: string = '', withDeploymentName = false) => {
let models: ChatModelCard[] = [];
let removeAll = false;
const removedModels: string[] = [];
const modelNames = modelString.split(/[,,]/).filter(Boolean);
for (const item of modelNames) {
const disable = item.startsWith('-');
const nameConfig = item.startsWith('+') || item.startsWith('-') ? item.slice(1) : item;
const [idAndDisplayName, ...capabilities] = nameConfig.split('<');
let [id, displayName] = idAndDisplayName.split('=');
let deploymentName: string | undefined;
if (withDeploymentName) {
[id, deploymentName] = id.split('->');
if (!deploymentName) deploymentName = id;
}
if (disable) {
// Disable all models.
if (id === 'all') {
removeAll = true;
}
removedModels.push(id);
continue;
}
// remove empty model name
if (!item.trim().length) {
continue;
}
// Remove duplicate model entries.
const existingIndex = models.findIndex(({ id: n }) => n === id);
if (existingIndex !== -1) {
models.splice(existingIndex, 1);
}
const model: ChatModelCard = {
displayName: displayName || undefined,
id,
};
if (deploymentName) {
model.deploymentName = deploymentName;
}
if (capabilities.length > 0) {
const [maxTokenStr, ...capabilityList] = capabilities[0].replace('>', '').split(':');
model.contextWindowTokens = parseInt(maxTokenStr, 10) || undefined;
for (const capability of capabilityList) {
switch (capability) {
case 'vision': {
model.vision = true;
break;
}
case 'fc': {
model.functionCall = true;
break;
}
case 'file': {
model.files = true;
break;
}
default: {
console.warn(`Unknown capability: ${capability}`);
}
}
}
}
models.push(model);
}
return {
add: models,
removeAll,
removed: removedModels,
};
};
/**
* Extract a special method to process chatModels
*/
export const transformToChatModelCards = ({
modelString = '',
defaultChatModels,
withDeploymentName = false,
}: {
defaultChatModels: ChatModelCard[];
modelString?: string;
withDeploymentName?: boolean;
}): ChatModelCard[] | undefined => {
if (!modelString) return undefined;
const modelConfig = parseModelString(modelString, withDeploymentName);
let chatModels = modelConfig.removeAll ? [] : defaultChatModels;
// 处理移除逻辑
if (!modelConfig.removeAll) {
chatModels = chatModels.filter((m) => !modelConfig.removed.includes(m.id));
}
return produce(chatModels, (draft) => {
// 处理添加或替换逻辑
for (const toAddModel of modelConfig.add) {
// first try to find the model in LOBE_DEFAULT_MODEL_LIST to confirm if it is a known model
const knownModel = LOBE_DEFAULT_MODEL_LIST.find((model) => model.id === toAddModel.id);
// if the model is known, update it based on the known model
if (knownModel) {
const index = draft.findIndex((model) => model.id === toAddModel.id);
const modelInList = draft[index];
// if the model is already in chatModels, update it
if (modelInList) {
draft[index] = {
...modelInList,
...toAddModel,
displayName: toAddModel.displayName || modelInList.displayName || modelInList.id,
enabled: true,
};
} else {
// if the model is not in chatModels, add it
draft.push({
...knownModel,
...toAddModel,
displayName: toAddModel.displayName || knownModel.displayName || knownModel.id,
enabled: true,
});
}
} else {
// if the model is not in LOBE_DEFAULT_MODEL_LIST, add it as a new custom model
draft.push({
...toAddModel,
displayName: toAddModel.displayName || toAddModel.id,
enabled: true,
});
}
}
});
};
export const extractEnabledModels = (modelString: string = '', withDeploymentName = false) => {
const modelConfig = parseModelString(modelString, withDeploymentName);
const list = modelConfig.add.map((m) => m.id);
if (list.length === 0) return;
return list;
};