Browse Source

llm模型接入调整

master
MSI-7950X\刘泽明 6 hours ago
parent
commit
53ff153443
  1. 124
      backend/agentscope_integration/factory.py
  2. 91
      backend/modules/memory/manager.py
  3. 135
      backend/modules/model_provider/router.py
  4. 10
      backend/modules/monitor/router.py
  5. 14
      frontend/src/views/flow/node-configs/LlmConfig.vue
  6. 113
      frontend/src/views/settings/Settings.vue

124
backend/agentscope_integration/factory.py

@ -3,6 +3,7 @@
提供统一的智能体创建接口根据用户类型员工/管理者/任务/文档创建对应的 AI 智能体实例 提供统一的智能体创建接口根据用户类型员工/管理者/任务/文档创建对应的 AI 智能体实例
支持智能体缓存以减少重复创建的开销 支持智能体缓存以减少重复创建的开销
""" """
from sqlalchemy import select
from agentscope.agent import AgentBase from agentscope.agent import AgentBase
from agentscope.agent._react_agent import ReActAgent from agentscope.agent._react_agent import ReActAgent
from agentscope.model import OpenAIChatModel from agentscope.model import OpenAIChatModel
@ -10,6 +11,8 @@ from agentscope.formatter import OpenAIChatFormatter
from agentscope.tool import Toolkit from agentscope.tool import Toolkit
from agentscope.message import Msg from agentscope.message import Msg
from config import settings from config import settings
from models import ModelInstance, ModelProvider
from database import get_db
from .memory.user_memory import UserIsolatedMemory from .memory.user_memory import UserIsolatedMemory
from .hooks.rbac_hook import register_rbac_hooks_for_user from .hooks.rbac_hook import register_rbac_hooks_for_user
@ -26,19 +29,126 @@ class AgentFactory:
_MAX_CACHE_SIZE = 50 # 智能体缓存上限 _MAX_CACHE_SIZE = 50 # 智能体缓存上限
@classmethod @classmethod
def _get_model(cls) -> OpenAIChatModel: async def _get_llm_config(cls, db, model_instance_id=None):
"""从数据库获取 LLM 配置信息。
根据提供的 model_instance_id 查询对应的模型实例和供应商配置
如果未提供或未找到则回退到默认模型is_default=True
最终如果都未找到则使用 settings 中的默认配置
Args:
db: 数据库会话对象
model_instance_id: 可选的模型实例 ID用于指定特定模型
Returns:
dict: 包含 LLM 配置的字典
- model_name (str): 模型名称
- api_key (str): API 密钥
- base_url (str): API 基础地址
- default_params (dict): 默认参数temperature, max_tokens
"""
try:
if model_instance_id:
stmt = select(ModelInstance).where(
ModelInstance.id == model_instance_id,
ModelInstance.is_active == True
)
result = await db.execute(stmt)
model_instance = result.scalar_one_or_none()
if model_instance:
stmt_provider = select(ModelProvider).where(
ModelProvider.id == model_instance.provider_id,
ModelProvider.is_active == True
)
result_provider = await db.execute(stmt_provider)
provider = result_provider.scalar_one_or_none()
if provider:
return {
"model_name": model_instance.model_name,
"api_key": provider.api_key or settings.LLM_API_KEY,
"base_url": provider.base_url or settings.LLM_API_BASE,
"default_params": model_instance.default_params or {},
}
stmt_default = select(ModelInstance).where(
ModelInstance.is_default == True,
ModelInstance.is_active == True,
ModelInstance.model_type == "llm"
).limit(1)
result_default = await db.execute(stmt_default)
default_instance = result_default.scalar_one_or_none()
if default_instance:
stmt_provider = select(ModelProvider).where(
ModelProvider.id == default_instance.provider_id,
ModelProvider.is_active == True
)
result_provider = await db.execute(stmt_provider)
provider = result_provider.scalar_one_or_none()
if provider:
return {
"model_name": default_instance.model_name,
"api_key": provider.api_key or settings.LLM_API_KEY,
"base_url": provider.base_url or settings.LLM_API_BASE,
"default_params": default_instance.default_params or {},
}
except Exception as e:
pass
return {
"model_name": settings.LLM_MODEL,
"api_key": settings.LLM_API_KEY,
"base_url": settings.LLM_API_BASE,
"default_params": {},
}
@classmethod
def _get_model(cls, llm_config: dict = None) -> OpenAIChatModel:
"""获取或创建全局共享的大语言模型实例。 """获取或创建全局共享的大语言模型实例。
支持通过 llm_config 参数动态指定模型配置如果未提供则使用默认配置
Args:
llm_config: 可选的 LLM 配置字典包含
- model_name (str): 模型名称
- api_key (str): API 密钥
- base_url (str): API 基础地址
- default_params (dict): 默认参数temperature, max_tokens
Returns: Returns:
OpenAIChatModel: 配置好的大语言模型实例 OpenAIChatModel: 配置好的大语言模型实例
""" """
if cls._model is None: if cls._model is None:
cls._model = OpenAIChatModel( if llm_config:
config_name="enterprise_model", model_name = llm_config.get("model_name", settings.LLM_MODEL)
model_name=settings.LLM_MODEL, api_key = llm_config.get("api_key", settings.LLM_API_KEY)
api_key=settings.LLM_API_KEY, base_url = llm_config.get("base_url", settings.LLM_API_BASE)
api_base=settings.LLM_API_BASE, default_params = llm_config.get("default_params", {})
)
model_kwargs = {
"config_name": "enterprise_model",
"model_name": model_name,
"api_key": api_key,
"api_base": base_url,
}
if default_params:
if "temperature" in default_params:
model_kwargs["temperature"] = default_params["temperature"]
if "max_tokens" in default_params:
model_kwargs["max_tokens"] = default_params["max_tokens"]
cls._model = OpenAIChatModel(**model_kwargs)
else:
cls._model = OpenAIChatModel(
config_name="enterprise_model",
model_name=settings.LLM_MODEL,
api_key=settings.LLM_API_KEY,
api_base=settings.LLM_API_BASE,
)
return cls._model return cls._model
@classmethod @classmethod

91
backend/modules/memory/manager.py

@ -18,9 +18,10 @@ from datetime import datetime, timezone
from typing import Callable from typing import Callable
from redis.asyncio import Redis from redis.asyncio import Redis
from sqlalchemy import text from sqlalchemy import text, select
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from config import settings from config import settings
from models import ModelInstance, ModelProvider
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -65,6 +66,62 @@ class MemoryManager:
self.db_factory = db_factory self.db_factory = db_factory
self.redis = redis self.redis = redis
self._extract_tasks: dict[str, asyncio.Task] = {} # 后台提取任务追踪 self._extract_tasks: dict[str, asyncio.Task] = {} # 后台提取任务追踪
self._llm_config_cache: dict | None = None # LLM 配置缓存
async def _get_llm_config(self) -> dict:
"""从 model_instances 表获取 LLM 配置,带缓存。
优先级数据库默认 LLM 环境变量回退
缓存有效期5分钟避免每次调用都查库
Returns:
dict: {model_name, api_key, base_url}
"""
if self._llm_config_cache is not None:
return self._llm_config_cache
try:
db = self.db_factory()
result = await db.execute(
select(ModelInstance, ModelProvider)
.join(ModelProvider, ModelInstance.provider_id == ModelProvider.id)
.where(ModelInstance.model_type == 'llm')
.where(ModelInstance.is_default == True)
.where(ModelInstance.is_active == True)
.limit(1)
)
row = result.first()
if not row:
result2 = await db.execute(
select(ModelInstance, ModelProvider)
.join(ModelProvider, ModelInstance.provider_id == ModelProvider.id)
.where(ModelInstance.model_type == 'llm')
.where(ModelInstance.is_active == True)
.limit(1)
)
row = result2.first()
if row:
instance, provider = row
config = {
"api_base": (provider.base_url or settings.LLM_API_BASE).rstrip("/"),
"model": instance.model_name,
"api_key": provider.api_key or settings.LLM_API_KEY,
}
else:
config = {
"api_base": settings.LLM_API_BASE.rstrip("/"),
"model": settings.LLM_MODEL,
"api_key": settings.LLM_API_KEY,
}
self._llm_config_cache = config
# 5分钟后自动清除缓存
asyncio.get_event_loop().call_later(300, lambda: setattr(self, '_llm_config_cache', None))
return config
except Exception:
return {
"api_base": settings.LLM_API_BASE.rstrip("/"),
"model": settings.LLM_MODEL,
"api_key": settings.LLM_API_KEY,
}
async def inject_memory( async def inject_memory(
self, self,
@ -394,19 +451,19 @@ class MemoryManager:
) )
import httpx import httpx
api_base = settings.LLM_API_BASE.rstrip("/") llm = await self._get_llm_config()
async with httpx.AsyncClient(timeout=30) as client: async with httpx.AsyncClient(timeout=30) as client:
resp = await client.post( resp = await client.post(
f"{api_base}/chat/completions", f"{llm['api_base']}/chat/completions",
json={ json={
"model": settings.LLM_MODEL, "model": llm["model"],
"messages": [{ "messages": [{
"role": "user", "role": "user",
"content": f"请用一段话简要总结以下对话的关键内容。保留人名、任务、决策、时间等关键信息。\n\n{dialogue}" "content": f"请用一段话简要总结以下对话的关键内容。保留人名、任务、决策、时间等关键信息。\n\n{dialogue}"
}], }],
"max_tokens": 200, "max_tokens": 200,
}, },
headers={"Authorization": f"Bearer {settings.LLM_API_KEY}"}, headers={"Authorization": f"Bearer {llm['api_key']}"},
) )
data = resp.json() data = resp.json()
summary = data.get("choices", [{}])[0].get("message", {}).get("content", "") summary = data.get("choices", [{}])[0].get("message", {}).get("content", "")
@ -515,17 +572,17 @@ class MemoryManager:
只返回JSON数组不要其他内容如果没有可提取的信息返回空数组[]""" 只返回JSON数组不要其他内容如果没有可提取的信息返回空数组[]"""
import httpx import httpx
api_base = settings.LLM_API_BASE.rstrip("/") llm = await self._get_llm_config()
async with httpx.AsyncClient(timeout=60) as client: async with httpx.AsyncClient(timeout=60) as client:
resp = await client.post( resp = await client.post(
f"{api_base}/chat/completions", f"{llm['api_base']}/chat/completions",
json={ json={
"model": settings.LLM_MODEL, "model": llm["model"],
"messages": [{"role": "user", "content": prompt}], "messages": [{"role": "user", "content": prompt}],
"max_tokens": 800, "max_tokens": 800,
"temperature": 0.3, "temperature": 0.3,
}, },
headers={"Authorization": f"Bearer {settings.LLM_API_KEY}"}, headers={"Authorization": f"Bearer {llm['api_key']}"},
) )
data = resp.json() data = resp.json()
result_text = data.get("choices", [{}])[0].get("message", {}).get("content", "[]") result_text = data.get("choices", [{}])[0].get("message", {}).get("content", "[]")
@ -704,17 +761,17 @@ class MemoryManager:
只返回JSON数组不要其他内容""" 只返回JSON数组不要其他内容"""
import httpx import httpx
api_base = settings.LLM_API_BASE.rstrip("/") llm = await self._get_llm_config()
async with httpx.AsyncClient(timeout=60) as client: async with httpx.AsyncClient(timeout=60) as client:
resp = await client.post( resp = await client.post(
f"{api_base}/chat/completions", f"{llm['api_base']}/chat/completions",
json={ json={
"model": settings.LLM_MODEL, "model": llm["model"],
"messages": [{"role": "user", "content": prompt}], "messages": [{"role": "user", "content": prompt}],
"max_tokens": 500, "max_tokens": 500,
"temperature": 0.3, "temperature": 0.3,
}, },
headers={"Authorization": f"Bearer {settings.LLM_API_KEY}"}, headers={"Authorization": f"Bearer {llm['api_key']}"},
) )
data = resp.json() data = resp.json()
result_text = data.get("choices", [{}])[0].get("message", {}).get("content", "[]") result_text = data.get("choices", [{}])[0].get("message", {}).get("content", "[]")
@ -817,17 +874,17 @@ class MemoryManager:
只返回JSON对象不要其他内容""" 只返回JSON对象不要其他内容"""
import httpx import httpx
api_base = settings.LLM_API_BASE.rstrip("/") llm = await self._get_llm_config()
async with httpx.AsyncClient(timeout=60) as client: async with httpx.AsyncClient(timeout=60) as client:
resp = await client.post( resp = await client.post(
f"{api_base}/chat/completions", f"{llm['api_base']}/chat/completions",
json={ json={
"model": settings.LLM_MODEL, "model": llm["model"],
"messages": [{"role": "user", "content": prompt}], "messages": [{"role": "user", "content": prompt}],
"max_tokens": 500, "max_tokens": 500,
"temperature": 0.3, "temperature": 0.3,
}, },
headers={"Authorization": f"Bearer {settings.LLM_API_KEY}"}, headers={"Authorization": f"Bearer {llm['api_key']}"},
) )
data = resp.json() data = resp.json()
result_text = data.get("choices", [{}])[0].get("message", {}).get("content", "{}") result_text = data.get("choices", [{}])[0].get("message", {}).get("content", "{}")

135
backend/modules/model_provider/router.py

@ -295,3 +295,138 @@ async def delete_model(provider_id: str, model_id: str, db: AsyncSession = Depen
await db.delete(m) await db.delete(m)
await db.commit() await db.commit()
return {"code": 200, "message": "已删除"} return {"code": 200, "message": "已删除"}
@router.get("/default-llm")
async def get_default_llm(db: AsyncSession = Depends(get_db)):
"""获取系统默认 LLM 模型配置。
model_instances 表中查找 is_default=True model_type='llm' 的模型实例
返回包含供应商连接信息的完整配置供系统中各模块统一使用
Returns:
dict: 默认 LLM 配置model_name, api_key, base_url, default_params
若未配置则返回 null
"""
result = await db.execute(
select(ModelInstance, ModelProvider)
.join(ModelProvider, ModelInstance.provider_id == ModelProvider.id)
.where(ModelInstance.model_type == 'llm')
.where(ModelInstance.is_default == True)
.where(ModelInstance.is_active == True)
.limit(1)
)
row = result.first()
if not row:
# 回退:取第一个启用的 LLM
result2 = await db.execute(
select(ModelInstance, ModelProvider)
.join(ModelProvider, ModelInstance.provider_id == ModelProvider.id)
.where(ModelInstance.model_type == 'llm')
.where(ModelInstance.is_active == True)
.order_by(ModelInstance.created_at)
.limit(1)
)
row = result2.first()
if not row:
return {"code": 200, "data": None}
instance, provider = row
return {
"code": 200,
"data": {
"id": str(instance.id),
"model_name": instance.model_name,
"display_name": instance.display_name or instance.model_name,
"model_type": instance.model_type,
"api_key": provider.api_key or "",
"base_url": provider.base_url or settings.LLM_API_BASE,
"default_params": instance.default_params or {},
"capabilities": instance.capabilities or {},
"is_default": instance.is_default,
"is_active": instance.is_active,
"provider_id": str(instance.provider_id),
"provider_name": provider.name,
},
}
async def resolve_model_config(db: AsyncSession, model_instance_id: str = None) -> dict:
"""根据模型实例 ID 解析完整的模型调用配置。
这是系统的核心模型解析函数各模块Agent工厂流程引擎记忆管理器等
应通过此函数获取模型配置实现统一的模型管理
解析优先级
1. 如果提供了 model_instance_id 从数据库读取该模型的完整配置
2. 否则 获取默认 LLM 配置is_default LLM 实例
3. 都没有 回退到环境变量 settings.LLM_*
Args:
db: 异步数据库会话
model_instance_id: 可选的模型实例 UUID 字符串
Returns:
dict: 包含 model_name, api_key, base_url, default_params 等的配置字典
"""
if model_instance_id:
try:
uid = uuid.UUID(model_instance_id)
result = await db.execute(
select(ModelInstance, ModelProvider)
.join(ModelProvider, ModelInstance.provider_id == ModelProvider.id)
.where(ModelInstance.id == uid)
.limit(1)
)
row = result.first()
if row:
instance, provider = row
return {
"model_name": instance.model_name,
"api_key": provider.api_key or settings.LLM_API_KEY,
"base_url": provider.base_url or settings.LLM_API_BASE,
"default_params": instance.default_params or {},
"capabilities": instance.capabilities or {},
}
except (ValueError, Exception):
pass
# 无指定实例或未找到:使用默认 LLM
result = await db.execute(
select(ModelInstance, ModelProvider)
.join(ModelProvider, ModelInstance.provider_id == ModelProvider.id)
.where(ModelInstance.model_type == 'llm')
.where(ModelInstance.is_default == True)
.where(ModelInstance.is_active == True)
.limit(1)
)
row = result.first()
if not row:
result = await db.execute(
select(ModelInstance, ModelProvider)
.join(ModelProvider, ModelInstance.provider_id == ModelProvider.id)
.where(ModelInstance.model_type == 'llm')
.where(ModelInstance.is_active == True)
.order_by(ModelInstance.created_at)
.limit(1)
)
row = result.first()
if row:
instance, provider = row
return {
"model_name": instance.model_name,
"api_key": provider.api_key or settings.LLM_API_KEY,
"base_url": provider.base_url or settings.LLM_API_BASE,
"default_params": instance.default_params or {},
"capabilities": instance.capabilities or {},
}
# 最终回退到环境变量
return {
"model_name": settings.LLM_MODEL,
"api_key": settings.LLM_API_KEY,
"base_url": settings.LLM_API_BASE,
"default_params": {},
"capabilities": {},
}

10
backend/modules/monitor/router.py

@ -12,6 +12,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from database import get_db from database import get_db
from models import User, ChatSession, ChatMessage from models import User, ChatSession, ChatMessage
from modules.org.router import _get_subordinate_ids, _user_to_out from modules.org.router import _get_subordinate_ids, _user_to_out
from modules.model_provider.router import resolve_model_config
from schemas import EmployeeAnalysis, UserOut from schemas import EmployeeAnalysis, UserOut
router = APIRouter(prefix="/api/monitor", tags=["monitor"]) # 监控模块路由前缀 router = APIRouter(prefix="/api/monitor", tags=["monitor"]) # 监控模块路由前缀
@ -204,12 +205,13 @@ async def get_employee_analysis(
f"[{m.role}] {m.content[:300]}" for m in messages f"[{m.role}] {m.content[:300]}" for m in messages
]) ])
# 初始化 LLM 模型 # 初始化 LLM 模型(从模型管理表读取配置)
llm_cfg = await resolve_model_config(db)
model = OpenAIChatModel( model = OpenAIChatModel(
config_name="analysis_model", config_name="analysis_model",
model_name=settings.LLM_MODEL, model_name=llm_cfg["model_name"],
api_key=settings.LLM_API_KEY, api_key=llm_cfg["api_key"],
api_base=settings.LLM_API_BASE, api_base=llm_cfg["base_url"],
) )
formatter = OpenAIChatFormatter() formatter = OpenAIChatFormatter()

14
frontend/src/views/flow/node-configs/LlmConfig.vue

@ -109,17 +109,21 @@ function getProviderName(providerId: string): string {
} }
function onModelSelect(val: string) { function onModelSelect(val: string) {
update('model', val) // model_name model_instance_id
// const model = val ? llmModels.value.find((m: any) => m.model_name === val) : null
if (!val) return
const model = llmModels.value.find((m: any) => m.model_name === val)
if (model) { if (model) {
// model + model_instance_id
const updated = { ...props.modelValue, model: val, model_instance_id: model.id }
//
const params = model.default_params || {} const params = model.default_params || {}
const updated = { ...props.modelValue }
if (params.temperature !== undefined && !updated.temperature) updated.temperature = params.temperature if (params.temperature !== undefined && !updated.temperature) updated.temperature = params.temperature
if (params.max_tokens && !updated.max_tokens) updated.max_tokens = params.max_tokens if (params.max_tokens && !updated.max_tokens) updated.max_tokens = params.max_tokens
if ((model.capabilities || {}).function_calling && updated.tool_call === false) updated.tool_call = true if ((model.capabilities || {}).function_calling && updated.tool_call === false) updated.tool_call = true
emit('update:modelValue', updated) emit('update:modelValue', updated)
} else {
// model model_instance_id
const updated = { ...props.modelValue, model: val, model_instance_id: undefined }
emit('update:modelValue', updated)
} }
} }
</script> </script>

113
frontend/src/views/settings/Settings.vue

@ -2,46 +2,49 @@
<div class="settings-page"> <div class="settings-page">
<el-page-header @back="$router.back()" content="系统配置" /> <el-page-header @back="$router.back()" content="系统配置" />
<!-- LLM 模型配置 - 引导到模型管理 -->
<el-card style="margin-top: 20px"> <el-card style="margin-top: 20px">
<template #header>LLM 模型配置</template> <template #header>
<el-form :model="form" label-width="140px" style="max-width: 600px"> <div style="display: flex; justify-content: space-between; align-items: center;">
<el-form-item label="API 地址"> <span>LLM 模型配置</span>
<el-input v-model="form.api_base" placeholder="https://api.openai.com/v1" /> <el-button size="small" type="primary" @click="$router.push('/admin/model/providers')">
</el-form-item> 前往模型管理
<el-form-item label="API Key"> </el-button>
<el-input v-model="form.api_key" type="password" show-password placeholder="sk-..." /> </div>
</el-form-item> </template>
<el-form-item label="默认模型"> <el-alert
<el-select v-model="form.model" style="width: 100%"> title="模型配置已迁移至「模型管理」页面"
<el-option label="GPT-4o-mini" value="gpt-4o-mini" /> type="info"
<el-option label="GPT-4o" value="gpt-4o" /> :closable="false"
<el-option label="GPT-4-turbo" value="gpt-4-turbo" /> show-icon
<el-option label="GPT-3.5-turbo" value="gpt-3.5-turbo" /> style="margin-bottom: 16px;"
<el-option label="DeepSeek-V3" value="deepseek-chat" /> >
<el-option label="DeepSeek-R1" value="deepseek-reasoner" /> <template #default>
<el-option label="Qwen-Max" value="qwen-max" /> <p style="margin: 0;">模型管理中配置 LLM/Embedding/Rerank 模型的供应商API Key参数等系统各模块AI 助手流程引擎知识库监控分析将统一使用模型管理中的配置</p>
<el-option label="自定义" value="__custom__" /> <el-button type="primary" link @click="$router.push('/admin/model/providers')" style="margin-top: 8px;">
</el-select> 前往管理模型
</el-form-item> </el-button>
<el-form-item v-if="form.model === '__custom__'" label="自定义模型名"> </template>
<el-input v-model="form.custom_model" placeholder="请输入模型名称" /> </el-alert>
</el-form-item>
<el-form-item label="Max Tokens"> <!-- 当前默认模型展示只读 -->
<el-input-number v-model="form.max_tokens" :min="100" :max="128000" :step="100" /> <div v-if="defaultModel" style="padding: 12px; background: #f5f7fa; border-radius: 8px;">
</el-form-item> <div style="display: flex; gap: 24px; font-size: 13px;">
<el-form-item label="温度 (创建任务)"> <div><strong>默认 LLM</strong>{{ defaultModel.display_name || defaultModel.model_name || '未配置' }}</div>
<el-slider v-model="form.temperature_task" :min="0" :max="2" :step="0.1" /> <div><strong>供应商</strong>{{ defaultModel.provider_name || '-' }}</div>
</el-form-item> <div><strong>状态</strong>
<el-form-item label="温度 (企业分析)"> <el-tag :type="defaultModel.is_active ? 'success' : 'danger'" size="small">
<el-slider v-model="form.temperature_analysis" :min="0" :max="2" :step="0.1" /> {{ defaultModel.is_active ? '启用' : '禁用' }}
</el-form-item> </el-tag>
<el-form-item> </div>
<el-button type="primary" @click="saveConfig" :loading="saving">保存配置</el-button> </div>
<el-button @click="testConnection" :loading="testing">测试连接</el-button> </div>
</el-form-item> <div v-else style="padding: 12px; background: #fef0f0; border-radius: 8px; color: #f56c6c; font-size: 13px;">
</el-form> 尚未配置默认 LLM 模型请前往模型管理添加并设为默认
</div>
</el-card> </el-card>
<!-- 企微配置 -->
<el-card style="margin-top: 20px"> <el-card style="margin-top: 20px">
<template #header>企微配置</template> <template #header>企微配置</template>
<el-form :model="form" label-width="140px" style="max-width: 600px"> <el-form :model="form" label-width="140px" style="max-width: 600px">
@ -63,6 +66,7 @@
</el-form> </el-form>
</el-card> </el-card>
<!-- RAG 知识库 -->
<el-card style="margin-top: 20px"> <el-card style="margin-top: 20px">
<template #header>RAG 知识库</template> <template #header>RAG 知识库</template>
<div style="display: flex; gap: 16px; align-items: flex-start"> <div style="display: flex; gap: 16px; align-items: flex-start">
@ -92,7 +96,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, reactive } from 'vue' import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus' import { ElMessage } from 'element-plus'
import { systemApi, ragApi } from '@/api' import { systemApi, ragApi } from '@/api'
import api from '@/api' import api from '@/api'
@ -102,15 +106,9 @@ const testing = ref(false)
const ragQuery = ref('') const ragQuery = ref('')
const ragSearching = ref(false) const ragSearching = ref(false)
const ragResults = ref<any[]>([]) const ragResults = ref<any[]>([])
const defaultModel = ref<any>(null)
const form = reactive({ const form = reactive({
api_base: '',
api_key: '',
model: 'gpt-4o-mini',
custom_model: '',
max_tokens: 4096,
temperature_task: 0.7,
temperature_analysis: 0.5,
wecom_corp_id: '', wecom_corp_id: '',
wecom_app_secret: '', wecom_app_secret: '',
wecom_agent_id: 0, wecom_agent_id: 0,
@ -118,24 +116,19 @@ const form = reactive({
wecom_encoding_aes: '', wecom_encoding_aes: '',
}) })
async function saveConfig() { async function loadDefaultModel() {
saving.value = true
try { try {
const finalModel = form.model === '__custom__' ? form.custom_model : form.model const res: any = await api.get('/model-providers/default-llm')
await systemApi.health() defaultModel.value = res?.data || res || null
ElMessage.success('配置保存成功') } catch {
localStorage.setItem('llm_config', JSON.stringify({ defaultModel.value = null
api_base: form.api_base, }
api_key: form.api_key,
model: finalModel,
max_tokens: form.max_tokens,
temperature_task: form.temperature_task,
temperature_analysis: form.temperature_analysis,
}))
} catch { ElMessage.warning('保存失败') }
finally { saving.value = false }
} }
onMounted(() => {
loadDefaultModel()
})
async function testConnection() { async function testConnection() {
testing.value = true testing.value = true
try { try {

Loading…
Cancel
Save