UNPKG

koishi-plugin-chatluna-rmkv-adapter

Version:
243 lines (235 loc) 9.43 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; var __export = (target, all) => { for (var name2 in all) __defProp(target, name2, { get: all[name2], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/locales/zh-CN.schema.yml var require_zh_CN_schema = __commonJS({ "src/locales/zh-CN.schema.yml"(exports2, module2) { module2.exports = { $inner: [{}, { $desc: "请求选项", apiKeys: { $inner: ["RWKV Runner 的 API Key", "RWKV Runner 的 API 地址", "是否启用此配置"], $desc: "RWKV Runner API 的 API Key 和请求地址列表。" } }, { $desc: "模型配置", maxContextRatio: "最大上下文使用比例(0~1),控制可用的模型上下文窗口大小的最大百分比。例如 0.35 表示最多使用模型上下文的 35%。", temperature: "回复的随机性程度,数值越高,回复越随机。", presencePenalty: "重复惩罚系数,数值越高,越不易重复出现已出现过至少一次的 Token(范围:-2~2,步长:0.1)。", frequencyPenalty: "频率惩罚系数,数值越高,越不易重复出现次数较多的 Token(范围:-2~2,步长:0.1)。" }] }; } }); // src/locales/en-US.schema.yml var require_en_US_schema = __commonJS({ "src/locales/en-US.schema.yml"(exports2, module2) { module2.exports = { $inner: [{}, { $desc: "API Configuration", apiKeys: { $inner: ["RWKV Runner API Key", "RWKV Runner API Endpoint", "Enabled"], $desc: "RWKV Runner API Keys and endpoints" } }, { $desc: "Model Parameters", maxContextRatio: "Maximum context usage ratio (0-1). Controls the maximum percentage of model context window available for use. For example, 0.35 means at most 35% of the model context can be used.", temperature: "Sampling temperature (higher values increase randomness)", presencePenalty: "Token presence penalty (-2 to 2, step 0.1, discourages token repetition)", frequencyPenalty: "Token frequency penalty (-2 to 2, step 0.1, reduces frequent token repetition)" }] }; } }); // src/index.ts var index_exports = {}; __export(index_exports, { Config: () => Config2, apply: () => apply, inject: () => inject, logger: () => logger, name: () => name }); module.exports = __toCommonJS(index_exports); var import_chat = require("koishi-plugin-chatluna/services/chat"); var import_koishi = require("koishi"); // src/client.ts var import_client = require("koishi-plugin-chatluna/llm-core/platform/client"); var import_model = require("koishi-plugin-chatluna/llm-core/platform/model"); var import_types = require("koishi-plugin-chatluna/llm-core/platform/types"); var import_error = require("koishi-plugin-chatluna/utils/error"); // src/requester.ts var import_api = require("koishi-plugin-chatluna/llm-core/platform/api"); var import_v1_shared_adapter = require("@chatluna/v1-shared-adapter"); var RWKVRequester = class extends import_api.ModelRequester { constructor(ctx, _configPool, _pluginConfig, _plugin) { super(ctx, _configPool, _pluginConfig, _plugin); this._pluginConfig = _pluginConfig; } static { __name(this, "RWKVRequester"); } async *completionStreamInternal(params) { const requestContext = (0, import_v1_shared_adapter.createRequestContext)( this.ctx, this._config.value, this._pluginConfig, this._plugin, this ); for await (const chunk of (0, import_v1_shared_adapter.completionStream)(requestContext, params)) { yield chunk; } } async embeddings(params) { const requestContext = (0, import_v1_shared_adapter.createRequestContext)( this.ctx, this._config.value, this._pluginConfig, this._plugin, this ); return await (0, import_v1_shared_adapter.createEmbeddings)(requestContext, params); } async getModels() { let data; try { const response = await this.get("models"); data = await response.text(); data = JSON.parse(data); return data.data.map((model) => model.id); } catch (e) { const error = new Error( "error when listing rwkv models, Result: " + JSON.stringify(data) ); error.stack = e.stack; error.cause = e.cause; throw error; } } get logger() { return this.ctx.logger("chatluna-rwkv-adapter"); } }; // src/client.ts var import_v1_shared_adapter2 = require("@chatluna/v1-shared-adapter"); var RWKVClient = class extends import_client.PlatformModelAndEmbeddingsClient { constructor(ctx, _config, plugin) { super(ctx, plugin.platformConfigPool); this._config = _config; this.plugin = plugin; this._requester = new RWKVRequester( ctx, plugin.platformConfigPool, _config, plugin ); } static { __name(this, "RWKVClient"); } platform = "rwkv"; _requester; async refreshModels() { try { const rawModels = await this._requester.getModels(); return rawModels.map((model) => { return { name: model, type: import_types.ModelType.llm, capabilities: [] }; }).concat([ { name: "rwkv-embeddings", type: import_types.ModelType.embeddings, capabilities: [] } ]); } catch (e) { throw new import_error.ChatLunaError(import_error.ChatLunaErrorCode.MODEL_INIT_ERROR, e); } } _createModel(model, report) { const info = this._modelInfos[model]; if (info == null) { logger.warn( `Model ${model} not found`, JSON.stringify(this._modelInfos) ); throw new import_error.ChatLunaError(import_error.ChatLunaErrorCode.MODEL_NOT_FOUND); } if (info.type === import_types.ModelType.llm) { const modelMaxContextSize = (0, import_v1_shared_adapter2.getModelMaxContextSize)(info); return new import_model.ChatLunaChatModel({ usageReporter: report, modelInfo: info, requester: this._requester, model, maxTokenLimit: Math.floor( (info.maxTokens || modelMaxContextSize || 128e3) * this._config.maxContextRatio ), modelMaxContextSize, frequencyPenalty: this._config.frequencyPenalty, presencePenalty: this._config.presencePenalty, timeout: this._config.timeout, temperature: this._config.temperature, maxRetries: this._config.maxRetries, llmType: "rwkv" }); } return new import_model.ChatLunaEmbeddings({ usageReporter: report, client: this._requester, maxRetries: this._config.maxRetries }); } }; // src/index.ts var import_logger = require("koishi-plugin-chatluna/utils/logger"); var logger; function apply(ctx, config) { logger = (0, import_logger.createLogger)(ctx, "chatluna-rwkv-adapter"); ctx.on("ready", async () => { const plugin = new import_chat.ChatLunaPlugin(ctx, config, "rwkv"); plugin.parseConfig((config2) => { return config2.apiKeys.filter(([apiKey, _, enabled]) => { return apiKey.length > 0 && enabled; }).map(([apiKey, apiEndpoint]) => { return { apiKey, apiEndpoint, platform: "rwkv", chatLimit: config2.chatTimeLimit, timeout: config2.timeout, maxRetries: config2.maxRetries, concurrentMaxSize: config2.chatConcurrentMaxSize }; }); }); plugin.registerClient(() => new RWKVClient(ctx, config, plugin)); await plugin.initClient(); }); } __name(apply, "apply"); var Config2 = import_koishi.Schema.intersect([ import_chat.ChatLunaPlugin.Config, import_koishi.Schema.object({ apiKeys: import_koishi.Schema.array( import_koishi.Schema.tuple([ import_koishi.Schema.string().role("secret").default(""), import_koishi.Schema.string().default("http://127.0.0.1:8000/v1"), import_koishi.Schema.boolean().default(true) ]) ).default([["", "http://127.0.0.1:8000/v1", true]]).role("table") }), import_koishi.Schema.object({ maxContextRatio: import_koishi.Schema.number().min(0).max(1).step(1e-4).role("slider").default(0.35), temperature: import_koishi.Schema.percent().min(0).max(2).step(0.1).default(1), presencePenalty: import_koishi.Schema.number().min(-2).max(2).step(0.1).default(0), frequencyPenalty: import_koishi.Schema.number().min(-2).max(2).step(0.1).default(0) }) ]).i18n({ "zh-CN": require_zh_CN_schema(), "en-US": require_en_US_schema() }); var inject = ["chatluna"]; var name = "chatluna-rmkv-adapter"; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Config, apply, inject, logger, name });