Skip to content

Commit

Permalink
Version 0.3.0: add history support
Browse files Browse the repository at this point in the history
  • Loading branch information
synw committed Dec 1, 2023
1 parent b470e6e commit 97d8c49
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "modprompt",
"version": "0.2.0",
"version": "0.3.0",
"description": "Prompt templates for language models",
"license": "MIT",
"scripts": {
Expand Down
50 changes: 40 additions & 10 deletions src/cls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LmTemplate, PromptBlock, TurnBlock, SpacingSlots } from "./interfaces";
import { LmTemplate, PromptBlock, TurnBlock, SpacingSlots, HistoryTurn } from "./interfaces";
import { templates } from "./db.js";

/**
Expand All @@ -12,6 +12,7 @@ class PromptTemplate {
name: string;
user: string;
assistant: string;
history: Array<HistoryTurn> = [];
system?: PromptBlock;
shots?: Array<TurnBlock>;
stop?: Array<string>;
Expand Down Expand Up @@ -187,6 +188,26 @@ class PromptTemplate {
return this
}


/**
* Render a turn block
*
* @param {TurnBlock | HistoryTurn} shot the shot to render
* @returns {string} ther rendered text
*/
renderShot(shot: TurnBlock | HistoryTurn): string {
const buf = [];
buf.push(this._buildUserBlock(shot.user));
let _assistantMsg = shot.assistant;
if (this.afterShot) {
_assistantMsg += this.afterShot
} else {
_assistantMsg += "\n\n"
}
buf.push(this._buildAssistantBlock(_assistantMsg));
return buf.join("")
}

/**
* Renders the template into a string representation.
*
Expand All @@ -213,16 +234,13 @@ class PromptTemplate {
// shots
if (this?.shots) {
for (const shot of this.shots) {
buf.push(this._buildUserBlock(shot.user));
let _assistantMsg = shot.assistant;
if (this.afterShot) {
_assistantMsg += this.afterShot
} else {
_assistantMsg += "\n\n"
}
buf.push(this._buildAssistantBlock(_assistantMsg));
buf.push(this.renderShot(shot));
}
}
// history
for (const turn of this.history) {
buf.push(this.renderShot(turn));
}
// user block
buf.push(this._buildUserBlock());
// assistant block
Expand All @@ -246,7 +264,19 @@ class PromptTemplate {
}


private _buildSystemBlock(skip_empty_system: boolean = false): string {
/**
* Push a turn into history
*
* @param {HistoryTurn} turn the history turn
* @returns {PromptTemplate}
*/
pushToHistory(turn: HistoryTurn): PromptTemplate {
this.history.push(turn)
return this
}


private _buildSystemBlock(skip_empty_system: boolean): string {
let res = "";
if (!this?.system) {
return ""
Expand Down
8 changes: 7 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,10 @@ interface LmTemplate {
prefix?: string;
}

export { SpacingSlots, PromptBlock, TurnBlock, LmTemplate }
interface HistoryTurn {
user: string;
assistant: string;
images?: Array<{ id: string, data: ArrayBuffer }>;
}

export { SpacingSlots, PromptBlock, TurnBlock, LmTemplate, HistoryTurn }

0 comments on commit 97d8c49

Please sign in to comment.