ForcePilot/web/src/views/SettingView.vue

624 lines
19 KiB
Vue
Raw Normal View History

<template>
2024-09-14 02:46:44 +08:00
<div class="">
<HeaderComponent title="设置" class="setting-header">
<template #description>
<p>配置文件也可以在 <code>src/static/config.yaml</code> 中修改</p>
2024-09-14 02:46:44 +08:00
</template>
<template #actions>
2024-09-28 00:41:18 +08:00
<a-button type="primary" v-if="isNeedRestart" @click="sendRestart">
<ReloadOutlined />需要重启
</a-button>
<a-button v-else @click="sendRestart">
2024-09-14 02:46:44 +08:00
<ReloadOutlined />重启服务
</a-button>
</template>
</HeaderComponent>
<div class="setting-container layout-container">
2024-09-28 00:41:18 +08:00
<div class="sider">
<a-button type="text" :class="{ activesec: state.section === 'base'}" @click="state.section='base'" :icon="h(SettingOutlined)"> 基本设置 </a-button>
<a-button type="text" :class="{ activesec: state.section === 'model'}" @click="state.section='model'" :icon="h(CodeOutlined)"> 模型配置 </a-button>
<a-button type="text" :class="{ activesec: state.section === 'path'}" @click="state.section='path'" :icon="h(FolderOutlined)"> 路径配置 </a-button>
</div>
<div class="setting" v-if="state.section === 'base'">
<h3>基础模型配置</h3>
2024-09-14 02:46:44 +08:00
<div class="section">
<div class="card">
2024-09-28 00:41:18 +08:00
<span class="label">{{ items?.embed_model.des }}</span>
2025-02-23 16:39:52 +08:00
<a-select style="width: 300px"
2024-09-14 02:46:44 +08:00
:value="configStore.config?.embed_model"
@change="handleChange('embed_model', $event)"
>
<a-select-option
v-for="(name, idx) in items?.embed_model.choices" :key="idx"
:value="name">{{ name }}
</a-select-option>
</a-select>
</div>
<div class="card">
2024-09-28 00:41:18 +08:00
<span class="label">{{ items?.reranker.des }}</span>
2025-02-23 16:39:52 +08:00
<a-select style="width: 300px"
2024-09-14 02:46:44 +08:00
:value="configStore.config?.reranker"
@change="handleChange('reranker', $event)"
:disabled="!configStore.config.enable_reranker"
>
<a-select-option
v-for="(name, idx) in items?.reranker.choices" :key="idx"
:value="name">{{ name }}
</a-select-option>
</a-select>
</div>
</div>
2024-09-14 02:46:44 +08:00
<h3>功能配置</h3>
<div class="section">
<div class="card">
2024-09-28 00:41:18 +08:00
<span class="label">{{ items?.enable_knowledge_base.des }}</span>
2024-09-14 02:46:44 +08:00
<a-switch
:checked="configStore.config.enable_knowledge_base"
@change="handleChange('enable_knowledge_base', !configStore.config.enable_knowledge_base)"
/>
</div>
<div class="card">
2024-09-28 00:41:18 +08:00
<span class="label">{{ items?.enable_knowledge_graph.des }}</span>
2024-09-14 02:46:44 +08:00
<a-switch
:checked="configStore.config.enable_knowledge_graph"
@change="handleChange('enable_knowledge_graph', !configStore.config.enable_knowledge_graph)"
/>
</div>
<div class="card">
<span class="label">{{ items?.enable_web_search.des }}</span>
2024-09-14 02:46:44 +08:00
<a-switch
:checked="configStore.config.enable_web_search"
@change="handleChange('enable_web_search', !configStore.config.enable_web_search)"
2024-09-14 02:46:44 +08:00
/>
</div>
2024-09-14 02:46:44 +08:00
<div class="card">
2024-09-28 00:41:18 +08:00
<span class="label">{{ items?.enable_reranker.des }}</span>
2024-09-14 02:46:44 +08:00
<a-switch
:checked="configStore.config.enable_reranker"
@change="handleChange('enable_reranker', !configStore.config.enable_reranker)"
/>
</div>
</div>
<h3>检索配置</h3>
<div class="section">
<div class="card">
<span class="label">{{ items?.use_rewrite_query.des }}</span>
<a-select style="width: 200px"
:value="configStore.config?.use_rewrite_query"
@change="handleChange('use_rewrite_query', $event)"
>
<a-select-option
v-for="(name, idx) in items?.use_rewrite_query.choices" :key="idx"
:value="name">{{ name }}
</a-select-option>
</a-select>
</div>
</div>
</div>
2024-11-23 11:14:26 +08:00
<div class="setting" v-if="state.section === 'model'">
2024-09-28 00:41:18 +08:00
<h3>模型配置</h3>
<p>请在 <code>src/.env</code> 文件中配置对应的 APIKEY</p>
2024-10-08 22:16:17 +08:00
<div class="model-provider-card">
<div class="card-header">
<h3>自定义模型</h3>
</div>
<div class="card-body">
<div
:class="{'model_selected': modelProvider == 'custom' && configStore.config.model_name == item.custom_id, 'card-models': true, 'custom-model': true}"
v-for="(item, key) in configStore.config.custom_models" :key="item.custom_id"
@click="handleChange('model_provider', 'custom'); handleChange('model_name', item.custom_id)"
2024-10-08 22:16:17 +08:00
>
<div class="card-models__header">
<div class="name">{{ item.name }}</div>
<div class="action">
<a-popconfirm
title="确认删除该模型?"
@confirm="handleDeleteCustomModel(item.custom_id)"
2024-10-08 22:16:17 +08:00
okText="确认删除"
cancelText="取消"
ok-type="danger"
:disabled="configStore.config.model_name == item.name"
>
<a-button type="text" :disabled="configStore.config.model_name == item.name" @click.stop><DeleteOutlined /></a-button>
</a-popconfirm>
<a-button type="text" @click.stop="handleEditCustomModel(item)"><EditOutlined /></a-button>
</div>
</div>
<div class="api_base">{{ item.api_base }}</div>
</div>
<div class="card-models custom-model" @click="customModel.visible=true">
<div class="card-models__header">
<div class="name"> + 添加模型</div>
</div>
<div class="api_base">添加兼容 OpenAI 的模型</div>
<a-modal
class="custom-model-modal"
v-model:open="customModel.visible"
:title="customModel.modelTitle"
@ok="handleAddCustomModel"
@cancel="handleCancelCustomModel"
:okText="'确认'"
:cancelText="'取消'"
:okButtonProps="{disabled: !customModel.name || !customModel.api_base}"
:ok-type="'primary'"
>
<p>添加的模型是兼容 OpenAI 的模型比如 vllmOllama</p>
<a-form :model="customModel" layout="vertical" >
<a-form-item label="模型名称" name="name" :rules="[{ required: true, message: '请输入模型名称' }]">
<a-input v-model:value="customModel.name" />
</a-form-item>
<a-form-item label="API Base" name="api_base" :rules="[{ required: true, message: '请输入API Base' }]">
2024-10-14 16:51:20 +08:00
<a-input v-model:value="customModel.api_base"/>
2024-10-08 22:16:17 +08:00
</a-form-item>
<a-form-item label="API KEY" name="api_key">
<a-input v-model:value="customModel.api_key" autocomplete="off"/>
</a-form-item>
</a-form>
</a-modal>
</div>
</div>
</div>
<div class="model-provider-card" v-for="(item, key) in modelKeys" :key="key">
2024-09-28 00:41:18 +08:00
<div class="card-header">
<div v-if="modelStatus[item]" class="success"></div>
2024-10-24 20:08:23 +08:00
<h3>{{ modelNames[item].name }}</h3>
<a :href="modelNames[item].url" target="_blank"><InfoCircleOutlined /></a>
2024-09-28 00:41:18 +08:00
</div>
<div class="card-body" v-if="modelStatus[item]">
<div
:class="{'model_selected': modelProvider == item && configStore.config.model_name == model, 'card-models': true}"
v-for="(model, idx) in modelNames[item].models" :key="idx"
@click="handleChange('model_provider', item); handleChange('model_name', model)"
>
<div class="model_name">{{ model }}</div>
</div>
</div>
</div>
<div class="model-provider-card" v-for="(item, key) in notModelKeys" :key="key">
<div class="card-header">
<h3 style="font-weight: 400">{{ modelNames[item].name }}</h3>
<a :href="modelNames[item].url" target="_blank"><InfoCircleOutlined /></a>
2024-09-28 00:41:18 +08:00
<div class="missing-keys">
<small>需配置</small> <span v-for="(key, idx) in modelNames[item].env" :key="idx">{{ key }}</span>
2024-09-28 00:41:18 +08:00
</div>
</div>
</div>
</div>
2024-11-23 11:14:26 +08:00
<div class="setting" v-if="state.section ==='path'">
<h3>本地模型配置</h3>
<p>如果是 Docker 启动务必确保在环境变量中设置了 MODEL_DIR或者设置了 volumes 映射</p>
<TableConfigComponent
:config="configStore.config?.model_local_paths"
@update:config="handleModelLocalPathsUpdate"
/>
2024-11-14 20:07:49 +08:00
</div>
</div>
</div>
</template>
<script setup>
import { message } from 'ant-design-vue';
2024-10-08 22:16:17 +08:00
import { computed, reactive, ref, h, watch } from 'vue'
2024-07-27 14:32:57 +08:00
import { useConfigStore } from '@/stores/config';
2024-09-28 00:41:18 +08:00
import {
ReloadOutlined,
SettingOutlined,
CodeOutlined,
ExceptionOutlined,
FolderOutlined,
2024-10-08 22:16:17 +08:00
DeleteOutlined,
EditOutlined,
2024-10-24 20:08:23 +08:00
InfoCircleOutlined,
2024-09-28 00:41:18 +08:00
} from '@ant-design/icons-vue';
2024-09-14 02:46:44 +08:00
import HeaderComponent from '@/components/HeaderComponent.vue';
import TableConfigComponent from '@/components/TableConfigComponent.vue';
2024-10-08 22:16:17 +08:00
import { notification, Button } from 'ant-design-vue';
const configStore = useConfigStore()
const items = computed(() => configStore.config._config_items)
2024-09-28 00:41:18 +08:00
const modelNames = computed(() => configStore.config?.model_names)
const modelStatus = computed(() => configStore.config?.model_provider_status)
const modelProvider = computed(() => configStore.config?.model_provider)
const isNeedRestart = ref(false)
2024-10-08 22:16:17 +08:00
const customModel = reactive({
modelTitle: '添加自定义模型',
visible: false,
custom_id: '', // Add this line
2024-10-08 22:16:17 +08:00
name: '',
api_key: '',
api_base: '',
edit_type: 'add',
})
const state = reactive({
loading: false,
2024-09-28 00:41:18 +08:00
section: 'base'
})
// 筛选 modelStatus 中为真的key
const modelKeys = computed(() => {
return Object.keys(modelStatus.value).filter(key => modelStatus.value[key])
})
const notModelKeys = computed(() => {
return Object.keys(modelStatus.value).filter(key => !modelStatus.value[key])
})
const generateRandomHash = (length) => {
let chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let hash = '';
for (let i = 0; i < length; i++) {
hash += chars.charAt(Math.floor(Math.random() * chars.length));
}
return hash;
}
const handleModelLocalPathsUpdate = (config) => {
handleChange('model_local_paths', config)
}
const handleChange = (key, e) => {
2024-09-03 16:37:59 +08:00
if (key == 'enable_knowledge_graph' && e && !configStore.config.enable_knowledge_base) {
2024-09-06 12:54:17 +08:00
message.error('启动知识图谱必须请先启用知识库功能')
2024-09-03 16:37:59 +08:00
return
}
2024-09-06 12:54:17 +08:00
if (key == 'enable_knowledge_base' && !e && configStore.config.enable_knowledge_graph) {
message.error('关闭知识库功能必须请先关闭知识图谱功能')
return
}
2024-10-24 13:09:59 +08:00
// 这些都是需要重启的配置
2024-09-28 00:41:18 +08:00
if (key == 'enable_reranker'
|| key == 'enable_knowledge_graph'
|| key == 'enable_knowledge_base'
|| key == 'model_provider'
|| key == 'model_name'
|| key == 'embed_model'
|| key == 'reranker'
|| key == 'model_local_paths') {
2024-09-28 00:41:18 +08:00
if (!isNeedRestart.value) {
isNeedRestart.value = true
2024-10-08 22:16:17 +08:00
notification.info({
message: '需要重启服务',
description: '请点击右下角按钮重启服务',
placement: 'topLeft',
duration: 0,
btn: h(Button, { type: 'primary', onClick: sendRestart }, '立即重启')
})
2024-09-28 00:41:18 +08:00
}
}
configStore.setConfigValue(key, e)
}
2024-10-08 22:16:17 +08:00
const handleAddCustomModel = async () => {
if (!customModel.name || !customModel.api_base) {
message.error('请填写完整模型信息')
return
}
if (!configStore.config.custom_models) {
configStore.config.custom_models = []
}
const model_info = {
custom_id: customModel.custom_id,
name: customModel.name,
api_key: customModel.api_key,
api_base: customModel.api_base,
2024-10-08 22:16:17 +08:00
}
if (customModel.edit_type == 'add') {
if (configStore.config.custom_models.find(item => item.custom_id == customModel.custom_id)) {
message.error('模型ID已存在')
return
}
const modelHash = generateRandomHash(4)
model_info.custom_id = `${customModel.name}-${modelHash}`
configStore.config.custom_models.push(model_info)
2024-10-08 22:16:17 +08:00
} else {
configStore.config.custom_models = configStore.config.custom_models.map(item => {
if (item.custom_id == customModel.custom_id) {
if (item.name != customModel.name) {
const modelHash = generateRandomHash(4)
model_info.custom_id = `${customModel.name}-${modelHash}`
configStore.setConfigValue('model_name', model_info.custom_id)
}
return model_info
2024-10-08 22:16:17 +08:00
}
return item
})
}
customModel.visible = false
await configStore.setConfigValue('custom_models', configStore.config.custom_models)
message.success('添加自定义模型成功')
}
const handleDeleteCustomModel = (custom_id) => {
const updatedModels = configStore.config.custom_models.filter(item => item.custom_id !== custom_id);
2024-10-24 13:09:59 +08:00
configStore.setConfigValue('custom_models', updatedModels);
2024-10-08 22:16:17 +08:00
}
const handleEditCustomModel = (item) => {
customModel.modelTitle = '编辑自定义模型'
customModel.custom_id = item.custom_id
2024-10-08 22:16:17 +08:00
customModel.name = item.name
customModel.api_key = item.api_key
customModel.api_base = item.api_base
customModel.visible = true
customModel.edit_type = 'edit'
}
const handleCancelCustomModel = () => {
customModel.custom_id = ''
2024-10-08 22:16:17 +08:00
customModel.name = ''
customModel.api_key = ''
customModel.api_base = ''
customModel.visible = false
}
const sendRestart = () => {
console.log('Restarting...')
2024-07-27 14:32:57 +08:00
message.loading({ content: '重新加载模型中', key: "restart", duration: 0 });
fetch('/api/restart', {
method: 'POST',
}).then(() => {
console.log('Restarted')
2024-07-27 14:32:57 +08:00
message.success({ content: '重新加载完成!', key: "restart", duration: 2 });
setTimeout(() => {
window.location.reload()
2024-07-31 20:22:05 +08:00
}, 200)
})
}
</script>
<style lang="less" scoped>
2024-09-28 00:41:18 +08:00
.setting-header {
height: 100px;
}
2024-09-14 02:46:44 +08:00
.setting-header p {
margin: 8px 0 0;
}
.setting-container {
padding: 0;
box-sizing: border-box;
display: flex;
2024-09-28 00:41:18 +08:00
position: relative;
2024-10-08 22:16:17 +08:00
min-height: 100%;
2024-09-28 00:41:18 +08:00
}
.sider {
width: 200px;
height: 100%;
padding: 0 20px;
position: sticky;
top: 100px;
display: flex;
flex-direction: column;
align-items: center;
border-right: 1px solid var(--main-light-3);
gap: 8px;
padding-top: 20px;
& > * {
width: 100%;
height: auto;
padding: 6px 16px;
cursor: pointer;
transition: all 0.1s;
text-align: left;
font-size: 15px;
border-radius: 8px;
color: var(--gray-700);
&:hover {
background: var(--gray-100);
}
&.activesec {
background: var(--gray-200);
color: var(--gray-900);
}
}
2024-09-14 02:46:44 +08:00
}
.setting {
2024-09-14 02:46:44 +08:00
width: 100%;
2024-09-28 00:41:18 +08:00
flex: 1;
margin: 0 auto;
2024-09-14 02:46:44 +08:00
height: 100%;
2024-09-28 00:41:18 +08:00
padding: 0 20px;
margin-bottom: 40px;
h3 {
margin-top: 20px;
}
.section {
margin-top: 20px;
2024-09-28 00:41:18 +08:00
background-color: var(--gray-10);
padding: 20px;
border-radius: 8px;
display: flex;
flex-direction: column;
gap: 16px;
// box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
2024-09-28 00:41:18 +08:00
border: 1px solid var(--gray-300);
}
.card {
display: flex;
align-items: center;
justify-content: space-between;
.label {
margin-right: 20px;
2024-09-06 21:30:49 +08:00
button {
margin-left: 10px;
height: 24px;
padding: 0 8px;
font-size: smaller;
}
}
}
2024-09-28 00:41:18 +08:00
.model-provider-card {
border: 1px solid var(--gray-300);
2024-10-08 22:16:17 +08:00
background-color: white;
2024-09-28 00:41:18 +08:00
border-radius: 8px;
margin-bottom: 16px;
padding: 16px;
.card-header {
display: flex;
align-items: baseline;
gap: 10px;
h3 {
margin: 0;
font-size: 1rem;
2024-10-24 20:08:23 +08:00
font-weight: bold;
2024-09-28 00:41:18 +08:00
}
a {
text-decoration: none;
2024-10-24 20:08:23 +08:00
color: var(--gray-500);
font-size: 12px;
2024-09-28 00:41:18 +08:00
transition: all 0.1s;
&:hover {
color: var(--gray-900);
}
}
2024-10-24 20:08:23 +08:00
.details, .missing-keys {
2024-09-28 00:41:18 +08:00
margin-left: auto;
}
.success {
2024-10-24 20:08:23 +08:00
width: 0.75rem;
height: 0.75rem;
2024-10-08 22:16:17 +08:00
background-color: rgb(91, 186, 91);
2024-09-28 00:41:18 +08:00
border-radius: 50%;
2024-10-24 20:08:23 +08:00
box-shadow: 0 0 10px 1px rgba( 0,128, 0, 0.1);
2024-09-28 00:41:18 +08:00
border: 2px solid white;
}
.missing-keys {
color: var(--gray-600);
& > span {
margin-left: 10px;
2024-10-24 20:08:23 +08:00
user-select: all;
2024-09-28 00:41:18 +08:00
}
}
}
.card-body {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 10px;
margin-top: 10px;
.card-models {
width: 100%;
border-radius: 8px;
border: 1px solid var(--gray-300);
padding: 10px 16px;
display: flex;
gap: 6px;
justify-content: space-between;
align-items: center;
cursor: pointer;
box-sizing: border-box;
2024-10-08 22:16:17 +08:00
background-color: var(--gray-10);
transition: box-shadow 0.1s;
2024-09-28 00:41:18 +08:00
&:hover {
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
.model_name {
font-size: 14px;
color: var(--gray-800);
}
.select-btn {
width: 16px;
height: 16px;
flex: 0 0 16px;
border-radius: 50%;
}
&.model_selected {
border: 2px solid var(--main-color);
padding: 9px 15px;
.model_name {
color: var(--gray-1000)
}
.select-btn {
border-color: var(--main-color);
border: 2px solid var(--main-color);
}
}
2024-10-08 22:16:17 +08:00
&.custom-model {
display: flex;
flex-direction: column;
align-items: flex-start;
padding-right: 8px;
gap: 10px;
.card-models__header {
width: 100%;
height: 32px;
display: flex;
justify-content: flex-start;
align-items: center;
.name {
color: var(--gray-1000);
font-weight: bold;
}
.action {
opacity: 0;
user-select: none;
margin-left: auto;
button {
padding: 4px 8px;
}
}
.custom-model-modal {
.ant-form-item {
margin-bottom: 10px;
}
}
}
.api_base {
font-size: 12px;
color: var(--gray-600);
}
&:hover {
.card-models__header {
.action {
opacity: 1;
}
}
}
}
&.model_selected.custom-model {
padding: 9px 7px 9px 15px;
.card-models__header {
.action {
opacity: 1;
}
}
}
2024-09-28 00:41:18 +08:00
}
2024-10-08 22:16:17 +08:00
2024-09-28 00:41:18 +08:00
}
}
}
2024-10-24 13:09:59 +08:00
</style>