修复模型绑定 API KEY 失败的 bug

This commit is contained in:
GeekMaster
2025-05-05 17:19:38 +08:00
parent 73f5a44e0a
commit 7bc55f3ed1
3 changed files with 146 additions and 113 deletions

View File

@@ -10,6 +10,7 @@
- 功能优化:支持自动迁移数据表结构,无需在手动执行 SQL 了 - 功能优化:支持自动迁移数据表结构,无需在手动执行 SQL 了
- 功能优化:移除首页的文字动画效果 - 功能优化:移除首页的文字动画效果
- 功能优化:在聊天页面增加对话列表展开和隐藏功能 - 功能优化:在聊天页面增加对话列表展开和隐藏功能
- 功能优化:聊天页面增加 AI 思考中动画效果
## v4.2.2 ## v4.2.2

View File

@@ -13,7 +13,7 @@ type ChatModel struct {
Description string `json:"description"` // 模型描述 Description string `json:"description"` // 模型描述
Category string `json:"category"` //模型类别 Category string `json:"category"` //模型类别
Temperature float32 `json:"temperature"` // 模型温度 Temperature float32 `json:"temperature"` // 模型温度
KeyId int `json:"key_id,omitempty"` KeyId uint `json:"key_id,omitempty"`
KeyName string `json:"key_name"` KeyName string `json:"key_name"`
Options map[string]string `json:"options"` Options map[string]string `json:"options"`
Type string `json:"type"` Type string `json:"type"`

View File

@@ -25,7 +25,7 @@
</template> </template>
</el-table-column> </el-table-column>
<el-table-column prop="category" label="模型类别" /> <el-table-column prop="category" label="标签" />
<el-table-column prop="value" label="模型值"> <el-table-column prop="value" label="模型值">
<template #default="scope"> <template #default="scope">
<span>{{ scope.row.value }}</span> <span>{{ scope.row.value }}</span>
@@ -70,7 +70,12 @@
</el-table> </el-table>
</el-row> </el-row>
<el-dialog v-model="showDialog" :title="title" :close-on-click-modal="false" style="width: 90%; max-width: 600px"> <el-dialog
v-model="showDialog"
:title="title"
:close-on-click-modal="false"
style="width: 90%; max-width: 600px"
>
<el-form :model="item" label-width="120px" ref="formRef" :rules="rules"> <el-form :model="item" label-width="120px" ref="formRef" :rules="rules">
<el-form-item label="模型类型" prop="type"> <el-form-item label="模型类型" prop="type">
<el-select v-model="item.type" placeholder="请选择模型类型"> <el-select v-model="item.type" placeholder="请选择模型类型">
@@ -87,7 +92,7 @@
<el-input v-model="item.value" autocomplete="off" /> <el-input v-model="item.value" autocomplete="off" />
</el-form-item> </el-form-item>
<el-form-item label="模型类别" prop="category"> <el-form-item label="模型标签" prop="category">
<el-input v-model="item.category" autocomplete="off" /> <el-input v-model="item.category" autocomplete="off" />
</el-form-item> </el-form-item>
@@ -95,11 +100,6 @@
<template #label> <template #label>
<div class="flex items-center"> <div class="flex items-center">
<span class="mr-1">消耗算力</span> <span class="mr-1">消耗算力</span>
<el-tooltip effect="dark" content="每日签到赠送算力" raw-content placement="right">
<el-icon>
<InfoFilled />
</el-icon>
</el-tooltip>
</div> </div>
</template> </template>
<el-input v-model.number="item.power" autocomplete="off" placeholder="消耗算力" /> <el-input v-model.number="item.power" autocomplete="off" placeholder="消耗算力" />
@@ -107,21 +107,34 @@
<div v-if="item.type === 'chat'"> <div v-if="item.type === 'chat'">
<el-form-item label="最长响应" prop="max_tokens"> <el-form-item label="最长响应" prop="max_tokens">
<el-input v-model.number="item.max_tokens" autocomplete="off" placeholder="模型最大响应长度" /> <el-input
v-model.number="item.max_tokens"
autocomplete="off"
placeholder="模型最大响应长度"
/>
</el-form-item> </el-form-item>
<el-form-item> <el-form-item>
<template #label> <template #label>
<div class="flex items-center"> <div class="flex items-center">
<span class="mr-1">最大上下文</span> <span class="mr-1">最大上下文</span>
<el-tooltip effect="dark" content="去各大模型的官方 API 文档查询模型支持的最大上下文长度" raw-content placement="right"> <el-tooltip
effect="dark"
content="去各大模型的官方 API 文档查询模型支持的最大上下文长度"
raw-content
placement="right"
>
<el-icon> <el-icon>
<InfoFilled /> <InfoFilled />
</el-icon> </el-icon>
</el-tooltip> </el-tooltip>
</div> </div>
</template> </template>
<el-input v-model.number="item.max_context" autocomplete="off" placeholder="模型最大上下文长度" /> <el-input
v-model.number="item.max_context"
autocomplete="off"
placeholder="模型最大上下文长度"
/>
</el-form-item> </el-form-item>
<el-form-item label="模型描述" prop="description"> <el-form-item label="模型描述" prop="description">
@@ -132,7 +145,12 @@
<template #label> <template #label>
<div class="flex items-center"> <div class="flex items-center">
<span class="mr-1">创意度</span> <span class="mr-1">创意度</span>
<el-tooltip effect="dark" content="OpenAI 0-2其他模型 0-1" raw-content placement="right"> <el-tooltip
effect="dark"
content="OpenAI 0-2其他模型 0-1"
raw-content
placement="right"
>
<el-icon> <el-icon>
<InfoFilled /> <InfoFilled />
</el-icon> </el-icon>
@@ -170,7 +188,12 @@
<template #label> <template #label>
<div class="flex items-center"> <div class="flex items-center">
<span class="mr-1">开放状态</span> <span class="mr-1">开放状态</span>
<el-tooltip effect="dark" content="开放后该模型将对所有用户可见<br/> 如果模型没有启用则当前设置无效" raw-content placement="right"> <el-tooltip
effect="dark"
content="开放后该模型将对所有用户可见<br/> 如果模型没有启用则当前设置无效"
raw-content
placement="right"
>
<el-icon> <el-icon>
<InfoFilled /> <InfoFilled />
</el-icon> </el-icon>
@@ -192,76 +215,75 @@
</template> </template>
<script setup> <script setup>
import { onMounted, onUnmounted, reactive, ref } from "vue"; import { httpGet, httpPost } from '@/utils/http'
import { httpGet, httpPost } from "@/utils/http"; import { dateFormat, removeArrayItem, substr } from '@/utils/libs'
import { ElMessage } from "element-plus"; import { DocumentCopy, InfoFilled, Plus, Search } from '@element-plus/icons-vue'
import { dateFormat, removeArrayItem, substr } from "@/utils/libs"; import ClipboardJS from 'clipboard'
import { DocumentCopy, InfoFilled, Plus, Search } from "@element-plus/icons-vue"; import { ElMessage } from 'element-plus'
import { Sortable } from "sortablejs"; import { Sortable } from 'sortablejs'
import ClipboardJS from "clipboard"; import { onMounted, onUnmounted, reactive, ref } from 'vue'
import Default from "md-editor-v3";
// 变量定义 // 变量定义
const items = ref([]); const items = ref([])
const query = ref({ name: "" }); const query = ref({ name: '' })
const item = ref({}); const item = ref({})
const showDialog = ref(false); const showDialog = ref(false)
const title = ref(""); const title = ref('')
const rules = reactive({ const rules = reactive({
type: [{ required: true, message: "请选择模型类型", trigger: "change" }], type: [{ required: true, message: '请选择模型类型', trigger: 'change' }],
name: [{ required: true, message: "请输入模型名称", trigger: "change" }], name: [{ required: true, message: '请输入模型名称', trigger: 'change' }],
value: [{ required: true, message: "请输入模型值", trigger: "change" }], value: [{ required: true, message: '请输入模型值', trigger: 'change' }],
power: [{ required: true, message: "请输入消耗算力", trigger: "change" }], power: [{ required: true, message: '请输入消耗算力', trigger: 'change' }],
}); })
const loading = ref(true); const loading = ref(true)
const formRef = ref(null); const formRef = ref(null)
const type = ref([ const type = ref([
{ label: "聊天", value: "chat" }, { label: '聊天', value: 'chat' },
{ label: "绘图", value: "img" }, { label: '绘图', value: 'img' },
{ label: "语音", value: "tts" }, { label: '语音', value: 'tts' },
]); ])
const voices = ref([ const voices = ref([
{ label: "Echo", value: "echo" }, { label: 'Echo', value: 'echo' },
{ label: "Fable", value: "fable" }, { label: 'Fable', value: 'fable' },
{ label: "Onyx", value: "onyx" }, { label: 'Onyx', value: 'onyx' },
{ label: "Nova", value: "nova" }, { label: 'Nova', value: 'nova' },
{ label: "Shimmer", value: "shimmer" }, { label: 'Shimmer', value: 'shimmer' },
]); ])
// 获取 API KEY // 获取 API KEY
const apiKeys = ref([]); const apiKeys = ref([])
httpGet("/api/admin/apikey/list") httpGet('/api/admin/apikey/list')
.then((res) => { .then((res) => {
apiKeys.value = res.data; apiKeys.value = res.data
}) })
.catch((e) => { .catch((e) => {
ElMessage.error("获取 API KEY 失败" + e.message); ElMessage.error('获取 API KEY 失败:' + e.message)
}); })
// 获取数据 // 获取数据
const fetchData = () => { const fetchData = () => {
httpGet("/api/admin/model/list", query.value) httpGet('/api/admin/model/list', query.value)
.then((res) => { .then((res) => {
if (res.data) { if (res.data) {
// 初始化数据 // 初始化数据
const arr = res.data; const arr = res.data
for (let i = 0; i < arr.length; i++) { for (let i = 0; i < arr.length; i++) {
arr[i].last_used_at = dateFormat(arr[i].last_used_at); arr[i].last_used_at = dateFormat(arr[i].last_used_at)
} }
items.value = arr; items.value = arr
} }
loading.value = false; loading.value = false
}) })
.catch(() => { .catch(() => {
ElMessage.error("获取数据失败"); ElMessage.error('获取数据失败')
}); })
}; }
const clipboard = ref(null); const clipboard = ref(null)
onMounted(() => { onMounted(() => {
fetchData(); fetchData()
const drawBodyWrapper = document.querySelector(".el-table__body tbody"); const drawBodyWrapper = document.querySelector('.el-table__body tbody')
// 初始化拖动排序插件 // 初始化拖动排序插件
Sortable.create(drawBodyWrapper, { Sortable.create(drawBodyWrapper, {
@@ -269,94 +291,104 @@ onMounted(() => {
animation: 500, animation: 500,
onEnd({ newIndex, oldIndex, from }) { onEnd({ newIndex, oldIndex, from }) {
if (oldIndex === newIndex) { if (oldIndex === newIndex) {
return; return
} }
const sortedData = Array.from(from.children).map((row) => row.querySelector(".sort").getAttribute("data-id")); const sortedData = Array.from(from.children).map((row) =>
const ids = []; row.querySelector('.sort').getAttribute('data-id')
const sorts = []; )
const ids = []
const sorts = []
sortedData.forEach((id, index) => { sortedData.forEach((id, index) => {
ids.push(parseInt(id)); ids.push(parseInt(id))
sorts.push(index + 1); sorts.push(index + 1)
items.value[index].sort_num = index + 1; items.value[index].sort_num = index + 1
}); })
httpPost("/api/admin/model/sort", { ids: ids, sorts: sorts }) httpPost('/api/admin/model/sort', { ids: ids, sorts: sorts })
.then(() => {}) .then(() => {})
.catch((e) => { .catch((e) => {
ElMessage.error("排序失败" + e.message); ElMessage.error('排序失败' + e.message)
}); })
}, },
}); })
clipboard.value = new ClipboardJS(".copy-model"); clipboard.value = new ClipboardJS('.copy-model')
clipboard.value.on("success", () => { clipboard.value.on('success', () => {
ElMessage.success("复制成功"); ElMessage.success('复制成功')
}); })
clipboard.value.on("error", () => { clipboard.value.on('error', () => {
ElMessage.error("复制失败"); ElMessage.error('复制失败')
}); })
}); })
onUnmounted(() => { onUnmounted(() => {
clipboard.value.destroy(); clipboard.value.destroy()
}); })
const add = function () { const add = function () {
title.value = "新增模型"; title.value = '新增模型'
showDialog.value = true; showDialog.value = true
item.value = { enabled: true, power: 1, open: true, description: "", max_tokens: 1024, max_context: 8192, temperature: 0.9 }; item.value = {
}; enabled: true,
power: 1,
open: true,
description: '',
max_tokens: 1024,
max_context: 8192,
temperature: 0.9,
}
}
const edit = function (row) { const edit = function (row) {
title.value = "修改模型"; title.value = '修改模型'
showDialog.value = true; showDialog.value = true
item.value = row; item.value = row
}; }
const save = function () { const save = function () {
formRef.value.validate((valid) => { formRef.value.validate((valid) => {
item.value.temperature = parseFloat(item.value.temperature); item.value.temperature = parseFloat(item.value.temperature)
if (valid) { if (valid) {
showDialog.value = false; showDialog.value = false
item.value.key_id = parseInt(item.value.key_id); item.value.key_id = parseInt(item.value.key_id)
httpPost("/api/admin/model/save", item.value) httpPost('/api/admin/model/save', item.value)
.then(() => { .then(() => {
ElMessage.success("操作成功"); ElMessage.success('操作成功')
fetchData(); fetchData()
}) })
.catch((e) => { .catch((e) => {
ElMessage.error("操作失败" + e.message); ElMessage.error('操作失败' + e.message)
}); })
} else { } else {
return false; return false
} }
}); })
}; }
const modelSet = (filed, row) => { const modelSet = (filed, row) => {
httpPost("/api/admin/model/set", { id: row.id, filed: filed, value: row[filed] }) httpPost('/api/admin/model/set', { id: row.id, filed: filed, value: row[filed] })
.then(() => { .then(() => {
ElMessage.success("操作成功"); ElMessage.success('操作成功')
}) })
.catch((e) => { .catch((e) => {
ElMessage.error("操作失败" + e.message); ElMessage.error('操作失败' + e.message)
}); })
}; }
const remove = function (row) { const remove = function (row) {
httpGet("/api/admin/model/remove?id=" + row.id) httpGet('/api/admin/model/remove?id=' + row.id)
.then(() => { .then(() => {
ElMessage.success("删除成功"); ElMessage.success('删除成功')
items.value = removeArrayItem(items.value, row, (v1, v2) => { items.value = removeArrayItem(items.value, row, (v1, v2) => {
return v1.id === v2.id; return v1.id === v2.id
}); })
}) })
.catch((e) => { .catch((e) => {
ElMessage.error("删除失败" + e.message); ElMessage.error('删除失败' + e.message)
}); })
}; }
</script> </script>
<style lang="stylus" scoped> <style lang="stylus" scoped>