281 lines
7.4 KiB
Vue
281 lines
7.4 KiB
Vue
<template>
|
|||
|
|
<div class="llm-config">
|
||
|
|
<div class="page-header">
|
||
|
|
<h2>LLM 配置管理</h2>
|
||
|
|
<el-button type="primary" @click="handleAdd">
|
||
|
|
<el-icon><Plus /></el-icon>
|
||
|
|
新增配置
|
||
|
|
</el-button>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<el-card>
|
||
|
|
<el-table :data="tableData" v-loading="loading" stripe>
|
||
|
|
<el-table-column prop="id" label="ID" width="80" />
|
||
|
|
<el-table-column prop="name" label="名称" min-width="120" />
|
||
|
|
<el-table-column prop="baseUrl" label="API 地址" min-width="200" show-overflow-tooltip />
|
||
|
|
<el-table-column prop="model" label="模型" min-width="120" />
|
||
|
|
<el-table-column prop="apiKey" label="API Key" min-width="150">
|
||
|
|
<template #default="{ row }">
|
||
|
|
<span>{{ maskApiKey(row.apiKey) }}</span>
|
||
|
|
</template>
|
||
|
|
</el-table-column>
|
||
|
|
<el-table-column prop="enabled" label="状态" width="100">
|
||
|
|
<template #default="{ row }">
|
||
|
|
<el-switch
|
||
|
|
v-model="row.enabled"
|
||
|
|
@change="handleToggleEnabled(row)"
|
||
|
|
/>
|
||
|
|
</template>
|
||
|
|
</el-table-column>
|
||
|
|
<el-table-column prop="createdAt" label="创建时间" width="180">
|
||
|
|
<template #default="{ row }">
|
||
|
|
{{ formatTime(row.createdAt) }}
|
||
|
|
</template>
|
||
|
|
</el-table-column>
|
||
|
|
<el-table-column label="操作" width="150" fixed="right">
|
||
|
|
<template #default="{ row }">
|
||
|
|
<el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
|
||
|
|
<el-button type="danger" link @click="handleDelete(row)">删除</el-button>
|
||
|
|
</template>
|
||
|
|
</el-table-column>
|
||
|
|
</el-table>
|
||
|
|
|
||
|
|
<div class="pagination-wrapper">
|
||
|
|
<el-pagination
|
||
|
|
v-model:current-page="pagination.page"
|
||
|
|
v-model:page-size="pagination.limit"
|
||
|
|
:total="pagination.total"
|
||
|
|
:page-sizes="[10, 20, 50]"
|
||
|
|
layout="total, sizes, prev, pager, next, jumper"
|
||
|
|
@size-change="loadData"
|
||
|
|
@current-change="loadData"
|
||
|
|
/>
|
||
|
|
</div>
|
||
|
|
</el-card>
|
||
|
|
|
||
|
|
<!-- 新增/编辑对话框 -->
|
||
|
|
<el-dialog
|
||
|
|
v-model="dialogVisible"
|
||
|
|
:title="isEdit ? '编辑 LLM 配置' : '新增 LLM 配置'"
|
||
|
|
width="500px"
|
||
|
|
@close="resetForm"
|
||
|
|
>
|
||
|
|
<el-form
|
||
|
|
ref="formRef"
|
||
|
|
:model="formData"
|
||
|
|
:rules="formRules"
|
||
|
|
label-width="100px"
|
||
|
|
>
|
||
|
|
<el-form-item label="名称" prop="name">
|
||
|
|
<el-input v-model="formData.name" placeholder="请输入配置名称" />
|
||
|
|
</el-form-item>
|
||
|
|
<el-form-item label="API 地址" prop="baseUrl">
|
||
|
|
<el-input v-model="formData.baseUrl" placeholder="如:https://api.openai.com/v1" />
|
||
|
|
</el-form-item>
|
||
|
|
<el-form-item label="模型" prop="model">
|
||
|
|
<el-input v-model="formData.model" placeholder="如:gpt-4、gpt-3.5-turbo" />
|
||
|
|
</el-form-item>
|
||
|
|
<el-form-item label="API Key" prop="apiKey">
|
||
|
|
<el-input
|
||
|
|
v-model="formData.apiKey"
|
||
|
|
type="password"
|
||
|
|
show-password
|
||
|
|
placeholder="请输入 API Key"
|
||
|
|
/>
|
||
|
|
</el-form-item>
|
||
|
|
<el-form-item label="启用状态">
|
||
|
|
<el-switch v-model="formData.enabled" />
|
||
|
|
</el-form-item>
|
||
|
|
</el-form>
|
||
|
|
<template #footer>
|
||
|
|
<el-button @click="dialogVisible = false">取消</el-button>
|
||
|
|
<el-button type="primary" @click="handleSubmit" :loading="submitting">
|
||
|
|
确定
|
||
|
|
</el-button>
|
||
|
|
</template>
|
||
|
|
</el-dialog>
|
||
|
|
</div>
|
||
|
|
</template>
|
||
|
|
|
||
|
|
<script setup lang="ts">
|
||
|
|
import { ref, reactive, onMounted } from 'vue'
|
||
|
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
||
|
|
import { Plus } from '@element-plus/icons-vue'
|
||
|
|
import type { FormInstance, FormRules } from 'element-plus'
|
||
|
|
import request from '@/api/request'
|
||
|
|
import type { LlmConfig } from '@/api/types'
|
||
|
|
|
||
|
|
const loading = ref(false)
|
||
|
|
const submitting = ref(false)
|
||
|
|
const dialogVisible = ref(false)
|
||
|
|
const isEdit = ref(false)
|
||
|
|
const tableData = ref<LlmConfig[]>([])
|
||
|
|
const formRef = ref<FormInstance>()
|
||
|
|
|
||
|
|
const pagination = reactive({
|
||
|
|
page: 1,
|
||
|
|
limit: 10,
|
||
|
|
total: 0,
|
||
|
|
})
|
||
|
|
|
||
|
|
const formData = reactive({
|
||
|
|
id: 0,
|
||
|
|
name: '',
|
||
|
|
baseUrl: '',
|
||
|
|
model: '',
|
||
|
|
apiKey: '',
|
||
|
|
enabled: true,
|
||
|
|
})
|
||
|
|
|
||
|
|
const formRules: FormRules = {
|
||
|
|
name: [{ required: true, message: '请输入配置名称', trigger: 'blur' }],
|
||
|
|
baseUrl: [{ required: true, message: '请输入 API 地址', trigger: 'blur' }],
|
||
|
|
model: [{ required: true, message: '请输入模型名称', trigger: 'blur' }],
|
||
|
|
apiKey: [{ required: true, message: '请输入 API Key', trigger: 'blur' }],
|
||
|
|
}
|
||
|
|
|
||
|
|
const loadData = async () => {
|
||
|
|
loading.value = true
|
||
|
|
try {
|
||
|
|
const res = await request.get('/llm-configs', {
|
||
|
|
params: {
|
||
|
|
page: pagination.page,
|
||
|
|
limit: pagination.limit,
|
||
|
|
},
|
||
|
|
})
|
||
|
|
tableData.value = res.data || []
|
||
|
|
pagination.total = res.pagination?.total || 0
|
||
|
|
} catch (error) {
|
||
|
|
console.error('加载数据失败', error)
|
||
|
|
} finally {
|
||
|
|
loading.value = false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const handleAdd = () => {
|
||
|
|
isEdit.value = false
|
||
|
|
dialogVisible.value = true
|
||
|
|
}
|
||
|
|
|
||
|
|
const handleEdit = (row: LlmConfig) => {
|
||
|
|
isEdit.value = true
|
||
|
|
Object.assign(formData, {
|
||
|
|
id: row.id,
|
||
|
|
name: row.name,
|
||
|
|
baseUrl: row.baseUrl,
|
||
|
|
model: row.model,
|
||
|
|
apiKey: row.apiKey,
|
||
|
|
enabled: row.enabled,
|
||
|
|
})
|
||
|
|
dialogVisible.value = true
|
||
|
|
}
|
||
|
|
|
||
|
|
const handleDelete = async (row: LlmConfig) => {
|
||
|
|
try {
|
||
|
|
await ElMessageBox.confirm(`确定要删除配置 "${row.name}" 吗?`, '提示', {
|
||
|
|
type: 'warning',
|
||
|
|
})
|
||
|
|
await request.delete(`/llm-configs/${row.id}`)
|
||
|
|
ElMessage.success('删除成功')
|
||
|
|
loadData()
|
||
|
|
} catch (error: any) {
|
||
|
|
if (error !== 'cancel') {
|
||
|
|
console.error('删除失败', error)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const handleToggleEnabled = async (row: LlmConfig) => {
|
||
|
|
try {
|
||
|
|
await request.put(`/llm-configs/${row.id}`, {
|
||
|
|
enabled: row.enabled,
|
||
|
|
})
|
||
|
|
ElMessage.success(row.enabled ? '已启用' : '已禁用')
|
||
|
|
} catch (error) {
|
||
|
|
row.enabled = !row.enabled
|
||
|
|
console.error('更新状态失败', error)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const handleSubmit = async () => {
|
||
|
|
if (!formRef.value) return
|
||
|
|
|
||
|
|
try {
|
||
|
|
await formRef.value.validate()
|
||
|
|
submitting.value = true
|
||
|
|
|
||
|
|
const data = {
|
||
|
|
name: formData.name,
|
||
|
|
baseUrl: formData.baseUrl,
|
||
|
|
model: formData.model,
|
||
|
|
apiKey: formData.apiKey,
|
||
|
|
enabled: formData.enabled,
|
||
|
|
}
|
||
|
|
|
||
|
|
if (isEdit.value) {
|
||
|
|
await request.put(`/llm-configs/${formData.id}`, data)
|
||
|
|
ElMessage.success('更新成功')
|
||
|
|
} else {
|
||
|
|
await request.post('/llm-configs', data)
|
||
|
|
ElMessage.success('创建成功')
|
||
|
|
}
|
||
|
|
|
||
|
|
dialogVisible.value = false
|
||
|
|
loadData()
|
||
|
|
} catch (error) {
|
||
|
|
console.error('提交失败', error)
|
||
|
|
} finally {
|
||
|
|
submitting.value = false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const resetForm = () => {
|
||
|
|
Object.assign(formData, {
|
||
|
|
id: 0,
|
||
|
|
name: '',
|
||
|
|
baseUrl: '',
|
||
|
|
model: '',
|
||
|
|
apiKey: '',
|
||
|
|
enabled: true,
|
||
|
|
})
|
||
|
|
formRef.value?.resetFields()
|
||
|
|
}
|
||
|
|
|
||
|
|
const maskApiKey = (key: string) => {
|
||
|
|
if (!key || key.length < 8) return '****'
|
||
|
|
return key.slice(0, 4) + '****' + key.slice(-4)
|
||
|
|
}
|
||
|
|
|
||
|
|
const formatTime = (time: string) => {
|
||
|
|
return new Date(time).toLocaleString('zh-CN')
|
||
|
|
}
|
||
|
|
|
||
|
|
onMounted(() => {
|
||
|
|
loadData()
|
||
|
|
})
|
||
|
|
</script>
|
||
|
|
|
||
|
|
<style scoped>
|
||
|
|
.llm-config {
|
||
|
|
height: 100%;
|
||
|
|
}
|
||
|
|
|
||
|
|
.page-header {
|
||
|
|
display: flex;
|
||
|
|
justify-content: space-between;
|
||
|
|
align-items: center;
|
||
|
|
margin-bottom: 20px;
|
||
|
|
}
|
||
|
|
|
||
|
|
.page-header h2 {
|
||
|
|
margin: 0;
|
||
|
|
font-size: 20px;
|
||
|
|
color: #303133;
|
||
|
|
}
|
||
|
|
|
||
|
|
.pagination-wrapper {
|
||
|
|
margin-top: 20px;
|
||
|
|
display: flex;
|
||
|
|
justify-content: flex-end;
|
||
|
|
}
|
||
|
|
</style>
|