From 02c35f51b68666e8407b88714eeb601b5cb40b7a Mon Sep 17 00:00:00 2001 From: dingyi Date: Sat, 23 Sep 2023 23:44:14 +0800 Subject: [PATCH] feat(core): add preset review --- packages/core/src/commands/preset.ts | 2 +- packages/core/src/llm-core/utils/tiktoken.ts | 19 ++++++++++++++++- .../core/src/middlewares/list_all_preset.ts | 21 +++++++++++++++++-- packages/core/src/preset.ts | 8 +++++-- packages/core/src/utils/pagination.ts | 10 +++++++-- packages/spark-adapter/src/requester.ts | 2 +- 6 files changed, 53 insertions(+), 9 deletions(-) diff --git a/packages/core/src/commands/preset.ts b/packages/core/src/commands/preset.ts index 4bdf6572..5bf21086 100644 --- a/packages/core/src/commands/preset.ts +++ b/packages/core/src/commands/preset.ts @@ -13,7 +13,7 @@ export function apply(ctx: Context, config: Config, chain: ChatChain) { .action(async ({ options, session }) => { await chain.receiveCommand(session, 'list_preset', { page: options.page ?? 1, - limit: options.limit ?? 5 + limit: options.limit ?? 3 }) }) diff --git a/packages/core/src/llm-core/utils/tiktoken.ts b/packages/core/src/llm-core/utils/tiktoken.ts index 79c5b984..9557c125 100644 --- a/packages/core/src/llm-core/utils/tiktoken.ts +++ b/packages/core/src/llm-core/utils/tiktoken.ts @@ -40,5 +40,22 @@ export async function encodingForModel( extendedSpecialTokens?: Record } ) { - return getEncoding(getEncodingNameForModel(model), options) + options = options ?? {} + + let timeout: NodeJS.Timeout + + if (options.signal == null) { + const abortController = new AbortController() + + options.signal = abortController.signal + + timeout = setTimeout(() => abortController.abort(), 1000 * 25) + } + const result = await getEncoding(getEncodingNameForModel(model), options) + + if (timeout != null) { + clearTimeout(timeout) + } + + return result } diff --git a/packages/core/src/middlewares/list_all_preset.ts b/packages/core/src/middlewares/list_all_preset.ts index 49a952e3..6dfcb328 100644 --- a/packages/core/src/middlewares/list_all_preset.ts +++ b/packages/core/src/middlewares/list_all_preset.ts @@ -5,7 +5,7 @@ import { Pagination } from '../utils/pagination' export function apply(ctx: Context, config: Config, chain: ChatChain) { const pagination = new Pagination({ - formatItem: (value) => value, + formatItem: (value) => formatPreset(ctx, value), formatString: { top: '以下是目前可用的预设列表:\n', bottom: '\n你可以使用 chathub.room.set -p 来设置默认使用的预设' @@ -23,7 +23,7 @@ export function apply(ctx: Context, config: Config, chain: ChatChain) { if (command !== 'list_preset') return ChainMiddlewareRunStatus.SKIPPED - const presets = await preset.getAllPreset() + const presets = await preset.getAllPreset(false) await pagination.push(presets) @@ -34,6 +34,23 @@ export function apply(ctx: Context, config: Config, chain: ChatChain) { .after('lifecycle-handle_command') } +async function formatPreset(ctx: Context, presetName: string) { + const buffer = [] + + const preset = await ctx.chathub.preset.getPreset(presetName) + + const previewContent = preset.messages + .map((value) => value.content) + .join('\n\n') + .substring(0, 130) + .concat('......') + + buffer.push(`预设关键词: ${preset.triggerKeyword.join(', ')}`) + buffer.push(`预设内容: ${previewContent}\n`) + + return buffer.join('\n') +} + declare module '../chains/chain' { interface ChainMiddlewareName { list_all_preset: never diff --git a/packages/core/src/preset.ts b/packages/core/src/preset.ts index b7907003..7df259cc 100644 --- a/packages/core/src/preset.ts +++ b/packages/core/src/preset.ts @@ -109,10 +109,14 @@ export class Preset { // throw new Error("No default preset found") } - async getAllPreset(): Promise { + async getAllPreset(concatKeyword: boolean = true): Promise { await this.loadAllPreset() - return this._presets.map((preset) => preset.triggerKeyword.join(', ')) + return this._presets.map((preset) => + concatKeyword + ? preset.triggerKeyword.join(', ') + : preset.triggerKeyword[0] + ) } async resetDefaultPreset(): Promise { diff --git a/packages/core/src/utils/pagination.ts b/packages/core/src/utils/pagination.ts index 2c528f5a..2b1d6b7c 100644 --- a/packages/core/src/utils/pagination.ts +++ b/packages/core/src/utils/pagination.ts @@ -35,7 +35,13 @@ export class Pagination { const buffer = [this.input.formatString.top] for (const item of sliceItems) { - buffer.push(this.input.formatItem(item)) + const itemLikePromise = this.input.formatItem(item) + + if (typeof itemLikePromise === 'string') { + buffer.push(itemLikePromise) + } else { + buffer.push(await itemLikePromise) + } } buffer.push(this.input.formatString.bottom) @@ -56,7 +62,7 @@ export interface PaginationInput { page?: number limit?: number - formatItem(item: T): string + formatItem(item: T): Promise | string formatString: { top: string bottom: string diff --git a/packages/spark-adapter/src/requester.ts b/packages/spark-adapter/src/requester.ts index 30c6205f..d857e7be 100644 --- a/packages/spark-adapter/src/requester.ts +++ b/packages/spark-adapter/src/requester.ts @@ -175,7 +175,7 @@ export class SparkRequester extends ModelRequester { /* writeFileSync('poe.json', JSON.stringify(jsonData)) */ const status = response.payload.choices?.status - if (!status) { + if (status == null) { return reject(new Error(e.data.toString())) }