|
| 1 | +import {ChatPromptWrapper} from "../ChatPromptWrapper.js"; |
| 2 | +import {getTextCompletion} from "../utils/getTextCompletion.js"; |
| 3 | + |
| 4 | +export class FalconChatPromptWrapper extends ChatPromptWrapper { |
| 5 | + public readonly wrapperName: string = "Falcon"; |
| 6 | + private readonly _instructionName: string; |
| 7 | + private readonly _responseName: string; |
| 8 | + |
| 9 | + public constructor({instructionName = "User", responseName = "Assistant"}: {instructionName?: string, responseName?: string} = {}) { |
| 10 | + super(); |
| 11 | + |
| 12 | + this._instructionName = instructionName; |
| 13 | + this._responseName = responseName; |
| 14 | + } |
| 15 | + |
| 16 | + public override wrapPrompt(prompt: string, {systemPrompt, promptIndex, lastStopString, lastStopStringSuffix}: { |
| 17 | + systemPrompt: string, promptIndex: number, lastStopString: string | null, lastStopStringSuffix: string | null |
| 18 | + }) { |
| 19 | + if (promptIndex === 0) |
| 20 | + return systemPrompt + `\n${this._instructionName}: ` + prompt + `\n${this._responseName}: `; |
| 21 | + |
| 22 | + return this._getPromptPrefix(lastStopString, lastStopStringSuffix) + prompt + `\n${this._responseName}: `; |
| 23 | + } |
| 24 | + |
| 25 | + public override getStopStrings(): string[] { |
| 26 | + return [ |
| 27 | + `\n${this._instructionName}: `, |
| 28 | + `\n${this._responseName}:` |
| 29 | + ]; |
| 30 | + } |
| 31 | + |
| 32 | + public override getDefaultStopString(): string { |
| 33 | + return `\n${this._instructionName}: `; |
| 34 | + } |
| 35 | + |
| 36 | + private _getPromptPrefix(lastStopString: string | null, lastStopStringSuffix: string | null) { |
| 37 | + return getTextCompletion((lastStopString ?? "") + (lastStopStringSuffix ?? ""), [ |
| 38 | + `\n${this._instructionName}: `, |
| 39 | + `${this._instructionName}: ` |
| 40 | + ]) ?? `\n${this._instructionName}: `; |
| 41 | + } |
| 42 | +} |
0 commit comments