update setting page

This commit is contained in:
Wenjie Zhang 2024-09-28 00:41:18 +08:00
parent e0cb01eb8d
commit 5dbb1f4d76
2 changed files with 328 additions and 132 deletions

View File

@ -49,10 +49,10 @@ class Config(SimpleConfig):
# 模型配置
## 注意这里是模型名,而不是具体的模型路径,默认使用 HuggingFace 的路径
## 如果需要自定义路径,则在 config/base.yaml 中配置 model_local_paths
self.add_item("model_provider", default="zhipu", des="模型提供商", choices=["openai", "qianfan", "vllm", "zhipu", "deepseek", "dashscope"])
self.add_item("model_provider", default="zhipu", des="模型提供商", choices=list(MODEL_NAMES.keys()))
self.add_item("model_name", default=None, des="模型名称")
self.add_item("embed_model", default="zhipu-embedding-3", des="Embedding 模型", choices=list(EMBED_MODEL_INFO.keys()))
self.add_item("reranker", default="bge-reranker-v2-m3", des="Re-Ranker 模型", choices=["bge-reranker-v2-m3"])
self.add_item("reranker", default="bge-reranker-v2-m3", des="Re-Ranker 模型", choices=list(RERANKER_LIST.keys()))
self.add_item("model_local_paths", default={}, des="本地模型路径")
### <<< 默认配置结束
@ -74,27 +74,25 @@ class Config(SimpleConfig):
blocklist = [
"_config_items",
"model_names",
"model_provider_status",
]
return {k: v for k, v in self.items() if k not in blocklist}
def handle_self(self):
### handle local model
# model_root_dir = os.getenv("MODEL_ROOT_DIR", None)
# if self.model_local_paths is not None:
# for model, model_rel_path in self.model_local_paths.items():
# # 如果 model_rel_path 不是绝对路径,那么拼接 model_root_dir
# if not model_rel_path.startswith("/"):
# self.model_local_paths[model] = os.path.join(model_root_dir, model_rel_path)
self.model_names = MODEL_NAMES
if self.model_name not in self.model_names[self.model_provider]:
if self.model_name not in self.model_names[self.model_provider]["models"]:
logger.warning(f"Model name {self.model_name} not in {self.model_provider}, using default model name")
self.model_name = self.model_names[self.model_provider][0]
self.model_name = self.model_names[self.model_provider]["default"]
default_model_name = self.model_names[self.model_provider][0]
default_model_name = self.model_names[self.model_provider]["default"]
self.model_name = self.get("model_name") or default_model_name
self.model_provider_status = {}
for provider in self.model_names:
conds = [bool(os.getenv(_k)) for _k in self.model_names[provider]["env"]]
self.model_provider_status[provider] = all(conds)
def load(self):
"""根据传入的文件覆盖掉默认配置"""
logger.info(f"Loading config from {self.filename}")
@ -144,53 +142,106 @@ class Config(SimpleConfig):
logger.info(f"Config file {self.filename} saved")
MODEL_NAMES = {
"openai": {
"name": "OpenAI",
"url": "https://platform.openai.com/docs/models",
"default": "gpt-3.5-turbo",
"env": ["OPENAI_API_KEY"],
"models": [
"gpt-4",
"gpt-4o",
"gpt-4-0125-preview",
"gpt-4o-mini",
"gpt-3.5-turbo"
]
},
# https://platform.deepseek.com/api-docs/zh-cn/pricing
"openai": [
"gpt-4",
"gpt-4o",
"gpt-4-0125-preview",
"gpt-4o-mini",
],
"deepseek": {
"name": "DeepSeek",
"url": "https://platform.deepseek.com/api-docs/zh-cn/pricing",
"default": "deepseek-chat",
"env": ["DEEPSEEK_API_KEY"],
"models": [
"deepseek-chat",
]
},
"deepseek": [
"deepseek-chat",
"deepseek-coder"
],
# https://open.bigmodel.cn/dev/api glm-4-0520、glm-4 、glm-4-air、glm-4-airx、 glm-4-flash
"zhipu": [
"glm-4",
"glm-4-0520",
"glm-4-air",
"glm-4-airx",
"glm-4-flash"
],
# https://open.bigmodel.cn/dev/api glm-4-plus、glm-4-0520、glm-4 、glm-4-air、glm-4-airx、glm-4-long 、 glm-4-flashx 、 glm-4-flash
"zhipu": {
"name": "智谱AI (Zhipu)",
"url": "https://open.bigmodel.cn/dev/api",
"default": "glm-4-flash",
"env": ["ZHIPUAI_API_KEY"],
"models": [
"glm-4",
"glm-4-plus",
"glm-4-air",
"glm-4-airx",
"glm-4-long",
"glm-4-flashx",
"glm-4-flash",
]
},
# {'ERNIE-4.0-8K-0104', 'ERNIE-Lite-8K-0308', 'ERNIE-Speed-128K', 'ERNIE-3.5-128K预览版', 'Yi-34B-Chat', 'ERNIE-4.0-8K-Preview-0518', 'ERNIE-Bot-4', 'ERNIE-3.5-128K', 'ChatGLM2-6B-32K', 'ERNIE-3.5-8K', 'EB-turbo-AppBuilder', 'ERNIE-Lite-AppBuilder-8K', 'ERNIE-4.0-8K-0329', 'AquilaChat-7B', 'Gemma-7B-it', 'Qianfan-Chinese-Llama-2-70B', 'Mixtral-8x7B-Instruct', 'Gemma-7B-It', 'ERNIE Speed-AppBuilder', 'ERNIE-Function-8K', 'ERNIE-4.0-8K-preview', 'ERNIE-Bot', 'Qianfan-BLOOMZ-7B-compressed', 'ERNIE-4.0-8K', 'BLOOMZ-7B', 'ERNIE-Character-8K', 'ERNIE-3.5-8K-0205', 'ERNIE-4.0-8K-0613', 'Llama-2-70B-Chat', 'ERNIE-Character-Fiction-8K', 'ERNIE-4.0-8K-Preview', 'ERNIE-3.5-8K-Preview', 'ERNIE-Speed', 'ERNIE-Tiny-8K', 'ERNIE-4.0-Turbo-8K-Preview', 'Meta-Llama-3-8B', 'ERNIE-4.0-8K-Latest', 'ERNIE 3.5', 'XuanYuan-70B-Chat-4bit', 'Llama-2-13B-Chat', 'ERNIE-Bot-turbo', 'ERNIE-3.5-8K-0613', 'ERNIE-Lite-AppBuilder-8K-0614', 'ERNIE-4.0-preview', 'Llama-2-7B-Chat', 'Qianfan-Chinese-Llama-2-13B', 'ERNIE-Bot-turbo-AI', 'Meta-Llama-3-70B', 'ERNIE-Functions-8K', 'ERNIE-Lite-8K-0922原ERNIE-Bot-turbo-0922', 'ERNIE Speed', 'ERNIE-3.5-preview', 'Qianfan-Chinese-Llama-2-7B', 'ERNIE-Speed-8K', 'ERNIE-Lite-8K-0922', 'ChatLaw', 'ERNIE-3.5-8K-0329', 'ERNIE-4.0-Turbo-8K', 'ERNIE-3.5-8K-preview', 'ERNIE-Lite-8K'}
"qianfan": [
"ERNIE-Speed",
"ERNIE-Speed-8K",
"ERNIE-Speed-128K",
"ERNIE-Tiny-8K",
"ERNIE-Lite-8K",
"ERNIE-4.0-8K-Latest",
"Yi-34B-Chat",
],
"qianfan": {
"name": "百度千帆 (QianFan)",
"url": "https://open.bigmodel.cn/dev/api",
"default": "ERNIE-Speed",
"env": ["QIANFAN_ACCESS_KEY", "QIANFAN_SECRET_KEY"],
"models": [
"ERNIE-Speed",
"ERNIE-Speed-8K",
"ERNIE-Speed-128K",
"ERNIE-Tiny-8K",
"ERNIE-Lite-8K",
"ERNIE-4.0-8K-Latest",
]
},
"vllm": [
"vllm",
],
"vllm": {
"name": "VLLM",
"default": "vllm",
"env": ["VLLM_API_KEY", "VLLM_API_BASE"],
"models": [
"vllm",
]
},
# https://bailian.console.aliyun.com/?switchAgent=10226727&productCode=p_efm#/model-market
"dashscope": [
"qwen-long",
"qwen2-7b-instruct",
"qwen2-1.5b-instruct",
"llama3.1-8b-instruct",
"llama3-8b-instruct",
"llama3.1-405b-instruct",
"qwen2-0.5b-instruct"
]
"dashscope": {
"name": "阿里百炼 (DashScope)",
"url": "https://bailian.console.aliyun.com/?switchAgent=10226727&productCode=p_efm#/model-market",
"default": "qwen2.5-72b-instruct",
"env": ["DASHSCOPE_API_KEY"],
"models": [
"qwen-max-latest",
"qwen-plus-latest",
"qwen-long-latest",
"qwen-turbo-latest",
"qwen2.5-72b-instruct",
"qwen2.5-32b-instruct",
"qwen2.5-14b-instruct",
"qwen2.5-7b-instruct",
"qwen2.5-3b-instruct",
"qwen2.5-1.5b-instruct",
"qwen2.5-0.5b-instruct",
]
},
# https://cloud.siliconflow.cn/models
"siliconflow": {
"name": "SiliconFlow",
"url": "https://cloud.siliconflow.cn/models",
"default": "meta-llama/Meta-Llama-3.1-8B-Instruct",
"env": ["SILICONFLOW_API_KEY"],
"models": [
"meta-llama/Meta-Llama-3.1-8B-Instruct",
"meta-llama/Meta-Llama-3.1-70B-Instruct",
"meta-llama/Meta-Llama-3.1-405B-Instruct",
]
}
}

View File

@ -5,59 +5,25 @@
<p>配置文件也可以在 <code>saves/config/config.yaml</code> 中修改</p>
</template>
<template #actions>
<a-button type="primary" @click="sendRestart">
<a-button type="primary" v-if="isNeedRestart" @click="sendRestart">
<ReloadOutlined />需要重启
</a-button>
<a-button v-else @click="sendRestart">
<ReloadOutlined />重启服务
</a-button>
</template>
</HeaderComponent>
<div class="setting-container layout-container">
<div class="setting">
<h3>模型配置</h3>
<p>请在 <code>src/.env</code> 文件中配置对应的 APIKEY可参考 <code>src/.env.template</code></p>
<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>
<div class="section">
<div class="card">
<span class="label">
{{ items?.model_provider.des }} &nbsp;
<a-button small v-if="needRestart.model_provider" @click="sendRestart">
<ReloadOutlined />需要重启
</a-button>
</span>
<a-select ref="select" style="width: 200px"
:value="configStore.config?.model_provider"
@change="handleChange('model_provider', $event)"
>
<a-select-option
v-for="(name, idx) in items?.model_provider.choices" :key="idx"
:value="name">{{ name }}
</a-select-option>
</a-select>
</div>
<div class="card">
<span class="label">
{{ items?.model_name.des }} &nbsp;
<a-button small v-if="needRestart.model_name" @click="sendRestart">
<ReloadOutlined />需要重启
</a-button>
</span>
<a-select ref="select" style="width: 200px"
:value="configStore.config?.model_name"
@change="handleChange('model_name', $event)"
v-if="configStore.config?.model_names && configStore.config?.model_provider && configStore.config?.model_names[configStore.config?.model_provider]"
>
<a-select-option
v-for="(name, idx) in configStore.config.model_names[configStore.config.model_provider]"
:key="idx"
:value="name">{{ name }}
</a-select-option>
</a-select>
</div>
<div class="card">
<span class="label">
{{ items?.embed_model.des }} &nbsp;
<a-button small v-if="needRestart.embed_model" @click="sendRestart">
<ReloadOutlined />需要重启
</a-button>
</span>
<span class="label">{{ items?.embed_model.des }}</span>
<a-select style="width: 200px"
:value="configStore.config?.embed_model"
@change="handleChange('embed_model', $event)"
@ -69,12 +35,7 @@
</a-select>
</div>
<div class="card">
<span class="label">
{{ items?.reranker.des }} &nbsp;
<a-button small v-if="needRestart.reranker" @click="sendRestart">
<ReloadOutlined />需要重启
</a-button>
</span>
<span class="label">{{ items?.reranker.des }}</span>
<a-select style="width: 200px"
:value="configStore.config?.reranker"
@change="handleChange('reranker', $event)"
@ -90,22 +51,14 @@
<h3>功能配置</h3>
<div class="section">
<div class="card">
<span class="label">{{ items?.enable_knowledge_base.des }}
<a-button small v-if="needRestart.enable_knowledge_base" @click="sendRestart">
<ReloadOutlined />需要重启
</a-button>
</span>
<span class="label">{{ items?.enable_knowledge_base.des }}</span>
<a-switch
:checked="configStore.config.enable_knowledge_base"
@change="handleChange('enable_knowledge_base', !configStore.config.enable_knowledge_base)"
/>
</div>
<div class="card">
<span class="label">{{ items?.enable_knowledge_graph.des }}
<a-button small v-if="needRestart.enable_knowledge_graph" @click="sendRestart">
<ReloadOutlined />需要重启
</a-button>
</span>
<span class="label">{{ items?.enable_knowledge_graph.des }}</span>
<a-switch
:checked="configStore.config.enable_knowledge_graph"
@change="handleChange('enable_knowledge_graph', !configStore.config.enable_knowledge_graph)"
@ -119,11 +72,7 @@
/>
</div>
<div class="card">
<span class="label">{{ items?.enable_reranker.des }}
<a-button small v-if="needRestart.enable_reranker" @click="sendRestart">
<ReloadOutlined />需要重启
</a-button>
</span>
<span class="label">{{ items?.enable_reranker.des }}</span>
<a-switch
:checked="configStore.config.enable_reranker"
@change="handleChange('enable_reranker', !configStore.config.enable_reranker)"
@ -131,26 +80,71 @@
</div>
</div>
</div>
<div class="setting" v-if="state.section == 'model'">
<h3>模型配置</h3>
<p>请在 <code>src/.env</code> 文件中配置对应的 APIKEY</p>
<div class="model-provider-card" v-for="(item, key) in modelKeys" :key="key">
<div class="card-header">
<h3>{{ modelNames[item].name }}</h3>
<a :href="modelNames[item].url" target="_blank">详情</a>
<div v-if="modelStatus[item]" class="success"></div>
</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 class="select-btn"></div>
</div>
</div>
</div>
<div class="model-provider-card" v-for="(item, key) in notModelKeys" :key="key">
<div class="card-header">
<h3>{{ modelNames[item].name }}</h3>
<a :href="modelNames[item].url" target="_blank">详情</a>
<div class="missing-keys">
需配置 <span v-for="(key, idx) in modelNames[item].env" :key="idx">{{ key }}</span>
</div>
</div>
</div>
</div>
</div>
</div>
</template>
<script setup>
import { message } from 'ant-design-vue';
import { computed, reactive, ref } from 'vue'
import { computed, reactive, ref, h } from 'vue'
import { useConfigStore } from '@/stores/config';
import { ReloadOutlined } from '@ant-design/icons-vue';
import {
ReloadOutlined,
SettingOutlined,
CodeOutlined,
ExceptionOutlined,
FolderOutlined,
} from '@ant-design/icons-vue';
import HeaderComponent from '@/components/HeaderComponent.vue';
const configStore = useConfigStore()
const needRestart = reactive({
model_provider: false,
embed_model: false,
reranker: false,
})
const items = computed(() => configStore.config._config_items)
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)
const state = reactive({
loading: false,
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 handleChange = (key, e) => {
@ -165,7 +159,20 @@ const handleChange = (key, e) => {
}
console.log('Change', key, e)
needRestart[key] = true
if (key == 'enable_reranker'
|| key == 'enable_knowledge_graph'
|| key == 'enable_knowledge_base'
|| key == 'model_provider'
|| key == 'model_name'
|| key == 'embed_model'
|| key == 'reranker') {
if (!isNeedRestart.value) {
isNeedRestart.value = true
message.info('修改配置后需要重启服务才能生效')
}
}
configStore.setConfigValue(key, e)
}
const sendRestart = () => {
@ -184,22 +191,65 @@ const sendRestart = () => {
</script>
<style lang="less" scoped>
.setting-header {
height: 100px;
}
.setting-header p {
margin: 8px 0 0;
}
.setting-container {
height: 100%;
padding: 0;
box-sizing: border-box;
display: flex;
background: inherit;
position: relative;
}
.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);
}
}
}
.setting {
width: 100%;
max-width: 800px;
flex: 1;
margin: 0 auto;
height: 100%;
padding: 0 20px;
margin-bottom: 40px;
h3 {
margin-top: 20px;
@ -207,14 +257,14 @@ const sendRestart = () => {
.section {
margin-top: 20px;
background-color: var(--main-light-4);
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);
border: 1px solid var(--main-light-3);
border: 1px solid var(--gray-300);
}
.card {
@ -234,5 +284,100 @@ const sendRestart = () => {
}
}
.model-provider-card {
background-color: var(--gray-10);
border: 1px solid var(--gray-300);
border-radius: 8px;
margin-bottom: 16px;
padding: 16px;
.card-header {
display: flex;
align-items: baseline;
gap: 10px;
h3 {
margin: 0;
font-size: 1rem;
}
a {
text-decoration: none;
color: var(--gray-700);
font-size: 14px;
transition: all 0.1s;
&:hover {
color: var(--gray-900);
}
}
.success, .missing-keys {
margin-left: auto;
}
.success {
width: 1rem;
height: 1rem;
background-color: green;
border-radius: 50%;
box-shadow: 0 0 2px rgba(0, 0, 0, 0.1);
border: 2px solid white;
}
.missing-keys {
color: var(--gray-600);
& > span {
margin-left: 10px;
}
}
}
.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);
background-color: var(--gray-50);
padding: 10px 16px;
display: flex;
gap: 6px;
justify-content: space-between;
align-items: center;
cursor: pointer;
box-sizing: border-box;
&:hover {
border-color: var(--gray-400);
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);
}
}
}
}
}
}
</style>