From 116479e34e8aed4854bc9a4002173e8b8904a3d9 Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Wed, 12 Mar 2025 17:55:12 +0800 Subject: [PATCH] fix: config --- apps/chat.js | 34 ++++++++++++ config/config.js | 120 +++++++++++++++++++++++++++++++++++++---- models/chaite/cloud.js | 19 ++++--- models/storage.js | 2 +- 4 files changed, 156 insertions(+), 19 deletions(-) diff --git a/apps/chat.js b/apps/chat.js index e69de29..bdb4795 100644 --- a/apps/chat.js +++ b/apps/chat.js @@ -0,0 +1,34 @@ +import Config from '../config/config.js' +import { Chaite, SendMessageOption } from 'chaite' +export class Chat extends plugin { + constructor () { + let toggleMode = Config.basic.toggleMode + let prefix = Config.basic.togglePrefix + super({ + name: 'ChatGPT-Plugin对话', + dsc: 'ChatGPT-Plugin对话', + event: 'message', + priority: 0, + rule: [ + { + reg: toggleMode === 'at' ? '^[^#][sS]*' : `^#?(图片)?${prefix}[^gpt][sS]*`, + fnc: 'chat' + } + ] + }) + } + + async chat (e) { + + const state = await Chaite.getInstance().getUserStateStorage().getItem(e.sender.user_id + '') + const userSettings = state.settings + const sendMessageOptions = SendMessageOption.create({ + model: userSettings.model, + temperature: userSettings.temperature, + max_tokens: userSettings.maxToken, + systemOverride: userSettings.systemOverride, + + }) + Chaite.getInstance().sendMessage(msg, e, ) + } +} diff --git a/config/config.js b/config/config.js index bb84c53..5962fa1 100644 --- a/config/config.js +++ b/config/config.js @@ -1,15 +1,115 @@ class ChatGPTConfig { - dataDir = 'data' - processorsDirPath = 'utils/processors' - toolsDirPath = 'utils/tools' - cloudBaseUrl = '' - cloudApiKey = '' - - embeddingModel = 'gemini-embedding-exp-03-07' - dimensions = 0 - - serverAuthKey = '' + /** + * 版本号 + * @type {string} + */ version = '3.0.0' + + /** + * 基本配置 + * @type {{ + * toggleMode: 'at' | 'prefix', + * debug: boolean, + * }} + */ + basic = { + // 触发方式,at触发或者前缀触发 + toggleMode: 'at', + // 触发前缀,仅在前缀触发时有效 + togglePrefix: '#chat', + // 是否开启调试模式 + debug: false + } + + /** + * 模型和对话相关配置 + * @type {{ + * defaultModel: string, + * embeddingModel: string, + * defaultChatPresetId: string, + * enableCustomPreset: boolean, + * customPresetUserWhiteList: string[], + * customPresetUserBlackList: string[], + * promptBlockWords: string[], + * responseBlockWords: string[], + * blockStrategy: 'full' | 'mask', + * blockWordMask: string + * }} + */ + llm = { + // 默认模型,初始化构建预设使用 + defaultModel: '', + // 嵌入模型 + embeddingModel: 'gemini-embedding-exp-03-07', + // 嵌入结果维度,0表示自动 + dimensions: 0, + // 默认对话预设ID + defaultChatPresetId: '', + // 是否启用允许其他人切换预设 + enableCustomPreset: false, + // 允许切换预设的用户白名单 + customPresetUserWhiteList: [], + // 禁止切换预设的用户黑名单 + customPresetUserBlackList: [], + // 用户对话屏蔽词 + promptBlockWords: [], + // 机器人回复屏蔽词 + responseBlockWords: [], + // 触发屏蔽词的策略,完全屏蔽或仅屏蔽关键词 + blockStrategy: 'full', + // 如果blockStrategy为mask,屏蔽词的替换字符 + blockWordMask: '***' + } + + /** + * 管理相关配置 + * @type {{ + * blackGroups: number[], + * whiteGroups: number[], + * blackUsers: string[], + * whiteUsers: string[], + * defaultRateLimit: number + * }} + */ + management = { + blackGroups: [], + whiteGroups: [], + blackUsers: [], + whiteUsers: [], + // 默认对话速率限制,0表示不限制,数字表示每分钟最多对话次数 + defaultRateLimit: 0 + } + + /** + * chaite相关配置 + * @type { + * { dataDir: string, + * processorsDirPath: string, + * toolsDirPath: string, + * cloudBaseUrl: string, + * cloudApiKey: string, + * authKey: string, + * host: string, + * port: number}} + */ + chaite = { + // 数据目录,相对于插件下 + dataDir: 'data', + // 处理器目录,相对于插件下 + processorsDirPath: 'utils/processors', + // 工具目录,相对于插件目录下 + toolsDirPath: 'utils/tools', + // 云端API url + cloudBaseUrl: '', + // 云端API Key + cloudApiKey: '', + // jwt key,非必要勿修改 + authKey: '', + // 管理面板监听地址 + host: '', + // 管理面板监听端口 + port: 48370 + } } export default new ChatGPTConfig() diff --git a/models/chaite/cloud.js b/models/chaite/cloud.js index 0e71bc8..0a29c21 100644 --- a/models/chaite/cloud.js +++ b/models/chaite/cloud.js @@ -17,7 +17,7 @@ import fs from 'fs' * @param apiKey * @returns {Promise | null} */ -export async function authCloud (apiKey = ChatGPTConfig.cloudApiKey) { +export async function authCloud (apiKey = ChatGPTConfig.chaite.cloudApiKey) { await Chaite.getInstance().auth(apiKey) return Chaite.getInstance().getToolsManager().cloudService.getUser() } @@ -56,7 +56,10 @@ export async function initRagManager (model, dimensions) { } const channel = channels[0] const client = await getIClientByChannel(channel) - const result = await client.getEmbedding(text) + const result = await client.getEmbedding(text, { + model, + dimensions + }) return result.embeddings[0] } @@ -90,7 +93,7 @@ export async function initRagManager (model, dimensions) { return results } }() - const vectorDBPath = path.resolve('./plugins/chatgpt-plugin', ChatGPTConfig.dataDir, 'vector_index') + const vectorDBPath = path.resolve('./plugins/chatgpt-plugin', ChatGPTConfig.chaite.dataDir, 'vector_index') if (!fs.existsSync(vectorDBPath)) { fs.mkdirSync(vectorDBPath, { recursive: true }) } @@ -103,12 +106,12 @@ export async function initRagManager (model, dimensions) { export async function initChaite () { await ChatGPTStorage.init() const channelsManager = await ChannelsManager.init(new LowDBChannelStorage(ChatGPTStorage), new DefaultChannelLoadBalancer()) - const toolsDir = path.resolve('./plugins/chatgpt-plugin', ChatGPTConfig.toolsDirPath) + const toolsDir = path.resolve('./plugins/chatgpt-plugin', ChatGPTConfig.chaite.toolsDirPath) if (!fs.existsSync(toolsDir)) { fs.mkdirSync(toolsDir, { recursive: true }) } const toolsManager = await ToolManager.init(toolsDir, new LowDBToolsStorage(ChatGPTStorage)) - const processorsDir = path.resolve('./plugins/chatgpt-plugin', ChatGPTConfig.processorsDirPath) + const processorsDir = path.resolve('./plugins/chatgpt-plugin', ChatGPTConfig.chaite.processorsDirPath) if (!fs.existsSync(processorsDir)) { fs.mkdirSync(processorsDir, { recursive: true }) } @@ -120,10 +123,10 @@ export async function initChaite () { let chaite = Chaite.init(channelsManager, toolsManager, processorsManager, chatPresetManager, userModeSelector, userStateStorage, historyManager, logger) logger.info('Chaite 初始化完成') - chaite.setCloudService(ChatGPTConfig.cloudBaseUrl) + chaite.setCloudService(ChatGPTConfig.chaite.cloudBaseUrl) logger.info('Chaite.Cloud 初始化完成') - ChatGPTConfig.cloudApiKey && await chaite.auth(ChatGPTConfig.cloudApiKey) - await initRagManager(ChatGPTConfig.embeddingModel, ChatGPTConfig.dimensions) + ChatGPTConfig.chaite.cloudApiKey && await chaite.auth(ChatGPTConfig.chaite.cloudApiKey) + await initRagManager(ChatGPTConfig.llm.embeddingModel, ChatGPTConfig.dimensions) // 监听Chaite配置变化,同步需要同步的配置 chaite.on('config-change', obj => { const { key, newVal, oldVal } = obj diff --git a/models/storage.js b/models/storage.js index b4d200c..2f204dd 100644 --- a/models/storage.js +++ b/models/storage.js @@ -348,7 +348,7 @@ export class LowDBCollection { } } -const dataDir = path.resolve('./plugins/chatgpt-plugin', ChatGPTConfig.dataDir) +const dataDir = path.resolve('./plugins/chatgpt-plugin', ChatGPTConfig.chaite.dataDir) if (!fs.existsSync(dataDir)) { fs.mkdirSync(dataDir, { recursive: true }) }