Skip to content

Commit

Permalink
feat(core): add preset review
Browse files Browse the repository at this point in the history
  • Loading branch information
dingyi222666 committed Sep 23, 2023
1 parent 6a50489 commit 02c35f5
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/commands/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})

Expand Down
19 changes: 18 additions & 1 deletion packages/core/src/llm-core/utils/tiktoken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,22 @@ export async function encodingForModel(
extendedSpecialTokens?: Record<string, number>
}
) {
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
}
21 changes: 19 additions & 2 deletions packages/core/src/middlewares/list_all_preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Pagination } from '../utils/pagination'

export function apply(ctx: Context, config: Config, chain: ChatChain) {
const pagination = new Pagination<string>({
formatItem: (value) => value,
formatItem: (value) => formatPreset(ctx, value),
formatString: {
top: '以下是目前可用的预设列表:\n',
bottom: '\n你可以使用 chathub.room.set -p <preset> 来设置默认使用的预设'
Expand All @@ -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)

Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,14 @@ export class Preset {
// throw new Error("No default preset found")
}

async getAllPreset(): Promise<string[]> {
async getAllPreset(concatKeyword: boolean = true): Promise<string[]> {
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<void> {
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/utils/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ export class Pagination<T> {
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)
Expand All @@ -56,7 +62,7 @@ export interface PaginationInput<T> {
page?: number
limit?: number

formatItem(item: T): string
formatItem(item: T): Promise<string> | string
formatString: {
top: string
bottom: string
Expand Down
2 changes: 1 addition & 1 deletion packages/spark-adapter/src/requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}

Expand Down

0 comments on commit 02c35f5

Please sign in to comment.