UNPKG

koishi-plugin-chatluna-rmkv-adapter

Version:
232 lines (224 loc) 8.05 kB
var __defProp = Object.defineProperty; var __getOwnPropNames = Object.getOwnPropertyNames; 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; }; // src/locales/zh-CN.schema.yml var require_zh_CN_schema = __commonJS({ "src/locales/zh-CN.schema.yml"(exports, module) { module.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"(exports, module) { module.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 import { ChatLunaPlugin } from "koishi-plugin-chatluna/services/chat"; import { Schema } from "koishi"; // src/client.ts import { PlatformModelAndEmbeddingsClient } from "koishi-plugin-chatluna/llm-core/platform/client"; import { ChatLunaChatModel, ChatLunaEmbeddings } from "koishi-plugin-chatluna/llm-core/platform/model"; import { ModelType } from "koishi-plugin-chatluna/llm-core/platform/types"; import { ChatLunaError, ChatLunaErrorCode } from "koishi-plugin-chatluna/utils/error"; // src/requester.ts import { ModelRequester } from "koishi-plugin-chatluna/llm-core/platform/api"; import { completionStream, createEmbeddings, createRequestContext } from "@chatluna/v1-shared-adapter"; var RWKVRequester = class extends ModelRequester { constructor(ctx, _configPool, _pluginConfig, _plugin) { super(ctx, _configPool, _pluginConfig, _plugin); this._pluginConfig = _pluginConfig; } static { __name(this, "RWKVRequester"); } async *completionStreamInternal(params) { const requestContext = createRequestContext( this.ctx, this._config.value, this._pluginConfig, this._plugin, this ); for await (const chunk of completionStream(requestContext, params)) { yield chunk; } } async embeddings(params) { const requestContext = createRequestContext( this.ctx, this._config.value, this._pluginConfig, this._plugin, this ); return await 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 import { getModelMaxContextSize } from "@chatluna/v1-shared-adapter"; var RWKVClient = class extends 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: ModelType.llm, capabilities: [] }; }).concat([ { name: "rwkv-embeddings", type: ModelType.embeddings, capabilities: [] } ]); } catch (e) { throw new ChatLunaError(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 ChatLunaError(ChatLunaErrorCode.MODEL_NOT_FOUND); } if (info.type === ModelType.llm) { const modelMaxContextSize = getModelMaxContextSize(info); return new 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 ChatLunaEmbeddings({ usageReporter: report, client: this._requester, maxRetries: this._config.maxRetries }); } }; // src/index.ts import { createLogger } from "koishi-plugin-chatluna/utils/logger"; var logger; function apply(ctx, config) { logger = createLogger(ctx, "chatluna-rwkv-adapter"); ctx.on("ready", async () => { const plugin = new 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 = Schema.intersect([ ChatLunaPlugin.Config, Schema.object({ apiKeys: Schema.array( Schema.tuple([ Schema.string().role("secret").default(""), Schema.string().default("http://127.0.0.1:8000/v1"), Schema.boolean().default(true) ]) ).default([["", "http://127.0.0.1:8000/v1", true]]).role("table") }), Schema.object({ maxContextRatio: Schema.number().min(0).max(1).step(1e-4).role("slider").default(0.35), temperature: Schema.percent().min(0).max(2).step(0.1).default(1), presencePenalty: Schema.number().min(-2).max(2).step(0.1).default(0), frequencyPenalty: 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"; export { Config2 as Config, apply, inject, logger, name };