From 1f5e445eaa24c7bf3d1b13990764b4fa1e087eb8 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Mon, 17 Nov 2025 15:33:43 +0800 Subject: [PATCH] feat: Introduce conversation configuration management and localization updates - Added new ConversationConfig interface and API endpoints for retrieving and updating conversation settings. - Enhanced AgentSettings.vue to include UI for configuring conversation parameters such as prompt, context template, temperature, and max tokens. - Updated localization files in English and Chinese to include new strings for conversation settings. - Implemented logic to load and save conversation configuration for tenants, ensuring defaults are applied when no specific configuration exists. --- frontend/src/api/system/index.ts | 15 + frontend/src/i18n/locales/en-US.ts | 30 ++ frontend/src/i18n/locales/zh-CN.ts | 30 ++ frontend/src/views/settings/AgentSettings.vue | 396 ++++++++++++++---- frontend/src/views/settings/Settings.vue | 2 +- internal/agent/prompts.go | 83 +++- internal/agent/tools/knowledge_search.go | 201 +++++++-- internal/application/service/agent_service.go | 92 ++-- internal/handler/session/handler.go | 6 +- internal/handler/session/helpers.go | 74 +++- internal/handler/tenant.go | 111 ++++- internal/types/tenant.go | 34 ++ .../mysql/06-add-conversation-config.sql | 7 + .../paradedb/06-add-conversation-config.sql | 7 + 14 files changed, 913 insertions(+), 175 deletions(-) create mode 100644 migrations/mysql/06-add-conversation-config.sql create mode 100644 migrations/paradedb/06-add-conversation-config.sql diff --git a/frontend/src/api/system/index.ts b/frontend/src/api/system/index.ts index 97e70fd5..e4bcf7bf 100644 --- a/frontend/src/api/system/index.ts +++ b/frontend/src/api/system/index.ts @@ -38,6 +38,13 @@ export interface AgentConfig { available_placeholders?: PlaceholderDefinition[] // GET 响应中包含,POST/PUT 不需要 } +export interface ConversationConfig { + prompt: string + context_template: string + temperature: number + max_tokens: number +} + export function getSystemInfo(): Promise<{ data: SystemInfo }> { return get('/api/v1/system/info') } @@ -49,3 +56,11 @@ export function getAgentConfig(): Promise<{ data: AgentConfig }> { export function updateAgentConfig(config: AgentConfig): Promise<{ data: AgentConfig }> { return put('/api/v1/tenants/kv/agent-config', config) } + +export function getConversationConfig(): Promise<{ data: ConversationConfig }> { + return get('/api/v1/tenants/kv/conversation-config') +} + +export function updateConversationConfig(config: ConversationConfig): Promise<{ data: ConversationConfig }> { + return put('/api/v1/tenants/kv/conversation-config', config) +} diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts index 29e0841a..d601dd05 100644 --- a/frontend/src/i18n/locales/en-US.ts +++ b/frontend/src/i18n/locales/en-US.ts @@ -132,6 +132,7 @@ export default { title: 'Settings', modelConfig: 'Model Settings', agentConfig: 'Agent Settings', + conversationConfig: 'Conversation Settings', webSearchConfig: 'Web Search', mcpService: 'MCP Service', systemSettings: 'System Settings', @@ -1333,6 +1334,35 @@ export default { autoDisabled: 'Agent configuration incomplete. Agent mode has been disabled automatically' } }, + conversationSettings: { + description: 'Configure default behavior and parameters for conversation modes, including Prompt settings for Agent mode and normal mode', + agentMode: 'Agent Mode', + normalMode: 'Normal Mode', + contextTemplate: { + label: 'Retrieval Result Summary Prompt', + desc: 'Prompt template for generating answers based on retrieval results in normal mode', + placeholder: 'Enter the prompt template for retrieval result summary...' + }, + systemPrompt: { + label: 'System Prompt', + desc: 'System-level prompt for normal mode conversations', + placeholder: 'Enter the system prompt...' + }, + temperature: { + label: 'Temperature', + desc: 'Controls randomness in outputs. 0 is most deterministic; 1 is most random' + }, + maxTokens: { + label: 'Max Tokens', + desc: 'Maximum number of tokens to generate in the response' + }, + toasts: { + contextTemplateSaved: 'Retrieval result summary prompt saved', + systemPromptSaved: 'System prompt saved', + temperatureSaved: 'Temperature saved', + maxTokensSaved: 'Max tokens saved' + } + }, // New: MCP Settings mcpSettings: { title: 'MCP Services', diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts index ef48ca03..9bd99a51 100644 --- a/frontend/src/i18n/locales/zh-CN.ts +++ b/frontend/src/i18n/locales/zh-CN.ts @@ -223,6 +223,7 @@ export default { title: "设置", modelConfig: "模型配置", agentConfig: "Agent配置", + conversationConfig: "对话设置", webSearchConfig: "网络搜索", mcpService: "MCP服务", systemSettings: "系统设置", @@ -1337,6 +1338,35 @@ export default { autoDisabled: "Agent 配置不完整,已自动关闭 Agent 模式", }, }, + conversationSettings: { + description: "配置对话模式的默认行为和参数,包括Agent模式和普通模式的Prompt设置", + agentMode: "Agent模式", + normalMode: "普通模式", + contextTemplate: { + label: "总结Prompt", + desc: "用于普通模式下基于检索结果生成回答的Prompt模板", + placeholder: "请输入检索结果总结的Prompt模板...", + }, + systemPrompt: { + label: "系统Prompt", + desc: "用于普通模式对话的系统级Prompt", + placeholder: "请输入系统Prompt...", + }, + temperature: { + label: "温度参数", + desc: "控制模型输出的随机性,0最确定,1最随机", + }, + maxTokens: { + label: "最大Token数", + desc: "生成回答的最大Token数量", + }, + toasts: { + contextTemplateSaved: "总结Prompt已保存", + systemPromptSaved: "系统Prompt已保存", + temperatureSaved: "温度参数已保存", + maxTokensSaved: "最大Token数已保存", + }, + }, // 新增:MCP 设置 mcpSettings: { title: "MCP 服务管理", diff --git a/frontend/src/views/settings/AgentSettings.vue b/frontend/src/views/settings/AgentSettings.vue index d7a738c0..a7121a64 100644 --- a/frontend/src/views/settings/AgentSettings.vue +++ b/frontend/src/views/settings/AgentSettings.vue @@ -1,11 +1,16 @@ @@ -281,13 +369,16 @@ import { useSettingsStore } from '@/stores/settings' import { MessagePlugin, DialogPlugin } from 'tdesign-vue-next' import { useI18n } from 'vue-i18n' import { listModels, type ModelConfig } from '@/api/model' -import { getAgentConfig, updateAgentConfig, type AgentConfig, type ToolDefinition, type PlaceholderDefinition } from '@/api/system' +import { getAgentConfig, updateAgentConfig, getConversationConfig, updateConversationConfig, type AgentConfig, type ConversationConfig, type ToolDefinition, type PlaceholderDefinition } from '@/api/system' const settingsStore = useSettingsStore() const router = useRouter() const { t } = useI18n() -// 本地状态 +// Tab 状态 +const activeTab = ref('agent') + +// Agent 模式本地状态 const localMaxIterations = ref(5) const localTemperature = ref(0.7) const localThinkingModelId = ref('') @@ -296,6 +387,17 @@ const localAllowedTools = ref([]) const localSystemPrompt = ref('') const localUseCustomSystemPrompt = ref(false) +// 普通模式本地状态 +const localContextTemplate = ref('') +const localSystemPromptNormal = ref('') +const localTemperatureNormal = ref(0.3) +const localMaxTokens = ref(2048) +const conversationConfigLoaded = ref(false) +let savedContextTemplate = '' +let savedSystemPromptNormal = '' +let savedTemperatureNormal = 0.3 +let savedMaxTokens = 2048 + // 计算 Agent 是否就绪 const isAgentReady = computed(() => { // 必须有思考模型、Rerank 模型 且 至少选择一个工具 @@ -496,6 +598,29 @@ onMounted(async () => { rerankModelId: config.rerank_model_id, allowedTools: config.allowed_tools || [] }) + + // 加载普通模式配置 + if (!conversationConfigLoaded.value) { + try { + const convRes = await getConversationConfig() + const convConfig = convRes.data + + localContextTemplate.value = convConfig.context_template || '' + savedContextTemplate = convConfig.context_template || '' + localSystemPromptNormal.value = convConfig.prompt || '' + savedSystemPromptNormal = convConfig.prompt || '' + localTemperatureNormal.value = convConfig.temperature || 0.3 + savedTemperatureNormal = convConfig.temperature || 0.3 + localMaxTokens.value = convConfig.max_tokens || 2048 + savedMaxTokens = convConfig.max_tokens || 2048 + + conversationConfigLoaded.value = true + } catch (error) { + console.error('加载普通模式配置失败:', error) + // 使用默认值 + conversationConfigLoaded.value = true + } + } // 等待下一个 tick,确保所有响应式更新完成 await nextTick() @@ -1145,6 +1270,95 @@ watch(isAgentReady, (newValue, oldValue) => { // 注意:配置从"未就绪"变为"就绪"时,不自动启用(让用户自己决定是否启用) } }) + +// 普通模式配置处理函数 +const handleContextTemplateChange = async () => { + if (!conversationConfigLoaded.value) return + + if (localContextTemplate.value === savedContextTemplate) { + return + } + + try { + const config: ConversationConfig = { + prompt: localSystemPromptNormal.value, + context_template: localContextTemplate.value, + temperature: localTemperatureNormal.value, + max_tokens: localMaxTokens.value + } + + await updateConversationConfig(config) + savedContextTemplate = localContextTemplate.value + MessagePlugin.success(t('conversationSettings.toasts.contextTemplateSaved')) + } catch (error) { + console.error('保存Context Template失败:', error) + MessagePlugin.error(getErrorMessage(error)) + } +} + +const handleSystemPromptNormalChange = async () => { + if (!conversationConfigLoaded.value) return + + if (localSystemPromptNormal.value === savedSystemPromptNormal) { + return + } + + try { + const config: ConversationConfig = { + prompt: localSystemPromptNormal.value, + context_template: localContextTemplate.value, + temperature: localTemperatureNormal.value, + max_tokens: localMaxTokens.value + } + + await updateConversationConfig(config) + savedSystemPromptNormal = localSystemPromptNormal.value + MessagePlugin.success(t('conversationSettings.toasts.systemPromptSaved')) + } catch (error) { + console.error('保存System Prompt失败:', error) + MessagePlugin.error(getErrorMessage(error)) + } +} + +const handleTemperatureNormalChange = async (value: number) => { + if (!conversationConfigLoaded.value) return + + try { + const config: ConversationConfig = { + prompt: localSystemPromptNormal.value, + context_template: localContextTemplate.value, + temperature: value, + max_tokens: localMaxTokens.value + } + + await updateConversationConfig(config) + savedTemperatureNormal = value + MessagePlugin.success(t('conversationSettings.toasts.temperatureSaved')) + } catch (error) { + console.error('保存Temperature失败:', error) + MessagePlugin.error(getErrorMessage(error)) + } +} + +const handleMaxTokensChange = async (value: number) => { + if (!conversationConfigLoaded.value) return + + try { + const config: ConversationConfig = { + prompt: localSystemPromptNormal.value, + context_template: localContextTemplate.value, + temperature: localTemperatureNormal.value, + max_tokens: value + } + + await updateConversationConfig(config) + savedMaxTokens = value + MessagePlugin.success(t('conversationSettings.toasts.maxTokensSaved')) + } catch (error) { + console.error('保存Max Tokens失败:', error) + MessagePlugin.error(getErrorMessage(error)) + } +}