Skip to content

Commit dc8c4cf

Browse files
author
linyuan.yang
committed
拆分 LLM
1 parent e40fe31 commit dc8c4cf

18 files changed

Lines changed: 476 additions & 230 deletions

File tree

‎packages/admin/src/views/models/ModelsView.vue‎

Lines changed: 141 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useToast, useConfirm, SButton, SInput, SSelect, SModal, SFormItem, SPag
77
import type { STableColumn } from 'sbot-ui'
88
import { ModelProvider } from '@/shared/types'
99
import type { ModelConfig } from '@/shared/types'
10+
import { isConfigFieldVisible, type ShowWhen } from '@/utils/configField'
1011
import ResourceRefs from '@/components/ResourceRefs.vue'
1112
import { useResourceRefs } from '@/composables/useResourceRefs'
1213
@@ -27,6 +28,27 @@ const modelColumns = computed<STableColumn[]>(() => [
2728
])
2829
2930
const { loadProfiles, makeResourceRefs } = useResourceRefs()
31+
interface ProviderField {
32+
label: string
33+
type: 'string' | 'textarea' | 'password' | 'boolean' | 'number' | 'select'
34+
required?: boolean
35+
description?: string
36+
default?: string | boolean | number
37+
options?: Array<{ label: string; value: string }>
38+
showWhen?: ShowWhen
39+
}
40+
41+
interface ModelProviderDefinition {
42+
type: string
43+
label: string
44+
configSchema: Record<string, ProviderField>
45+
defaults?: { baseURL?: string; model?: string; config?: Record<string, any> }
46+
apiKeyEnabled?: boolean
47+
apiKeyRequired?: boolean
48+
supportsModelListing: boolean
49+
}
50+
51+
const providers = ref<ModelProviderDefinition[]>([])
3052
const refs = makeResourceRefs({
3153
channel: (c, id) => c.intentModel === id,
3254
profile: (p, id) => p.intentModel === id,
@@ -36,42 +58,73 @@ const refs = makeResourceRefs({
3658
agendaProfile: (p, id) => p.syncModel === id,
3759
})
3860
const expandedIds = ref<string[]>([])
39-
onMounted(loadProfiles)
61+
onMounted(() => {
62+
void loadProfiles()
63+
void loadProviders()
64+
})
4065
4166
const showModal = ref(false)
4267
const editingName = ref<string | null>(null)
4368
const showApiKey = ref(false)
69+
const privateFieldVisible = ref<Record<string, boolean>>({})
4470
const form = ref<ModelConfig>({
45-
name: '', provider: ModelProvider.OpenAI, baseURL: '', apiKey: '', model: '', temperature: undefined, maxTokens: undefined, contextWindow: undefined, maxTools: undefined,
71+
name: '', provider: ModelProvider.OpenAI, baseURL: '', apiKey: '', model: '', temperature: undefined, maxTokens: undefined, contextWindow: undefined, maxTools: undefined, config: {},
4672
})
4773
48-
const isOllama = computed(() => form.value.provider === ModelProvider.Ollama)
49-
const isAnthropic = computed(() => form.value.provider === ModelProvider.Anthropic)
50-
const isGemini = computed(() => form.value.provider === ModelProvider.Gemini || form.value.provider === ModelProvider.GeminiImage)
51-
52-
const thinkingType = computed({
53-
get: () => form.value.anthropic?.thinking?.type ?? '',
54-
set: (v: string) => {
55-
if (!v) { form.value.anthropic = { ...form.value.anthropic, thinking: undefined }; return }
56-
form.value.anthropic = {
57-
...form.value.anthropic,
58-
thinking: { type: v as any, ...(v === 'enabled' ? { budget_tokens: form.value.anthropic?.thinking?.budget_tokens ?? 8192 } : {}) },
59-
}
60-
},
61-
})
62-
const thinkingBudget = computed({
63-
get: () => form.value.anthropic?.thinking?.budget_tokens,
64-
set: (v: number | undefined) => { if (form.value.anthropic?.thinking) form.value.anthropic.thinking.budget_tokens = v },
74+
const currentProvider = computed(() => providers.value.find(provider => provider.type === form.value.provider))
75+
const providerOptions = computed<ModelProviderDefinition[]>(() => {
76+
const options = providers.value.length > 0
77+
? [...providers.value]
78+
: Object.values(ModelProvider).map(type => ({ type, label: type, configSchema: {}, supportsModelListing: false }))
79+
if (form.value.provider && !options.some(provider => provider.type === form.value.provider)) {
80+
options.push({ type: form.value.provider, label: form.value.provider, configSchema: {}, supportsModelListing: false })
81+
}
82+
return options
6583
})
66-
const promptCaching = computed({
67-
get: () => form.value.anthropic?.promptCaching ?? false,
68-
set: (v: boolean) => { form.value.anthropic = { ...form.value.anthropic, promptCaching: v || undefined } },
84+
const currentSchema = computed(() => currentProvider.value?.configSchema ?? {})
85+
const providerConfig = computed<Record<string, any>>({
86+
get: () => form.value.config ?? {},
87+
set: value => { form.value.config = value },
6988
})
70-
const geminiApiVersion = computed({
71-
get: () => form.value.gemini?.apiVersion ?? '',
72-
set: (v: string) => { form.value.gemini = { ...form.value.gemini, apiVersion: v || undefined } },
89+
const visibleSchemaEntries = computed(() =>
90+
Object.entries(currentSchema.value).filter(([, field]) => isConfigFieldVisible(field, form.value.config)),
91+
)
92+
const apiKeyEnabled = computed(() => currentProvider.value?.apiKeyEnabled !== false)
93+
const apiKeyRequired = computed(() => currentProvider.value?.apiKeyRequired ?? form.value.provider !== ModelProvider.Ollama)
94+
const providerBaseURL = computed(() => currentProvider.value?.defaults?.baseURL ?? '')
95+
const canPickModels = computed(() => {
96+
if (!currentProvider.value?.supportsModelListing) return false
97+
if (!form.value.baseURL) return false
98+
return !apiKeyRequired.value || !!form.value.apiKey
7399
})
74100
101+
async function loadProviders() {
102+
try {
103+
const res = await apiFetch('/api/llm-providers')
104+
providers.value = res.data as ModelProviderDefinition[]
105+
} catch (e: any) {
106+
show(e.message, 'error')
107+
}
108+
}
109+
110+
function defaultPrivateConfig(provider = currentProvider.value): Record<string, any> {
111+
const result = { ...(provider?.defaults?.config ?? {}) }
112+
for (const [key, field] of Object.entries(provider?.configSchema ?? {})) {
113+
if (result[key] === undefined && field.default !== undefined) result[key] = field.default
114+
}
115+
return result
116+
}
117+
118+
function onProviderChange() {
119+
const provider = currentProvider.value
120+
form.value.baseURL = provider?.defaults?.baseURL ?? ''
121+
form.value.model = provider?.defaults?.model ?? ''
122+
form.value.apiKey = ''
123+
form.value.config = defaultPrivateConfig(provider)
124+
showApiKey.value = false
125+
privateFieldVisible.value = {}
126+
}
127+
75128
const showPicker = ref(false)
76129
const pickerLoading = ref(false)
77130
const pickerModels = ref<string[]>([])
@@ -90,6 +143,7 @@ async function openPicker() {
90143
baseURL: form.value.baseURL,
91144
apiKey: form.value.apiKey,
92145
provider: form.value.provider,
146+
config: form.value.config,
93147
})
94148
pickerModels.value = res.data as string[]
95149
} catch (e: any) {
@@ -108,12 +162,25 @@ function pickModel(m: string) {
108162
function openAdd() {
109163
editingName.value = null
110164
showApiKey.value = false
111-
form.value = { name: '', provider: ModelProvider.OpenAI, baseURL: '', apiKey: '', model: '', temperature: undefined, maxTokens: undefined, contextWindow: undefined, maxTools: undefined }
165+
const provider = providers.value.find(item => item.type === ModelProvider.OpenAI) ?? providers.value[0]
166+
form.value = {
167+
name: '',
168+
provider: provider?.type ?? ModelProvider.OpenAI,
169+
baseURL: provider?.defaults?.baseURL ?? '',
170+
apiKey: '',
171+
model: provider?.defaults?.model ?? '',
172+
temperature: undefined,
173+
maxTokens: undefined,
174+
contextWindow: undefined,
175+
maxTools: undefined,
176+
config: defaultPrivateConfig(provider),
177+
}
112178
showModal.value = true
113179
}
114180
115181
function openEdit(id: string) {
116182
const m = models.value[id]
183+
const provider = providers.value.find(item => item.type === m.provider)
117184
editingName.value = id
118185
showApiKey.value = false
119186
form.value = {
@@ -126,32 +193,40 @@ function openEdit(id: string) {
126193
maxTokens: m.maxTokens,
127194
contextWindow: m.contextWindow,
128195
maxTools: m.maxTools,
129-
anthropic: m.anthropic ? { ...m.anthropic } : undefined,
130-
gemini: m.gemini ? { ...m.gemini } : undefined,
196+
config: { ...defaultPrivateConfig(provider), ...(m.config ?? {}) },
131197
}
132198
showModal.value = true
133199
}
134200
135201
async function save() {
136202
if (!form.value.name.trim()) { show(t('common.name_required'), 'error'); return }
137203
if (!form.value.baseURL.trim()) { show(t('common.base_url_required'), 'error'); return }
138-
if (!isOllama.value && !form.value.apiKey.trim()) { show(t('common.api_key_required'), 'error'); return }
204+
if (apiKeyRequired.value && !form.value.apiKey.trim()) { show(t('common.api_key_required'), 'error'); return }
139205
if (!form.value.model.trim()) { show(t('common.model_required'), 'error'); return }
206+
for (const [key, field] of visibleSchemaEntries.value) {
207+
const value = form.value.config?.[key]
208+
if (field.required && (value === undefined || value === null || value === '')) {
209+
show(`${field.label} is required`, 'error')
210+
return
211+
}
212+
}
140213
try {
141214
const body: any = { ...form.value }
142215
if (body.temperature === undefined || body.temperature === null) delete body.temperature
143216
if (body.maxTokens === undefined || body.maxTokens === null) delete body.maxTokens
144217
if (body.contextWindow === undefined || body.contextWindow === null) delete body.contextWindow
145218
if (body.maxTools === undefined || body.maxTools === null) delete body.maxTools
146-
if (body.anthropic) {
147-
if (!body.anthropic.thinking) delete body.anthropic.thinking
148-
else if (body.anthropic.thinking.type !== 'enabled') delete body.anthropic.thinking.budget_tokens
149-
if (!body.anthropic.promptCaching) delete body.anthropic.promptCaching
150-
if (!Object.keys(body.anthropic).length) delete body.anthropic
151-
}
152-
if (body.gemini) {
153-
if (!body.gemini.apiVersion) delete body.gemini.apiVersion
154-
if (!Object.keys(body.gemini).length) delete body.gemini
219+
if (currentProvider.value) {
220+
const providerConfig: Record<string, any> = {}
221+
for (const [key, field] of Object.entries(currentSchema.value)) {
222+
if (!isConfigFieldVisible(field, body.config)) continue
223+
const value = body.config?.[key]
224+
if (value !== undefined && value !== null && value !== '') providerConfig[key] = value
225+
}
226+
if (Object.keys(providerConfig).length > 0) body.config = providerConfig
227+
else delete body.config
228+
} else if (!body.config || Object.keys(body.config).length === 0) {
229+
delete body.config
155230
}
156231
const id = editingName.value
157232
const res = id
@@ -226,28 +301,46 @@ async function refresh() {
226301
<SInput v-model="form.name" :placeholder="t('models.name_placeholder')" />
227302
</SFormItem>
228303
<SFormItem :label="t('common.provider') + ' *'">
229-
<SSelect v-model="form.provider">
230-
<option v-for="p in Object.values(ModelProvider)" :key="p" :value="p">{{ p }}</option>
304+
<SSelect v-model="form.provider" @change="onProviderChange">
305+
<option v-for="provider in providerOptions" :key="provider.type" :value="provider.type">{{ provider.label }}</option>
231306
</SSelect>
232307
</SFormItem>
233308
<SFormItem :label="t('common.base_url') + ' *'">
234-
<SInput v-model="form.baseURL" :placeholder="isOllama ? 'http://localhost:11434' : isAnthropic ? 'https://api.anthropic.com' : isGemini ? '' : 'https://api.openai.com/v1'" />
309+
<SInput v-model="form.baseURL" :placeholder="providerBaseURL" />
235310
</SFormItem>
236-
<SFormItem v-if="!isOllama" :label="t('common.api_key') + ' *'">
311+
<SFormItem v-if="apiKeyEnabled" :label="t('common.api_key') + (apiKeyRequired ? ' *' : '')">
237312
<div class="apikey-field">
238-
<SInput v-model="form.apiKey" :type="showApiKey ? 'text' : 'password'" :placeholder="isAnthropic ? 'sk-ant-...' : isGemini ? 'AIza...' : 'sk-...'" class="apikey-input" />
313+
<SInput v-model="form.apiKey" :type="showApiKey ? 'text' : 'password'" placeholder="API Key" class="apikey-input" />
239314
<SButton type="outline" size="sm" @click="showApiKey = !showApiKey">{{ showApiKey ? t('common.hide') : t('common.show') }}</SButton>
240315
</div>
241316
</SFormItem>
242317
<SFormItem :label="t('models.model') + ' *'">
243318
<div class="model-field">
244-
<SInput v-model="form.model" :placeholder="isOllama ? 'llama3' : isAnthropic ? 'claude-sonnet-4-6' : isGemini ? 'gemini-2.0-flash' : 'gpt-4'" class="model-input" />
245-
<SButton type="outline" size="sm" :disabled="!isAnthropic && !isGemini && !form.baseURL" @click="openPicker">{{ t('models.pick') }}</SButton>
319+
<SInput v-model="form.model" placeholder="Model ID" class="model-input" />
320+
<SButton type="outline" size="sm" :disabled="!canPickModels" @click="openPicker">{{ t('models.pick') }}</SButton>
246321
</div>
247322
</SFormItem>
248-
<SFormItem v-if="isGemini" :label="t('models.api_version')">
249-
<SInput v-model="geminiApiVersion" placeholder="v1beta" />
250-
</SFormItem>
323+
<template v-for="[key, field] in visibleSchemaEntries" :key="key">
324+
<SFormItem :label="field.label + (field.required ? ' *' : '')">
325+
<SSelect v-if="field.type === 'select'" v-model="providerConfig[key]">
326+
<option v-for="option in field.options" :key="option.value" :value="option.value">{{ option.label }}</option>
327+
</SSelect>
328+
<label v-else-if="field.type === 'boolean'" class="checkbox-label">
329+
<input v-model="providerConfig[key]" type="checkbox" />
330+
{{ field.description || '' }}
331+
</label>
332+
<SInput v-else-if="field.type === 'number'" v-model.number="providerConfig[key]" type="number" :placeholder="field.description || ''" />
333+
<div v-else-if="field.type === 'password'" class="apikey-field">
334+
<SInput v-model="providerConfig[key]" :type="privateFieldVisible[key] ? 'text' : 'password'" :placeholder="field.description || ''" class="apikey-input" />
335+
<SButton type="outline" size="sm" @click="privateFieldVisible[key] = !privateFieldVisible[key]">
336+
{{ privateFieldVisible[key] ? t('common.hide') : t('common.show') }}
337+
</SButton>
338+
</div>
339+
<SInput v-else-if="field.type === 'textarea'" v-model="providerConfig[key]" multiline :placeholder="field.description || ''" />
340+
<SInput v-else v-model="providerConfig[key]" :placeholder="field.description || ''" />
341+
<template v-if="field.type !== 'boolean' && field.description" #hint>{{ field.description }}</template>
342+
</SFormItem>
343+
</template>
251344
<SFormItem :label="t('models.temperature')">
252345
<SInput v-model.number="form.temperature" type="number" step="0.1" placeholder="0.7" />
253346
</SFormItem>
@@ -260,22 +353,6 @@ async function refresh() {
260353
<SFormItem :label="t('models.max_tools')">
261354
<SInput v-model.number="form.maxTools" type="number" step="1" :placeholder="t('models.no_limit')" />
262355
</SFormItem>
263-
<SFormItem v-if="isAnthropic" :label="t('models.thinking')">
264-
<SSelect v-model="thinkingType">
265-
<option value="">{{ t('models.thinking_none') }}</option>
266-
<option value="adaptive">{{ t('models.thinking_adaptive') }}</option>
267-
<option value="enabled">{{ t('models.thinking_enabled') }}</option>
268-
</SSelect>
269-
</SFormItem>
270-
<SFormItem v-if="isAnthropic && thinkingType === 'enabled'" :label="t('models.thinking_budget')">
271-
<SInput v-model.number="thinkingBudget" type="number" step="1024" placeholder="8192" />
272-
</SFormItem>
273-
<SFormItem v-if="isAnthropic" :label="t('models.prompt_caching')">
274-
<label class="checkbox-label">
275-
<input v-model="promptCaching" type="checkbox" />
276-
{{ t('models.prompt_caching_desc') }}
277-
</label>
278-
</SFormItem>
279356
<template #footer>
280357
<SButton type="outline" @click="showModal = false">{{ t('common.cancel') }}</SButton>
281358
<SButton type="primary" @click="save">{{ t('common.save') }}</SButton>

‎packages/sbot.commons/src/settings.ts‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,18 @@ export enum ModelProvider {
3131
GeminiImage = "gemini-image",
3232
}
3333

34-
export interface ThinkingConfig {
35-
type: "adaptive" | "enabled" | "disabled"
36-
budget_tokens?: number
37-
}
38-
39-
export interface AnthropicConfig {
40-
thinking?: ThinkingConfig
41-
promptCaching?: boolean
42-
}
43-
44-
export interface GeminiConfig {
45-
apiVersion?: string
46-
}
47-
4834
export interface ModelConfig {
4935
name: string
50-
provider: ModelProvider
36+
provider: ModelProvider | string
5137
baseURL: string
5238
apiKey: string
5339
model: string
5440
temperature?: number
5541
maxTokens?: number
5642
contextWindow?: number
5743
maxTools?: number
58-
anthropic?: AnthropicConfig
59-
gemini?: GeminiConfig
44+
/** Provider 私有参数,由对应 provider 的 configSchema 定义。 */
45+
config?: Record<string, any>
6046
}
6147

6248
export enum EmbeddingProvider {

‎packages/sbot/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"wiki.gdrive": "workspace:*",
7171
"scorpio.ai": "workspace:*",
7272
"scorpio.di": "workspace:*",
73+
"scorpio.llm": "workspace:*",
7374
"scorpio.llm.anthropic": "workspace:*",
7475
"scorpio.llm.cohere": "workspace:*",
7576
"scorpio.llm.gemini": "workspace:*",

‎packages/sbot/src/Server/helpers/modelInfo.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class ModelInfoHelper {
2020
maxOutputTokens = data.max_tokens;
2121
}
2222
} else if (mc.provider === ModelProvider.Gemini || mc.provider === ModelProvider.GeminiImage) {
23-
const ver = mc.gemini?.apiVersion || 'v1beta';
23+
const ver = mc.config?.apiVersion ?? 'v1';
2424
const url = `${base}/${ver}/models/${encodeURIComponent(mc.model)}`;
2525
const headers: Record<string, string> = { 'x-goog-api-key': mc.apiKey };
2626
const res = await fetch(url, { headers });

0 commit comments

Comments
 (0)