-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathAIConfig.vue
333 lines (311 loc) · 9.41 KB
/
AIConfig.vue
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<script setup lang="ts">
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select'
import { onMounted, reactive, ref, watch } from 'vue'
const emit = defineEmits([`saved`])
const serviceOptions = [
{
value: `deepseek`,
label: `DeepSeek`,
endpoint: `https://api.deepseek.com/v1`,
models: [`deepseek-chat`, `deepseek-reasoner`],
},
{
value: `openai`,
label: `OpenAI`,
endpoint: `https://api.openai.com/v1`,
models: [`gpt-4.1`, `gpt-4.1-mini`, `gpt-4.1-nano`, `gpt-4-turbo`, `gpt-4o`, `gpt-3.5-turbo`],
},
{
value: `qwen`,
label: `通义千问`,
endpoint: `https://dashscope.aliyuncs.com/compatible-mode/v1`,
models: [
`qwen-vl-max-2025-04-02`,
`deepseek-v3`,
`deepseek-r1-distill-llama-70b`,
`deepseek-r1-distill-qwen-32b`,
`deepseek-r1-distill-qwen-14b`,
`deepseek-r1-distill-llama-8b`,
`deepseek-r1-distill-qwen-1.5b`,
`deepseek-r1-distill-qwen-7b`,
`deepseek-r1`,
`qwen1.5-7b-chat`,
// `qwen-vl-ocr-latest`,
// `qwen-vl-ocr`,
`qwen-coder-plus-1106`,
`qwen-coder-plus`,
`qwen-coder-plus-latest`,
`qwen2.5-coder-3b-instruct`,
`qwen2.5-coder-0.5b-instruct`,
`qwen2.5-coder-14b-instruct`,
`qwen2.5-coder-32b-instruct`,
`qwen-coder-turbo-0919`,
`qwen2.5-0.5b-instruct`,
`qwen2.5-1.5b-instruct`,
`qwen2.5-3b-instruct`,
`qwen2.5-7b-instruct`,
`qwen2.5-14b-instruct`,
`qwen2.5-32b-instruct`,
`qwen2.5-72b-instruct`,
`qwen2.5-coder-7b-instruct`,
`qwen2.5-math-1.5b-instruct`,
`qwen2.5-math-7b-instruct`,
`qwen2.5-math-72b-instruct`,
`qwen-turbo-0919`,
`qwen-turbo-latest`,
`qwen-plus-0919`,
`qwen-plus-latest`,
`qwen-max-0919`,
`qwen-max-latest`,
`qwen-coder-turbo`,
`qwen-coder-turbo-latest`,
`qwen-math-turbo-0919`,
`qwen-math-turbo`,
`qwen-math-turbo-latest`,
`qwen-math-plus-0919`,
`qwen-math-plus`,
`qwen-math-plus-latest`,
`qwen2-57b-a14b-instruct`,
`qwen2-72b-instruct`,
`qwen2-7b-instruct`,
`qwen2-0.5b-instruct`,
`qwen2-1.5b-instruct`,
`qwen-long`,
`qwen-vl-max`,
`qwen-vl-plus`,
`qwen-max-0428`,
`qwen1.5-110b-chat`,
`qwen-72b-chat`,
`codeqwen1.5-7b-chat`,
`qwen1.5-0.5b-chat`,
`qwen-1.8b-chat`,
`qwen-1.8b-longcontext-chat`,
`qwen-7b-chat`,
`qwen-14b-chat`,
`qwen1.5-14b-chat`,
`qwen1.5-1.8b-chat`,
`qwen1.5-32b-chat`,
`qwen1.5-72b-chat`,
`qwen-max-1201`,
`qwen-max-longcontext`,
`qwen-max-0403`,
`qwen-max-0107`,
`qwen-turbo`,
`qwen-max`,
`qwen-plus`,
],
},
{
value: `hunyuan`,
label: `腾讯混元`,
endpoint: `https://api.hunyuan.cloud.tencent.com/v1`,
models: [
`hunyuan-pro`,
`hunyuan-vision`,
`hunyuan-lite`,
`hunyuan-standard`,
`hunyuan-standard-32K`,
`hunyuan-standard-256k`,
`hunyuan-code`,
`hunyuan-role`,
`hunyuan-functioncall`,
`hunyuan-turbo-vision`,
`hunyuan-turbo`,
],
},
{
value: `custom`,
label: `自定义兼容 OpenAI API 的服务`,
endpoint: ``,
models: [],
},
]
const config = reactive({
type: `deepseek`,
endpoint: ``,
apiKey: ``,
model: ``,
temperature: 1,
maxToken: 1024,
})
const loading = ref(false)
const testResult = ref(``)
// 获取当前服务配置
function currentService() {
return serviceOptions.find(service => service.value === config.type) || serviceOptions[0]
}
// 初始化配置
function initConfigFromStorage() {
const savedType = localStorage.getItem(`openai_type`) || `deepseek`
const service = serviceOptions.find(s => s.value === savedType) || serviceOptions[0]
config.type = savedType
config.endpoint = localStorage.getItem(`openai_endpoint`) || service.endpoint
config.apiKey = localStorage.getItem(`openai_key_${savedType}`) || ``
config.model = service.models.includes(localStorage.getItem(`openai_model`) || ``)
? localStorage.getItem(`openai_model`)!
: service.models[0] || ``
config.temperature = Number(localStorage.getItem(`openai_temperature`) || 1)
config.maxToken = Number(localStorage.getItem(`openai_max_token`) || 1024)
}
onMounted(() => {
initConfigFromStorage()
})
// 服务类型变化时自动更新 endpoint、model 和 API Key
watch(() => config.type, () => {
const service = currentService()
config.endpoint = service.endpoint
const savedModel = localStorage.getItem(`openai_model`)
if (savedModel && service.models.includes(savedModel)) {
config.model = savedModel
}
else {
config.model = service.models[0] || ``
}
config.apiKey = localStorage.getItem(`openai_key_${config.type}`) || ``
})
// 保存配置
function saveConfig() {
localStorage.setItem(`openai_type`, config.type)
localStorage.setItem(`openai_endpoint`, config.endpoint)
localStorage.setItem(`openai_key_${config.type}`, config.apiKey)
localStorage.setItem(`openai_model`, config.model)
localStorage.setItem(`openai_temperature`, config.temperature.toString())
localStorage.setItem(`openai_max_token`, config.maxToken.toString())
testResult.value = `✅ 配置已保存`
emit(`saved`)
}
// 清空配置
function clearConfig() {
localStorage.removeItem(`openai_type`)
localStorage.removeItem(`openai_endpoint`)
localStorage.removeItem(`openai_model`)
localStorage.removeItem(`openai_temperature`)
localStorage.removeItem(`openai_max_token`)
serviceOptions.forEach((service) => {
localStorage.removeItem(`openai_key_${service.value}`)
})
initConfigFromStorage()
testResult.value = `🗑️ 当前 AI 配置已清除`
}
// 测试连接
async function testConnection() {
testResult.value = ``
loading.value = true
try {
const res = await window.fetch(`${config.endpoint}/models`, {
method: `GET`,
headers: {
Authorization: `Bearer ${config.apiKey}`,
},
})
testResult.value = res.ok ? `✅ 测试成功,API 可用` : `❌ 测试失败:${res.statusText}`
}
catch (e) {
console.error(e)
testResult.value = `❌ 网络错误或配置有误`
}
finally {
loading.value = false
}
}
</script>
<template>
<div class="space-y-4 text-sm">
<div class="text-gray-800 font-medium">
AI 配置
</div>
<!-- 服务类型 -->
<div>
<Label class="mb-1 block text-sm font-medium">服务类型</Label>
<Select v-model="config.type">
<SelectTrigger class="w-full">
<SelectValue>
{{ currentService().label }}
</SelectValue>
</SelectTrigger>
<SelectContent>
<SelectItem v-for="service in serviceOptions" :key="service.value" :value="service.value">
{{ service.label }}
</SelectItem>
</SelectContent>
</Select>
</div>
<!-- API 端点 -->
<div>
<Label class="mb-1 block text-sm font-medium">API 端点</Label>
<Input
v-model="config.endpoint" placeholder="输入 API 端点 URL"
class="focus:border-gray-400 focus:ring-1 focus:ring-gray-300"
/>
</div>
<!-- API 密钥 -->
<div>
<Label class="mb-1 block text-sm font-medium">API 密钥</Label>
<Input
v-model="config.apiKey" type="password" placeholder="sk-..."
class="focus:border-gray-400 focus:ring-1 focus:ring-gray-300"
/>
</div>
<!-- 模型名称 -->
<div>
<Label class="mb-1 block text-sm font-medium">模型名称</Label>
<Select v-if="currentService().models.length > 0" v-model="config.model">
<SelectTrigger class="w-full">
<SelectValue>
{{ config.model || '请选择模型' }}
</SelectValue>
</SelectTrigger>
<SelectContent>
<SelectItem v-for="model in currentService().models" :key="model" :value="model">
{{ model }}
</SelectItem>
</SelectContent>
</Select>
<Input
v-else v-model="config.model" placeholder="输入模型名称"
class="focus:border-gray-400 focus:ring-1 focus:ring-gray-300"
/>
</div>
<!-- 温度 -->
<div>
<Label class="mb-1 block text-sm font-medium">温度</Label>
<Input
v-model.number="config.temperature" type="number" step="0.1" min="0" max="2" placeholder="0 ~ 2,默认 1"
class="focus:border-gray-400 focus:ring-1 focus:ring-gray-300"
/>
</div>
<!-- 最大 Token -->
<div>
<Label class="mb-1 block text-sm font-medium">最大 Token 数</Label>
<Input
v-model.number="config.maxToken" type="number" min="1" max="32768" placeholder="比如 1024"
class="focus:border-gray-400 focus:ring-1 focus:ring-gray-300"
/>
</div>
<!-- 操作按钮 -->
<div class="mt-2 flex gap-2">
<Button size="sm" @click="saveConfig">
保存
</Button>
<Button size="sm" variant="ghost" @click="clearConfig">
清空
</Button>
<Button size="sm" variant="outline" :disabled="loading" @click="testConnection">
{{ loading ? '测试中...' : '测试连接' }}
</Button>
</div>
<!-- 测试结果 -->
<div v-if="testResult" class="mt-1 text-xs text-gray-500">
{{ testResult }}
</div>
</div>
</template>